2021-09-29 06:27:18 +00:00
|
|
|
const models = require('vn-loopback/server/server').models;
|
2023-03-23 09:05:02 +00:00
|
|
|
const LoopBackContext = require('loopback-context');
|
2018-08-14 10:48:12 +00:00
|
|
|
|
2022-10-21 06:01:22 +00:00
|
|
|
describe('sale updatePrice()', () => {
|
2023-03-23 09:05:02 +00:00
|
|
|
beforeAll(async() => {
|
|
|
|
const activeCtx = {
|
|
|
|
accessToken: {userId: 18},
|
|
|
|
http: {
|
|
|
|
req: {
|
|
|
|
headers: {origin: 'http://localhost'}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({
|
|
|
|
active: activeCtx
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-09-29 06:27:18 +00:00
|
|
|
const ctx = {
|
|
|
|
req: {
|
|
|
|
accessToken: {userId: 18},
|
|
|
|
headers: {origin: 'localhost:5000'},
|
|
|
|
__: () => {}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const saleId = 7;
|
2018-08-14 10:48:12 +00:00
|
|
|
|
2019-06-03 06:01:24 +00:00
|
|
|
it('should throw an error if the ticket is not editable', async() => {
|
2021-09-29 06:27:18 +00:00
|
|
|
const tx = await models.Sale.beginTransaction({});
|
|
|
|
|
|
|
|
try {
|
|
|
|
const options = {transaction: tx};
|
|
|
|
|
|
|
|
const immutableSaleId = 1;
|
|
|
|
const price = 5;
|
|
|
|
|
|
|
|
await models.Sale.updatePrice(ctx, immutableSaleId, price, options);
|
|
|
|
|
|
|
|
await tx.rollback();
|
|
|
|
} catch (e) {
|
|
|
|
await tx.rollback();
|
|
|
|
error = e;
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(error).toEqual(new Error(`The sales of this ticket can't be modified`));
|
2019-05-29 12:59:26 +00:00
|
|
|
});
|
2018-08-14 10:48:12 +00:00
|
|
|
|
2019-05-29 12:59:26 +00:00
|
|
|
it('should return 0 if the price is an empty string', async() => {
|
2021-09-29 06:27:18 +00:00
|
|
|
const tx = await models.Sale.beginTransaction({});
|
|
|
|
|
|
|
|
try {
|
|
|
|
const options = {transaction: tx};
|
2021-07-29 15:40:45 +00:00
|
|
|
|
2021-09-29 06:27:18 +00:00
|
|
|
const price = '';
|
2019-06-03 06:01:24 +00:00
|
|
|
|
2021-09-29 06:27:18 +00:00
|
|
|
await models.Sale.updatePrice(ctx, saleId, price, options);
|
|
|
|
const updatedSale = await models.Sale.findById(saleId, null, options);
|
2020-10-08 14:00:19 +00:00
|
|
|
|
2021-09-29 06:27:18 +00:00
|
|
|
expect(updatedSale.price).toEqual(0);
|
2018-08-14 10:48:12 +00:00
|
|
|
|
2021-09-29 06:27:18 +00:00
|
|
|
await tx.rollback();
|
|
|
|
} catch (e) {
|
|
|
|
await tx.rollback();
|
|
|
|
throw e;
|
|
|
|
}
|
2018-08-14 10:48:12 +00:00
|
|
|
});
|
2019-05-29 12:59:26 +00:00
|
|
|
|
2020-10-08 14:00:19 +00:00
|
|
|
it('should now set price as a number in a string', async() => {
|
2021-09-29 06:27:18 +00:00
|
|
|
const tx = await models.Sale.beginTransaction({});
|
|
|
|
|
|
|
|
try {
|
|
|
|
const options = {transaction: tx};
|
2021-07-29 15:40:45 +00:00
|
|
|
|
2021-09-29 06:27:18 +00:00
|
|
|
const price = '8';
|
2019-05-29 12:59:26 +00:00
|
|
|
|
2021-09-29 06:27:18 +00:00
|
|
|
await models.Sale.updatePrice(ctx, saleId, price, options);
|
|
|
|
const updatedSale = await models.Sale.findById(saleId, null, options);
|
2020-10-08 14:00:19 +00:00
|
|
|
|
2021-09-29 06:27:18 +00:00
|
|
|
expect(updatedSale.price).toEqual(8);
|
2019-05-29 12:59:26 +00:00
|
|
|
|
2021-09-29 06:27:18 +00:00
|
|
|
await tx.rollback();
|
|
|
|
} catch (e) {
|
|
|
|
await tx.rollback();
|
|
|
|
throw e;
|
|
|
|
}
|
2019-05-29 12:59:26 +00:00
|
|
|
});
|
|
|
|
|
2024-10-25 06:47:43 +00:00
|
|
|
it('should check if priceFixed has changed', async() => {
|
|
|
|
const tx = await models.Sale.beginTransaction({});
|
|
|
|
|
|
|
|
try {
|
|
|
|
const options = {transaction: tx};
|
|
|
|
|
|
|
|
const price = 3;
|
|
|
|
const beforeUpdate = await models.Sale.findById(saleId, null, options);
|
|
|
|
await models.Sale.updatePrice(ctx, saleId, price, options);
|
|
|
|
const afterUpdate = await models.Sale.findById(saleId, null, options);
|
|
|
|
|
|
|
|
expect(beforeUpdate.priceFixed).not.toEqual(afterUpdate.priceFixed);
|
|
|
|
await tx.rollback();
|
|
|
|
} catch (e) {
|
|
|
|
await tx.rollback();
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-09-29 06:27:18 +00:00
|
|
|
it('should set price as a decimal number and check the sale has the mana component changing the salesPersonMana', async() => {
|
|
|
|
const tx = await models.Sale.beginTransaction({});
|
|
|
|
|
|
|
|
try {
|
|
|
|
const options = {transaction: tx};
|
|
|
|
|
|
|
|
const price = 5.4;
|
|
|
|
const originalSalesPersonMana = await models.WorkerMana.findById(18, null, options);
|
|
|
|
const manaComponent = await models.Component.findOne({where: {code: 'mana'}}, options);
|
2022-10-26 07:04:53 +00:00
|
|
|
const teamOne = 96;
|
2022-10-21 06:00:48 +00:00
|
|
|
const userId = ctx.req.accessToken.userId;
|
|
|
|
|
|
|
|
const business = await models.Business.findOne({where: {workerFk: userId}}, options);
|
2022-10-26 07:04:53 +00:00
|
|
|
await business.updateAttribute('departmentFk', teamOne, options);
|
2019-05-29 12:59:26 +00:00
|
|
|
|
2021-09-29 06:27:18 +00:00
|
|
|
await models.Sale.updatePrice(ctx, saleId, price, options);
|
|
|
|
const updatedSale = await models.Sale.findById(saleId, null, options);
|
2022-10-20 12:25:55 +00:00
|
|
|
const createdSaleComponent = await models.SaleComponent.findOne({
|
|
|
|
where: {
|
|
|
|
saleFk: saleId, componentFk: manaComponent.id
|
|
|
|
}}, options);
|
2019-05-29 12:59:26 +00:00
|
|
|
|
2021-09-29 06:27:18 +00:00
|
|
|
expect(updatedSale.price).toBe(price);
|
2023-09-20 06:12:07 +00:00
|
|
|
expect(createdSaleComponent.value).toEqual(-2.34);
|
2019-06-03 06:01:24 +00:00
|
|
|
|
2021-09-29 06:27:18 +00:00
|
|
|
const updatedSalesPersonMana = await models.WorkerMana.findById(18, null, options);
|
2019-05-29 12:59:26 +00:00
|
|
|
|
2021-09-29 06:27:18 +00:00
|
|
|
expect(updatedSalesPersonMana.amount).not.toEqual(originalSalesPersonMana.amount);
|
2020-10-08 14:00:19 +00:00
|
|
|
|
2021-09-29 06:27:18 +00:00
|
|
|
await tx.rollback();
|
|
|
|
} catch (e) {
|
|
|
|
await tx.rollback();
|
|
|
|
throw e;
|
|
|
|
}
|
2019-05-29 12:59:26 +00:00
|
|
|
});
|
2018-08-14 10:48:12 +00:00
|
|
|
});
|