/var/www/verywell.gr/server/delivery/api/controllers
NameSizeModeActions
account/-0755rm
tableReserv/-0755rm
auth.controller.js131870644editdlrm
branch.controller.js324430644editdlrm
branchReview.controller.js59750644editdlrm
category.controller.js55210644editdlrm
company.controller.js226320644editdlrm
companyReview.controller.js63960644editdlrm
companyTypes.controller.js27650644editdlrm
countryCodes.controller.js26480644editdlrm
delivery.controller.js76240644editdlrm
deliveryTypes.controller.js23970644editdlrm
driver.controller.js46030644editdlrm
language.controller.js27090644editdlrm
order.controller.js403260644editdlrm
orderCancelCause.controller.js34600644editdlrm
payTypes.controller.js23290644editdlrm
permission.controller.js26050644editdlrm
plan.controller.js36300644editdlrm
planConfig.controller.js34650644editdlrm
product.controller.js257930644editdlrm
productGroups.controller.js66180644editdlrm
productReview.controller.js59920644editdlrm
productTypeFeatures.controller.js27130644editdlrm
productTypes.controller.js41540644editdlrm
role.controller.js40120644editdlrm
room.controller.js34570644editdlrm
table.controller.js154950644editdlrm
user.controller.js222130644editdlrm
userCart.controller.js72310644editdlrm
waiter.controller.js61730644editdlrm
Edit: /var/www/verywell.gr/server/delivery/api/controllers/branch.controller.js (32443B)
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 SERVICES.AuthService.CheckUserPermissions({ user: req.user, key: 'branch_list', companyId }) 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.branchPrintTemplate, as: 'branchPrintTemplates' }, { model: db.account }, { 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 } }) branch.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 } })).map(x => { u = x.user u.role = x.role return u }) const branchPayTypes = await db.branchPayTypes.findAll({ include: [{ model: db.payType }, { model: db.deliveryType }], where: { branchId: branch.id } }) if (!branch) { throw new NotFound({ lang: req.user?.languageCode }) } branch.rooms = rooms 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() let transaction = await db.sequelize.transaction() 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) const account = await db.account.create({ branchId: branch?.id, type: ACCOUNT_TYPE.BRANCH }) if (!account) throw new NotFound({ code: 1030, lang: req.user?.languageCode }) await db.accountHistory.create({ createdId: req.user?.id, accountId: account.id, event: ACCOUNT_HISTORY_EVENT.CREATE }) const accountCash = await db.account.create({ branchId: branch?.id, type: ACCOUNT_TYPE.CASH }) if (!accountCash) throw new NotFound({ code: 1030, lang: req.user?.languageCode }) await db.accountHistory.create({ createdId: req.user?.id, accountId: accountCash.id, event: ACCOUNT_HISTORY_EVENT.CREATE }) await transaction.commit() await SERVICES.BranchService.AddBranch(branch) return res.status(200).json({ status: 'success', uid: branch.uid }) } catch (err) { transaction?.rollback() 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() 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() SERVICES.BranchService.ReloadBranch(branch.uid) 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() 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() 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() 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) } } // PRINT TEMPLATES this.SetPrintTemplate = async (req, res, next) => { logger.child({ path: req.path, request: req.body }).info() try { let t1 = performance.now() checkPermission(req, 'branch_edit') const { branchPrintTemplate } = req.body if (!isSet(branchPrintTemplate) || isEmpty(branchPrintTemplate)) { throw new BadRequest({ lang: req.user?.languageCode }) } let template = JSON.parse(branchPrintTemplate) if (!isSet(template.branchUid) || isEmpty(template.branchUid)) { throw new BadRequest({ lang: req.user?.languageCode }) } const branch = await db.branch.findOne({ where: { uid: template.branchUid } }) if (!branch) { throw new BadRequest({ lang: req.user?.languageCode }) } let result, temp template.branchId = branch.id if (template.uid != undefined) { temp = await db.branchPrintTemplate.findOne({ where: { uid: template.uid, branchId: branch.id } }) } if (temp) { temp.active = template.active temp.name = template.name temp.json = template.json temp.branchId = template.branchId temp.save() result = temp } else { delete template['branchUid'] delete template['uid'] result = await db.branchPrintTemplate.create(template) } const time = performance.now() - t1 return res.status(200).json({ status: 'success', uid: result.uid }) } 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, 'branch_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, 'branch_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, 'branch_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 }) } 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() SERVICES.BranchService.ReloadBranch(branch.uid) return res.status(200).json({ status: 'success' }) } catch (err) { next(err) } } this.ShiftClose = async (req, res, next) => { logger.child({ path: req.path, request: req.body }).info() let t1 = performance.now() let transaction = await db.sequelize.transaction() try { const { branchUid } = req.body if (!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 company = await db.company.findOne({ where: { id: branch.companyId } }) if (!company) throw new NotFound({ lang: req.user?.languageCode }) let branchShift = await db.branchShift.findOne({ where: { branchId: branch.id, isActive: true } }) if (!branchShift) throw new NotFound({ lang: req.user?.languageCode }) // снять с кассы const accountBranch = await db.account.findOne({ where: { branchId: branch.id, type: ACCOUNT_TYPE.CASH } }) if (!accountBranch) throw new NotFound({ code: 1030, lang: req.user?.languageCode }) const accountBranchBalance = parseFloat(accountBranch.balance) if (accountBranchBalance > 0) { accountBranch.balance = 0 await accountBranch.save() // сохранить историю await db.accountHistory.create({ accountId: accountBranch.id, summ: accountBranchBalance * -1, balanceBefore: accountBranchBalance, balanceAfter: 0, event: ACCOUNT_HISTORY_EVENT.TRANSFER }) // положить на счет компании const accountCompany = await db.account.findOne({ where: { companyId: company.id, type: ACCOUNT_TYPE.COMPANY } }) if (!accountCompany) throw new NotFound({ code: 1030, lang: req.user?.languageCode }) const accountCompanyBalance = parseFloat(accountCompany.balance) accountCompany.balance = parseFloat(accountCompanyBalance) + parseFloat(accountBranchBalance) await accountCompany.save() // сохранить историю await db.accountHistory.create({ accountId: accountCompany.id, summ: accountBranchBalance, balanceBefore: accountCompanyBalance, balanceAfter: parseFloat(accountCompanyBalance) + parseFloat(accountBranchBalance), event: ACCOUNT_HISTORY_EVENT.TRANSFER }) } branchShift.closedId = req.user.id branchShift.summ = accountBranchBalance branchShift.isActive = false await branchShift.save() await transaction.commit() const time = performance.now() - t1 return res.status(200).json({ status: 'success', uid: branchShift.uid, time }) } catch (err) { transaction?.rollback() next(err) } } this.ShiftOpen = async (req, res, next) => { logger.child({ path: req.path, request: req.body }).info() let t1 = performance.now() let transaction = await db.sequelize.transaction() try { const { branchUid } = req.body if (!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 company = await db.company.findOne({ where: { id: branch.companyId } }) if (!company) throw new NotFound({ lang: req.user?.languageCode }) let branchShift = await db.branchShift.findOne({ where: { branchId: branch.id, isActive: true } }) if (!branchShift) { branchShift = await db.branchShift.create({ branchId: branch.id, openedId: req.user.id, isActive: true }) } await transaction.commit() const time = performance.now() - t1 return res.status(200).json({ status: 'success', uid: branchShift.uid, time }) } catch (err) { transaction?.rollback() next(err) } } this.ShiftList = async (req, res, next) => { logger.child({ path: req.path, request: req.query }).info() try { let t1 = performance.now() //checkPermission(req, 'branch_list') // SERVICES.AuthService.CheckUserPermissions({ user: req.user, key: 'branch_list', companyId }) const { branchUid, openedId, closedId, isActive, page, limit } = req.query let where = {} if (branchUid) { const branch = await db.branch.findOne({ where: { uid: branchUid } }) if (!branch) throw new NotFound({ lang: req.user?.languageCode }) where['branchId'] = parseInt(branch.id) } if (openedId) { where['openedId'] = parseInt(openedId) } if (closedId) { where['closedId'] = parseInt(closedId) } if (isActive) { where['isActive'] = parseBool(isActive) } let offset = page ? (page - 1) * (limit || 10) : 0 const shifts = await db.branchShift.findAll({ where, offset, limit: parseInt(limit) || 10 }) if (!Array.isArray(shifts) || shifts.length == 0) { throw new NotFound({ lang: req.user?.languageCode }) } const time = performance.now() - t1 return res.status(200).json(shifts) } catch (err) { next(err) } } }