salix/services/db/tests/vn/ticketCreateWithUser.spec.js

215 lines
6.9 KiB
JavaScript

const app = require(`../../../ticket/server/server`);
describe('ticket ticketCreateWithUser()', () => {
const today = new Date();
it('should confirm the procedure creates the expected ticket', async() => {
let params = {
clientFk: 101,
shipped: today,
warehouseFk: 1,
companyFk: 442,
addressFk: 1,
agencyModeFk: 2,
routeFk: null,
landed: today,
userId: 18
};
let query = `
START TRANSACTION;
CALL vn.ticketCreateWithUser(?, ?, ?, ?, ?, ?, ?, ?, ?, @newTicketId);
SELECT @newTicketId newTicketId;
SELECT * FROM vn.ticket WHERE id = @newTicketId;
ROLLBACK;`;
let result = await app.models.Ticket.rawSql(query, [
params.clientFk,
params.shipped,
params.warehouseFk,
params.companyFk,
params.addressFk,
params.agencyModeFk,
params.routeFk,
params.landed,
params.userId
]);
let ticketResult = result[2][0];
expect(ticketResult.newTicketId).toBeGreaterThan(21);
let createdTicket = result[3][0];
expect(createdTicket.clientFk).toEqual(params.clientFk);
expect(createdTicket.warehouseFk).toEqual(params.warehouseFk);
expect(createdTicket.companyFk).toEqual(params.companyFk);
expect(createdTicket.addressFk).toEqual(params.addressFk);
expect(createdTicket.agencyModeFk).toEqual(params.agencyModeFk);
expect(createdTicket.routeFk).toEqual(params.routeFk);
});
it('should confirm the procedure creates the expected observations in the ticket', async() => {
let params = {
clientFk: 101,
shipped: today,
warehouseFk: 1,
companyFk: 442,
addressFk: 1,
agencyModeFk: 2,
routeFk: null,
landed: today,
userId: 18
};
let query = `
START TRANSACTION;
CALL vn.ticketCreateWithUser(?, ?, ?, ?, ?, ?, ?, ?, ?, @newTicketId);
SELECT @newTicketId newTicketId;
SELECT * FROM vn.ticketObservation WHERE ticketFk = @newTicketId;
ROLLBACK;`;
let result = await app.models.Ticket.rawSql(query, [
params.clientFk,
params.shipped,
params.warehouseFk,
params.companyFk,
params.addressFk,
params.agencyModeFk,
params.routeFk,
params.landed,
params.userId
]);
let firstTicketObservation = result[3][0];
let secondTicketObservation = result[3][1];
let thirdTicketObservation = result[3][2];
expect(firstTicketObservation.observationTypeFk).toEqual(1);
expect(firstTicketObservation.description).toEqual('under the floor');
expect(secondTicketObservation.observationTypeFk).toEqual(2);
expect(secondTicketObservation.description).toEqual('wears leather and goes out at night');
expect(thirdTicketObservation.observationTypeFk).toEqual(3);
expect(thirdTicketObservation.description).toEqual('care with the dog');
});
it('should confirm the procedure sets companyFk as Tenerife if vProvinceName is SANTA CRUZ DE TENERIFE', async() => {
let params = {
clientFk: 101,
shipped: today,
warehouseFk: 1,
companyFk: 442,
addressFk: 1,
agencyModeFk: 2,
routeFk: null,
landed: today,
userId: 18
};
let tenerife = 1381;
let changeProvinceToTenerife = `UPDATE province p, client c SET p.name ='SANTA CRUZ DE TENERIFE' WHERE p.id = c.provinceFk AND c.id = ?;`;
let query = `
START TRANSACTION;
${changeProvinceToTenerife}
CALL vn.ticketCreateWithUser(?, ?, ?, ?, ?, ?, ?, ?, ?, @newTicketId);
SELECT @newTicketId newTicketId;
SELECT companyFk FROM vn.ticket WHERE id = @newTicketId;
ROLLBACK;`;
let result = await app.models.Ticket.rawSql(query, [
params.clientFk,
params.clientFk,
params.shipped,
params.warehouseFk,
params.companyFk,
params.addressFk,
params.agencyModeFk,
params.routeFk,
params.landed,
params.userId
]);
let ticket = result[4][0];
expect(ticket.companyFk).toEqual(tenerife);
});
it('should confirm the procedure sets address if it received it null', async() => {
let params = {
clientFk: 101,
shipped: today,
warehouseFk: 1,
companyFk: 442,
addressFk: 0,
agencyModeFk: 2,
routeFk: null,
landed: today,
userId: 18
};
let query = `
START TRANSACTION;
CALL vn.ticketCreateWithUser(?, ?, ?, ?, ?, ?, ?, ?, ?, @newTicketId);
SELECT @newTicketId newTicketId;
SELECT addressFk FROM vn.ticket WHERE id = @newTicketId;
SELECT id FROM vn.address WHERE clientFk = ? AND isDefaultAddress = 1;
ROLLBACK;`;
let result = await app.models.Ticket.rawSql(query, [
params.clientFk,
params.shipped,
params.warehouseFk,
params.companyFk,
params.addressFk,
params.agencyModeFk,
params.routeFk,
params.landed,
params.userId,
params.clientFk
]);
let ticket = result[2][0];
let clientDefaultAddress = result[3][0];
expect(ticket.addressFk).toEqual(clientDefaultAddress.id);
});
it('should confirm the procedure creates the state as delivered if the client has isCreatedAsServed TRUE', async() => {
let params = {
clientFk: 101,
shipped: today,
warehouseFk: 1,
companyFk: 442,
addressFk: 0,
agencyModeFk: 2,
routeFk: null,
landed: today,
userId: 18
};
let setAsIsDelivered = `UPDATE vn.client SET isCreatedAsServed = 1 WHERE id = ?;`;
let query = `
START TRANSACTION;
${setAsIsDelivered}
CALL vn.ticketCreateWithUser(?, ?, ?, ?, ?, ?, ?, ?, ?, @newTicketId);
SELECT @newTicketId newTicketId;
SELECT code FROM vn.ticketState WHERE ticketFk = @newTicketId;
ROLLBACK;`;
let result = await app.models.Ticket.rawSql(query, [
params.clientFk,
params.clientFk,
params.shipped,
params.warehouseFk,
params.companyFk,
params.addressFk,
params.agencyModeFk,
params.routeFk,
params.landed,
params.userId,
params.clientFk
]);
let ticketState = result[4][0];
expect(ticketState.code).toEqual('DELIVERED');
});
});