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');
|
2021-05-26 07:14:18 +00:00
|
|
|
|
|
|
|
describe('sale canEdit()', () => {
|
2022-10-31 13:13:06 +00:00
|
|
|
const employeeId = 1;
|
2023-03-23 09:05:02 +00:00
|
|
|
beforeAll(async() => {
|
|
|
|
const activeCtx = {
|
|
|
|
accessToken: {userId: 9},
|
|
|
|
http: {
|
|
|
|
req: {
|
|
|
|
headers: {origin: 'http://localhost'}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({
|
|
|
|
active: activeCtx
|
|
|
|
});
|
|
|
|
});
|
2021-05-26 07:14:18 +00:00
|
|
|
|
2022-10-31 13:13:06 +00:00
|
|
|
describe('sale editTracked', () => {
|
|
|
|
it('should return true if the role is production regardless of the saleTrackings', async() => {
|
|
|
|
const tx = await models.Sale.beginTransaction({});
|
2021-05-26 07:14:18 +00:00
|
|
|
|
2022-10-31 13:13:06 +00:00
|
|
|
try {
|
|
|
|
const options = {transaction: tx};
|
2021-05-26 07:14:18 +00:00
|
|
|
|
2022-10-31 13:13:06 +00:00
|
|
|
const productionUserID = 49;
|
|
|
|
const ctx = {req: {accessToken: {userId: productionUserID}}};
|
2021-09-29 06:27:18 +00:00
|
|
|
|
2022-10-31 13:13:06 +00:00
|
|
|
const sales = [25];
|
2021-09-29 06:27:18 +00:00
|
|
|
|
2022-10-31 13:13:06 +00:00
|
|
|
const result = await models.Sale.canEdit(ctx, sales, options);
|
2021-09-29 06:27:18 +00:00
|
|
|
|
2022-10-31 13:13:06 +00:00
|
|
|
expect(result).toEqual(true);
|
2021-05-26 07:14:18 +00:00
|
|
|
|
2022-10-31 13:13:06 +00:00
|
|
|
await tx.rollback();
|
|
|
|
} catch (e) {
|
|
|
|
await tx.rollback();
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
});
|
2021-09-29 06:27:18 +00:00
|
|
|
|
2022-10-31 13:13:06 +00:00
|
|
|
it('should return true if the role is not production and none of the sales has saleTracking', async() => {
|
|
|
|
const tx = await models.Sale.beginTransaction({});
|
2021-09-29 06:27:18 +00:00
|
|
|
|
2022-10-31 13:13:06 +00:00
|
|
|
try {
|
|
|
|
const options = {transaction: tx};
|
2021-05-26 07:14:18 +00:00
|
|
|
|
2022-10-31 13:13:06 +00:00
|
|
|
const salesPersonUserID = 18;
|
|
|
|
const ctx = {req: {accessToken: {userId: salesPersonUserID}}};
|
2021-05-26 07:14:18 +00:00
|
|
|
|
2022-10-31 13:13:06 +00:00
|
|
|
const sales = [10];
|
2021-05-26 07:14:18 +00:00
|
|
|
|
2022-10-31 13:13:06 +00:00
|
|
|
const result = await models.Sale.canEdit(ctx, sales, options);
|
2021-09-29 06:27:18 +00:00
|
|
|
|
2022-10-31 13:13:06 +00:00
|
|
|
expect(result).toEqual(true);
|
2021-05-26 07:14:18 +00:00
|
|
|
|
2022-10-31 13:13:06 +00:00
|
|
|
await tx.rollback();
|
|
|
|
} catch (e) {
|
|
|
|
await tx.rollback();
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2022-10-05 13:13:36 +00:00
|
|
|
|
2022-10-31 13:13:06 +00:00
|
|
|
describe('sale editCloned', () => {
|
2022-11-07 08:19:20 +00:00
|
|
|
const saleCloned = [29];
|
2022-10-06 13:04:27 +00:00
|
|
|
|
2022-10-31 13:13:06 +00:00
|
|
|
it('should return true if any of the sales is cloned and has the correct role', async() => {
|
|
|
|
const tx = await models.Sale.beginTransaction({});
|
|
|
|
const roleEnabled = await models.ACL.findOne({
|
2022-10-10 11:06:49 +00:00
|
|
|
where: {
|
2022-10-31 13:13:06 +00:00
|
|
|
model: 'Sale',
|
|
|
|
property: 'editCloned',
|
|
|
|
permission: 'ALLOW'
|
2022-10-10 11:06:49 +00:00
|
|
|
}
|
|
|
|
});
|
2022-10-31 13:13:06 +00:00
|
|
|
if (!roleEnabled || !roleEnabled.principalId) return await tx.rollback();
|
|
|
|
|
|
|
|
try {
|
|
|
|
const options = {transaction: tx};
|
|
|
|
|
|
|
|
const role = await models.Role.findOne({
|
|
|
|
where: {
|
|
|
|
name: roleEnabled.principalId
|
|
|
|
}
|
|
|
|
});
|
|
|
|
const ctx = {req: {accessToken: {userId: role.id}}};
|
2022-10-05 13:13:36 +00:00
|
|
|
|
2022-11-07 08:19:20 +00:00
|
|
|
const result = await models.Sale.canEdit(ctx, saleCloned, options);
|
2022-10-10 11:06:49 +00:00
|
|
|
|
2022-10-31 13:13:06 +00:00
|
|
|
expect(result).toEqual(true);
|
|
|
|
|
|
|
|
await tx.rollback();
|
|
|
|
} catch (e) {
|
|
|
|
await tx.rollback();
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
});
|
2022-10-10 11:06:49 +00:00
|
|
|
});
|
|
|
|
|
2022-10-31 13:13:06 +00:00
|
|
|
describe('sale editFloramondo', () => {
|
|
|
|
it('should return false if any of the sales isFloramondo', async() => {
|
|
|
|
const tx = await models.Sale.beginTransaction({});
|
2022-11-07 08:19:20 +00:00
|
|
|
const sales = [26];
|
2023-01-31 08:04:45 +00:00
|
|
|
let error;
|
2022-10-31 13:13:06 +00:00
|
|
|
try {
|
|
|
|
const options = {transaction: tx};
|
|
|
|
|
|
|
|
const ctx = {req: {accessToken: {userId: employeeId}}};
|
|
|
|
const saleToEdit = await models.Sale.findById(sales[0], null, options);
|
|
|
|
await saleToEdit.updateAttribute('itemFk', 9, options);
|
|
|
|
|
2023-01-31 08:04:45 +00:00
|
|
|
await models.Sale.canEdit(ctx, sales, options);
|
2022-10-31 13:13:06 +00:00
|
|
|
await tx.rollback();
|
|
|
|
} catch (e) {
|
|
|
|
await tx.rollback();
|
2023-01-31 08:04:45 +00:00
|
|
|
error = e;
|
2022-10-10 11:06:49 +00:00
|
|
|
}
|
2023-01-31 08:04:45 +00:00
|
|
|
|
|
|
|
expect(error).toEqual(
|
|
|
|
new Error('It is not possible to modify sales that their articles are from Floramondo'));
|
2022-10-10 11:06:49 +00:00
|
|
|
});
|
|
|
|
|
2022-10-31 13:13:06 +00:00
|
|
|
it('should return true if any of the sales is of isFloramondo and has the correct role', async() => {
|
|
|
|
const tx = await models.Sale.beginTransaction({});
|
2022-11-07 08:19:20 +00:00
|
|
|
const sales = [26];
|
2022-10-10 11:06:49 +00:00
|
|
|
|
2022-10-31 13:13:06 +00:00
|
|
|
const roleEnabled = await models.ACL.findOne({
|
2022-10-10 11:06:49 +00:00
|
|
|
where: {
|
2022-10-31 13:13:06 +00:00
|
|
|
model: 'Sale',
|
|
|
|
property: 'editFloramondo',
|
|
|
|
permission: 'ALLOW'
|
2022-10-10 11:06:49 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-10-31 13:13:06 +00:00
|
|
|
if (!roleEnabled || !roleEnabled.principalId) return await tx.rollback();
|
2022-10-05 13:13:36 +00:00
|
|
|
|
2022-10-31 13:13:06 +00:00
|
|
|
try {
|
|
|
|
const options = {transaction: tx};
|
2022-10-05 13:13:36 +00:00
|
|
|
|
2022-10-31 13:13:06 +00:00
|
|
|
const role = await models.Role.findOne({
|
|
|
|
where: {
|
|
|
|
name: roleEnabled.principalId
|
|
|
|
}
|
|
|
|
});
|
|
|
|
const ctx = {req: {accessToken: {userId: role.id}}};
|
2022-10-05 13:13:36 +00:00
|
|
|
|
2022-10-31 13:13:06 +00:00
|
|
|
// For test
|
|
|
|
const saleToEdit = await models.Sale.findById(sales[0], null, options);
|
|
|
|
await saleToEdit.updateAttribute('itemFk', 9, options);
|
|
|
|
|
|
|
|
const result = await models.Sale.canEdit(ctx, sales, options);
|
|
|
|
|
|
|
|
expect(result).toEqual(true);
|
|
|
|
|
|
|
|
await tx.rollback();
|
|
|
|
} catch (e) {
|
|
|
|
await tx.rollback();
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
});
|
2022-10-05 13:13:36 +00:00
|
|
|
});
|
2021-05-26 07:14:18 +00:00
|
|
|
});
|