salix/back/methods/mrw-config/specs/createShipment.spec.js

163 lines
5.3 KiB
JavaScript
Raw Normal View History

2024-02-07 15:52:43 +00:00
const models = require('vn-loopback/server/server').models;
const axios = require('axios');
2024-02-16 07:28:40 +00:00
const fs = require('fs');
2024-02-07 15:52:43 +00:00
2024-06-14 15:40:08 +00:00
const filter = {notificationFk: 'mrw-deadline'};
2024-02-07 15:52:43 +00:00
const mockBase64Binary = 'base64BinaryString';
const ticket1 = {
'id': '44',
'clientFk': 1101,
'shipped': Date.vnNew(),
'nickname': 'MRW',
'addressFk': 1,
2024-02-12 08:24:37 +00:00
'agencyModeFk': 999
2024-02-07 15:52:43 +00:00
};
const expedition1 = {
2024-02-16 13:15:13 +00:00
'id': 17,
2024-02-12 08:24:37 +00:00
'agencyModeFk': 999,
2024-02-07 15:52:43 +00:00
'ticketFk': 44,
'freightItemFk': 71,
'created': '2001-01-01',
'counter': 1,
'workerFk': 18,
'packagingFk': '94',
'hostFk': '',
'stateTypeFk': 3,
'hasNewRoute': 0,
'isBox': 71,
'editorFk': 100
};
2024-02-16 13:15:13 +00:00
2024-06-10 10:36:15 +00:00
describe('MRWConfig createShipment()', () => {
2024-06-10 10:24:23 +00:00
beforeAll(async() => {
2024-02-12 08:24:37 +00:00
await models.Agency.create(
2024-06-10 10:24:23 +00:00
{'id': 999, 'name': 'mrw'}
2024-02-12 08:24:37 +00:00
);
await models.AgencyMode.create(
2024-06-10 10:24:23 +00:00
{'id': 999, 'name': 'mrw', 'agencyFk': 999, 'code': 'mrw'}
2024-02-07 15:52:43 +00:00
2024-02-12 08:24:37 +00:00
);
2024-02-07 15:52:43 +00:00
2024-06-10 10:24:23 +00:00
await createMrwConfig();
2024-02-12 08:24:37 +00:00
await models.Application.rawSql(
`INSERT INTO vn.mrwService
2024-02-16 07:28:40 +00:00
SET agencyModeCodeFk = 'mrw',
clientType = 1,
serviceType = 1,
2024-06-10 10:24:23 +00:00
kg = 1`, null
2024-02-12 08:24:37 +00:00
);
2024-06-10 10:24:23 +00:00
await models.Ticket.create(ticket1);
await models.Expedition.create(expedition1);
2024-02-19 10:33:56 +00:00
});
2024-06-14 15:40:08 +00:00
afterAll(async() => {
await cleanFixtures();
await models.Ticket.destroyAll(ticket1);
await models.Expedition.destroyAll(ticket1);
2024-06-14 15:40:08 +00:00
});
beforeEach(async() => {
2024-02-19 10:33:56 +00:00
const mockPostResponses = [
{data: fs.readFileSync(__dirname + '/mockGetLabel.xml', 'utf-8')},
{data: fs.readFileSync(__dirname + '/mockCreateShipment.xml', 'utf-8')}
];
spyOn(axios, 'post').and.callFake(() => Promise.resolve(mockPostResponses.pop()));
2024-06-14 15:40:08 +00:00
await cleanFixtures();
2024-06-10 10:24:23 +00:00
});
2024-02-07 15:52:43 +00:00
2024-06-14 15:40:08 +00:00
async function cleanFixtures() {
await models.NotificationQueue.destroyAll(filter);
await models.MrwConfig.updateAll({id: 1}, {expeditionDeadLine: null, notified: null});
}
2024-06-10 10:24:23 +00:00
async function createMrwConfig() {
await models.MrwConfig.create(
{
'id': 1,
'url': 'https://url.com',
'user': 'user',
'password': 'password',
'franchiseCode': 'franchiseCode',
'subscriberCode': 'subscriberCode'
}
);
}
2024-06-14 15:40:08 +00:00
async function getLastNotification() {
return models.NotificationQueue.findOne({
order: 'id DESC',
where: filter
});
}
2024-06-10 10:24:23 +00:00
it('should create a shipment and return a base64Binary label', async() => {
const {file} = await models.MrwConfig.createShipment(expedition1.id);
2024-02-07 15:52:43 +00:00
2024-05-20 11:58:32 +00:00
expect(file).toEqual(mockBase64Binary);
2024-02-07 15:52:43 +00:00
});
2024-02-12 08:24:37 +00:00
2024-02-16 13:15:13 +00:00
it('should fail if mrwConfig has no data', async() => {
let error;
2024-06-10 10:24:23 +00:00
await models.MrwConfig.destroyAll();
2024-02-16 13:15:13 +00:00
await models.MrwConfig.createShipment(expedition1.id).catch(e => {
error = e;
}).finally(async() => {
2024-06-14 15:40:08 +00:00
expect(error.message).toEqual(`MRW service is not configured`);
2024-02-16 13:15:13 +00:00
});
2024-06-10 10:24:23 +00:00
await createMrwConfig();
2024-02-16 07:28:40 +00:00
2024-02-16 13:15:13 +00:00
expect(error).toBeDefined();
});
2024-02-16 07:28:40 +00:00
2024-02-16 13:15:13 +00:00
it('should fail if expeditionFk is not a MrwExpedition', async() => {
let error;
2024-06-10 10:24:23 +00:00
await models.MrwConfig.createShipment(undefined).catch(e => {
2024-02-16 13:15:13 +00:00
error = e;
}).finally(async() => {
expect(error.message).toEqual(`This expedition is not a MRW shipment`);
});
});
2024-06-10 10:24:23 +00:00
it('should fail if the creation date of this ticket is before the current date', async() => {
2024-02-16 13:15:13 +00:00
let error;
const yesterday = Date.vnNew();
yesterday.setDate(yesterday.getDate() - 1);
2024-06-10 10:24:23 +00:00
await models.Ticket.updateAll({id: ticket1.id}, {shipped: yesterday});
await models.MrwConfig.createShipment(expedition1.id).catch(e => {
2024-02-16 13:15:13 +00:00
error = e;
}).finally(async() => {
expect(error.message).toEqual(`This ticket has a shipped date earlier than today`);
});
2024-06-10 10:24:23 +00:00
await models.Ticket.updateAll({id: ticket1.id}, {shipped: Date.vnNew()});
});
it('should send mail if you are past the dead line and is not notified today', async() => {
await models.MrwConfig.updateAll({id: 1}, {expeditionDeadLine: '10:00:00', notified: null});
await models.MrwConfig.createShipment(expedition1.id);
2024-06-14 15:40:08 +00:00
const notification = await getLastNotification();
2024-06-10 10:24:23 +00:00
2024-06-14 15:40:08 +00:00
expect(notification.notificationFk).toEqual(filter.notificationFk);
2024-06-10 10:24:23 +00:00
});
it('should send mail if you are past the dead line and it is notified from another day', async() => {
await models.MrwConfig.updateAll({id: 1}, {expeditionDeadLine: '10:00:00', notified: new Date()});
await models.MrwConfig.createShipment(expedition1.id);
2024-06-14 15:40:08 +00:00
const notification = await getLastNotification();
2024-06-10 10:24:23 +00:00
2024-06-14 15:40:08 +00:00
expect(notification.notificationFk).toEqual(filter.notificationFk);
2024-06-10 10:24:23 +00:00
});
it('should not send mail if you are past the dead line and it is notified', async() => {
await models.MrwConfig.updateAll({id: 1}, {expeditionDeadLine: '10:00:00', notified: Date.vnNew()});
await models.MrwConfig.createShipment(expedition1.id);
2024-06-14 15:40:08 +00:00
const notification = await getLastNotification();
2024-06-10 10:24:23 +00:00
2024-06-14 15:40:08 +00:00
expect(notification).toEqual(null);
2024-02-16 13:15:13 +00:00
});
});