diff --git a/src/controller/request/validators/invoice-request-spec.ts b/src/controller/request/validators/invoice-request-spec.ts index 00a48861e..fd4490041 100644 --- a/src/controller/request/validators/invoice-request-spec.ts +++ b/src/controller/request/validators/invoice-request-spec.ts @@ -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'; @@ -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(p: T) { - if (!p.transactionIDs) return toPass(p); + if (p.transactionIDs.length === 0) return toFail(NO_TRANSACTION_IDS()); const relations: FindOptionsRelations = { from: true, @@ -111,8 +111,8 @@ async function differentState(p: T) { */ function baseInvoiceRequestSpec(): Specification { return [ - validTransactionIds, [[userMustExist], 'forId', new ValidationError('forId:')], + validTransactionIds, ]; } diff --git a/src/controller/request/validators/validation-errors.ts b/src/controller/request/validators/validation-errors.ts index da807624c..8634c6fa1 100644 --- a/src/controller/request/validators/validation-errors.ts +++ b/src/controller/request/validators/validation-errors.ts @@ -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.'); diff --git a/test/unit/controller/invoice-controller.ts b/test/unit/controller/invoice-controller.ts index 9bc5e8172..1a6691ffd 100644 --- a/test/unit/controller/invoice-controller.ts +++ b/test/unit/controller/invoice-controller.ts @@ -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, @@ -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 () => {