194 lines
6.0 KiB
JavaScript
194 lines
6.0 KiB
JavaScript
const models = require('vn-loopback/server/server').models;
|
|
|
|
describe('sale canEdit()', () => {
|
|
const employeeId = 1;
|
|
|
|
describe('sale editTracked', () => {
|
|
it('should return true if the role is production regardless of the saleTrackings', async() => {
|
|
const tx = await models.Sale.beginTransaction({});
|
|
|
|
try {
|
|
const options = {transaction: tx};
|
|
|
|
const productionUserID = 49;
|
|
const ctx = {req: {accessToken: {userId: productionUserID}}};
|
|
|
|
const sales = [25];
|
|
|
|
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 true if the role is not production and none of the sales has saleTracking', async() => {
|
|
const tx = await models.Sale.beginTransaction({});
|
|
|
|
try {
|
|
const options = {transaction: tx};
|
|
|
|
const salesPersonUserID = 18;
|
|
const ctx = {req: {accessToken: {userId: salesPersonUserID}}};
|
|
|
|
const sales = [10];
|
|
|
|
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 has a saleTracking record', async() => {
|
|
const tx = await models.Sale.beginTransaction({});
|
|
|
|
try {
|
|
const options = {transaction: tx};
|
|
|
|
const buyerId = 35;
|
|
const ctx = {req: {accessToken: {userId: buyerId}}};
|
|
|
|
const sales = [31];
|
|
|
|
const result = await models.Sale.canEdit(ctx, sales, options);
|
|
|
|
expect(result).toEqual(false);
|
|
|
|
await tx.rollback();
|
|
} catch (e) {
|
|
await tx.rollback();
|
|
throw e;
|
|
}
|
|
});
|
|
});
|
|
|
|
describe('sale editCloned', () => {
|
|
const saleCloned = [29];
|
|
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}}};
|
|
|
|
const result = await models.Sale.canEdit(ctx, saleCloned, 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({});
|
|
const roleEnabled = await models.ACL.findOne({
|
|
where: {
|
|
model: 'Sale',
|
|
property: 'editCloned',
|
|
permission: 'ALLOW'
|
|
}
|
|
});
|
|
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}}};
|
|
|
|
const result = await models.Sale.canEdit(ctx, saleCloned, options);
|
|
|
|
expect(result).toEqual(true);
|
|
|
|
await tx.rollback();
|
|
} catch (e) {
|
|
await tx.rollback();
|
|
throw e;
|
|
}
|
|
});
|
|
});
|
|
|
|
describe('sale editFloramondo', () => {
|
|
it('should return false if any of the sales isFloramondo', async() => {
|
|
const tx = await models.Sale.beginTransaction({});
|
|
const sales = [26];
|
|
|
|
try {
|
|
const options = {transaction: tx};
|
|
|
|
const ctx = {req: {accessToken: {userId: employeeId}}};
|
|
|
|
// 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(false);
|
|
|
|
await tx.rollback();
|
|
} catch (e) {
|
|
await tx.rollback();
|
|
throw e;
|
|
}
|
|
});
|
|
|
|
it('should return true if any of the sales is of isFloramondo and has the correct role', async() => {
|
|
const tx = await models.Sale.beginTransaction({});
|
|
const sales = [26];
|
|
|
|
const roleEnabled = await models.ACL.findOne({
|
|
where: {
|
|
model: 'Sale',
|
|
property: 'editFloramondo',
|
|
permission: 'ALLOW'
|
|
}
|
|
});
|
|
|
|
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}}};
|
|
|
|
// 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;
|
|
}
|
|
});
|
|
});
|
|
});
|