/
var
/
www
/
verywell.gr
/
server
/
delivery
/
api
/
controllers
/
/var/www/verywell.gr/server/delivery/api/controllers
mkdir
upload
Name
Size
Mode
Actions
account/
-
0755
rm
tableReserv/
-
0755
rm
auth.controller.js
13187
0644
edit
dl
rm
branch.controller.js
32443
0644
edit
dl
rm
branchReview.controller.js
5975
0644
edit
dl
rm
category.controller.js
5521
0644
edit
dl
rm
company.controller.js
22632
0644
edit
dl
rm
companyReview.controller.js
6396
0644
edit
dl
rm
companyTypes.controller.js
2765
0644
edit
dl
rm
countryCodes.controller.js
2648
0644
edit
dl
rm
delivery.controller.js
7624
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
40326
0644
edit
dl
rm
orderCancelCause.controller.js
3460
0644
edit
dl
rm
payTypes.controller.js
2329
0644
edit
dl
rm
permission.controller.js
2605
0644
edit
dl
rm
plan.controller.js
3630
0644
edit
dl
rm
planConfig.controller.js
3465
0644
edit
dl
rm
product.controller.js
25793
0644
edit
dl
rm
productGroups.controller.js
6618
0644
edit
dl
rm
productReview.controller.js
5992
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
22213
0644
edit
dl
rm
userCart.controller.js
7231
0644
edit
dl
rm
waiter.controller.js
6173
0644
edit
dl
rm
Edit:
/var/www/verywell.gr/server/delivery/api/controllers/company.controller.js
(22632B)
const logger = require('../../logger')({ service: 'api' }) const checkPermission = require('../utils/checkPermission.util') const config = require('../../config/app.json') 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 } ], limit: 10 }, { model: db.plan, as: "plan", include: [db.planConfig] }, { model: db.companyType, as: 'companyTypes' }, { model: db.productGroup, as: 'product_groups' }, { model: db.companyImage } ], where: { uid: uid } }) 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') const { name, active, enabled, description, tag, image, planId } = 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, planId }) 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: { 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 }) } } 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.SendToUser(1, message, {}) return res.status(200).json({ status: 'success', id: company.id, uid: company.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, 'company_edit') const { uid, name, active, enabled, description, tag, image, planId } = 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 company.planId = planId 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 }) // 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 { 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) // 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 { companyUid, userUid, discount } = req.body if (!isSet(companyUid) || !isSet(userUid) || !isSet(discount) || isEmpty(companyUid) || isEmpty(userUid) || isEmpty(discount)) { 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 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 { 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 }) } 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 { 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