import { type Ref, computed, defineComponent, inject, ref } from 'vue'; import { useRoute, useRouter } from 'vue-router'; import { useVuelidate } from '@vuelidate/core'; import TransactionService from './transaction.service'; import { useValidation } from '@/shared/composables'; import { useAlertService } from '@/shared/alert/alert.service'; import EventService from '@/entities/event/event.service'; import { type IEvent } from '@/shared/model/event.model'; import { type ITransaction, Transaction } from '@/shared/model/transaction.model'; import { TransactionType } from '@/shared/model/enumerations/transaction-type.model'; import { type IUserAccount } from '@/shared/model/user-account.model'; import UserAccountService from '@/entities/user-account/user-account.service'; export default defineComponent({ compatConfig: { MODE: 3 }, name: 'TransactionUpdate', setup() { const transactionService = inject('transactionService', () => new TransactionService()); const alertService = inject('alertService', () => useAlertService(), true); const transaction: Ref = ref(new Transaction()); transaction.value.transactionItems = []; const dictUserAccounts = ref([]) as Ref; const eventService = inject('eventService', () => new EventService()); const userAccountService = inject('userAccountService', () => new UserAccountService()); const events: Ref = ref([]); const transactionTypeValues: Ref = ref(Object.keys(TransactionType)); const isSaving = ref(false); const currentLanguage = inject('currentLanguage', () => computed(() => navigator.language ?? 'en'), true); const route = useRoute(); const router = useRouter(); const previousState = () => router.go(-1); const retrieveTransaction = async (transactionId, opposing) => { try { if (dictUserAccounts.value.length == 0) { const res2 = await userAccountService().retrieve(); dictUserAccounts.value = res2.data; } const res = await transactionService().find(transactionId, opposing); transaction.value = res; transaction.value.transactionItems.forEach((item, index) => { const matchingAccount = dictUserAccounts.value.find(account => account.id === item.userAccount.id); transaction.value.transactionItems[index].userAccount = matchingAccount || null; }); } catch (error) { alertService.showHttpError(error.response); } }; const retrieveTransactionForPayments = async transactionId => { try { if (dictUserAccounts.value.length == 0) { const res2 = await userAccountService().retrieve(); dictUserAccounts.value = res2.data; } const res = await transactionService().payments(transactionId); transaction.value = res; transaction.value.transactionItems.forEach((item, index) => { const matchingAccount = dictUserAccounts.value.find(account => account.id === item.userAccount.id); transaction.value.transactionItems[index].userAccount = matchingAccount || null; }); } catch (error) { alertService.showHttpError(error.response); } }; const retrieveTransactionForFieldPayment = async eventId => { try { if (dictUserAccounts.value.length == 0) { const res2 = await userAccountService().retrieve(); dictUserAccounts.value = res2.data; } const res = await transactionService().fieldPayment(eventId); transaction.value = res; transaction.value.transactionItems.forEach((item, index) => { const matchingAccount = dictUserAccounts.value.find(account => account.id === item.userAccount.id); transaction.value.transactionItems[index].userAccount = matchingAccount || null; }); } catch (error) { alertService.showHttpError(error.response); } }; const retrieveUserAccounts = async () => { if (dictUserAccounts.value.length == 0) { try { const res = await userAccountService().retrieve(); console.log('got accounts' + res); dictUserAccounts.value = res.data; } catch (error) { alertService.showHttpError(error.response); } } }; retrieveUserAccounts(); if (route.params?.transactionId) { console.log('a1'); retrieveTransaction(route.params.transactionId, false); } if (route.params?.opposingTransactionId) { console.log('a2'); console.log('getting opposite transaction for ' + route.params?.opposingTransactionId); retrieveTransaction(route.params.opposingTransactionId, true); } if (route.params?.paymentsForTransactionId) { console.log('a3'); retrieveTransactionForPayments(route.params.paymentsForTransactionId); } if (route.params?.paymentForFieldByEventId) { console.log('a4'); retrieveTransactionForFieldPayment(route.params.paymentForFieldByEventId); } console.log('a5'); const initRelationships = () => { eventService() .retrieve() .then(res => { events.value = res.data; }); }; initRelationships(); const validations = useValidation(); const validationRules = { type: {}, date: {}, comment: {}, event: {}, // transactionItems: { // $each: { // amount: { required: validations.required, minValue: validations.minValue(0.01) }, // comment: {}, // locked: {}, // userAccount: {}, // }, // }, }; const v$ = useVuelidate(validationRules, transaction as any); v$.value.$validate(); const addTransactionItem = () => { transaction.value.transactionItems.push({ amount: 0, comment: '', locked: false, userAccount: null, }); }; const removeTransactionItem = (index: number) => { transaction.value.transactionItems.splice(index, 1); }; const calculateUnsettledBalance = (): string => { const balance = transaction.value.transactionItems.reduce((sum, item) => sum + item.amount, 0); return balance.toFixed(2); }; return { transactionService, alertService, transaction, previousState, transactionTypeValues, isSaving, currentLanguage, events, v$, dictUserAccounts, addTransactionItem, removeTransactionItem, calculateUnsettledBalance, }; }, methods: { save(): void { this.isSaving = true; if (this.transaction.id) { this.transactionService() .update(this.transaction) .then(param => { this.isSaving = false; this.previousState(); this.alertService.showInfo(`A Transaction is updated with identifier ${param.id}`); }) .catch(error => { this.isSaving = false; this.alertService.showHttpError(error.response); }); } else { this.transactionService() .create(this.transaction) .then(param => { this.isSaving = false; this.previousState(); this.alertService.showSuccess(`A Transaction is created with identifier ${param.id}`); }) .catch(error => { this.isSaving = false; this.alertService.showHttpError(error.response); }); } }, }, });