/
var
/
www
/
greso.tech
/
server
/
nsm
/
controllers
/
report
/
/var/www/greso.tech/server/nsm/controllers/report
mkdir
upload
Name
Size
Mode
Actions
leads_source.js
3012
0644
edit
dl
rm
saleman_permormance.js
4907
0644
edit
dl
rm
Edit:
/var/www/greso.tech/server/nsm/controllers/report/saleman_permormance.js
(4907B)
module.exports = function ({ db, logger }) { const { NotFound, BadRequest } = require('../../responseModels/errors') const { isSet, isEmail, isPhone, isEmpty, minLength, isNumber, isStatusBiggerThen } = require('../../utils/validator.util') const { SalemanPerformanceListModel } = require('../../responseModels') const { Op } = require('sequelize') return async (req, res, next) => { logger.child({ path: req.path, request: req.query }).info() let t1 = performance.now() try { let { limit, page, dateFrom, dateTo } = req.body let offset = page ? (page - 1) * (limit || 100) : 0 let where = {} const salesmanRole = await SERVICES.AuthService.GetRoleByKey('sales_rep') if (!salesmanRole) { throw new NotFound({ lang: req.user?.languageCode }) } // LOGIC --> let companies = [] let salemans = [] let salemansIds = [] let total = { id: '', name: 'Total', totalIn: 0, emailsSent: 0, estimatesSent: 0, booked: 0, local: 0, long: 0, canceled: 0, bookedEstimatesSum: 0, customerPayments: 0, refund: 0, } // const branchUsers = req.user.branches // let branches = {} // for (let branchUser of branchUsers) { // branches[branchUser.branchId] = { totalIn: 0 } // } // Get user companies const companyUsers = req.user.companies for (let companyUser of companyUsers) { if (companyUser.roleId == salesmanRole.id) { if (companies.indexOf(companyUser.companyId) === -1) { companies.push(companyUser.companyId) } } } for (let companyId of companies) { const users = await db.companyUsers.findAll({ where: { companyId: companyId, roleId: salesmanRole.id }, }) for (let user of users) { if (salemansIds.indexOf(user.userId) === -1) { const userEmail = await db.userEmail.findOne({ where: { userId: user.userId }, }) salemans.push({ id: user.userId, name: userEmail.name ?? '-', totalIn: 0, emailsSent: 0, estimatesSent: 0, booked: 0, local: 0, long: 0, canceled: 0, bookedEstimatesSum: 0, customerPayments: 0, refund: 0, }) salemansIds.push(user.userId) } } } salemans.push(total) if (!isSet(dateTo) || isEmpty(dateTo)) { dateTo = dateFrom } where['createdAt'] = { [Op.between]: [new Date(dateFrom + " 00:00:00"), new Date(dateTo + " 23:59:59")] } if (salemansIds.length > 0) { where['salesRepId'] = { [Op.in]: salemansIds } } // const count = await db.order.count(); const orders = await db.order.findAll({ where: where, offset, limit: parseInt(limit) || 100 }) // if (!orders || orders.length == 0) { // throw new NotFound({ lang: req.user?.languageCode }) // } const count = orders.length for (let saleman of salemans) { for (let order of orders) { if (order.salesRepId == saleman.id) { const company = await SERVICES.CompanyService.GetCompany(order.companyId) if (company) { if (company.long_distance) { saleman.long++; total.long++; } else { saleman.local++; total.local++; } } saleman.totalIn++; total.totalIn++; if ( isStatusBiggerThen(order.status, 'estimate' )) { saleman.estimatesSent++; total.estimatesSent++; } if ( isStatusBiggerThen(order.status, 'booked' )) { saleman.booked++; total.booked++; } if ( order.status == 'cancel' || order.status == 'deleted' ) { saleman.canceled++; total.canceled++; } // inventory const inventory = await db.orderInventory.findOne({ where: { "id": order.orderInventoryId } }); if (inventory) { saleman.bookedEstimatesSum = saleman.bookedEstimatesSum + parseFloat(inventory.total) total.bookedEstimatesSum = total.bookedEstimatesSum + parseFloat(inventory.total) } // payments const payments = await db.orderPayment.findAll({ where: { "orderId": order.id } }); let summ = 0 for (let payment of payments) { if (payment.confirmed) { summ = summ + parseFloat(payment.summ) } } saleman.customerPayments = saleman.customerPayments + summ total.customerPayments = total.customerPayments + summ // Email count const emailsCount = await db.orderMessage.count({ where: { "orderId": order.id, "type": "email" } }); saleman.emailsSent = saleman.emailsSent + emailsCount total.emailsSent = total.emailsSent + emailsCount } } } const time = performance.now() - t1 return res.status(200).json(new SalemanPerformanceListModel({ rows: salemans, page, limit, count, time })) } catch (err) { next(err) } } }
Save
cmd:
run