Skip to content

Commit

Permalink
fix: update invoice transfer (#310)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustSamuel committed Sep 11, 2024
1 parent ad3cdf4 commit 747ed7a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/service/invoice-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
/**
Expand Down Expand Up @@ -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.
Expand All @@ -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.

Expand Down
36 changes: 36 additions & 0 deletions test/unit/service/invoice-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 747ed7a

Please sign in to comment.