Merge pull request 'feat: refs #7235 add serialType parameter to getInvoiceDate and implement corresponding tests' (!3306) from 7235-InvoiceOut into dev
gitea/salix/pipeline/head This commit looks good Details

Reviewed-on: #3306
Reviewed-by: Alex Moreno <alexm@verdnatura.es>
Reviewed-by: Carlos Andrés <carlosap@verdnatura.es>
This commit is contained in:
Javi Gallego 2024-12-18 12:34:56 +00:00
commit 6c2d7641f5
3 changed files with 51 additions and 7 deletions

View File

@ -29,14 +29,14 @@ module.exports = Self => {
const models = Self.app.models;
const myOptions = {};
let tx;
if (typeof options == 'object')
Object.assign(myOptions, options);
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
};
}
try {
const user = await models.VnUser.findOne({

View File

@ -7,7 +7,12 @@ module.exports = Self => {
arg: 'companyFk',
type: 'number',
required: true
}
},
{
arg: 'serialType',
type: 'string',
required: true
},
],
returns: {
type: ['object'],
@ -19,16 +24,16 @@ module.exports = Self => {
}
});
Self.getInvoiceDate = async companyFk => {
Self.getInvoiceDate = async(companyFk, serialType) => {
const models = Self.app.models;
const [invoiceDate] = await models.InvoiceOut.rawSql(
`SELECT MAX(io.issued) issued
FROM invoiceOut io
JOIN invoiceOutSerial ios ON ios.code = io.serial
WHERE ios.type = 'global'
AND io.issued
WHERE ios.type = ?
AND io.issued
AND io.companyFk = ?`,
[companyFk]
[serialType, companyFk]
);
return invoiceDate;
};

View File

@ -0,0 +1,39 @@
const models = require('vn-loopback/server/server').models;
const moment = require('moment');
describe('getInvoiceDate()', () => {
const companyFk = 442;
let tx;
let options;
beforeEach(async() => {
tx = await models.InvoiceOut.beginTransaction({});
options = {transaction: tx};
});
afterEach(async() => {
await tx.rollback();
});
it('should return a correct date for serialType "global"', async() => {
const serialType = 'global';
const result = await models.InvoiceOut.getInvoiceDate(companyFk, serialType, options);
expect(moment(result.issued).format('YYYY-MM-DD')).toEqual('2000-12-01');
});
it('should return null for serialType "multiple"', async() => {
const serialType = 'multiple';
const result = await models.InvoiceOut.getInvoiceDate(companyFk, serialType, options);
expect(result.issued).toBeNull();
});
it('should return correct date for serialType "quick"', async() => {
const serialType = 'quick';
const result = await models.InvoiceOut.getInvoiceDate(companyFk, serialType, options);
expect(moment(result.issued).format('YYYY-MM-DD')).toEqual('2001-01-01');
});
});