/var/www/qr4y.gr/server/delivery/api/controllers
NameSizeModeActions
tableReserv/-0755rm
userAccount/-0755rm
auth.controller.js123060644editdlrm
branch.controller.js251870644editdlrm
branchReview.controller.js56200644editdlrm
category.controller.js55210644editdlrm
company.controller.js205950644editdlrm
companyReview.controller.js57730644editdlrm
companyTypes.controller.js27220644editdlrm
countryCodes.controller.js26480644editdlrm
delivery.controller.js74700644editdlrm
deliveryTypes.controller.js23970644editdlrm
driver.controller.js46030644editdlrm
language.controller.js27090644editdlrm
order.controller.js313010644editdlrm
orderCancelCause.controller.js34600644editdlrm
payTypes.controller.js22780644editdlrm
permission.controller.js26050644editdlrm
product.controller.js257930644editdlrm
productGroups.controller.js65680644editdlrm
productReview.controller.js52330644editdlrm
productTypeFeatures.controller.js27130644editdlrm
productTypes.controller.js41540644editdlrm
role.controller.js40120644editdlrm
room.controller.js34570644editdlrm
table.controller.js154950644editdlrm
user.controller.js217940644editdlrm
userCart.controller.js72310644editdlrm
waiter.controller.js61730644editdlrm
Edit: /var/www/qr4y.gr/server/delivery/api/controllers/branch.controller.js (25187B)
const logger = require('../../logger')({ service: 'api' }) const checkPermission = require('../utils/checkPermission.util') const { NotFound, BadRequest } = require('../responseModels/errors') const { isSet, isEmpty, minLength, isPhone, isEmail } = require('../utils/validator.util') const { performance } = require('perf_hooks') const { BranchModel, BranchListModel, BranchUsersListModel, BranchListModelByArea } = require('../responseModels') module.exports = function ({ db }) { this.List = async (req, res, next) => { logger.child({ path: req.path, request: req.query }).info() try { let t1 = performance.now() checkPermission(req, 'branch_list') const { companyId, page, limit } = req.query let where = {} if (companyId) { where['companyId'] = parseInt(companyId) } let offset = page ? (page - 1) * (limit || 10) : 0 const branches = await db.branch.findAll({ include: [db.workTime, db.deliveryType, db.address, db.shippingRate, db.room], where, offset, limit: parseInt(limit) || 10 }) if (!Array.isArray(branches) || branches.length == 0) { throw new NotFound({ lang: req.user?.languageCode }) } const time = performance.now() - t1 return res.status(200).json(new BranchListModel({ branches, time })) } catch (err) { next(err) } } this.ListByArea = async (req, res, next) => { logger.child({ path: req.path, request: req.query }).info() try { let t1 = performance.now() let { active, working, latitude, longtitude, companyTypeId, page, limit, sort } = req.query if (!isSet(latitude)) { throw new BadRequest({ lang: req.user?.languageCode }) } if (!isSet(longtitude)) { throw new BadRequest({ lang: req.user?.languageCode }) } if (!isSet(companyTypeId)) { companyTypeId = 0 } if (!isSet(page)) { page = 1 } if (!isSet(limit)) { limit = 10 } if (!isSet(sort)) { sort = 'asc' } const branches = await SERVICES.BranchService.GetBranchesByArea({ active, working, latitude, longtitude, companyTypeId, page, limit, sort }) const time = performance.now() - t1 return res .status(200) .json(new BranchListModelByArea({ branches, time, latitude, longtitude })) } catch (err) { next(err) } } this.Details = async (req, res, next) => { logger.child({ path: req.path, request: req.params }).info() try { let t1 = performance.now() checkPermission(req, 'branch_list') const { uid } = req.params if (!uid) { throw new BadRequest({ lang: req.user?.languageCode }) } let branch = await db.branch.findOne({ include: [ { model: db.company, as: 'company', include: [ // { model: db.companyType, as: 'companyTypes'}, // { model: db.productGroup, as: 'product_groups' }, { model: db.companyImage } ] }, { model: db.branchPrinter, as: 'branchPrinters' }, { model: db.address }, { model: db.workTime }, { model: db.deliveryType }, { model: db.shippingRate } // { model: db.room, // // include: { // // model: db.table // // } // }, // { // model: db.user, // include: [ // { model: db.userPhone, as: 'userPhones' }, // { model: db.language }, // { model: db.role } // ] // } ], where: { uid: uid } }) const rooms = await db.room.findAll({ include: [db.table], // order: [[db.table,'name']], where: { branchId: branch.id } }) const users = await db.branchUsers.findAll({ include: [ { model: db.user, include: [{ model: db.userPhone, as: 'userPhones' }, { model: db.language }] }, { model: db.role } ], where: { branchId: branch.id } }) const branchPayTypes = await db.branchPayTypes.findAll({ include: [{ model: db.payType }, { model: db.deliveryType }], where: { branchId: branch.id } }) u2 = users.map((user) => { u = user.user u.role = user.role return u }) if (!branch) { throw new NotFound({ lang: req.user?.languageCode }) } branch.rooms = rooms branch.users = u2 branch.deliveryTypes.map((dt) => { branchPayTypes.map((bpt) => { if (dt.id == bpt.deliveryType.id) { if (dt.payTypes == undefined) { dt.payTypes = [] } dt.payTypes.push(bpt.payType) } }) }) let discount = 0 const companyClient = await db.companyClients.findOne({ where: { companyId: branch.companyId, userId: req.user.id } }) if (companyClient) { discount = companyClient.discount } const time = performance.now() - t1 return res.status(200).json(new BranchModel(branch, time, discount)) } catch (err) { next(err) } } this.Create = async (req, res, next) => { logger.child({ path: req.path, request: req.body }).info() try { checkPermission(req, 'branch_edit') const { active, name, workMode, description, site, email, phone, area, companyId, minimum_order, wifi_enabled, wifi_show_pass, wifi_ssid, wifi_pass, wifi_type, wifi_hide } = req.body const branch = await db.branch.create({ active, name, workMode, description, site, email, phone, area, companyId, minimum_order, wifi_enabled, wifi_show_pass, wifi_ssid, wifi_pass, wifi_type, wifi_hide }) const user = await db.user.findOne({ where: { uid: req.user.uid } }) branch.addUser(user) await SERVICES.BranchService.AddBranch(branch) return res.status(200).json({ status: 'success', uid: branch.uid }) } catch (err) { next(err) } } this.Edit = async (req, res, next) => { logger.child({ path: req.path, request: req.body }).info() try { checkPermission(req, 'branch_edit') const { uid, active, name, workMode, description, site, email, phone, area, minimum_order, wifi_enabled, wifi_show_pass, wifi_ssid, wifi_pass, wifi_type, wifi_hide } = req.body const branch = await db.branch.findOne({ where: { uid: uid } }) if (!branch) { throw new NotFound({ lang: req.user?.languageCode }) } if (active) branch.active = active if (name) branch.name = name if (workMode) branch.work_mode = workMode if (description) branch.description = description if (site) branch.site = site if (email) branch.email = email if (phone) branch.phone = phone if (area) branch.area = area if (minimum_order) branch.minimum_order = minimum_order if (wifi_enabled) branch.wifi_enabled = wifi_enabled if (wifi_show_pass) branch.wifi_show_pass = wifi_show_pass if (wifi_ssid) branch.wifi_ssid = wifi_ssid if (wifi_pass) branch.wifi_pass = wifi_pass if (wifi_type) branch.wifi_type = wifi_type if (wifi_hide) branch.wifi_hide = wifi_hide await branch.save() await SERVICES.BranchService.ReloadBranch(branch.uid) return res.status(200).json(new BranchModel(branch)) } catch (err) { next(err) } } this.Delete = async (req, res, next) => { logger.child({ path: req.path, request: req.body }).info() try { checkPermission(req, 'branch_edit') const { uid } = req.body if (!isSet(uid)) { throw new BadRequest({ lang: req.user?.languageCode }) } const branch = await db.branch.findOne({ where: { uid: uid } }) if (!branch) { throw new NotFound({ lang: req.user?.languageCode }) } await SERVICES.BranchService.RemoveBranch(branch) await db.branch.destroy({ where: { uid: uid } }) return res.status(200).json({ status: 'success', uid: branch.uid }) } catch (err) { next(err) } } this.Users = async (req, res, next) => { logger.child({ path: req.path, request: req.query }).info() try { let t1 = performance.now() const { uid, roleInCompanyUid, limit, page } = req.query let offset = page ? (page - 1) * (limit || 10) : 0 let where = {} const branch = await db.branch.findOne({ where: { uid } }) if (!branch) { throw new BadRequest({ lang: req.user?.languageCode }) } where['branchId'] = branch.id if (roleInCompanyUid) { const role = await db.role.findOne({ where: { uid: roleInCompanyUid } }) if (!role) { throw new BadRequest({ lang: req.user?.languageCode }) } where['roleId'] = role.id } const rows = await db.branchUsers.findAll({ include: [ db.branch, db.role, { model: db.user, include: [db.role, db.userPhone, db.language] } ], where, offset, limit: parseInt(limit) || 10 }) if (!rows || rows.length == 0) { throw new NotFound({ lang: req.user?.languageCode }) } const time = performance.now() - t1 return res.status(200).json(new BranchUsersListModel({ rows, page, time })) } catch (err) { next(err) } } this.AddUser = async (req, res, next) => { logger.child({ path: req.path, request: req.body }).info() try { checkPermission(req, 'branch_edit') const { branchUid, userUid, roleUid } = req.body if (!isSet(branchUid) || isEmpty(branchUid)) { throw new BadRequest({ lang: req.user?.languageCode }) } if (!isSet(roleUid) || isEmpty(roleUid)) { throw new BadRequest({ lang: req.user?.languageCode }) } if (!isSet(userUid) || isEmpty(userUid)) { throw new BadRequest({ lang: req.user?.languageCode }) } const rows = await Promise.all([ db.branch.findOne({ where: { uid: branchUid } }), db.user.findOne({ where: { uid: userUid } }), db.role.findOne({ where: { uid: roleUid } }) ]) const [branch, user, role] = rows if (!branch) { throw new NotFound({ lang: req.user?.languageCode }) } if (!role) { throw new NotFound({ lang: req.user?.languageCode }) } if (!user) { throw new NotFound({ lang: req.user?.languageCode }) } let branchUser = await db.branchUsers.findOne({ where: { userId: user.id, branchId: branch.id, roleId: role.id } }) if (branchUser) { // запись существует throw new BadRequest({ lang: req.user?.languageCode }) } await db.branchUsers.create({ userId: user.id, branchId: branch.id, roleId: role.id }) return res.status(200).json({ status: 'success' }) } catch (err) { next(err) } } this.RemoveUser = async (req, res, next) => { logger.child({ path: req.path, request: req.body }).info() try { checkPermission(req, 'branch_edit') const { branchUid, userUid, roleUid } = req.body if (!isSet(branchUid) || isEmpty(branchUid)) { throw new BadRequest({ lang: req.user?.languageCode }) } if (!isSet(roleUid) || isEmpty(roleUid)) { throw new BadRequest({ lang: req.user?.languageCode }) } if (!isSet(userUid) || isEmpty(userUid)) { throw new BadRequest({ lang: req.user?.languageCode }) } const rows = await Promise.all([ db.branch.findOne({ where: { uid: branchUid } }), db.user.findOne({ where: { uid: userUid } }), db.role.findOne({ where: { uid: roleUid } }) ]) const [branch, user, role] = rows if (!branch) { throw new NotFound({ lang: req.user?.languageCode }) } if (!role) { throw new NotFound({ lang: req.user?.languageCode }) } if (!user) { throw new NotFound({ lang: req.user?.languageCode }) } let branchUser = await db.branchUsers.findOne({ where: { userId: user.id, branchId: branch.id, roleId: role.id } }) if (!branchUser) { // запись не существует throw new NotFound({ lang: req.user?.languageCode }) } await branchUser.destroy() return res.status(200).json({ status: 'success' }) } catch (err) { next(err) } } this.UpdateWorkTime = async (req, res, next) => { logger.child({ path: req.path, request: req.body }).info() try { checkPermission(req, 'branch_edit') const { branchUid, active, dayOfWeek, startTime, finishTime, startTimeBreak, finishTimeBreak } = req.body if (!isSet(branchUid) || isEmpty(branchUid)) { throw new BadRequest({ lang: req.user?.languageCode }) } const branch = await db.branch.findOne({ where: { uid: branchUid } }) if (!branch) { throw new NotFound({ lang: req.user?.languageCode }) } const workTime = await db.workTime.findOne({ where: { branchId: branch.id, dayOfWeek: dayOfWeek } }) if (!workTime) { const workTime = await db.workTime.create({ active, dayOfWeek, startTime, finishTime, startTimeBreak, finishTimeBreak, branchId: branch.id }) if (workTime) { return res.status(200).json({ status: 'success' }) } } else { workTime.active = active workTime.startTime = startTime workTime.finishTime = finishTime workTime.startTimeBreak = startTimeBreak workTime.finishTimeBreak = finishTimeBreak await workTime.save() if (workTime) { return res.status(200).json({ status: 'success' }) } } } catch (err) { next(err) } } this.SetAddress = async (req, res, next) => { logger.child({ path: req.path, request: req.body }).info() try { checkPermission(req, 'branch_edit') const { uid, address, latitude, longtitude, addressId, street, street_number, number, zip, comment, doorbell, floor } = req.body if (!isSet(address) || isEmpty(address)) { throw new BadRequest({ code: 1004, lang: req.user?.languageCode }) } var foundAddress if (isSet(addressId) || !isEmpty(addressId)) { foundAddress = await db.address.findOne({ where: { id: addressId } }) } if (foundAddress) { foundAddress.adres = address foundAddress.latitude = latitude foundAddress.longtitude = longtitude foundAddress.street = street foundAddress.street_number = street_number foundAddress.number = number foundAddress.zip = zip foundAddress.comment = comment foundAddress.doorbell = doorbell foundAddress.floor = floor await foundAddress.save() } else { foundAddress = await db.address.create({ adres: address, latitude: latitude, longtitude: longtitude, street: street, street_number: street_number, number: number, zip: zip, comment: comment, doorbell: doorbell, floor: floor // userId: user.id }) } const branch = await db.branch.findOne({ where: { uid: uid } }) if (!branch) { throw new NotFound({ lang: req.user?.languageCode }) } branch.addressId = foundAddress.id await branch.save() await SERVICES.BranchService.ReloadBranch(branch.uid) return res.status(200).json({ status: 'success', address: foundAddress }) } catch (err) { next(err) } } this.AddDeliveryType = async (req, res, next) => { logger.child({ path: req.path, request: req.body }).info() try { checkPermission(req, 'branch_edit') const { branchUid, deliveryTypeUid } = req.body if (!branchUid || !deliveryTypeUid) { throw new BadRequest({ lang: req.user.languageCode }) } let branch = await db.branch.findOne({ where: { uid: branchUid } }) const deliveryType = await db.deliveryType.findOne({ where: { uid: deliveryTypeUid } }) if (!branch) { throw new NotFound({ lang: req.user.languageCode }) } if (!deliveryType) { throw new NotFound({ lang: req.user.languageCode }) } branch.addDeliveryType(deliveryType) await branch.save() await SERVICES.BranchService.ReloadBranch(branch.uid) return res.status(200).json({ status: 'success' }) } catch (err) { next(err) } } this.RemoveDeliveryType = async (req, res, next) => { logger.child({ path: req.path, request: req.body }).info() try { checkPermission(req, 'branch_edit') const { branchUid, deliveryTypeUid } = req.body let branch = await db.branch.findOne({ where: { uid: branchUid } }) const deliveryType = await db.deliveryType.findOne({ where: { uid: deliveryTypeUid } }) if (!branch) { throw new NotFound({ lang: req.user.languageCode }) } if (!deliveryType) { throw new NotFound({ lang: req.user.languageCode }) } branch.removeDeliveryType(deliveryType) await branch.save() await SERVICES.BranchService.ReloadBranch(branch.uid) return res.status(200).json({ status: 'success' }) } catch (err) { next(err) } } this.ListFavorite = async (req, res, next) => { logger.child({ path: req.path, request: req.query }).info() try { let t1 = performance.now() const uid = req.user.uid const { limit, page } = req.query let offset = page ? (page - 1) * (limit || 10) : 0 const { rows } = await db.branch.findAndCountAll({ include: [ { model: db.company, as: 'company', paranoid: false, where: { deletedAt: null }, include: { model: db.companyImage } }, { model: db.user, through: 'favorite_branches', as: 'favoriteUsers', where: { uid } }, { model: db.address }, db.workTime, db.deliveryType, db.shippingRate ], where: {}, offset, limit: parseInt(limit) || 10 }) if (!rows || rows.length == 0) { throw new NotFound({ lang: req.user?.languageCode }) } const time = performance.now() - t1 return res .status(200) .json(new BranchListModel({ branches: rows, count: rows.length, page, time })) } catch (err) { next(err) } } this.AddFavorite = async (req, res, next) => { logger.child({ path: req.path, request: req.body }).info() try { const { branchUid, userUid } = req.body if (!isSet(branchUid) || !isSet(userUid) || isEmpty(branchUid) || isEmpty(userUid)) { throw new BadRequest({ lang: req.user?.languageCode }) } const branch = await db.branch.findOne({ where: { uid: branchUid } }) const user = await db.user.findOne({ where: { uid: userUid } }) if (!user || !branch) { throw new NotFound({ lang: req.user?.languageCode }) } user.addFavoriteBranch(branch) return res.status(200).json({ status: 'success' }) } catch (err) { next(err) } } this.RemoveFavorite = async (req, res, next) => { logger.child({ path: req.path, request: req.body }).info() try { const { branchUid, userUid } = req.body if (!isSet(branchUid) || !isSet(userUid) || isEmpty(branchUid) || isEmpty(userUid)) { throw new BadRequest({ lang: req.user?.languageCode }) } const branch = await db.branch.findOne({ where: { uid: branchUid } }) const user = await db.user.findOne({ where: { uid: userUid } }) if (!user || !branch) { throw new NotFound({ lang: req.user?.languageCode }) } user.removeFavoriteBranch(branch) return res.status(200).json({ status: 'success' }) } catch (err) { next(err) } } // PRINTERS this.AddPrinter = async (req, res, next) => { logger.child({ path: req.path, request: req.body }).info() try { let t1 = performance.now() checkPermission(req, 'product_edit') const { branchUid, branchPrinter } = req.body if (!isSet(branchUid) || isEmpty(branchUid)) { throw new BadRequest({ lang: req.user?.languageCode }) } if (!isSet(branchPrinter) || isEmpty(branchPrinter)) { throw new BadRequest({ lang: req.user?.languageCode }) } const branch = await db.branch.findOne({ where: { uid: branchUid } }) if (!branch) { throw new NotFound({ lang: req.user?.languageCode }) } let printer = JSON.parse(branchPrinter) printer.branchId = branch.id const result = await db.branchPrinter.create(printer) const time = performance.now() - t1 return res.status(200).json({ status: 'success', uid: result.uid }) } catch (err) { next(err) } } this.EditPrinter = async (req, res, next) => { logger.child({ path: req.path, request: req.body }).info() try { let t1 = performance.now() checkPermission(req, 'product_edit') const { printerUid, name, ip, port } = req.body if (!isSet(printerUid) || isEmpty(printerUid)) { throw new BadRequest({ lang: req.user?.languageCode }) } const branchPrinter = await db.branchPrinter.findOne({ where: { uid: printerUid } }) if (!branchPrinter) { throw new NotFound({ lang: req.user?.languageCode }) } branchPrinter.name = name branchPrinter.ip = ip branchPrinter.port = port branchPrinter.save() const time = performance.now() - t1 return res.status(200).json({ status: 'success', uid: branchPrinter.uid }) } catch (err) { next(err) } } this.RemovePrinter = async (req, res, next) => { logger.child({ path: req.path, request: req.body }).info() try { let t1 = performance.now() checkPermission(req, 'product_edit') const { printerUid } = req.body if (!isSet(printerUid) || isEmpty(printerUid)) { throw new BadRequest({ lang: req.user?.languageCode }) } await db.branchPrinter.destroy({ where: { uid: printerUid } }) const time = performance.now() - t1 return res.status(200).json({ status: 'success', uid: printerUid }) } catch (err) { next(err) } } this.ListPayTypes = async (req, res, next) => { logger.child({ path: req.path, request: req.query }).info() try { let t1 = performance.now() //checkPermission(req, 'branch_list') const { branchUid, page, limit } = req.query let where = {} if (branchUid) { const branch = await db.branch.findOne({ where: { uid: branchUid } }) if (!branch) throw new BadRequest({ lang: req.user?.languageCode }) where['branchId'] = branch.id } else { throw new BadRequest({ lang: req.user?.languageCode }) } let offset = page ? (page - 1) * (limit || 10) : 0 const rows = await db.branchPayTypes.findAll({ include: [db.payType, db.deliveryType], where, offset, limit: parseInt(limit) || 10 }) if (!Array.isArray(rows) || rows.length == 0) { throw new NotFound({ lang: req.user?.languageCode }) } const time = performance.now() - t1 return res.status(200).json({ list: rows.map((r) => { const { payType, deliveryType } = r return { id: r.id, deliveryType: { id: deliveryType.id, uid: deliveryType.uid, name: deliveryType.name, key: deliveryType.key }, payType: { id: payType.id, uid: payType.uid, name: payType.name, key: deliveryType.key } } }), time }) } catch (err) { next(err) } } this.AddPayType = async (req, res, next) => { logger.child({ path: req.path, request: req.body }).info() try { //checkPermission(req, 'deliveryType_edit') const { deliveryTypeUid, payTypeUid, branchUid } = req.body if (!deliveryTypeUid || !payTypeUid || !branchUid) { throw new BadRequest({ lang: req.user.languageCode }) } const deliveryType = await db.deliveryType.findOne({ where: { uid: deliveryTypeUid } }) if (!deliveryType) { throw new NotFound({ lang: req.user.languageCode }) } const payType = await db.payType.findOne({ where: { uid: payTypeUid } }) if (!payType) { throw new NotFound({ lang: req.user.languageCode }) } const branch = await db.branch.findOne({ where: { uid: branchUid } }) if (!branch) { throw new NotFound({ lang: req.user.languageCode }) } const branchPayType = await db.branchPayTypes.findOne({ where: { branchId: branch.id, deliveryTypeId: deliveryType.id, payTypeId: payType.id } }) if (!branchPayType) { await db.branchPayTypes.create({ branchId: branch.id, deliveryTypeId: deliveryType.id, payTypeId: payType.id }) } await SERVICES.BranchService.ReloadBranch(branch.uid) return res.status(200).json({ status: 'success' }) } catch (err) { next(err) } } this.RemovePayType = async (req, res, next) => { logger.child({ path: req.path, request: req.body }).info() try { //checkPermission(req, 'deliveryType_edit') const { deliveryTypeUid, payTypeUid, branchUid } = req.body if (!deliveryTypeUid || !payTypeUid || !branchUid) { throw new BadRequest({ lang: req.user.languageCode }) } const deliveryType = await db.deliveryType.findOne({ where: { uid: deliveryTypeUid } }) if (!deliveryType) { throw new NotFound({ lang: req.user.languageCode }) } const payType = await db.payType.findOne({ where: { uid: payTypeUid } }) if (!payType) { throw new NotFound({ lang: req.user.languageCode }) } const branch = await db.branch.findOne({ where: { uid: branchUid } }) if (!branch) { throw new NotFound({ lang: req.user.languageCode }) } const branchPayType = await db.branchPayTypes.findOne({ where: { branchId: branch.id, deliveryTypeId: deliveryType.id, payTypeId: payType.id } }) if (!branchPayType) { throw new NotFound({ lang: req.user.languageCode }) } await branchPayType.destroy() await SERVICES.BranchService.ReloadBranch(branch.uid) return res.status(200).json({ status: 'success' }) } catch (err) { next(err) } } }