TransactionEntity

This commit is contained in:
2024-11-13 14:46:28 +01:00
parent c2bf40c1a4
commit c609f1ecc6
40 changed files with 2272 additions and 10 deletions
@@ -0,0 +1,111 @@
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';
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());
const eventService = inject('eventService', () => new EventService());
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 => {
try {
const res = await transactionService().find(transactionId);
transaction.value = res;
} catch (error) {
alertService.showHttpError(error.response);
}
};
if (route.params?.transactionId) {
retrieveTransaction(route.params.transactionId);
}
const initRelationships = () => {
eventService()
.retrieve()
.then(res => {
events.value = res.data;
});
};
initRelationships();
const validations = useValidation();
const validationRules = {
type: {},
date: {},
comment: {},
event: {},
};
const v$ = useVuelidate(validationRules, transaction as any);
v$.value.$validate();
return {
transactionService,
alertService,
transaction,
previousState,
transactionTypeValues,
isSaving,
currentLanguage,
events,
v$,
};
},
created(): void {},
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);
});
}
},
},
});