2021-09-09 12:43:18 +00:00
|
|
|
const models = require('vn-loopback/server/server').models;
|
2021-03-24 11:45:26 +00:00
|
|
|
const LoopBackContext = require('loopback-context');
|
|
|
|
|
|
|
|
describe('Client Model', () => {
|
|
|
|
const activeCtx = {
|
|
|
|
accessToken: {userId: 9},
|
|
|
|
http: {
|
|
|
|
req: {
|
|
|
|
headers: {origin: 'http://localhost'},
|
|
|
|
[`__`]: value => {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const ctx = {req: activeCtx};
|
2021-09-09 12:43:18 +00:00
|
|
|
const chatModel = models.Chat;
|
|
|
|
const instance = {id: 1101, name: 'Bruce Banner'};
|
2021-06-23 11:24:23 +00:00
|
|
|
const previousWorkerId = 1106; // DavidCharlesHaller
|
|
|
|
const currentWorkerId = 1107; // HankPym
|
2021-03-24 11:45:26 +00:00
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({
|
|
|
|
active: activeCtx
|
|
|
|
});
|
2021-03-23 15:19:45 +00:00
|
|
|
});
|
|
|
|
|
2021-03-24 11:45:26 +00:00
|
|
|
describe('notifyAssignment()', () => {
|
|
|
|
it('should call to the Chat send() method for both workers', async() => {
|
|
|
|
spyOn(chatModel, 'send').and.callThrough();
|
2021-03-23 15:19:45 +00:00
|
|
|
|
2021-09-09 12:43:18 +00:00
|
|
|
await models.Client.notifyAssignment(instance, previousWorkerId, currentWorkerId);
|
2021-03-23 15:19:45 +00:00
|
|
|
|
2021-03-24 11:45:26 +00:00
|
|
|
expect(chatModel.send).toHaveBeenCalledWith(ctx, '@DavidCharlesHaller', `Client assignment has changed`);
|
|
|
|
expect(chatModel.send).toHaveBeenCalledWith(ctx, '@HankPym', `Client assignment has changed`);
|
2021-03-23 15:19:45 +00:00
|
|
|
});
|
|
|
|
|
2021-03-24 11:45:26 +00:00
|
|
|
it('should call to the Chat send() method for the previous worker', async() => {
|
|
|
|
spyOn(chatModel, 'send').and.callThrough();
|
2021-03-23 15:19:45 +00:00
|
|
|
|
2021-09-09 12:43:18 +00:00
|
|
|
await models.Client.notifyAssignment(instance, null, currentWorkerId);
|
2021-03-23 15:19:45 +00:00
|
|
|
|
2021-03-24 11:45:26 +00:00
|
|
|
expect(chatModel.send).toHaveBeenCalledWith(ctx, '@HankPym', `Client assignment has changed`);
|
|
|
|
});
|
2021-03-23 15:19:45 +00:00
|
|
|
|
2021-03-24 11:45:26 +00:00
|
|
|
it('should call to the Chat send() method for the current worker', async() => {
|
|
|
|
spyOn(chatModel, 'send').and.callThrough();
|
2021-03-23 15:19:45 +00:00
|
|
|
|
2021-09-09 12:43:18 +00:00
|
|
|
await models.Client.notifyAssignment(instance, previousWorkerId, null);
|
2021-03-23 15:19:45 +00:00
|
|
|
|
2021-03-24 11:45:26 +00:00
|
|
|
expect(chatModel.send).toHaveBeenCalledWith(ctx, '@DavidCharlesHaller', `Client assignment has changed`);
|
2021-03-23 15:19:45 +00:00
|
|
|
});
|
|
|
|
});
|
2021-09-09 12:43:18 +00:00
|
|
|
|
|
|
|
describe('changeCredit()', () => {
|
2022-09-14 10:32:19 +00:00
|
|
|
it('should fail to change the credit as a salesAssistant set to zero by a financialBoss', async() => {
|
2021-09-09 12:43:18 +00:00
|
|
|
const tx = await models.Client.beginTransaction({});
|
|
|
|
|
|
|
|
let error;
|
|
|
|
|
|
|
|
try {
|
|
|
|
const options = {transaction: tx};
|
2023-06-28 12:10:21 +00:00
|
|
|
const ctx = {options};
|
2021-09-09 12:43:18 +00:00
|
|
|
|
2023-01-23 14:24:00 +00:00
|
|
|
const financialBoss = await models.VnUser.findOne({
|
2021-09-09 12:43:18 +00:00
|
|
|
where: {name: 'financialBoss'}
|
|
|
|
}, options);
|
2023-06-28 12:10:21 +00:00
|
|
|
ctx.options.accessToken = {userId: financialBoss.id};
|
2021-09-09 12:43:18 +00:00
|
|
|
|
2023-11-02 13:05:14 +00:00
|
|
|
const testClient = await models.Client.findById(instance.id, options);
|
|
|
|
await testClient.updateAttributes({credit: 0}, ctx.options);
|
2021-09-09 12:43:18 +00:00
|
|
|
|
2023-01-23 14:24:00 +00:00
|
|
|
const salesAssistant = await models.VnUser.findOne({
|
2021-09-09 12:43:18 +00:00
|
|
|
where: {name: 'salesAssistant'}
|
|
|
|
}, options);
|
2023-06-28 12:10:21 +00:00
|
|
|
ctx.options.accessToken = {userId: salesAssistant.id};
|
2021-09-09 12:43:18 +00:00
|
|
|
|
2023-06-28 12:10:21 +00:00
|
|
|
await models.Client.changeCredit(ctx, instance, {credit: 300});
|
2021-09-09 12:43:18 +00:00
|
|
|
|
|
|
|
await tx.rollback();
|
|
|
|
} catch (e) {
|
|
|
|
error = e;
|
|
|
|
await tx.rollback();
|
|
|
|
}
|
|
|
|
|
2022-09-14 10:32:19 +00:00
|
|
|
expect(error.message).toEqual(`You can't change the credit set to zero from a financialBoss`);
|
2021-09-09 12:43:18 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should fail to change to a high credit amount as a salesAssistant', async() => {
|
|
|
|
const tx = await models.Client.beginTransaction({});
|
|
|
|
|
|
|
|
let error;
|
|
|
|
|
|
|
|
try {
|
|
|
|
const options = {transaction: tx};
|
2023-06-28 12:10:21 +00:00
|
|
|
const ctx = {options};
|
2021-09-09 12:43:18 +00:00
|
|
|
|
2023-01-23 14:24:00 +00:00
|
|
|
const salesAssistant = await models.VnUser.findOne({
|
2021-09-09 12:43:18 +00:00
|
|
|
where: {name: 'salesAssistant'}
|
|
|
|
}, options);
|
2023-06-28 12:10:21 +00:00
|
|
|
ctx.options.accessToken = {userId: salesAssistant.id};
|
2021-09-09 12:43:18 +00:00
|
|
|
|
2023-06-28 12:10:21 +00:00
|
|
|
await models.Client.changeCredit(ctx, instance, {credit: 99999});
|
2021-09-09 12:43:18 +00:00
|
|
|
|
|
|
|
await tx.rollback();
|
|
|
|
} catch (e) {
|
|
|
|
error = e;
|
|
|
|
await tx.rollback();
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(error.message).toEqual(`You don't have enough privileges to set this credit amount`);
|
|
|
|
});
|
|
|
|
});
|
2021-03-23 15:19:45 +00:00
|
|
|
});
|