refactor: unify duplicated code

This commit is contained in:
Vicent Llopis 2022-10-21 08:00:24 +02:00
parent 6891190c72
commit 5da29fe381
3 changed files with 5 additions and 16 deletions

View File

@ -78,13 +78,7 @@ module.exports = Self => {
const oldPrice = sale.price; const oldPrice = sale.price;
const userId = ctx.req.accessToken.userId; const userId = ctx.req.accessToken.userId;
// const salesDepartment = await models.Department.findOne({where: {code: 'VT'}, fields: 'id'}, myOptions); const usesMana = await models.Sale.usesMana(ctx, myOptions);
// const departments = await models.Department.getLeaves(salesDepartment.id, null, myOptions);
// const workerDepartment = await models.WorkerDepartment.findById(userId);
// const usesMana = departments.find(department => department.id == workerDepartment.departmentFk);
const usesMana = await models.Sale.usesMana();
console.log(usesMana ? 'mana' : 'buyerDiscount');
const componentCode = usesMana ? 'mana' : 'buyerDiscount'; const componentCode = usesMana ? 'mana' : 'buyerDiscount';
const discount = await models.Component.findOne({where: {code: componentCode}}, myOptions); const discount = await models.Component.findOne({where: {code: componentCode}}, myOptions);
const componentId = discount.id; const componentId = discount.id;

View File

@ -1,7 +1,7 @@
module.exports = Self => { module.exports = Self => {
Self.remoteMethodCtx('usesMana', { Self.remoteMethodCtx('usesMana', {
description: 'Returns if the worker uses mana', description: 'Returns if the worker uses mana',
accessType: 'WRITE', accessType: 'READ',
accepts: [], accepts: [],
returns: { returns: {
type: 'boolean', type: 'boolean',
@ -9,7 +9,7 @@ module.exports = Self => {
}, },
http: { http: {
path: `/usesMana`, path: `/usesMana`,
verb: 'POST' verb: 'GET'
} }
}); });
@ -23,7 +23,7 @@ module.exports = Self => {
const salesDepartment = await models.Department.findOne({where: {code: 'VT'}, fields: 'id'}, myOptions); const salesDepartment = await models.Department.findOne({where: {code: 'VT'}, fields: 'id'}, myOptions);
const departments = await models.Department.getLeaves(salesDepartment.id, null, myOptions); const departments = await models.Department.getLeaves(salesDepartment.id, null, myOptions);
const workerDepartment = await models.WorkerDepartment.findById(userId); const workerDepartment = await models.WorkerDepartment.findById(userId, null, myOptions);
const usesMana = departments.find(department => department.id == workerDepartment.departmentFk); const usesMana = departments.find(department => department.id == workerDepartment.departmentFk);
return usesMana ? true : false; return usesMana ? true : false;

View File

@ -98,12 +98,7 @@ module.exports = Self => {
if (isLocked || (!hasAllowedRoles && alertLevel > 0)) if (isLocked || (!hasAllowedRoles && alertLevel > 0))
throw new UserError(`The sales of this ticket can't be modified`); throw new UserError(`The sales of this ticket can't be modified`);
const usesMana = await models.WorkerMana.findOne({ const usesMana = await models.Sale.usesMana(ctx);
where: {
workerFk: userId
},
fields: 'amount'}, myOptions);
const componentCode = usesMana ? manaCode : 'buyerDiscount'; const componentCode = usesMana ? manaCode : 'buyerDiscount';
const discountComponent = await models.Component.findOne({ const discountComponent = await models.Component.findOne({
where: {code: componentCode}}, myOptions); where: {code: componentCode}}, myOptions);