2021-09-29 06:27:18 +00:00
|
|
|
const models = require('vn-loopback/server/server').models;
|
2021-05-26 07:14:18 +00:00
|
|
|
|
|
|
|
describe('sale canEdit()', () => {
|
|
|
|
it('should return true if the role is production regardless of the saleTrackings', async() => {
|
2021-09-29 06:27:18 +00:00
|
|
|
const tx = await models.Sale.beginTransaction({});
|
2021-05-26 07:14:18 +00:00
|
|
|
|
2021-09-29 06:27:18 +00:00
|
|
|
try {
|
|
|
|
const options = {transaction: tx};
|
2021-05-26 07:14:18 +00:00
|
|
|
|
2021-09-29 06:27:18 +00:00
|
|
|
const productionUserID = 49;
|
|
|
|
const ctx = {req: {accessToken: {userId: productionUserID}}};
|
2021-05-26 07:14:18 +00:00
|
|
|
|
2022-10-05 13:13:36 +00:00
|
|
|
const sales = [25];
|
2021-09-29 06:27:18 +00:00
|
|
|
|
|
|
|
const result = await models.Sale.canEdit(ctx, sales, options);
|
|
|
|
|
|
|
|
expect(result).toEqual(true);
|
|
|
|
|
|
|
|
await tx.rollback();
|
|
|
|
} catch (e) {
|
|
|
|
await tx.rollback();
|
|
|
|
throw e;
|
|
|
|
}
|
2021-05-26 07:14:18 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should return true if the role is not production and none of the sales has saleTracking', async() => {
|
2021-09-29 06:27:18 +00:00
|
|
|
const tx = await models.Sale.beginTransaction({});
|
|
|
|
|
|
|
|
try {
|
|
|
|
const options = {transaction: tx};
|
|
|
|
|
|
|
|
const salesPersonUserID = 18;
|
|
|
|
const ctx = {req: {accessToken: {userId: salesPersonUserID}}};
|
2021-05-26 07:14:18 +00:00
|
|
|
|
2022-10-04 11:22:11 +00:00
|
|
|
const sales = [10];
|
2021-05-26 07:14:18 +00:00
|
|
|
|
2021-09-29 06:27:18 +00:00
|
|
|
const result = await models.Sale.canEdit(ctx, sales, options);
|
2021-05-26 07:14:18 +00:00
|
|
|
|
2021-09-29 06:27:18 +00:00
|
|
|
expect(result).toEqual(true);
|
|
|
|
|
|
|
|
await tx.rollback();
|
|
|
|
} catch (e) {
|
|
|
|
await tx.rollback();
|
|
|
|
throw e;
|
|
|
|
}
|
2021-05-26 07:14:18 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should return false if any of the sales has a saleTracking record', async() => {
|
2021-09-29 06:27:18 +00:00
|
|
|
const tx = await models.Sale.beginTransaction({});
|
|
|
|
|
|
|
|
try {
|
|
|
|
const options = {transaction: tx};
|
|
|
|
|
2022-10-05 13:13:36 +00:00
|
|
|
const buyerId = 35;
|
|
|
|
const ctx = {req: {accessToken: {userId: buyerId}}};
|
2021-09-29 06:27:18 +00:00
|
|
|
|
2022-10-05 13:13:36 +00:00
|
|
|
const sales = [31];
|
2021-05-26 07:14:18 +00:00
|
|
|
|
2021-09-29 06:27:18 +00:00
|
|
|
const result = await models.Sale.canEdit(ctx, sales, options);
|
2021-05-26 07:14:18 +00:00
|
|
|
|
2021-09-29 06:27:18 +00:00
|
|
|
expect(result).toEqual(false);
|
2021-05-26 07:14:18 +00:00
|
|
|
|
2021-09-29 06:27:18 +00:00
|
|
|
await tx.rollback();
|
|
|
|
} catch (e) {
|
|
|
|
await tx.rollback();
|
|
|
|
throw e;
|
|
|
|
}
|
2021-05-26 07:14:18 +00:00
|
|
|
});
|
2022-10-05 13:13:36 +00:00
|
|
|
|
|
|
|
it('should return false if any of the sales is cloned', async() => {
|
|
|
|
const tx = await models.Sale.beginTransaction({});
|
|
|
|
|
|
|
|
try {
|
|
|
|
const options = {transaction: tx};
|
|
|
|
|
|
|
|
const buyerId = 35;
|
|
|
|
const ctx = {req: {accessToken: {userId: buyerId}}};
|
|
|
|
|
2022-10-10 11:06:49 +00:00
|
|
|
const sales = [27];
|
2022-10-05 13:13:36 +00:00
|
|
|
|
|
|
|
const result = await models.Sale.canEdit(ctx, sales, options);
|
|
|
|
|
|
|
|
expect(result).toEqual(false);
|
|
|
|
|
|
|
|
await tx.rollback();
|
|
|
|
} catch (e) {
|
|
|
|
await tx.rollback();
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return true if any of the sales is cloned and has the correct role', async() => {
|
|
|
|
const tx = await models.Sale.beginTransaction({});
|
2022-10-18 09:50:16 +00:00
|
|
|
const roleEnabled = await models.ACL.findOne({
|
2022-10-10 11:06:49 +00:00
|
|
|
where: {
|
|
|
|
model: 'Sale',
|
|
|
|
property: 'editCloned'
|
|
|
|
}
|
|
|
|
});
|
2022-10-18 09:50:16 +00:00
|
|
|
if (!roleEnabled || !roleEnabled.principalId) return;
|
2022-10-06 13:04:27 +00:00
|
|
|
|
2022-10-05 13:13:36 +00:00
|
|
|
try {
|
|
|
|
const options = {transaction: tx};
|
|
|
|
|
2022-10-10 11:06:49 +00:00
|
|
|
const roleId = await models.Role.findOne({
|
|
|
|
where: {
|
2022-10-18 09:50:16 +00:00
|
|
|
name: roleEnabled.principalId
|
2022-10-10 11:06:49 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
const ctx = {req: {accessToken: {userId: roleId}}};
|
2022-10-05 13:13:36 +00:00
|
|
|
|
2022-10-10 11:06:49 +00:00
|
|
|
const sales = [27];
|
|
|
|
|
|
|
|
const result = await models.Sale.canEdit(ctx, sales, options);
|
|
|
|
|
|
|
|
expect(result).toEqual(true);
|
|
|
|
|
|
|
|
await tx.rollback();
|
|
|
|
} catch (e) {
|
|
|
|
await tx.rollback();
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return false if any of the sales is of ticket weekly', async() => {
|
|
|
|
const tx = await models.Sale.beginTransaction({});
|
|
|
|
|
|
|
|
try {
|
|
|
|
const options = {transaction: tx};
|
|
|
|
|
|
|
|
const employeeId = 1;
|
|
|
|
const ctx = {req: {accessToken: {userId: employeeId}}};
|
|
|
|
|
|
|
|
const sales = [33];
|
|
|
|
|
|
|
|
const result = await models.Sale.canEdit(ctx, sales, options);
|
|
|
|
|
|
|
|
expect(result).toEqual(false);
|
|
|
|
|
|
|
|
await tx.rollback();
|
|
|
|
} catch (e) {
|
|
|
|
await tx.rollback();
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return true if any of the sales is of ticketWeekly and has the correct role', async() => {
|
|
|
|
const tx = await models.Sale.beginTransaction({});
|
2022-10-18 09:50:16 +00:00
|
|
|
const roleEnabled = await models.ACL.findOne({
|
2022-10-10 11:06:49 +00:00
|
|
|
where: {
|
|
|
|
model: 'Sale',
|
|
|
|
property: 'editWeekly'
|
|
|
|
}
|
|
|
|
});
|
2022-10-18 09:50:16 +00:00
|
|
|
if (!roleEnabled || !roleEnabled.principalId) return;
|
2022-10-10 11:06:49 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
const options = {transaction: tx};
|
|
|
|
|
|
|
|
const roleId = await models.Role.findOne({
|
|
|
|
where: {
|
2022-10-18 09:50:16 +00:00
|
|
|
name: roleEnabled.principalId
|
2022-10-10 11:06:49 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
const ctx = {req: {accessToken: {userId: roleId}}};
|
|
|
|
|
|
|
|
const sales = [33];
|
2022-10-05 13:13:36 +00:00
|
|
|
|
|
|
|
const result = await models.Sale.canEdit(ctx, sales, options);
|
|
|
|
|
|
|
|
expect(result).toEqual(true);
|
|
|
|
|
|
|
|
await tx.rollback();
|
|
|
|
} catch (e) {
|
|
|
|
await tx.rollback();
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
});
|
2021-05-26 07:14:18 +00:00
|
|
|
});
|