2019-01-24 08:08:28 +00:00
|
|
|
const app = require('vn-loopback/server/server');
|
2020-10-08 14:00:19 +00:00
|
|
|
const LoopBackContext = require('loopback-context');
|
2018-08-21 09:27:42 +00:00
|
|
|
|
2018-10-25 09:50:18 +00:00
|
|
|
describe('ticket changeState()', () => {
|
2020-10-08 14:00:19 +00:00
|
|
|
const salesPersonId = 18;
|
|
|
|
const employeeId = 1;
|
|
|
|
const productionId = 49;
|
|
|
|
let activeCtx = {
|
|
|
|
accessToken: {userId: 9},
|
|
|
|
};
|
|
|
|
let ctx = {req: activeCtx};
|
2018-10-25 09:50:18 +00:00
|
|
|
let ticket;
|
|
|
|
|
2019-03-01 10:50:26 +00:00
|
|
|
beforeAll(async done => {
|
2020-10-08 14:00:19 +00:00
|
|
|
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({
|
|
|
|
active: activeCtx
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
beforeEach(async done => {
|
2018-10-25 09:50:18 +00:00
|
|
|
let originalTicket = await app.models.Ticket.findOne({where: {id: 16}});
|
|
|
|
originalTicket.id = null;
|
|
|
|
ticket = await app.models.Ticket.create(originalTicket);
|
2019-03-01 10:50:26 +00:00
|
|
|
|
|
|
|
done();
|
2018-10-25 09:50:18 +00:00
|
|
|
});
|
|
|
|
|
2019-03-01 10:50:26 +00:00
|
|
|
afterAll(async done => {
|
2018-10-25 09:50:18 +00:00
|
|
|
await app.models.Ticket.destroyById(ticket.id);
|
2019-03-01 10:50:26 +00:00
|
|
|
|
|
|
|
done();
|
2018-10-25 09:50:18 +00:00
|
|
|
});
|
|
|
|
|
2019-09-26 07:05:18 +00:00
|
|
|
it('should throw if the ticket is not editable and the user isnt production', async() => {
|
2020-10-08 14:00:19 +00:00
|
|
|
activeCtx.accessToken.userId = salesPersonId;
|
2018-08-21 09:27:42 +00:00
|
|
|
let params = {ticketFk: 2, stateFk: 3};
|
2019-01-29 15:37:59 +00:00
|
|
|
|
|
|
|
let errCode;
|
2018-08-21 09:27:42 +00:00
|
|
|
try {
|
|
|
|
await app.models.TicketTracking.changeState(ctx, params);
|
|
|
|
} catch (e) {
|
2019-01-29 15:37:59 +00:00
|
|
|
errCode = e.code;
|
2018-08-21 09:27:42 +00:00
|
|
|
}
|
|
|
|
|
2019-01-29 15:37:59 +00:00
|
|
|
expect(errCode).toBe('ACCESS_DENIED');
|
2018-08-21 09:27:42 +00:00
|
|
|
});
|
|
|
|
|
2019-08-19 05:56:20 +00:00
|
|
|
it('should throw an error if a worker with employee role attemps to a forbidden state', async() => {
|
2020-10-08 14:00:19 +00:00
|
|
|
activeCtx.accessToken.userId = employeeId;
|
2019-08-19 05:56:20 +00:00
|
|
|
let params = {ticketFk: 11, stateFk: 13};
|
2019-01-29 15:37:59 +00:00
|
|
|
|
|
|
|
let errCode;
|
2019-01-22 09:04:42 +00:00
|
|
|
try {
|
|
|
|
await app.models.TicketTracking.changeState(ctx, params);
|
|
|
|
} catch (e) {
|
2019-01-29 15:37:59 +00:00
|
|
|
errCode = e.code;
|
2019-01-22 09:04:42 +00:00
|
|
|
}
|
|
|
|
|
2019-01-29 15:37:59 +00:00
|
|
|
expect(errCode).toBe('ACCESS_DENIED');
|
2019-01-22 09:04:42 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should be able to create a ticket tracking line for a not editable ticket if the user has the production role', async() => {
|
2020-10-08 14:00:19 +00:00
|
|
|
activeCtx.accessToken.userId = productionId;
|
2019-01-22 09:04:42 +00:00
|
|
|
let params = {ticketFk: ticket.id, stateFk: 3};
|
2018-08-21 09:27:42 +00:00
|
|
|
|
2020-10-08 14:00:19 +00:00
|
|
|
let ticketTracking = await app.models.TicketTracking.changeState(ctx, params);
|
2018-08-21 09:27:42 +00:00
|
|
|
|
2020-10-08 14:00:19 +00:00
|
|
|
expect(ticketTracking.__data.ticketFk).toBe(params.ticketFk);
|
|
|
|
expect(ticketTracking.__data.stateFk).toBe(params.stateFk);
|
|
|
|
expect(ticketTracking.__data.workerFk).toBe(49);
|
|
|
|
expect(ticketTracking.__data.id).toBeDefined();
|
2018-08-21 09:27:42 +00:00
|
|
|
|
2020-10-08 14:00:19 +00:00
|
|
|
// restores
|
|
|
|
await app.models.TicketTracking.destroyById(ticketTracking.__data.id);
|
2019-01-22 09:04:42 +00:00
|
|
|
});
|
|
|
|
|
2020-10-08 14:00:19 +00:00
|
|
|
it('should update the ticket tracking line when the user is salesperson, uses the state assigned and a valid worker id', async() => {
|
2019-01-22 09:04:42 +00:00
|
|
|
let ctx = {req: {accessToken: {userId: 18}}};
|
|
|
|
let assignedState = await app.models.State.findOne({where: {code: 'PICKER_DESIGNED'}});
|
|
|
|
let params = {ticketFk: ticket.id, stateFk: assignedState.id, workerFk: 1};
|
|
|
|
let res = await app.models.TicketTracking.changeState(ctx, params);
|
|
|
|
|
|
|
|
expect(res.__data.ticketFk).toBe(params.ticketFk);
|
|
|
|
expect(res.__data.stateFk).toBe(params.stateFk);
|
|
|
|
expect(res.__data.workerFk).toBe(params.workerFk);
|
|
|
|
expect(res.__data.workerFk).toBe(1);
|
2018-08-21 09:27:42 +00:00
|
|
|
expect(res.__data.id).toBeDefined();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|