120 lines
3.5 KiB
JavaScript
120 lines
3.5 KiB
JavaScript
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');
|
|
|
|
describe('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.Ticket.findById(ticketId, null, options);
|
|
|
|
expect(ticketAfter.refFk).toBeNull();
|
|
});
|
|
|
|
it('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();
|
|
});
|
|
});
|