Skip to content

Commit

Permalink
fix: disallow the creation of invoices with no transactions (#325)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustSamuel authored Sep 18, 2024
1 parent ccf0de9 commit 8e01e97
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
6 changes: 3 additions & 3 deletions src/controller/request/validators/invoice-request-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import {
INVALID_INVOICE_ID,
INVALID_TRANSACTION_IDS,
INVALID_TRANSACTION_OWNER,
INVOICE_IS_DELETED,
INVOICE_IS_DELETED, NO_TRANSACTION_IDS,
SAME_INVOICE_STATE, SUBTRANSACTION_ALREADY_INVOICED,
} from './validation-errors';
import { InvoiceState } from '../../../entity/invoices/invoice-status';
Expand All @@ -46,7 +46,7 @@ import Invoice from '../../../entity/invoices/invoice';
* Checks whether all the transactions exists and are credited to the debtor or sold in case of credit Invoice.
*/
async function validTransactionIds<T extends BaseInvoice>(p: T) {
if (!p.transactionIDs) return toPass(p);
if (p.transactionIDs.length === 0) return toFail(NO_TRANSACTION_IDS());

const relations: FindOptionsRelations<Transaction> = {
from: true,
Expand Down Expand Up @@ -111,8 +111,8 @@ async function differentState<T extends UpdateInvoiceParams>(p: T) {
*/
function baseInvoiceRequestSpec<T extends BaseInvoice>(): Specification<T, ValidationError> {
return [
validTransactionIds,
[[userMustExist], 'forId', new ValidationError('forId:')],
validTransactionIds,
];
}

Expand Down
2 changes: 2 additions & 0 deletions src/controller/request/validators/validation-errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ export const INVALID_TRANSACTION_OWNER = () => new ValidationError('Not all tran

export const INVALID_TRANSACTION_IDS = () => new ValidationError('Not all transaction IDs are valid.');

export const NO_TRANSACTION_IDS = () => new ValidationError('No transaction IDs provided.');

export const INVALID_INVOICE_ID = () => new ValidationError('Invoice with this ID does not exist.');

export const INVOICE_IS_DELETED = () => new ValidationError('Invoice is deleted.');
Expand Down
15 changes: 10 additions & 5 deletions test/unit/controller/invoice-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import {
import Transaction from '../../../src/entity/transactions/transaction';
import {
INVALID_TRANSACTION_OWNER,
INVALID_USER_ID,
INVALID_USER_ID, NO_TRANSACTION_IDS,
SAME_INVOICE_STATE,
SUBTRANSACTION_ALREADY_INVOICED,
ZERO_LENGTH_STRING,
Expand Down Expand Up @@ -270,12 +270,17 @@ describe('InvoiceController', async () => {
const req: CreateInvoiceRequest = { ...ctx.validInvoiceRequest, transactionIDs };
await expectError(req, INVALID_TRANSACTION_OWNER().value);
});
it('should verity that forId is a valid user', async () => {
const req: CreateInvoiceRequest = { ...ctx.validInvoiceRequest, forId: -1 };
it('should verify that forId is a valid user', async () => {
const req: CreateInvoiceRequest = { ...ctx.validInvoiceRequest, forId: -1, transactionIDs: [1] };
await expectError(req, `forId: ${INVALID_USER_ID().value}`);
});
it('should verity that description is a valid string', async () => {
const req: CreateInvoiceRequest = { ...ctx.validInvoiceRequest, description: '' };
it('should verify that transactionIDs is not empty', async () => {
const req: CreateInvoiceRequest = { ...ctx.validInvoiceRequest, transactionIDs: [] };
await expectError(req, NO_TRANSACTION_IDS().value);
});
it('should verify that description is a valid string', async () => {
const transactionIDs = (await Transaction.find({ relations: ['from'] })).filter((i) => i.from.id === ctx.validInvoiceRequest.forId).map((t) => t.id);
const req: CreateInvoiceRequest = { ...ctx.validInvoiceRequest, description: '', transactionIDs };
await expectError(req, `description: ${ZERO_LENGTH_STRING().value}`);
});
it('should disallow double invoicing of a transaction', async () => {
Expand Down

0 comments on commit 8e01e97

Please sign in to comment.