/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/company.controller.js (25111B)
const logger = require('../../logger')({ service: 'api' }) const checkPermission = require('../utils/checkPermission.util') const config = require('../../config/app.json') const { Op, where } = require('sequelize') const { NotFound, BadRequest } = require('../responseModels/errors') const { isSet, isEmpty, minLength, isPhone, isEmail } = require('../utils/validator.util') const { CompanyListModel, CompanyModel, CompanyUsersListModel, CompanyClientsListModel } = require('../responseModels') const { performance } = require('perf_hooks') module.exports = function ({ db }) { this.Search = async (req, res, next) => { logger.child({ path: req.path, request: req.query }).info() try { let t1 = performance.now() // checkPermission(req, 'company_list') const { limit, page, name } = req.query let offset = page ? (page - 1) * (limit || 10) : 0 let where = {} if (isSet(name)) { where['tag'] = { [Op.like]: `%${name}%` } } const { count, rows } = await db.company.findAndCountAll({ include: [ { model: db.companyImage }, { model: db.companyType }, { model: db.language }, { model: db.branch, as: 'branches', include: [ { model: db.branchDeliveryTypes, include: [ db.deliveryType, { model: db.branchPayTypes, include: [ db.payType ] } ] }, db.address, db.room ] } ], 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 CompanyListModel({ companies: rows, count, page, time })) } catch (err) { next(err) } } this.List = async (req, res, next) => { logger.child({ path: req.path, request: req.query }).info() try { let t1 = performance.now() checkPermission(req, 'company_list') const { limit, page } = req.query let offset = page ? (page - 1) * (limit || 10) : 0 const { count, rows } = await db.company.findAndCountAll({ include: [ { model: db.companyType }, { model: db.companyImage }, { model: db.language }, { model: db.branch, as: 'branches', include: [db.address] } ], 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 CompanyListModel({ companies: rows, count, page, time })) } 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 { count, rows } = await db.company.findAndCountAll({ include: [ { model: db.companyImage }, { model: db.language }, { model: db.companyTypes }, { model: db.user, through: 'favorite_companies', as: 'favoriteUsers', where: { id } } ], 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 CompanyListModel({ companies: rows, count, page, time })) } catch (err) { next(err) } } this.AddFavorite = async (req, res, next) => { logger.child({ path: req.path, request: req.body }).info() try { //checkPermission(req, 'company_add_user') const { companyId, userId } = req.body if (!isSet(companyId) || !isSet(userId) || isEmpty(companyId) || isEmpty(userId)) { throw new BadRequest({ lang: req.user?.languageCode }) } const company = await db.company.findOne({ where: { id: companyId } }) const user = await db.user.findOne({ where: { id: userId } }) if (!user || !company) { throw new NotFound({ lang: req.user?.languageCode }) } user.addFavoriteCompany(company) 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 { //checkPermission(req, 'company_add_user') const { companyId, userId } = req.body if (!isSet(companyId) || !isSet(userId) || isEmpty(companyId) || isEmpty(userId)) { throw new BadRequest({ lang: req.user?.languageCode }) } const company = await db.company.findOne({ where: { id: companyId } }) const user = await db.user.findOne({ where: { id: userId } }) if (!user || !company) { throw new NotFound({ lang: req.user?.languageCode }) } user.removeFavoriteCompany(company) return res.status(200).json({ status: 'success' }) } 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, limit, page } = req.query let offset = page ? (page - 1) * (limit || 10) : 0 const company = await db.company.findOne({ where: { id } }) if (!company) { throw new BadRequest({ lang: req.user?.languageCode }) } const rows = await db.companyUsers.findAll({ include: [ db.company, db.role, { model: db.user, include: [db.role, db.userPhone, db.language] } ], where: { companyId: company.id }, 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 CompanyUsersListModel({ rows, page, time })) } catch (err) { next(err) } } this.Clients = async (req, res, next) => { logger.child({ path: req.path, request: req.query }).info() try { let t1 = performance.now() const { id, phone, email, limit, page } = req.query let offset = page ? (page - 1) * (limit || 10) : 0 let phone_where = {} if (isSet(phone)) { phone_where['phone'] = { [Op.like]: `%${phone}%` } } let email_where = {} if (isSet(email)) { email_where['email'] = { [Op.like]: `%${email}%` } } const company = await db.company.findOne({ where: { id } }) if (!company) { throw new BadRequest({ lang: req.user?.languageCode }) } const rows = await db.companyClients.findAll({ include: [ { model: db.user, include: [ db.language, { model: db.userPhone, where: phone_where } ], where: email_where }], where: { companyId: company.id }, 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 CompanyClientsListModel({ rows, page, 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 { latitude, longtitude, 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' } let offset = page ? (page - 1) * (limit || 10) : 0 const branches = await SERVICES.BranchService.GetBranchesByArea({ latitude, longtitude, page: 1, limit: 100000, sort }) let companiesIds = [] for (let branch of branches) { const idx = companiesIds.findIndex((x) => x === branch.companyId) if (idx === -1) { companiesIds.push(branch.companyId) } } const { count, rows } = await db.company.findAndCountAll({ include: [ { model: db.companyImage }, { model: db.language }, { model: db.companyType }, { model: db.branch, as: 'branches', include: [db.address] } ], where: { id: { [Op.in]: companiesIds } }, offset, limit: parseInt(limit) || 10 }) const time = performance.now() - t1 return res .status(200) .json(new CompanyListModel({ companies: rows, count, page, time })) } catch (err) { next(err) } } this.Details = async (req, res, next) => { logger.child({ path: req.path, request: req.params }).info() try { checkPermission(req, 'company_list') const { id } = req.params if (!id) { throw new BadRequest({ lang: req.user?.languageCode }) } const company = await db.company.findOne({ include: [ { model: db.branch, as: 'branches', include: [ { model: db.user, through: 'branch_users', as: 'users', include: [{ model: db.role }] }, { model: db.branchDeliveryTypes, include: [ db.deliveryType, { model: db.branchPayTypes, include: [ db.payType ] } ] }, { model: db.address } ], limit: 10 }, { model: db.companyType }, { model: db.language }, { model: db.companyImage } ], where: { id: id } }) company.clients = (await db.companyClients.findAll({ include: [ { model: db.user, include: [db.language, db.userPhone] }], where: { companyId: company.id }, limit: 10 })).map(x => { return x.user }) company.users = (await db.companyUsers.findAll({ include: [ { model: db.user, include: [{ model: db.userPhone, as: 'userPhones' }, { model: db.language }] }, { model: db.role } ], where: { companyId: company.id }, limit: 10 })).map(x => { u = x.user u.role = x.role return u }) if (!company) { throw new NotFound({ lang: req.user?.languageCode }) } return res.status(200).json(new CompanyModel(company)) } 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, 'company_edit') var { name, active, enabled, description, tag, image, planId, countryCodeId, deliveryServerId, currency } = req.body if (!isSet(name) || isEmpty(name)) throw new BadRequest({ code: 1037, lang: req.user?.languageCode }) if (!isSet(description)) description = '' if (!isSet(tag)) tag = '' if (!isSet(currency)) currency = '' const company = await db.company.create({ name, active, enabled, description, tag, image, planId, countryCodeId, deliveryServerId, currency }) const account = await db.account.create({ companyId: company?.id, type: ACCOUNT_TYPE.COMPANY }) 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 user = await db.user.findOne({ where: { id: req.user.id } }) if (user) { const role = await db.role.findOne({ where: { key: 'owner' } }) let userCompany = await db.companyUsers.findOne({ where: { userId: user.id, companyId: company.id } }) if (!role) throw new BadRequest({ lang: req.user?.languageCode }) // роль не найдена if (userCompany) { // пользователь существует // обновить роль userCompany.roleId = role.id await userCompany.save() } else { await db.companyUsers.create({ userId: user.id, companyId: company.id, roleId: role.id }) } } await transaction.commit() // отправить PUSH userId == 1 let message = { notification: { title: config.app.name, body: 'New company ' + company.name + ' created by user ' + user.name }, // data: { // channel_id: threadId, // }, android: { ttl: 3600 * 1000, notification: { priority: 'high', sound: 'default', notificationCount: 1 // icon: 'stock_ticker_update', // color: '#f45342', } }, apns: { payload: { aps: { badge: 1, sound: 'default' } } }, fcmOptions: { analyticsLabel: 'company_created' } } await SERVICES.Firebase.SendToSuperUsers(message, {}) return res.status(200).json({ status: 'success', id: company.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, 'company_edit') const { id, name, active, enabled, description, tag, image, planId, countryCodeId, deliveryServerId, currency } = req.body const company = await db.company.findOne({ include: [ { model: db.branch, as: 'branches' }], where: { id: id } }) if (!company) { throw new NotFound({ lang: req.user?.languageCode }) } company.name = name company.active = active company.enabled = enabled company.tag = tag company.description = description company.image = image company.currency = currency company.countryCodeId = countryCodeId company.deliveryServerId = deliveryServerId // if(planId != null){ // company.planId = planId // } await company.save() for (let branch of company.branches) { await SERVICES.BranchService.ReloadBranch(branch.id) } return res.status(200).json(company) } catch (err) { next(err) } } this.Delete = async (req, res, next) => { logger.child({ path: req.path, request: req.body }).info() try { checkPermission(req, 'company_edit') const { id } = req.body if (!id) { throw new BadRequest({ lang: req.user?.languageCode }) } const company = await db.company.findOne({ where: { id: id } }) if (!company) { throw new NotFound({ lang: req.user?.languageCode }) } await db.company.destroy({ where: { id: id } }) return res.status(200).json({ status: 'success', id: company.id }) } catch (err) { next(err) } } this.AddImage = async (req, res, next) => { logger.child({ path: req.path, request: req.body }).info() try { checkPermission(req, 'company_edit') const { companyId, companyImage } = req.body if (!companyId) { throw new BadRequest({ lang: req.user?.languageCode }) } var image = JSON.parse(companyImage) if (!companyImage) { throw new BadRequest({ lang: req.user?.languageCode }) } const company = await db.company.findOne({ where: { id: companyId } }) if (!company) { throw new NotFound({ lang: req.user?.languageCode }) } image.companyId = company.id const result = await db.companyImage.create(image) return res.status(200).json({ status: 'success', id: result.id }) } catch (err) { next(err) } } this.RemoveImage = async (req, res, next) => { logger.child({ path: req.path, request: req.body }).info() try { checkPermission(req, 'company_edit') const { imageId } = req.body if (!imageId) { throw new BadRequest({ lang: req.user?.languageCode }) } await db.companyImage.destroy({ where: { id: imageId } }) return res.status(200).json({ status: 'success', id: imageId }) } catch (err) { next(err) } } this.SetDefaultImage = async (req, res, next) => { logger.child({ path: req.path, request: req.body }).info() try { checkPermission(req, 'company_edit') const { imageId } = req.body if (!imageId) { throw new BadRequest({ lang: req.user?.languageCode }) } let company_image = await db.companyImage.findOne({ where: { id: imageId } }) await db.companyImage.update( { isDefault: false }, { where: { companyId: company_image.companyId } } ) company_image.isDefault = true company_image.save() return res.status(200).json({ status: 'success', id: imageId }) } catch (err) { next(err) } } this.AddClient = async (req, res, next) => { logger.child({ path: req.path, request: req.body }).info() try { // checkPermission(req, 'company_edit') const { companyId, userId, discount } = req.body if (!isSet(companyId) || !isSet(userId) || isEmpty(companyId) || isEmpty(userId)) { throw new BadRequest({ lang: req.user?.languageCode }) } const company = await db.company.findOne({ where: { id: companyId } }) const user = await db.user.findOne({ where: { id: userId } }) if (!user || !company) { throw new NotFound({ lang: req.user?.languageCode }) } //company.addClient(user) const companyClient = await db.companyClients.findOne({ where: { companyId: company.id, userId: user.id } }) if (companyClient) { if (discount) { companyClient.discount = parseInt(discount) await companyClient.save() } } else { await db.companyClients.create({ userId: user.id, companyId: company.id, discount: parseInt(discount) || 0 }) // company counters company.countClients++ company.save() } return res.status(200).json({ status: 'success' }) } catch (err) { next(err) } } this.RemoveClient = async (req, res, next) => { logger.child({ path: req.path, request: req.body }).info() try { // checkPermission(req, 'company_edit') const { companyId, userId } = req.body if (!isSet(companyId) || !isSet(userId) || isEmpty(companyId) || isEmpty(userId)) { throw new BadRequest({ lang: req.user?.languageCode }) } const company = await db.company.findOne({ where: { id: companyId } }) const user = await db.user.findOne({ where: { id: userId } }) if (!user || !company) { throw new NotFound({ lang: req.user?.languageCode }) } company.removeClient(user) // company counters if (company.countClients > 0) company.countClients-- company.save() return res.status(200).json({ status: 'success' }) } catch (err) { next(err) } } this.EditClientDiscount = async (req, res, next) => { logger.child({ path: req.path, request: req.body }).info() try { // checkPermission(req, 'company_edit') const { companyId, userId, discount } = req.body if (!isSet(companyId) || !isSet(userId) || !isSet(discount) || isEmpty(companyId) || isEmpty(userId) || isEmpty(discount)) { throw new BadRequest({ lang: req.user?.languageCode }) } const company = await db.company.findOne({ where: { id: companyId } }) const user = await db.user.findOne({ where: { id: userId } }) const companyClient = await db.companyClients.findOne({ where: { companyId: company.id, userId: user.id } }) if (!user || !company) { throw new NotFound({ lang: req.user?.languageCode }) } if (companyClient) { if (discount) { companyClient.discount = parseInt(discount) await companyClient.save() } } return res.status(200).json({ status: 'success' }) } catch (err) { next(err) } } this.AddUser = async (req, res, next) => { logger.child({ path: req.path, request: req.body }).info() let t1 = performance.now() let transaction = await db.sequelize.transaction() try { checkPermission(req, 'company_edit') const { companyId, userId, roleId } = req.body if ( !isSet(companyId) || !isSet(userId) || isEmpty(companyId) || isEmpty(userId) || isEmpty(roleId) ) { throw new BadRequest({ lang: req.user?.languageCode }) } const company = await db.company.findOne({ where: { id: companyId } }) const user = await db.user.findOne({ where: { id: userId } }) const role = await db.role.findOne({ where: { id: roleId } }) if (!user || !company || !role) { throw new NotFound({ lang: req.user?.languageCode }) } let userCompany = await db.companyUsers.findOne({ where: { userId: user.id, companyId: company.id } }) if (userCompany) { // пользователь существует // обновить роль userCompany.roleId = role.id await userCompany.save() } else { await db.companyUsers.create({ userId: user.id, companyId: company.id, roleId: role.id }) } const userAccount = await db.account.findOne({ where: { userId: user.id, companyId: company.id, type: ACCOUNT_TYPE.EMPLOYEE } }) if (!userAccount) { await db.account.create({ userId: user.id, companyId: company.id, type: ACCOUNT_TYPE.EMPLOYEE }) } await transaction.commit() const time = performance.now() - t1 return res.status(200).json({ status: 'success', time }) } catch (err) { transaction?.rollback() next(err) } } this.RemoveUser = async (req, res, next) => { logger.child({ path: req.path, request: req.body }).info() try { checkPermission(req, 'company_edit') const { companyId, userId, roleId } = req.body if ( !isSet(companyId) || !isSet(userId) || isEmpty(companyId) || isEmpty(userId) || isEmpty(roleId) ) { throw new BadRequest({ lang: req.user?.languageCode }) } const company = await db.company.findOne({ where: { id: companyId } }) const user = await db.user.findOne({ where: { id: userId } }) const role = await db.role.findOne({ where: { id: roleId } }) if (!user || !company || !role) { throw new NotFound({ lang: req.user?.languageCode }) } const userCompany = await db.companyUsers.findOne({ where: { userId: user.id, companyId: company.id, roleId: role.id } }) if (!userCompany) { throw new NotFound({ lang: req.user?.languageCode }) } userCompany.destroy() return res.status(200).json({ status: 'success' }) } catch (err) { next(err) } } this.AddCompanyType = async (req, res, next) => { logger.child({ path: req.path, request: req.body }).info() try { checkPermission(req, 'company_edit') const { companyId, companyTypeId } = req.body if (!companyId || !companyTypeId) { throw new BadRequest({ lang: req.user.languageCode }) } let company = await db.company.findOne({ where: { id: companyId } }) const companyType = await db.companyType.findOne({ where: { id: companyTypeId } }) if (!company) { throw new NotFound({ lang: req.user.languageCode }) } if (!companyType) { throw new NotFound({ lang: req.user.languageCode }) } company.addCompanyType(companyType) await company.save() return res.status(200).json({ status: 'success' }) } catch (err) { next(err) } } this.RemoveCompanyType = async (req, res, next) => { logger.child({ path: req.path, request: req.body }).info() try { checkPermission(req, 'company_edit') const { companyId, companyTypeId } = req.body let company = await db.company.findOne({ where: { id: companyId } }) const companyType = await db.companyType.findOne({ where: { id: companyTypeId } }) if (!company) { throw new NotFound({ lang: req.user.languageCode }) } if (!companyType) { throw new NotFound({ lang: req.user.languageCode }) } company.removeCompanyType(companyType) await company.save() return res.status(200).json({ status: 'success' }) } catch (err) { next(err) } } this.AddLanguage = async (req, res, next) => { logger.child({ path: req.path, request: req.body }).info() try { checkPermission(req, 'company_edit') const { companyId, languageId } = req.body if (!companyId || !languageId) { throw new BadRequest({ lang: req.user.languageCode }) } let company = await db.company.findOne({ where: { id: companyId } }) const language = await db.language.findOne({ where: { id: languageId } }) if (!company) { throw new NotFound({ lang: req.user.languageCode }) } if (!language) { throw new NotFound({ lang: req.user.languageCode }) } company.addLanguage(language) await company.save() return res.status(200).json({ status: 'success' }) } catch (err) { next(err) } } this.RemoveLanguage = async (req, res, next) => { logger.child({ path: req.path, request: req.body }).info() try { checkPermission(req, 'company_edit') const { companyId, languageId } = req.body let company = await db.company.findOne({ where: { id: companyId } }) const language = await db.language.findOne({ where: { id: languageId } }) if (!company) { throw new NotFound({ lang: req.user.languageCode }) } if (!language) { throw new NotFound({ lang: req.user.languageCode }) } company.removeLanguage(language) await company.save() return res.status(200).json({ status: 'success' }) } catch (err) { next(err) } } }