180 lines
6.1 KiB
TypeScript
180 lines
6.1 KiB
TypeScript
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<ITransaction> = ref(new Transaction());
|
|
|
|
transaction.value.transactionItems = [];
|
|
const dictUserAccounts = ref([]) as Ref<IUserAccount[]>;
|
|
|
|
const eventService = inject('eventService', () => new EventService());
|
|
const userAccountService = inject('userAccountService', () => new UserAccountService());
|
|
|
|
const events: Ref<IEvent[]> = ref([]);
|
|
const transactionTypeValues: Ref<string[]> = 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();
|
|
console.log('got accounts' + res2);
|
|
dictUserAccounts.value = res2.data;
|
|
}
|
|
const res = await transactionService().find(transactionId, opposing);
|
|
transaction.value = res;
|
|
// console.log('len'+transaction.value.transactionItems?.length);
|
|
// console.log('len'+dictUserAccounts.value.length);
|
|
// console.log('id'+transaction.value.transactionItems[0].userAccount.id);
|
|
// console.log('id'+(dictUserAccounts.value.find(account => account.id === transaction.value.transactionItems[0].userAccount.id) || null));
|
|
// transaction.value.transactionItems[0].userAccount = dictUserAccounts.value.find(account => account.id === transaction.value.transactionItems[0].userAccount.id) || null;
|
|
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) {
|
|
retrieveTransaction(route.params.transactionId, false);
|
|
}
|
|
|
|
if (route.params?.opposingTransactionId) {
|
|
console.log('getting opposite transaction for ' + route.params?.opposingTransactionId);
|
|
retrieveTransaction(route.params.opposingTransactionId, true);
|
|
}
|
|
|
|
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);
|
|
});
|
|
}
|
|
},
|
|
},
|
|
});
|