import { type Ref, defineComponent, inject, onMounted, ref } from 'vue'; import TransactionService from './transaction.service'; import { type ITransaction } from '@/shared/model/transaction.model'; import { useAlertService } from '@/shared/alert/alert.service'; import type AccountService from '@/account/account.service'; export default defineComponent({ compatConfig: { MODE: 3 }, name: 'Transaction', props: { userAccountId: { type: [Number, String], required: false, default: null, }, }, setup(props) { const transactionService = inject('transactionService', () => new TransactionService()); const alertService = inject('alertService', () => useAlertService(), true); const transactions: Ref = ref([]); const hasAnyAuthorityValues: Ref = ref({}); const accountService = inject('accountService'); const isFetching = ref(false); const sums: Ref = ref([]); const clear = () => {}; // console.log(props.userAccountId); const retrieveTransactions = async () => { isFetching.value = true; try { // console.log(props); const res = await transactionService().retrieve(); // const res = await transactionService().retrieveForAccount(props.userAccountId); transactions.value = res.data; sums.value = sumAmountsPerColumn(transactions.value); } catch (err) { alertService.showHttpError(err.response); } finally { isFetching.value = false; } }; const handleSyncList = () => { retrieveTransactions(); }; onMounted(async () => { await retrieveTransactions(); }); const removeId: Ref = ref(null); const removeEntity = ref(null); const prepareRemove = (instance: ITransaction) => { removeId.value = instance.id; removeEntity.value.show(); }; const closeDialog = () => { removeEntity.value.hide(); }; const removeTransaction = async () => { try { await transactionService().delete(removeId.value); const message = `A Transaction is deleted with identifier ${removeId.value}`; alertService.showInfo(message, { variant: 'danger' }); removeId.value = null; retrieveTransactions(); closeDialog(); } catch (error) { alertService.showHttpError(error.response); } }; function sumAmountsPerColumn(transactions: ITransaction[]): number[] { // Jeżeli nie ma transakcji, zwracamy pustą tablicę if (transactions.length === 0) return []; // Liczba kolumn to liczba transactionItems w pierwszej transakcji const columnCount = transactions[0].items.length; // Inicjalizacja tablicy sum z zerami dla każdej kolumny const sums = new Array(columnCount).fill(0); // Iteracja przez każdą transakcję for (const transaction of transactions) { for (let i = 0; i < columnCount; i++) { // Dodajemy amount z każdego transactionItem do odpowiedniej kolumny sums[i] += transaction.items[i].amount; } } // Zaokrąglanie każdej sumy do dwóch miejsc po przecinku return sums.map(sum => Number(sum.toFixed(2))); } return { transactions, handleSyncList, isFetching, retrieveTransactions, clear, sums, removeId, accountService, hasAnyAuthorityValues, removeEntity, prepareRemove, closeDialog, removeTransaction, }; }, methods: { hasAnyAuthority(authorities: any): boolean { this.accountService.hasAnyAuthorityAndCheckAuth(authorities).then(value => { if (this.hasAnyAuthorityValues[authorities] !== value) { this.hasAnyAuthorityValues = { ...this.hasAnyAuthorityValues, [authorities]: value }; } }); return this.hasAnyAuthorityValues[authorities] ?? false; }, }, });