feat: refs #3505 testBack closure

This commit is contained in:
Javi Gallego 2024-09-16 13:24:33 +02:00
parent f1a8e206b5
commit e4a74ba0e9
16 changed files with 262 additions and 237 deletions

View File

@ -12,9 +12,8 @@ const ticket1 = {
'addressFk': 1,
'agencyModeFk': 999
};
let expedition;
const expedition1 = {
'id': 17,
'agencyModeFk': 999,
'ticketFk': 44,
'freightItemFk': 71,
@ -47,7 +46,7 @@ describe('MRWConfig createShipment()', () => {
await createMrwConfig();
await models.Ticket.create(ticket1);
await models.Expedition.create(expedition1);
expedition = await models.Expedition.create(expedition1);
});
afterAll(async() => {
@ -93,7 +92,7 @@ describe('MRWConfig createShipment()', () => {
}
it('should create a shipment and return a base64Binary label', async() => {
const {file} = await models.MrwConfig.createShipment(expedition1.id);
const {file} = await models.MrwConfig.createShipment(expedition.id);
expect(file).toEqual(mockBase64Binary);
});
@ -101,7 +100,7 @@ describe('MRWConfig createShipment()', () => {
it('should fail if mrwConfig has no data', async() => {
let error;
await models.MrwConfig.destroyAll();
await models.MrwConfig.createShipment(expedition1.id).catch(e => {
await models.MrwConfig.createShipment(expedition.id).catch(e => {
error = e;
}).finally(async() => {
expect(error.message).toEqual(`MRW service is not configured`);
@ -126,7 +125,7 @@ describe('MRWConfig createShipment()', () => {
yesterday.setDate(yesterday.getDate() - 1);
await models.Ticket.updateAll({id: ticket1.id}, {shipped: yesterday});
await models.MrwConfig.createShipment(expedition1.id).catch(e => {
await models.MrwConfig.createShipment(expedition.id).catch(e => {
error = e;
}).finally(async() => {
expect(error.message).toEqual(`This ticket has a shipped date earlier than today`);
@ -136,7 +135,7 @@ describe('MRWConfig createShipment()', () => {
it('should send mail if you are past the dead line and is not notified today', async() => {
await models.MrwConfig.updateAll({id: 1}, {expeditionDeadLine: '10:00:00', notified: null});
await models.MrwConfig.createShipment(expedition1.id);
await models.MrwConfig.createShipment(expedition.id);
const notification = await getLastNotification();
expect(notification.notificationFk).toEqual(filter.notificationFk);
@ -144,7 +143,7 @@ describe('MRWConfig createShipment()', () => {
it('should send mail if you are past the dead line and it is notified from another day', async() => {
await models.MrwConfig.updateAll({id: 1}, {expeditionDeadLine: '10:00:00', notified: new Date()});
await models.MrwConfig.createShipment(expedition1.id);
await models.MrwConfig.createShipment(expedition.id);
const notification = await getLastNotification();
expect(notification.notificationFk).toEqual(filter.notificationFk);
@ -152,7 +151,7 @@ describe('MRWConfig createShipment()', () => {
it('should not send mail if you are past the dead line and it is notified', async() => {
await models.MrwConfig.updateAll({id: 1}, {expeditionDeadLine: '10:00:00', notified: Date.vnNew()});
await models.MrwConfig.createShipment(expedition1.id);
await models.MrwConfig.createShipment(expedition.id);
const notification = await getLastNotification();
expect(notification).toEqual(null);

View File

@ -335,21 +335,21 @@ INSERT INTO `vn`.`payDem`(`id`, `payDem`)
(2, 20),
(7, 0);
INSERT INTO `vn`.`autonomy`(`id`, `name`, `countryFk`)
INSERT INTO `vn`.`autonomy`(`id`, `name`, `countryFk`, `hasDailyInvoice`)
VALUES
(1, 'Autonomy one', 1),
(2, 'Autonomy two', 1),
(3, 'Autonomy three', 2),
(4, 'Autonomy four', 13);
(1, 'Autonomy one', 1, 1),
(2, 'Autonomy two', 1, 0),
(3, 'Autonomy three', 2, 0),
(4, 'Autonomy four', 13, 0);
INSERT INTO `vn`.`province`(`id`, `name`, `countryFk`, `autonomyFk`, `warehouseFk`)
VALUES
(1, 'Province one', 1, 1, NULL),
(2, 'Province two', 1, 1, NULL),
(3, 'Province three', 30, 2, NULL),
(4, 'Province four', 2, 3, NULL),
(5, 'Province five', 13, 4, NULL);
(1, 'Province one', 1, 1, NULL),
(2, 'Province two', 1, 1, NULL),
(3, 'Province three', 30, 2, NULL),
(4, 'Province four', 2, 3, NULL),
(5, 'Province five', 13, 4, NULL);
INSERT INTO `vn`.`town`(`id`, `name`, `provinceFk`)
VALUES
@ -361,11 +361,11 @@ INSERT INTO `vn`.`town`(`id`, `name`, `provinceFk`)
INSERT INTO `vn`.`postCode`(`code`, `townFk`, `geoFk`)
VALUES
('46000', 1, 6),
('46460', 2, 6),
('46680', 3, 6),
('46600', 4, 7),
('EC170150', 5, 8);
('46000', 1, 6),
('46460', 2, 6),
('46680', 3, 6),
('46600', 4, 7),
('EC170150', 5, 8);
INSERT INTO `vn`.`clientType`(`code`, `type`)
VALUES
@ -397,7 +397,7 @@ INSERT INTO `vn`.`client`(`id`,`name`,`fi`,`socialName`,`contact`,`street`,`city
(1107, 'Hank Pym', '09854837G', 'ANT MAN', 'Hawk', 'ANTHILL, SAN FRANCISCO, CALIFORNIA', 'Gotham', 46460, 1111111111, 222222222, 1, 'HankPym@mydomain.com', NULL, 0, 1234567890, 0, 1, 1, 300, 1, 1, NULL, 10, 5, util.VN_CURDATE(), 1, 5, 1, 1, 1, '0000-00-00', 1, 0, 0, NULL, 0, 0, 19, 0, 'florist','normal'),
(1108, 'Charles Xavier', '22641921P', 'PROFESSOR X', 'Beast', '3800 VICTORY PKWY, CINCINNATI, OH 45207, USA', 'Gotham', 46460, 1111111111, 222222222, 1, 'CharlesXavier@mydomain.com', NULL, 0, 1234567890, 0, 5, 1, 300, 13, 1, NULL, 10, 5, util.VN_CURDATE(), 1, 5, 1, 1, 1, '0000-00-00', 1, 1, 1, NULL, 0, 0, 19, 0, 'florist','normal'),
(1109, 'Bruce Banner', '16104829E', 'HULK', 'Black widow', 'SOMEWHERE IN NEW YORK', 'Gotham', 46460, 1111111111, 222222222, 1, 'BruceBanner@mydomain.com', NULL, 0, 1234567890, 0, 1, 1, 300, 1, 1, NULL, 10, 5, util.VN_CURDATE(), 1, 5, 1, 1, 1, '0000-00-00', 1, 0, 0, NULL, 0, 0, 9, 0, 'florist','normal'),
(1110, 'Jessica Jones', '58282869H', 'JESSICA JONES', 'Luke Cage', 'NYCC 2015 POSTER', 'Gotham', 46460, 1111111111, 222222222, 1, 'JessicaJones@mydomain.com', NULL, 0, 1234567890, 0, 1, 1, 300, 1, 1, NULL, 10, 5, util.VN_CURDATE(), 1, 5, 1, 1, 1, '0000-00-00', 1, 0, 0, NULL, 0, 0, NULL, 0, 'florist','normal'),
(1110, 'Jessica Jones', '58282869H', 'JESSICA JONES', 'Luke Cage', 'NYCC 2015 POSTER', 'Gotham', 46460, 1111111111, 222222222, 1, 'JessicaJones@mydomain.com', NULL, 0, 1234567890, 0, 1, 1, 300, 1, 1, NULL, 10, 5, util.VN_CURDATE(), 1, 5, 1, 1, 1, '0000-00-00', 1, 1, 0, NULL, 0, 0, NULL, 1, 'florist','normal'),
(1111, 'Missing', NULL, 'MISSING MAN', 'Anton', 'THE SPACE, UNIVERSE FAR AWAY', 'Gotham', 46460, 1111111111, 222222222, 1, NULL, NULL, 0, 1234567890, 0, 1, 1, 300, 1, 1, NULL, 10, 5, util.VN_CURDATE(), 1, 5, 1, 1, 1, '0000-00-00', 0, 1, 0, NULL, 1, 0, NULL, 0, 'others','loses'),
(1112, 'Trash', NULL, 'GARBAGE MAN', 'Unknown name', 'NEW YORK CITY, UNDERGROUND', 'Gotham', 46460, 1111111111, 222222222, 1, NULL, NULL, 0, 1234567890, 0, 1, 1, 300, 1, 1, NULL, 10, 5, util.VN_CURDATE(), 1, 5, 1, 1, 1, '0000-00-00', 0, 1, 0, NULL, 1, 0, NULL, 0, 'others','loses');
@ -821,10 +821,10 @@ INSERT INTO `vn`.`ticketTracking`(`ticketFk`, `stateFk`, `userFk`, `created`)
(12, 3, 19, util.VN_NOW()),
(13, 3, 19, util.VN_NOW()),
(14, 3, 19, util.VN_NOW()),
(15, 2, 19, util.VN_NOW()),
(15, 10, 19, util.VN_NOW()),
(16, 3, 19, util.VN_NOW()),
(17, 2, 19, util.VN_NOW()),
(18, 2, 19, util.VN_NOW()),
(37, 10, 19, util.VN_NOW()),
(19, 2, 19, util.VN_NOW()),
(20, 1, 19, DATE_ADD(util.VN_NOW(), INTERVAL +1 MONTH)),
(21, 1, 19, DATE_ADD(util.VN_NOW(), INTERVAL +1 MONTH)),
@ -1031,19 +1031,20 @@ INSERT INTO `vn`.`expeditionStateType`(`id`, `description`, `code`)
INSERT INTO `vn`.`expedition`(`id`, `agencyModeFk`, `ticketFk`, `freightItemFk`, `created`, `counter`, `workerFk`, `externalId`, `packagingFk`, `stateTypeFk`, `hostFk`)
VALUES
(1, 1, 1, 71, DATE_ADD(util.VN_CURDATE(), INTERVAL -1 MONTH), 1, 18, 'UR9000006041', 94, 1, 'pc1'),
(2, 1, 1, 71, DATE_ADD(util.VN_CURDATE(), INTERVAL -1 MONTH), 2, 18, 'UR9000006041', 94, 1, NULL),
(3, 1, 1, 71, DATE_ADD(util.VN_CURDATE(), INTERVAL -1 MONTH), 3, 18, 'UR9000006041', 94, 2, NULL),
(4, 1, 1, 71, DATE_ADD(util.VN_CURDATE(), INTERVAL -1 MONTH), 4, 18, 'UR9000006041', 94, 2, NULL),
(5, 1, 2, 71, DATE_ADD(util.VN_CURDATE(), INTERVAL -1 MONTH), 1, 18, NULL, 94, 3, NULL),
(6, 7, 3, 71, DATE_ADD(util.VN_CURDATE(), INTERVAL -2 MONTH), 1, 18, NULL, 94, 3, NULL),
(7, 2, 4, 71, DATE_ADD(util.VN_CURDATE(), INTERVAL -3 MONTH), 1, 18, NULL, 94, NULL,NULL),
(8, 3, 5, 71, DATE_ADD(util.VN_CURDATE(), INTERVAL -4 MONTH), 1, 18, NULL, 94, 1, NULL),
(9, 3, 6, 71, DATE_ADD(util.VN_CURDATE(), INTERVAL -1 MONTH), 1, 18, NULL, 94, 2, NULL),
(10, 7, 7, 71, util.VN_NOW(), 1, 18, NULL, 94, 3, NULL),
(11, 7, 8, 71, util.VN_NOW(), 1, 18, NULL, 94, 3, NULL),
(12, 7, 9, 71, util.VN_NOW(), 1, 18, NULL, 94, 3, NULL),
(13, 1, 10,71, util.VN_NOW(), 1, 18, NULL, 94, 3, NULL);
(1, 1, 1, 71, DATE_ADD(util.VN_CURDATE(), INTERVAL -1 MONTH), 1, 18, 'UR9000006041', 94, 1, 'pc1'),
(2, 1, 1, 71, DATE_ADD(util.VN_CURDATE(), INTERVAL -1 MONTH), 2, 18, 'UR9000006041', 94, 1, NULL),
(3, 1, 1, 71, DATE_ADD(util.VN_CURDATE(), INTERVAL -1 MONTH), 3, 18, 'UR9000006041', 94, 2, NULL),
(4, 1, 1, 71, DATE_ADD(util.VN_CURDATE(), INTERVAL -1 MONTH), 4, 18, 'UR9000006041', 94, 2, NULL),
(5, 1, 2, 71, DATE_ADD(util.VN_CURDATE(), INTERVAL -1 MONTH), 1, 18, NULL, 94, 3, NULL),
(6, 7, 3, 71, DATE_ADD(util.VN_CURDATE(), INTERVAL -2 MONTH), 1, 18, NULL, 94, 3, NULL),
(7, 2, 4, 71, DATE_ADD(util.VN_CURDATE(), INTERVAL -3 MONTH), 1, 18, NULL, 94, NULL,NULL),
(8, 3, 5, 71, DATE_ADD(util.VN_CURDATE(), INTERVAL -4 MONTH), 1, 18, NULL, 94, 1, NULL),
(9, 3, 6, 71, DATE_ADD(util.VN_CURDATE(), INTERVAL -1 MONTH), 1, 18, NULL, 94, 2, NULL),
(10, 7, 7, 71, util.VN_NOW(), 1, 18, NULL, 94, 3, NULL),
(11, 7, 8, 71, util.VN_NOW(), 1, 18, NULL, 94, 3, NULL),
(12, 7, 9, 71, util.VN_NOW(), 1, 18, NULL, 94, 3, NULL),
(13, 1, 10, 71, util.VN_NOW(), 1, 18, NULL, 94, 3, NULL),
(14, 1, 37, 71, util.VN_NOW(), 1, 18, NULL, 94, 3, NULL);
INSERT INTO `vn`.`expeditionState`(`id`, `created`, `expeditionFk`, `typeFk`, `userFk`)
@ -1478,7 +1479,8 @@ INSERT INTO `vn`.`ticketWeekly`(`ticketFk`, `weekDay`)
(2, 1),
(3, 2),
(5, 6),
(15, 6);
(15, 6),
(17, 6);
INSERT INTO `vn`.`awb` (id, code, package, weight, created, amount, transitoryFk, taxFk)
VALUES
@ -3970,6 +3972,6 @@ VALUES
(5, 'Referencia Nominas', 'payRef'),
(6, 'ABA', 'aba');
INSERT IGNORE INTO ormConfig
INSERT IGNORE INTO ormConfig
SET id =1,
selectLimit = 1000;

View File

@ -83,8 +83,10 @@ module.exports = Self => {
const invoiceOutSerial = await Self.app.models.InvoiceOutSerial.findById(serial);
if (invoiceOutSerial?.taxAreaFk == 'WORLD') {
const address = await Self.app.models.Address.findById(addressId);
if (!address || !address.customsAgentFk || !address.incotermsFk)
throw new UserError('The address of the customer must have information about Incoterms and Customs Agent');
if (!address?.customsAgentFk || !address.incotermsFk) {
throw new UserError(
'The address of the customer must have information about Incoterms and Customs Agent');
}
}
return serial;

View File

@ -39,7 +39,7 @@ describe('SalesMonitor salesFilter()', () => {
const filter = {};
const result = await models.SalesMonitor.salesFilter(ctx, filter, options);
expect(result.length).toBeGreaterThan(10);
expect(result.length).toBeGreaterThan(9);
await tx.rollback();
} catch (e) {
@ -146,13 +146,20 @@ describe('SalesMonitor salesFilter()', () => {
try {
const options = {transaction: tx};
const alertLevel = await models.AlertLevel.findOne({
where: {code: 'FREE'},
options
});
const alertLevelFree = alertLevel.id;
const ctx = {req: {accessToken: {userId: 9}}, args: {pending: false}};
const filter = {order: 'alertLevel ASC'};
const result = await models.SalesMonitor.salesFilter(ctx, filter, options);
const firstRow = result[0];
expect(result.length).toEqual(15);
expect(firstRow.alertLevel).not.toEqual(0);
result.forEach(row => {
expect(row.alertLevel).toBeGreaterThan(alertLevelFree);
});
await tx.rollback();
} catch (e) {

View File

@ -38,14 +38,17 @@ module.exports = Self => {
Object.assign(myOptions, options);
const where = buildFilter(ctx.args, (param, value) => {
switch (param) {
case 'search':
return {or: [
{'t.id': value},
{'c.id': value},
{'c.name': {like: `%${value}%`}}
]};
if (param === 'search') {
return {
or: [
{'t.id': value},
{'c.id': value},
{'c.name': {like: `%${value}%`}}
]
};
}
return {};
});
filter = mergeFilters(ctx.args.filter, {where});

View File

@ -13,11 +13,11 @@ describe('ticket-weekly filter()', () => {
const ctx = {req: {accessToken: {userId: authUserId}}, args: {filter: filter}};
const result = await models.TicketWeekly.filter(ctx, null, options);
const totalRecords = await models.TicketWeekly.count();
const firstRow = result[0];
expect(firstRow.ticketFk).toEqual(2);
expect(result.length).toEqual(4);
expect(result.length).toEqual(totalRecords);
await tx.rollback();
} catch (e) {

View File

@ -1,79 +0,0 @@
const closure = require('./closure');
module.exports = Self => {
Self.remoteMethodCtx('closeByAgency', {
description: 'Makes the closure process by agency mode',
accessType: 'WRITE',
accepts: [
{
arg: 'agencyModeFk',
type: ['number'],
required: true,
description: 'The agencies mode ids',
},
{
arg: 'warehouseFk',
type: 'number',
description: 'The ticket warehouse id',
required: true
},
{
arg: 'to',
type: 'date',
description: 'Max closure date',
required: true
}
],
returns: {
type: 'object',
root: true
},
http: {
path: `/close-by-agency`,
verb: 'POST'
}
});
Self.closeByAgency = async ctx => {
const args = ctx.args;
const tickets = await Self.rawSql(`
SELECT
t.id,
t.clientFk,
t.companyFk,
c.name clientName,
c.email recipient,
c.salesPersonFk,
c.isToBeMailed,
c.hasToInvoice,
co.hasDailyInvoice,
eu.email salesPersonEmail
FROM expedition e
JOIN ticket t ON t.id = e.ticketFk
JOIN ticketState ts ON ts.ticketFk = t.id
JOIN alertLevel al ON al.id = ts.alertLevel
JOIN client c ON c.id = t.clientFk
JOIN province p ON p.id = c.provinceFk
JOIN country co ON co.id = p.countryFk
LEFT JOIN account.emailUser eu ON eu.userFk = c.salesPersonFk
WHERE al.code = 'PACKED'
AND t.agencyModeFk IN(?)
AND t.warehouseFk = ?
AND DATE(t.shipped) BETWEEN DATE_ADD(?, INTERVAL -2 DAY)
AND util.dayEnd(?)
AND t.refFk IS NULL
GROUP BY e.ticketFk`, [
args.agencyModeFk,
args.warehouseFk,
args.to,
args.to
]);
await closure(Self, tickets);
return {
message: 'Success'
};
};
};

View File

@ -1,75 +0,0 @@
const closure = require('./closure');
const {Email} = require('vn-print');
module.exports = Self => {
Self.remoteMethodCtx('closeByRoute', {
description: 'Makes the closure process by route',
accessType: 'WRITE',
accepts: [
{
arg: 'routeFk',
type: 'number',
required: true,
description: 'The routes ids',
},
],
returns: {
type: 'object',
root: true
},
http: {
path: `/close-by-route`,
verb: 'POST'
}
});
Self.closeByRoute = async ctx => {
const args = ctx.args;
const tickets = await Self.rawSql(`
SELECT
t.id,
t.clientFk,
t.companyFk,
c.name clientName,
c.email recipient,
c.salesPersonFk,
c.isToBeMailed,
c.hasToInvoice,
co.hasDailyInvoice,
eu.email salesPersonEmail
FROM expedition e
JOIN ticket t ON t.id = e.ticketFk
JOIN ticketState ts ON ts.ticketFk = t.id
JOIN alertLevel al ON al.id = ts.alertLevel
JOIN client c ON c.id = t.clientFk
JOIN province p ON p.id = c.provinceFk
JOIN country co ON co.id = p.countryFk
LEFT JOIN account.emailUser eu ON eu.userFk = c.salesPersonFk
WHERE al.code = 'PACKED'
AND t.routeFk = ?
AND t.refFk IS NULL
GROUP BY e.ticketFk`, [args.routeFk]);
await closure(Self, tickets);
// Send route report to the agency
const [agencyMail] = await Self.rawSql(`
SELECT am.reportMail
FROM route r
JOIN agencyMode am ON am.id = r.agencyModeFk
WHERE r.id = ?`, [args.routeFk]);
if (agencyMail) {
const email = new Email('driver-route', {
id: args.routeFk,
recipient: agencyMail
});
await email.send();
}
return {
message: 'Success'
};
};
};

View File

@ -23,11 +23,25 @@ module.exports = Self => {
}
});
Self.closeByTicket = async ctx => {
Self.closeByTicket = async(ctx, options) => {
let tx;
const myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
}
const userId = ctx.req.accessToken.userId;
myOptions.userId = userId;
const args = ctx.args;
const tickets = await Self.rawSql(`
SELECT
SELECT
t.id,
t.clientFk,
t.companyFk,
@ -37,7 +51,8 @@ module.exports = Self => {
c.isToBeMailed,
c.hasToInvoice,
co.hasDailyInvoice,
eu.email salesPersonEmail
eu.email salesPersonEmail,
t.addressFk
FROM expedition e
JOIN ticket t ON t.id = e.ticketFk
JOIN ticketState ts ON ts.ticketFk = t.id
@ -49,9 +64,14 @@ module.exports = Self => {
WHERE al.code = 'PACKED'
AND t.id = ?
AND t.refFk IS NULL
GROUP BY e.ticketFk`, [args.id]);
GROUP BY e.ticketFk`,
[args.id],
myOptions);
await closure(Self, tickets);
await closure(ctx, Self, tickets, myOptions);
if (tx)
await tx.commit();
return {
message: 'Success'

View File

@ -1,13 +1,22 @@
/* eslint max-len: ["error", { "code": 150 }]*/
const Report = require('vn-print/core/report');
const Email = require('vn-print/core/email');
const smtp = require('vn-print/core/smtp');
const config = require('vn-print/core/config');
const storage = require('vn-print/core/storage');
module.exports = async function(ctx, Self, tickets, reqArgs = {}) {
module.exports = async function(ctx, Self, tickets, options) {
const userId = ctx.req.accessToken.userId;
const myOptions = {userId};
if (typeof options == 'object')
Object.assign(myOptions, options);
let tx;
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
}
if (tickets.length == 0) return;
const failedtickets = [];
@ -17,7 +26,7 @@ module.exports = async function(ctx, Self, tickets, reqArgs = {}) {
await Self.rawSql(
`CALL vn.ticket_closeByTicket(?)`,
[ticket.id],
{userId}
myOptions
);
const [invoiceOut] = await Self.rawSql(`
@ -26,7 +35,7 @@ module.exports = async function(ctx, Self, tickets, reqArgs = {}) {
JOIN invoiceOut io ON io.ref = t.refFk
JOIN company cny ON cny.id = io.companyFk
WHERE t.id = ?
`, [ticket.id]);
`, [ticket.id], myOptions);
const mailOptions = {
overrideAttachments: true,
@ -63,7 +72,7 @@ module.exports = async function(ctx, Self, tickets, reqArgs = {}) {
await Self.rawSql(
'UPDATE invoiceOut SET hasPdf = true WHERE id = ?',
[invoiceOut.id],
{userId},
myOptions
);
if (isToBeMailed) {
@ -108,7 +117,9 @@ module.exports = async function(ctx, Self, tickets, reqArgs = {}) {
WHERE t.clientFk = ?
AND NOT t.isDeleted
AND c.isVies
`, [ticket.clientFk]);
`,
[ticket.clientFk],
myOptions);
if (firstOrder == 1) {
const args = {
@ -127,20 +138,22 @@ module.exports = async function(ctx, Self, tickets, reqArgs = {}) {
SELECT id
FROM sample
WHERE code = 'incoterms-authorization'
`);
`,
null,
myOptions);
await Self.rawSql(`
INSERT INTO clientSample (clientFk, typeFk, companyFk) VALUES(?, ?, ?)
`, [ticket.clientFk, sample.id, ticket.companyFk], {userId});
`,
[ticket.clientFk, sample.id, ticket.companyFk],
myOptions);
}
} catch (error) {
// Domain not found
if (error.responseCode == 450) {
await invalidEmail(ticket);
continue;
}
// Save tickets on a list of failed ids
failedtickets.push({
id: ticket.id,
stacktrace: error,
@ -148,7 +161,6 @@ module.exports = async function(ctx, Self, tickets, reqArgs = {}) {
}
}
// Send email with failed tickets
if (failedtickets.length > 0) {
let body = 'This following tickets have failed:<br/><br/>';
@ -164,11 +176,14 @@ module.exports = async function(ctx, Self, tickets, reqArgs = {}) {
}).catch(err => console.error(err));
}
if (tx)
await tx.commit();
async function invalidEmail(ticket) {
await Self.rawSql(
`UPDATE client SET email = NULL WHERE id = ?`,
[ticket.clientFk],
{userId},
myOptions
);
const body = `No se ha podido enviar el albarán <strong>${ticket.id}</strong>

View File

@ -39,7 +39,7 @@ module.exports = Self => {
}, myOptions);
for (const ticketId of tickets) {
const ticket = await models.Ticket.findById(ticketId, myOptions);
const ticket = await models.Ticket.findById(ticketId, null, myOptions);
if (ticket.cmrFk) {
const hasDmsCmr = await Self.rawSql(`

View File

@ -0,0 +1,119 @@
const models = require('vn-loopback/server/server').models;
const LoopBackContext = require('loopback-context');
const smtp = require('vn-print/core/smtp');
const Email = require('vn-print/core/email');
const config = require('vn-print/core/config');
const closure = require('../closure');
fdescribe('Ticket closure functionality', () => {
const userId = 19;
const companyFk = 442;
const activeCtx = {
getLocale: () => 'es',
accessToken: {userId: userId},
headers: {origin: 'http://localhost:5000'},
};
let ctx = {req: activeCtx};
let tx;
let options;
beforeEach(async() => {
LoopBackContext.getCurrentContext = () => ({
active: activeCtx,
});
tx = await models.Sale.beginTransaction({});
options = {transaction: tx};
});
afterEach(async() => {
await tx.rollback();
});
it('should successfully close a ticket and not invoiced', async() => {
const ticketId = 15;
const tickets = [{
id: ticketId,
clientFk: 1101,
companyFk,
addressFk: 1,
isToBeMailed: true,
recipient: 'some@email.com',
salesPersonFk: userId
}];
const ticketStateBefore = await models.TicketState.findById(ticketId, null, options);
await closure(ctx, models.Ticket, tickets, options);
const ticketStateAfter = await models.TicketState.findById(ticketId, null, options);
expect(ticketStateBefore.code).not.toBe(ticketStateAfter.code);
const ticketAfter = await models.TicketState.findById(ticketId, null, options);
expect(ticketAfter.refFk).toBeUndefined();
});
fit('should send Incoterms authorization email on first order', async() => {
const ticketId = 37;
ctx.args = {
id: ticketId,
};
const ticket = await models.Ticket.findById(ticketId, null, options);
spyOn(Email.prototype, 'send').and.returnValue(Promise.resolve());
await models.Ticket.closeByTicket(ctx, options);
const sample = await models.Sample.findOne({
where: {code: 'incoterms-authorization'},
fields: ['id']
},
options);
const insertedSample = await models.ClientSample.findOne({
where: {
clientFk: ticket.clientFk,
typeFk: sample.id,
companyFk: companyFk
}
}, options);
expect(insertedSample.clientFk).toEqual(ticket.clientFk);
});
it('should report failed tickets and set client email to null', async() => {
const ticketId = 37;
const clientId = 1110;
const tickets = [{
id: ticketId,
clientFk: clientId,
companyFk,
addressFk: 1,
isToBeMailed: true,
recipient: 'invalid@example.com',
salesPersonFk: userId
}];
spyOn(Email.prototype, 'send').and.callFake(() => {
const error = new Error('Invalid email');
error.responseCode = 450;
return Promise.reject(error);
});
await closure(ctx, models.Ticket, tickets, options);
const client = await models.Client.findById(clientId, null, options);
expect(client.email).toBeNull();
const reportEmail = await smtp.send({
to: config.app.reportEmail,
subject: '[API] Nightly ticket closure report',
html: `This following tickets have failed:`
});
expect(reportEmail).not.toBeNull();
});
});

View File

@ -21,7 +21,7 @@ describe('ticket filter()', () => {
}
});
it('should return the tickets matching the problems on true', async() => {
it('should return at least one ticket matching the problems on true', async() => {
const tx = await models.Ticket.beginTransaction({});
try {
@ -41,7 +41,15 @@ describe('ticket filter()', () => {
const filter = {};
const result = await models.Ticket.filter(ctx, filter, options);
expect(result.length).toBeGreaterThan(3);
const hasProblemTicket = result.some(ticket =>
ticket.isFreezed === true ||
ticket.hasRisk === true ||
ticket.hasTicketRequest === true ||
(typeof ticket.hasRounding === 'string' && ticket.hasRounding.trim().length > 0) ||
(typeof ticket.itemShortage === 'string' && ticket.itemShortage.trim().length > 0)
);
expect(hasProblemTicket).toBe(true);
await tx.rollback();
} catch (e) {
@ -71,7 +79,13 @@ describe('ticket filter()', () => {
const filter = {};
const result = await models.Ticket.filter(ctx, filter, options);
expect(result.length).toEqual(11);
result.forEach(ticket => {
expect(ticket.isFreezed).toEqual(null);
expect(ticket.hasRisk).toEqual(null);
expect(ticket.hasTicketRequest).toEqual(null);
expect(ticket.itemShortage).toEqual(null);
expect(ticket.hasRounding).toEqual(null);
});
await tx.rollback();
} catch (e) {

View File

@ -65,7 +65,7 @@ describe('ticket isEditableOrThrow()', () => {
const options = {transaction: tx};
const ctx = {req: {accessToken: {userId: 1}}};
await models.Ticket.isEditableOrThrow(ctx, 15, options);
await models.Ticket.isEditableOrThrow(ctx, 17, options);
await tx.rollback();
} catch (e) {
error = e;

View File

@ -33,8 +33,6 @@ module.exports = function(Self) {
require('../methods/ticket/deliveryNoteCsvEmail')(Self);
require('../methods/ticket/closeAll')(Self);
require('../methods/ticket/closeByTicket')(Self);
require('../methods/ticket/closeByAgency')(Self);
require('../methods/ticket/closeByRoute')(Self);
require('../methods/ticket/getTicketsFuture')(Self);
require('../methods/ticket/merge')(Self);
require('../methods/ticket/getTicketsAdvance')(Self);

View File

@ -33,7 +33,7 @@ class Email extends Component {
const attachments = [];
const getAttachments = async(componentPath, files) => {
for (const file of files) {
const fileCopy = Object.assign({}, file);
const fileCopy = {...file};
const fileName = fileCopy.filename;
if (options.overrideAttachments && !fileName.includes('.png')) continue;