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 userId = ctx.req.accessToken.userId;
// const salesDepartment = await models.Department.findOne({where: {code: 'VT'}, fields: 'id'}, 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 usesMana = await models.Sale.usesMana(ctx, myOptions);
const componentCode = usesMana ? 'mana' : 'buyerDiscount';
const discount = await models.Component.findOne({where: {code: componentCode}}, myOptions);
const componentId = discount.id;

View File

@ -1,7 +1,7 @@
module.exports = Self => {
Self.remoteMethodCtx('usesMana', {
description: 'Returns if the worker uses mana',
accessType: 'WRITE',
accessType: 'READ',
accepts: [],
returns: {
type: 'boolean',
@ -9,7 +9,7 @@ module.exports = Self => {
},
http: {
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 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);
return usesMana ? true : false;

View File

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