diff --git a/db/dump/fixtures.sql b/db/dump/fixtures.sql index a3791f9f5..fa204ddd5 100644 --- a/db/dump/fixtures.sql +++ b/db/dump/fixtures.sql @@ -2887,7 +2887,9 @@ INSERT INTO `vn`.`report` (`id`, `name`, `paperSizeFk`, `method`) INSERT INTO `vn`.`payDemDetail` (`id`, `detail`) VALUES - (1, 1); + (1, 1), + (2, 20), + (7, 1); INSERT INTO `vn`.`workerConfig` (`id`, `businessUpdated`, `roleFk`, `payMethodFk`, `businessTypeFk`) VALUES diff --git a/modules/invoiceIn/back/methods/invoice-in-due-day/new.js b/modules/invoiceIn/back/methods/invoice-in-due-day/new.js new file mode 100644 index 000000000..37d2f6d05 --- /dev/null +++ b/modules/invoiceIn/back/methods/invoice-in-due-day/new.js @@ -0,0 +1,37 @@ +module.exports = Self => { + Self.remoteMethodCtx('new', { + description: 'Creates a new invoiceIn due day', + accessType: 'WRITE', + accepts: { + arg: 'id', + type: 'number', + required: true, + description: 'The invoiceIn id', + }, + http: { + path: '/new', + verb: 'POST' + } + }); + + Self.new = async(ctx, id, options) => { + let tx; + const myOptions = {userId: ctx.req.accessToken.userId}; + + if (typeof options == 'object') + Object.assign(myOptions, options); + + if (!myOptions.transaction) { + tx = await Self.beginTransaction({}); + myOptions.transaction = tx; + } + + try { + await Self.rawSql(`CALL vn.invoiceInDueDay_calculate(?)`, [id], myOptions); + if (tx) await tx.commit(); + } catch (e) { + if (tx) await tx.rollback(); + throw e; + } + }; +}; diff --git a/modules/invoiceIn/back/methods/invoice-in-due-day/specs/new.spec.js b/modules/invoiceIn/back/methods/invoice-in-due-day/specs/new.spec.js new file mode 100644 index 000000000..c188a511d --- /dev/null +++ b/modules/invoiceIn/back/methods/invoice-in-due-day/specs/new.spec.js @@ -0,0 +1,45 @@ +const LoopBackContext = require('loopback-context'); +const models = require('vn-loopback/server/server').models; + +describe('invoiceInDueDay new()', () => { + beforeAll(async() => { + const activeCtx = { + accessToken: {userId: 9}, + }; + spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({ + active: activeCtx + }); + }); + + it('should correctly create a new due day', async() => { + const userId = 9; + const invoiceInFk = 6; + + const ctx = { + req: { + + accessToken: {userId: userId}, + } + }; + + const tx = await models.InvoiceIn.beginTransaction({}); + const options = {transaction: tx}; + + try { + await models.InvoiceInDueDay.destroyAll({ + invoiceInFk + }, options); + + await models.InvoiceInDueDay.new(ctx, invoiceInFk, options); + + const result = await models.InvoiceInDueDay.find({where: {invoiceInFk}}, options); + + expect(result).toBeDefined(); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } + }); +}); diff --git a/modules/invoiceIn/back/models/invoice-in-due-day.js b/modules/invoiceIn/back/models/invoice-in-due-day.js new file mode 100644 index 000000000..2375c6566 --- /dev/null +++ b/modules/invoiceIn/back/models/invoice-in-due-day.js @@ -0,0 +1,3 @@ +module.exports = Self => { + require('../methods/invoice-in-due-day/new')(Self); +};