119 lines
3.7 KiB
JavaScript
119 lines
3.7 KiB
JavaScript
const models = require('vn-loopback/server/server').models;
|
|
const LoopBackContext = require('loopback-context');
|
|
|
|
describe('ticket setDeleted()', () => {
|
|
const userId = 1106;
|
|
const employeeUser = 1110;
|
|
const activeCtx = {
|
|
accessToken: {userId: userId},
|
|
};
|
|
|
|
it('should throw an error if the given ticket has a claim', async() => {
|
|
const tx = await models.Ticket.beginTransaction({});
|
|
|
|
let error;
|
|
try {
|
|
const options = {transaction: tx};
|
|
|
|
const ctx = {req: activeCtx};
|
|
const ticketId = 16;
|
|
|
|
await models.Ticket.setDeleted(ctx, ticketId, options);
|
|
|
|
await tx.rollback();
|
|
} catch (e) {
|
|
await tx.rollback();
|
|
error = e;
|
|
}
|
|
|
|
expect(error.message).toEqual('You must delete the claim id %d first');
|
|
});
|
|
|
|
it('should delete the ticket, remove the stowaway link and change the stowaway ticket state to "FIXING" and get rid of the itemshelving', async() => {
|
|
const tx = await models.Ticket.beginTransaction({});
|
|
|
|
try {
|
|
const options = {transaction: tx};
|
|
|
|
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({
|
|
active: activeCtx
|
|
});
|
|
const ctx = {
|
|
req: {
|
|
accessToken: {userId: employeeUser},
|
|
headers: {
|
|
origin: 'http://localhost:5000'
|
|
},
|
|
__: () => {}
|
|
}
|
|
};
|
|
|
|
const sampleTicket = await models.Ticket.findById(12);
|
|
const sampleStowaway = await models.Ticket.findById(13);
|
|
|
|
sampleTicket.id = undefined;
|
|
const shipTicket = await models.Ticket.create(sampleTicket, options);
|
|
|
|
sampleStowaway.id = undefined;
|
|
const stowawayTicket = await models.Ticket.create(sampleStowaway, options);
|
|
|
|
await models.Stowaway.rawSql(`
|
|
INSERT INTO vn.stowaway(id, shipFk)
|
|
VALUES (?, ?)`, [stowawayTicket.id, shipTicket.id], options);
|
|
|
|
const boardingState = await models.State.findOne({
|
|
where: {
|
|
code: 'BOARDING'
|
|
}
|
|
}, options);
|
|
|
|
await models.TicketTracking.create({
|
|
ticketFk: stowawayTicket.id,
|
|
stateFk: boardingState.id,
|
|
workerFk: ctx.req.accessToken.userId
|
|
}, options);
|
|
|
|
const okState = await models.State.findOne({
|
|
where: {
|
|
code: 'OK'
|
|
}
|
|
}, options);
|
|
|
|
await models.TicketTracking.create({
|
|
ticketFk: shipTicket.id,
|
|
stateFk: okState.id,
|
|
workerFk: ctx.req.accessToken.userId
|
|
}, options);
|
|
|
|
let stowawayTicketState = await models.TicketState.findOne({
|
|
where: {
|
|
ticketFk: stowawayTicket.id
|
|
}
|
|
}, options);
|
|
|
|
let stowaway = await models.Stowaway.findById(shipTicket.id, null, options);
|
|
|
|
expect(stowaway).toBeDefined();
|
|
expect(stowawayTicketState.code).toEqual('BOARDING');
|
|
|
|
await models.Ticket.setDeleted(ctx, shipTicket.id, options);
|
|
|
|
stowawayTicketState = await models.TicketState.findOne({
|
|
where: {
|
|
ticketFk: stowawayTicket.id
|
|
}
|
|
}, options);
|
|
|
|
stowaway = await models.Stowaway.findById(shipTicket.id, null, options);
|
|
|
|
expect(stowaway).toBeNull();
|
|
expect(stowawayTicketState.code).toEqual('FIXING');
|
|
|
|
await tx.rollback();
|
|
} catch (e) {
|
|
await tx.rollback();
|
|
throw e;
|
|
}
|
|
});
|
|
});
|