/
var
/
www
/
qr4y.com
/
server
/
delivery
/
api
/
controllers
/
/var/www/qr4y.com/server/delivery/api/controllers
mkdir
upload
Name
Size
Mode
Actions
account/
-
0755
rm
auth/
-
0755
rm
country_code/
-
0755
rm
delivery_server/
-
0755
rm
iap/
-
0755
rm
order/
-
0755
rm
qr/
-
0755
rm
system/
-
0755
rm
branch.controller.js
29869
0644
edit
dl
rm
branchApp.controller.js
4686
0644
edit
dl
rm
branchReview.controller.js
6321
0644
edit
dl
rm
company.controller.js
25111
0644
edit
dl
rm
companyReview.controller.js
6368
0644
edit
dl
rm
companyTypes.controller.js
3134
0644
edit
dl
rm
delivery.controller.js
2827
0644
edit
dl
rm
deliveryTypes.controller.js
2382
0644
edit
dl
rm
driver.controller.js
4580
0644
edit
dl
rm
gallery.controller.js
7929
0644
edit
dl
rm
language.controller.js
2709
0644
edit
dl
rm
orderCancelCause.controller.js
3442
0644
edit
dl
rm
payTypes.controller.js
2314
0644
edit
dl
rm
permission.controller.js
2590
0644
edit
dl
rm
plan.controller.js
3618
0644
edit
dl
rm
planConfig.controller.js
3453
0644
edit
dl
rm
product.controller.js
13402
0644
edit
dl
rm
productFilters.controller.js
3301
0644
edit
dl
rm
productGroups.controller.js
6538
0644
edit
dl
rm
productReview.controller.js
6164
0644
edit
dl
rm
productTypeFilters.controller.js
2964
0644
edit
dl
rm
productTypeFiltersValues.controller.js
3149
0644
edit
dl
rm
productTypes.controller.js
2690
0644
edit
dl
rm
role.controller.js
3982
0644
edit
dl
rm
room.controller.js
3443
0644
edit
dl
rm
table.controller.js
15454
0644
edit
dl
rm
user.controller.js
25502
0644
edit
dl
rm
userCart.controller.js
5827
0644
edit
dl
rm
waiter.controller.js
6167
0644
edit
dl
rm
Edit:
/var/www/qr4y.com/server/delivery/api/controllers/branch.controller.js
(29869B)
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: [ { model: db.branchDeliveryTypes, include: [ db.deliveryType, { model: db.branchPayTypes, include: [ db.payType ] } ] }, db.address, db.room, db.branchApp ], 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, countryCodeId, companyTypeId, deliveryTypeId, 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(page)) { page = 1 } if (!isSet(limit)) { limit = 10 } if (!isSet(sort)) { sort = 'asc' } const branches = await SERVICES.BranchService.GetBranchesByArea({ user: req.user, active, working, latitude, longtitude, countryCodeId, companyTypeId, deliveryTypeId, 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 { id } = req.params if (!id) { throw new BadRequest({ lang: req.user?.languageCode }) } let branch = await db.branch.findOne({ include: [ { model: db.company, as: 'company', include: [ { model: db.companyImage } ] }, { model: db.branchPrinter, as: 'branchPrinters' }, { model: db.branchPrintTemplate, as: 'branchPrintTemplates' }, { model: db.account }, { model: db.address }, { model: db.branchDeliveryTypes, include: [ db.deliveryType, { model: db.branchPayTypes, include: [ db.payType ] } ] }, { model: db.branchApp } // { model: db.room, // // include: { // // model: db.table // // } // }, // { // model: db.user, // include: [ // { model: db.userPhone, as: 'userPhones' }, // { model: db.language }, // { model: db.role } // ] // } ], where: { id: id } }) if (!branch) { throw new NotFound({ lang: req.user?.languageCode }) } 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 }) if (!branch) { throw new NotFound({ lang: req.user?.languageCode }) } branch.rooms = rooms let discount = 0 if (req.user !== undefined) { 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, searchType, radius, name, work_mode, work_json, description, site, email, phone, area, companyId, minimum_order, delivery_time, wifi_enabled, wifi_show_pass, wifi_ssid, wifi_pass, wifi_type, wifi_hide } = req.body const branch = await db.branch.create({ active, searchType, radius, name, work_mode, work_json, description, site, email, phone, area, companyId, minimum_order, delivery_time, wifi_enabled, wifi_show_pass, wifi_ssid, wifi_pass, wifi_type, wifi_hide }) // const user = await db.user.findOne({ where: { id: req.user.id } }) // branch.addUser(user) const account = await db.account.create({ // companyId: companyId, branchId: branch?.id, // userId: req.user.id, // balance: 0, // default: 0, // description: '', type: ACCOUNT_TYPE.BRANCH // createdAt: Date.now(), // updatedAt: Date.now() }) 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', id: branch.id }) } 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 { id, active, searchType, radius, name, work_mode, work_json, description, site, email, phone, area, minimum_order, delivery_time, wifi_enabled, wifi_show_pass, wifi_ssid, wifi_pass, wifi_type, wifi_hide } = req.body const branch = await db.branch.findOne({ where: { id: id } }) if (!branch) { throw new NotFound({ lang: req.user?.languageCode }) } if (active) branch.active = active if (searchType) branch.searchType = searchType if (radius) branch.radius = radius if (name) branch.name = name if (work_mode) branch.work_mode = work_mode if (work_json) branch.work_json = work_json 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 (delivery_time) branch.delivery_time = delivery_time 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.id) 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 { id } = req.body if (!isSet(id)) { throw new BadRequest({ lang: req.user?.languageCode }) } const branch = await db.branch.findOne({ where: { id: id } }) if (!branch) { throw new NotFound({ lang: req.user?.languageCode }) } await SERVICES.BranchService.RemoveBranch(branch) await db.branch.destroy({ where: { id: id } }) return res.status(200).json({ status: 'success', id: branch.id }) } 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 { id, roleInCompanyId, limit, page } = req.query let offset = page ? (page - 1) * (limit || 10) : 0 let where = {} const branch = await db.branch.findOne({ where: { id } }) if (!branch) { throw new BadRequest({ lang: req.user?.languageCode }) } where['branchId'] = branch.id if (roleInCompanyId) { const role = await db.role.findOne({ where: { id: roleInCompanyId } }) 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 { branchId, userId, roleId } = req.body if (!isSet(branchId) || isEmpty(branchId)) { throw new BadRequest({ lang: req.user?.languageCode }) } if (!isSet(roleId) || isEmpty(roleId)) { throw new BadRequest({ lang: req.user?.languageCode }) } if (!isSet(userId) || isEmpty(userId)) { throw new BadRequest({ lang: req.user?.languageCode }) } const rows = await Promise.all([ db.branch.findOne({ where: { id: branchId } }), db.user.findOne({ where: { id: userId } }), db.role.findOne({ where: { id: roleId } }) ]) 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 { branchId, userId, roleId } = req.body if (!isSet(branchId) || isEmpty(branchId)) { throw new BadRequest({ lang: req.user?.languageCode }) } if (!isSet(roleId) || isEmpty(roleId)) { throw new BadRequest({ lang: req.user?.languageCode }) } if (!isSet(userId) || isEmpty(userId)) { throw new BadRequest({ lang: req.user?.languageCode }) } const rows = await Promise.all([ db.branch.findOne({ where: { id: branchId } }), db.user.findOne({ where: { id: userId } }), db.role.findOne({ where: { id: roleId } }) ]) 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.SetAddress = async (req, res, next) => { logger.child({ path: req.path, request: req.body }).info() try { checkPermission(req, 'branch_edit') const { id, 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: { id: id } }) if (!branch) { throw new NotFound({ lang: req.user?.languageCode }) } branch.addressId = foundAddress.id await branch.save() SERVICES.BranchService.ReloadBranch(branch.id) 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 { branchId, deliveryTypeId } = req.body if (!branchId || !deliveryTypeId) { throw new BadRequest({ lang: req.user.languageCode }) } let bdt = await db.branchDeliveryTypes.findOne({ where: { branchId: branchId, deliveryTypeId: deliveryTypeId } }) if (bdt) { throw new NotFound({ lang: req.user.languageCode }) } bdt = await db.branchDeliveryTypes.create({ branchId: branchId, deliveryTypeId: deliveryTypeId, }) if (!bdt) { throw new NotFound({ lang: req.user.languageCode }) } SERVICES.BranchService.ReloadBranch(branchId) return res.status(200).json({ status: 'success', id: bdt.id }) } catch (err) { next(err) } } this.EditDeliveryType = async (req, res, next) => { logger.child({ path: req.path, request: req.body }).info() try { checkPermission(req, 'branch_edit') const { branchDeliveryTypeId, rate_json, branchId } = req.body if (!branchDeliveryTypeId || !rate_json || !branchId) { throw new BadRequest({ lang: req.user.languageCode }) } let bdt = await db.branchDeliveryTypes.findOne({ where: { id: branchDeliveryTypeId, } }) if (!bdt) { throw new NotFound({ lang: req.user.languageCode }) } bdt.rate_json = rate_json await bdt.save() SERVICES.BranchService.ReloadBranch(branchId) return res.status(200).json({ status: 'success', id: bdt.id }) } 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 { branchId, deliveryTypeId } = req.body let bdt = await db.branchDeliveryTypes.findOne({ where: { branchId: branchId, deliveryTypeId: deliveryTypeId } }) if (!bdt) { throw new NotFound({ lang: req.user.languageCode }) } await bdt.destroy() SERVICES.BranchService.ReloadBranch(branchId) return res.status(200).json({ status: 'success' }) } 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 { branchDeliveryTypeId, payTypeId, branchId } = req.body if (!branchDeliveryTypeId || !payTypeId || !branchId) { throw new BadRequest({ lang: req.user.languageCode }) } let bpt = await db.branchPayTypes.findOne({ where: { branchDeliveryTypeId: branchDeliveryTypeId, payTypeId: payTypeId } }) if (bpt) { throw new NotFound({ lang: req.user.languageCode }) } bpt = await db.branchPayTypes.create({ branchDeliveryTypeId: branchDeliveryTypeId, payTypeId: payTypeId, }) if (!bpt) { throw new NotFound({ lang: req.user.languageCode }) } SERVICES.BranchService.ReloadBranch(branchId) return res.status(200).json({ status: 'success', id: bpt.id }) } 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 { branchDeliveryTypeId, payTypeId, branchId } = req.body if (!branchDeliveryTypeId || !payTypeId || !branchId) { throw new BadRequest({ lang: req.user.languageCode }) } let bdt = await db.branchPayTypes.findOne({ where: { branchDeliveryTypeId: branchDeliveryTypeId, payTypeId: payTypeId } }) if (!bdt) { throw new NotFound({ lang: req.user.languageCode }) } await bdt.destroy() SERVICES.BranchService.ReloadBranch(branchId) 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 id = req.user.id const { limit, page } = req.query let offset = page ? (page - 1) * (limit || 10) : 0 const { rows: branches } = 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: { id } }, { model: db.address }, { model: db.branchDeliveryTypes, include: [ db.deliveryType, { model: db.branchPayTypes, include: [ db.payType ] } ] }, ], where: {}, offset, limit: parseInt(limit) || 10 }) if (!branches || branches.length == 0) { throw new NotFound({ lang: req.user?.languageCode }) } const time = performance.now() - t1 return res .status(200) .json(new BranchListModel({ branches: branches, count: branches.length, page, time })) } catch (err) { next(err) } } this.AddFavorite = async (req, res, next) => { logger.child({ path: req.path, request: req.body }).info() try { const { branchId, userId } = req.body if (!isSet(branchId) || !isSet(userId) || isEmpty(branchId) || isEmpty(userId)) { throw new BadRequest({ lang: req.user?.languageCode }) } const branch = await db.branch.findOne({ where: { id: branchId } }) const user = await db.user.findOne({ where: { id: userId } }) 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 { branchId, userId } = req.body if (!isSet(branchId) || !isSet(userId) || isEmpty(branchId) || isEmpty(userId)) { throw new BadRequest({ lang: req.user?.languageCode }) } const branch = await db.branch.findOne({ where: { id: branchId } }) const user = await db.user.findOne({ where: { id: userId } }) 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.branchId) || isEmpty(template.branchId)) { throw new BadRequest({ lang: req.user?.languageCode }) } const branch = await db.branch.findOne({ where: { id: template.branchId } }) if (!branch) { throw new BadRequest({ lang: req.user?.languageCode }) } let result, temp template.branchId = branch.id if (template.id != undefined) { temp = await db.branchPrintTemplate.findOne({ where: { id: template.id, 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['branchId'] delete template['id'] result = await db.branchPrintTemplate.create(template) } const time = performance.now() - t1 return res.status(200).json({ status: 'success', id: result.id }) } 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 { branchId, branchPrinter } = req.body if (!isSet(branchId) || isEmpty(branchId)) { 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: { id: branchId } }) 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', id: result.id }) } 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 { printerId, name, ip, port } = req.body if (!isSet(printerId) || isEmpty(printerId)) { throw new BadRequest({ lang: req.user?.languageCode }) } const branchPrinter = await db.branchPrinter.findOne({ where: { id: printerId } }) 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', id: branchPrinter.id }) } 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 { printerId } = req.body if (!isSet(printerId) || isEmpty(printerId)) { throw new BadRequest({ lang: req.user?.languageCode }) } await db.branchPrinter.destroy({ where: { id: printerId } }) const time = performance.now() - t1 return res.status(200).json({ status: 'success', id: printerId }) } 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 { branchId } = req.body if (!branchId) { throw new BadRequest({ lang: req.user?.languageCode }) } const branch = await db.branch.findOne({ where: { id: branchId } }) 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', id: branchShift.id, 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 { branchId } = req.body if (!branchId) { throw new BadRequest({ lang: req.user?.languageCode }) } const branch = await db.branch.findOne({ where: { id: branchId } }) 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', id: branchShift.id, 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 { branchId, openedId, closedId, isActive, page, limit } = req.query let where = {} if (branchId) { const branch = await db.branch.findOne({ where: { id: branchId } }) 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) } } }
Save
cmd:
run