118 lines
4.1 KiB
JavaScript
118 lines
4.1 KiB
JavaScript
const models = require('vn-loopback/server/server').models;
|
|
|
|
describe('claim regularizeClaim()', () => {
|
|
const userId = 18;
|
|
const ctx = beforeAll.mockLoopBackContext(userId);
|
|
ctx.req.__ = (value, params) => {
|
|
return params.nickname;
|
|
};
|
|
const chatModel = models.Chat;
|
|
const claimId = 1;
|
|
const ticketId = 1;
|
|
const pendentState = 1;
|
|
const resolvedState = 3;
|
|
const trashDestination = 2;
|
|
const okDestination = 1;
|
|
const trashAddress = 12;
|
|
let claimEnds = [];
|
|
let trashTicket;
|
|
|
|
async function importTicket(ticketId, claimId, userId, options) {
|
|
const ticketSales = await models.Sale.find({
|
|
where: {ticketFk: ticketId}
|
|
}, options);
|
|
const claimEnds = [];
|
|
for (let sale of ticketSales) {
|
|
claimEnds.push({
|
|
saleFk: sale.id,
|
|
claimFk: claimId,
|
|
workerFk: userId
|
|
});
|
|
}
|
|
|
|
return await models.ClaimEnd.create(claimEnds, options);
|
|
}
|
|
|
|
it('should send a chat message with value "Trash" and then change claim state to resolved', async() => {
|
|
const tx = await models.Claim.beginTransaction({});
|
|
|
|
try {
|
|
const options = {transaction: tx};
|
|
|
|
spyOn(chatModel, 'sendCheckingPresence').and.callThrough();
|
|
|
|
claimEnds = await importTicket(ticketId, claimId, userId, options);
|
|
|
|
for (const claimEnd of claimEnds)
|
|
await claimEnd.updateAttributes({claimDestinationFk: trashDestination}, options);
|
|
|
|
let claimBefore = await models.Claim.findById(claimId, null, options);
|
|
await models.Claim.regularizeClaim(ctx, claimId, options);
|
|
let claimAfter = await models.Claim.findById(claimId, null, options);
|
|
|
|
trashTicket = await models.Ticket.findOne({where: {addressFk: 12}}, options);
|
|
|
|
expect(trashTicket.addressFk).toEqual(trashAddress);
|
|
expect(claimBefore.claimStateFk).toEqual(pendentState);
|
|
expect(claimAfter.claimStateFk).toEqual(resolvedState);
|
|
expect(chatModel.sendCheckingPresence).toHaveBeenCalledWith(ctx, 18, 'Trash');
|
|
expect(chatModel.sendCheckingPresence).toHaveBeenCalledTimes(4);
|
|
|
|
await tx.rollback();
|
|
} catch (e) {
|
|
await tx.rollback();
|
|
throw e;
|
|
}
|
|
});
|
|
|
|
it('should send a chat message with value "Bueno" and then change claim state to resolved', async() => {
|
|
const tx = await models.Claim.beginTransaction({});
|
|
|
|
try {
|
|
const options = {transaction: tx};
|
|
|
|
spyOn(chatModel, 'sendCheckingPresence').and.callThrough();
|
|
|
|
claimEnds = await importTicket(ticketId, claimId, userId, options);
|
|
|
|
for (claimEnd of claimEnds)
|
|
await claimEnd.updateAttributes({claimDestinationFk: okDestination}, options);
|
|
|
|
await models.Claim.regularizeClaim(ctx, claimId, options);
|
|
|
|
expect(chatModel.sendCheckingPresence).toHaveBeenCalledWith(ctx, 18, 'Bueno');
|
|
expect(chatModel.sendCheckingPresence).toHaveBeenCalledTimes(4);
|
|
|
|
await tx.rollback();
|
|
} catch (e) {
|
|
await tx.rollback();
|
|
throw e;
|
|
}
|
|
});
|
|
|
|
it('should send a chat message to the salesPerson when claim isPickUp is enabled', async() => {
|
|
const tx = await models.Claim.beginTransaction({});
|
|
|
|
try {
|
|
const options = {transaction: tx};
|
|
|
|
spyOn(chatModel, 'sendCheckingPresence').and.callThrough();
|
|
|
|
claimEnds = await importTicket(ticketId, claimId, userId, options);
|
|
|
|
for (claimEnd of claimEnds)
|
|
await claimEnd.updateAttributes({claimDestinationFk: okDestination}, options);
|
|
|
|
await models.Claim.regularizeClaim(ctx, claimId, options);
|
|
|
|
expect(chatModel.sendCheckingPresence).toHaveBeenCalledWith(ctx, 18, 'Bueno');
|
|
expect(chatModel.sendCheckingPresence).toHaveBeenCalledTimes(4);
|
|
|
|
await tx.rollback();
|
|
} catch (e) {
|
|
await tx.rollback();
|
|
throw e;
|
|
}
|
|
});
|
|
});
|