/
var
/
www
/
qr4y.gr
/
server
/
delivery
/
api
/
controllers
/
/var/www/qr4y.gr/server/delivery/api/controllers
mkdir
upload
Name
Size
Mode
Actions
tableReserv/
-
0755
rm
userAccount/
-
0755
rm
auth.controller.js
12306
0644
edit
dl
rm
branch.controller.js
25187
0644
edit
dl
rm
branchReview.controller.js
5620
0644
edit
dl
rm
category.controller.js
5521
0644
edit
dl
rm
company.controller.js
20595
0644
edit
dl
rm
companyReview.controller.js
5773
0644
edit
dl
rm
companyTypes.controller.js
2722
0644
edit
dl
rm
countryCodes.controller.js
2648
0644
edit
dl
rm
delivery.controller.js
7470
0644
edit
dl
rm
deliveryTypes.controller.js
2397
0644
edit
dl
rm
driver.controller.js
4603
0644
edit
dl
rm
language.controller.js
2709
0644
edit
dl
rm
order.controller.js
31301
0644
edit
dl
rm
orderCancelCause.controller.js
3460
0644
edit
dl
rm
payTypes.controller.js
2278
0644
edit
dl
rm
permission.controller.js
2605
0644
edit
dl
rm
product.controller.js
25793
0644
edit
dl
rm
productGroups.controller.js
6568
0644
edit
dl
rm
productReview.controller.js
5233
0644
edit
dl
rm
productTypeFeatures.controller.js
2713
0644
edit
dl
rm
productTypes.controller.js
4154
0644
edit
dl
rm
role.controller.js
4012
0644
edit
dl
rm
room.controller.js
3457
0644
edit
dl
rm
table.controller.js
15495
0644
edit
dl
rm
user.controller.js
21794
0644
edit
dl
rm
userCart.controller.js
7231
0644
edit
dl
rm
waiter.controller.js
6173
0644
edit
dl
rm
Edit:
/var/www/qr4y.gr/server/delivery/api/controllers/company.controller.js
(20595B)
const logger = require('../../logger')({ service: 'api' }) const checkPermission = require('../utils/checkPermission.util') const { Op } = 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.branch, as: 'branches', // include: [db.workTime], include: [ db.workTime, db.deliveryType, db.address, db.shippingRate, 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.companyImage }, { model: db.branch, as: 'branches', // include: [db.workTime], 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 uid = req.user.uid 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.user, through: 'favorite_companies', as: 'favoriteUsers', where: { uid } } ], 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 { companyUid, userUid } = req.body if (!isSet(companyUid) || !isSet(userUid) || isEmpty(companyUid) || isEmpty(userUid)) { throw new BadRequest({ lang: req.user?.languageCode }) } const company = await db.company.findOne({ where: { uid: companyUid } }) const user = await db.user.findOne({ where: { uid: userUid } }) 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 { companyUid, userUid } = req.body if (!isSet(companyUid) || !isSet(userUid) || isEmpty(companyUid) || isEmpty(userUid)) { throw new BadRequest({ lang: req.user?.languageCode }) } const company = await db.company.findOne({ where: { uid: companyUid } }) const user = await db.user.findOne({ where: { uid: userUid } }) 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 { uid, limit, page } = req.query let offset = page ? (page - 1) * (limit || 10) : 0 const company = await db.company.findOne({ where: { uid } }) if (!company) { throw 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 { uid, limit, page } = req.query let offset = page ? (page - 1) * (limit || 10) : 0 const company = await db.company.findOne({ where: { uid } }) if (!company) { throw BadRequest({ lang: req.user?.languageCode }) } const rows = await db.companyClients.findAll({ include: [{ model: db.user, include: [db.language, db.userPhone] }], 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.branch, as: 'branches', // include: [db.workTime], 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 { uid } = req.params if (!uid) { 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.workTime }, { model: db.address } ] }, { model: db.companyType, as: 'companyTypes' }, { model: db.productGroup, as: 'product_groups' }, { model: db.companyImage }, { model: db.user, through: db.companyUsers, as: 'users', include: [{ model: db.role }] }, { model: db.user, through: 'copmany_clients', as: 'clients' } ], where: { uid: uid } }) 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() try { checkPermission(req, 'company_edit') const { name, active, enabled, description, tag, image } = req.body if (!isSet(name) || isEmpty(name)) throw new BadRequest({ code: 1037, lang: req.user?.languageCode }) const company = await db.company.create({ name, active, enabled, description, tag, image }) const user = await db.user.findOne({ where: { uid: req.user.uid } }) 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 }) } } return res.status(200).json({ status: 'success', uid: company.uid }) } catch (err) { next(err) } } this.Registration = async (req, res, next) => { logger.child({ path: req.path, request: req.body }).info() let transaction = await db.sequelize.transaction() try { let t1 = performance.now() const { name, description, tag, image, userUid } = req.body if (!isSet(name) || isEmpty(name)) throw new BadRequest({ code: 1037, lang: req.user?.languageCode }) if (!isSet(userUid) || isEmpty(userUid)) throw new BadRequest({ code: 1026, lang: req.user?.languageCode }) const user = await db.user.findOne({ where: { uid: userUid } }) if (!user) throw new NotFound({ code: 1028, lang: req.user?.languageCode }) const company = await db.company.create({ name, description, tag, image, active: false }) await db.companyUsers.create({ userId: user.id, companyId: company.id, active: false, roleId: COMPANY_OWNER_ROLE_ID }) const branch = await db.branch.create({ name: `${name} #1`, companyId: company.id }) await db.branchUsers.create({ userId: user.id, branchId: branch.id, roleId: COMPANY_OWNER_ROLE_ID }) transaction.commit() transaction = null const time = performance.now() - t1 return res.status(200).json({ status: 'success', uid: company.uid, time }) } 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 { uid, name, active, enabled, description, tag, image } = req.body const company = await db.company.findOne({ include: [ { model: db.branch, as: 'branches' }], where: { uid: uid } }) 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 await company.save() for(let branch of company.branches){ await SERVICES.BranchService.ReloadBranch(branch.uid) } 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 { uid } = req.body if (!uid) { throw new BadRequest({ lang: req.user?.languageCode }) } const company = await db.company.findOne({ where: { uid: uid } }) if (!company) { throw new NotFound({ lang: req.user?.languageCode }) } await db.company.destroy({ where: { uid: uid } }) return res.status(200).json({ status: 'success', uid: company.uid }) } 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 { companyUid, companyImage } = req.body if (!companyUid) { 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: { uid: companyUid } }) 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', uid: result.uid }) } 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 { imageUid } = req.body if (!imageUid) { throw new BadRequest({ lang: req.user?.languageCode }) } await db.companyImage.destroy({ where: { uid: imageUid } }) return res.status(200).json({ status: 'success', uid: imageUid }) } 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 { imageUid } = req.body if (!imageUid) { throw new BadRequest({ lang: req.user?.languageCode }) } let company_image = await db.companyImage.findOne({ where: { uid: imageUid } }) 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', uid: imageUid }) } 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 { companyUid, userUid, discount } = req.body if (!isSet(companyUid) || !isSet(userUid) || isEmpty(companyUid) || isEmpty(userUid)) { throw new BadRequest({ lang: req.user?.languageCode }) } const company = await db.company.findOne({ where: { uid: companyUid } }) const user = await db.user.findOne({ where: { uid: userUid } }) 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 }) } 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 { companyUid, userUid } = req.body if (!isSet(companyUid) || !isSet(userUid) || isEmpty(companyUid) || isEmpty(userUid)) { throw new BadRequest({ lang: req.user?.languageCode }) } const company = await db.company.findOne({ where: { uid: companyUid } }) const user = await db.user.findOne({ where: { uid: userUid } }) if (!user || !company) { throw new NotFound({ lang: req.user?.languageCode }) } company.removeClient(user) 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() try { checkPermission(req, 'company_edit') const { companyUid, userUid, roleUid } = req.body if ( !isSet(companyUid) || !isSet(userUid) || isEmpty(companyUid) || isEmpty(userUid) || isEmpty(roleUid) ) { throw new BadRequest({ lang: req.user?.languageCode }) } const company = await db.company.findOne({ where: { uid: companyUid } }) const user = await db.user.findOne({ where: { uid: userUid } }) const role = await db.role.findOne({ where: { uid: roleUid } }) 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 }) } 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, 'company_edit') const { companyUid, userUid, roleUid } = req.body if ( !isSet(companyUid) || !isSet(userUid) || isEmpty(companyUid) || isEmpty(userUid) || isEmpty(roleUid) ) { throw new BadRequest({ lang: req.user?.languageCode }) } const company = await db.company.findOne({ where: { uid: companyUid } }) const user = await db.user.findOne({ where: { uid: userUid } }) const role = await db.role.findOne({ where: { uid: roleUid } }) 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 { companyUid, companyTypeUid } = req.body if (!companyUid || !companyTypeUid) { throw new BadRequest({ lang: req.user.languageCode }) } let company = await db.company.findOne({ where: { uid: companyUid } }) const companyType = await db.companyType.findOne({ where: { uid: companyTypeUid } }) 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 { companyUid, companyTypeUid } = req.body let company = await db.company.findOne({ where: { uid: companyUid } }) const companyType = await db.companyType.findOne({ where: { uid: companyTypeUid } }) 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) } } }
Save
cmd:
run