/var/www/qr4y.com/server/delivery/api/controllers
NameSizeModeActions
account/-0755rm
auth/-0755rm
country_code/-0755rm
delivery_server/-0755rm
iap/-0755rm
order/-0755rm
qr/-0755rm
system/-0755rm
branch.controller.js298690644editdlrm
branchApp.controller.js46860644editdlrm
branchReview.controller.js63210644editdlrm
company.controller.js251110644editdlrm
companyReview.controller.js63680644editdlrm
companyTypes.controller.js31340644editdlrm
delivery.controller.js28270644editdlrm
deliveryTypes.controller.js23820644editdlrm
driver.controller.js45800644editdlrm
gallery.controller.js79290644editdlrm
language.controller.js27090644editdlrm
orderCancelCause.controller.js34420644editdlrm
payTypes.controller.js23140644editdlrm
permission.controller.js25900644editdlrm
plan.controller.js36180644editdlrm
planConfig.controller.js34530644editdlrm
product.controller.js134020644editdlrm
productFilters.controller.js33010644editdlrm
productGroups.controller.js65380644editdlrm
productReview.controller.js61640644editdlrm
productTypeFilters.controller.js29640644editdlrm
productTypeFiltersValues.controller.js31490644editdlrm
productTypes.controller.js26900644editdlrm
role.controller.js39820644editdlrm
room.controller.js34430644editdlrm
table.controller.js154540644editdlrm
user.controller.js255020644editdlrm
userCart.controller.js58270644editdlrm
waiter.controller.js61670644editdlrm
Edit: /var/www/qr4y.com/server/delivery/api/controllers/driver.controller.js (4580B)
const logger = require('../../logger')({ service: 'api' }) const { Op } = require('sequelize') const { NotFound, BadRequest, ApiError } = require('../responseModels/errors') const { isSet, isEmpty, isArray } = require('../utils/validator.util') const checkPermission = require('../utils/checkPermission.util') module.exports = function ({ db }) { this.List = async (req, res, next) => { logger.child({ path: req.path, request: req.query }).info() try { const t1 = performance.now() const { branchId, companyId, phone, email, name, limit, page } = req.query let required = { phones: false, companies: false, branches: false } let whereUser = {} if (isSet(name)) { whereUser['name'] = { [Op.like]: `%${name}%` } } if (isSet(email)) { whereUser['email'] = { [Op.like]: `%${email}%` } } let whereBranch = {} if (isSet(branchId)) { whereBranch['id'] = parseInt(branchId) required.branches = true } let whereCompany = {} if (isSet(companyId)) { whereCompany['id'] = parseInt(companyId) required.companies = true } let wherePhone = {} if (isSet(phone)) { wherePhone['phone'] = { [Op.like]: `%${phone}%` } required.phones = true } let offset = page ? (page - 1) * (limit || 10) : 0 const drivers = await db.driver.findAll({ include: [ { model: db.user, where: whereUser, include: [ { model: db.userPhone, required: required.phones, as: 'userPhones', where: wherePhone }, { model: db.branch, required: required.branches, as: 'branches', through: { attributes: [] }, where: whereBranch }, { model: db.company, required: required.companies, as: 'companies', through: { attributes: [] }, where: whereCompany } ] } ], offset, limit: parseInt(limit) || 10 }) if (!drivers || drivers.length == 0) { throw new NotFound({ lang: req.user?.languageCode }) } const time = performance.now() - t1 return res.status(200).json({ drivers, page, time }) } catch (err) { next(err) } } this.Details = async (req, res, next) => { try { const t1 = performance.now() const { id } = req.params if (!isSet(id) || isEmpty(id)) { throw new BadRequest({ code: 400, lang: req.user?.languageCode }) } const driver = await db.driver.findOne({ include: [{ model: db.user }], where: id }) if (!driver) { throw new NotFound({ lang: req.user?.languageCode }) } const time = performance.now() - t1 return res.status(200).json(driver) } catch (err) { next(err) } } this.Create = async (req, res, next) => { try { let { userId, description } = req.body if (!userId) { throw new BadRequest({ code: 400, lang: req.user?.languageCode }) } if (!description) { description = '' } const user = await db.user.findOne({where: {id: userId}}) if(!user){ throw new BadRequest({ code: 404, lang: req.user?.languageCode }) } const driver = await db.driver.create({ userId: user.id, description }) return res.status(200).json({ status: 'success', id: driver.id }) } catch (err) { next(err) } } this.Edit = async (req, res, next) => { try { const { id, status, description } = req.body if (!isSet(id) || isEmpty(id)) { throw new BadRequest({ code: 400, lang: req.user?.languageCode }) } if (!isSet(status) || isEmpty(status)) { throw new BadRequest({ code: 400, lang: req.user?.languageCode }) } if (!isSet(description) || isEmpty(description)) { throw new BadRequest({ code: 400, lang: req.user?.languageCode }) } const driver = await db.driver.findOne({ where: {id} }) if (!driver) { throw new BadRequest({ code: 404, lang: req.user?.languageCode }) } driver.status = status driver.description = description driver.save() return res.status(200).json(driver) } catch (err) { next(err) } } this.Delete = async (req, res, next) => { try { const { id } = req.body if (!isSet(id) || isEmpty(id)) { throw new BadRequest({ code: 400, lang: req.user?.languageCode }) } const driver = await db.driver.findOne({ where: { id: id } }) if (!driver) { throw new NotFound({ lang: req.user?.languageCode }) } await db.driver.destroy({ where: { id: id } }) return res.status(200).json({ status: 'success', id: driver.id }) } catch (err) { next(err) } } }