From 747ed7a29e24e6bcce71a7680401ebc483bf49c7 Mon Sep 17 00:00:00 2001 From: Samuel Date: Mon, 9 Sep 2024 14:42:28 +0200 Subject: [PATCH] fix: update invoice transfer (#310) --- src/service/invoice-service.ts | 4 +++- test/unit/service/invoice-service.ts | 36 ++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/src/service/invoice-service.ts b/src/service/invoice-service.ts index 8f8240cd8..a9c0a1c0e 100644 --- a/src/service/invoice-service.ts +++ b/src/service/invoice-service.ts @@ -53,6 +53,7 @@ import SubTransaction from '../entity/transactions/sub-transaction'; import InvoiceUser, { InvoiceUserDefaults } from '../entity/user/invoice-user'; import Transfer from '../entity/transactions/transfer'; import WithManager from '../database/with-manager'; +import DineroTransformer from '../entity/transformer/dinero-transformer'; export interface InvoiceFilterParameters { /** @@ -267,7 +268,7 @@ export default class InvoiceService extends WithManager { * @param update */ public async updateInvoice(update: UpdateInvoiceParams) { - const { byId, invoiceId, state, ...props } = update; + const { byId, invoiceId, state, amount, ...props } = update; const base: Invoice = await this.manager.findOne(Invoice, InvoiceService.getOptions({ invoiceId })); // Return undefined if base does not exist. @@ -292,6 +293,7 @@ export default class InvoiceService extends WithManager { }); } + if (amount) await this.manager.update(Transfer, { id: base.transfer.id }, { amountInclVat: DineroTransformer.Instance.from(amount.amount) }); await this.manager.update(Invoice, { id: base.id }, { ...props, date: props.date ? new Date(props.date) : undefined }); // Return the newly updated Invoice. diff --git a/test/unit/service/invoice-service.ts b/test/unit/service/invoice-service.ts index c26d6736e..99f0a1a0b 100644 --- a/test/unit/service/invoice-service.ts +++ b/test/unit/service/invoice-service.ts @@ -45,6 +45,7 @@ import InvoiceUser from '../../../src/entity/user/invoice-user'; import { truncateAllTables } from '../../setup'; import { finishTestDB } from '../../helpers/test-helpers'; import { InvoiceSeeder, TransactionSeeder, UserSeeder } from '../../seed'; +import Transfer from '../../../src/entity/transactions/transfer'; chai.use(deepEqualInAnyOrder); @@ -521,6 +522,41 @@ describe('InvoiceService', () => { }, ); }); + it('should update invoice transfer amount if update.amount is provided', async () => { + await inUserContext( + await (await UserFactory()).clone(2), + async (debtor: User, creditor: User) => { + const invoice = await createInvoiceWithTransfers(debtor.id, creditor.id, 1); + const validUpdateInvoiceParams = { + amount: { + amount: 100, + currency: 'EUR', + precision: 2, + }, + invoiceId: invoice.id, + byId: creditor.id, + }; + const updatedInvoice = await AppDataSource.manager.transaction(async (manager) => { + return new InvoiceService(manager).updateInvoice( + validUpdateInvoiceParams, + ); + }); + expect(updatedInvoice.transfer.amountInclVat.getAmount()).is.equal(validUpdateInvoiceParams.amount.amount); + expect(updatedInvoice.transfer.amountInclVat.getCurrency()).is.equal(validUpdateInvoiceParams.amount.currency); + expect(updatedInvoice.transfer.amountInclVat.getPrecision()).is.equal(validUpdateInvoiceParams.amount.precision); + + const fromDB = await Invoice.findOne({ where: { id: invoice.id }, relations: ['transfer'] }); + expect(fromDB.transfer.amountInclVat.getAmount()).is.equal(validUpdateInvoiceParams.amount.amount); + expect(fromDB.transfer.amountInclVat.getCurrency()).is.equal(validUpdateInvoiceParams.amount.currency); + expect(fromDB.transfer.amountInclVat.getPrecision()).is.equal(validUpdateInvoiceParams.amount.precision); + + const transfer = await Transfer.findOne({ where: { id: fromDB.transfer.id } }); + expect(transfer.amountInclVat.getAmount()).is.equal(validUpdateInvoiceParams.amount.amount); + expect(transfer.amountInclVat.getCurrency()).is.equal(validUpdateInvoiceParams.amount.currency); + expect(transfer.amountInclVat.getPrecision()).is.equal(validUpdateInvoiceParams.amount.precision); + }, + ); + }); it('should update an Invoice state', async () => { await inUserContext(