/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/branchReview.controller.js (6321B)
const logger = require('../../logger')({ service: 'api' }) const { NotFound, BadRequest } = require('../responseModels/errors') const { isSet, isEmpty, minLength, isPhone, isEmail } = require('../utils/validator.util') module.exports = function ({ db }) { this.List = async (req, res, next) => { logger.child({ path: req.path, request: req.query }).info() try { let t1 = performance.now() const { isVerified, userId, branchId, limit, page } = req.query let where = {} if (userId) { where['userId'] = userId } if (branchId) { where['branchId'] = branchId } if (typeof isVerified === 'undefined') { where['isVerified'] = true } else { where['isVerified'] = isVerified } let offset = page > 0 ? (page - 1) * (limit || 10) : 0 const rows = await db.branchReview.findAll({ include: db.user, where, offset, order: [ ['createdAt', 'DESC'] // ['name', 'ASC'], ], limit: parseInt(limit) || 10 }) const time = performance.now() - t1 return res.status(200).json({ list: rows, count: rows.length, time, page }) } catch (err) { next(err) } } this.Details = async (req, res, next) => { logger.child({ path: req.path, request: req.params }).info() try { const { id } = req.params const review = await db.branchReview.findOne({ where: { id } }) return res.status(200).json(review) } catch (err) { next(err) } } this.Accept = async (req, res, next) => { logger.child({ path: req.path, request: req.body }).info() let transaction = null try { const { id } = req.body transaction = await db.sequelize.transaction() const review = await db.branchReview.findOne({ where: { id } }) review.isVerified = true await review.save() await transaction.commit() await calculateBranchRating(review.branchId) return res.status(200).json({ status: 'success', id }) } catch (err) { transaction?.rollback() next(err) } } this.Create = async (req, res, next) => { logger.child({ path: req.path, request: req.body }).info() try { const { branchId, text, rating, orderId } = req.body if (!isSet(branchId) || isEmpty(branchId)) { throw new BadRequest({ lang: req.user?.languageCode }) } if (!isSet(rating)) { throw new BadRequest({ lang: req.user?.languageCode }) } if (!isSet(text)) { throw new BadRequest({ lang: req.user?.languageCode }) } let branch = await db.branch.findOne({ where: { id: branchId } }) const review = await db.branchReview.create({ branchId: branch.id, userId: req.user.id, text, rating }) // Расчет рейтинга не нужен. Расчет будет после подтверждения. // ---> Пока что принимаем все подряд let transaction = null try { transaction = await db.sequelize.transaction() if (isSet(orderId) && !isEmpty(orderId)) { const order = await db.order.findOne({ where: { id: orderId } }) order.rate = rating order.save() } const review2 = await db.branchReview.findOne({ where: { id: review.id } }) review2.isVerified = true await review2.save() // company counters const company = await db.company.findOne({ where: { id: branch.companyId } }) if (company) { company.countBranchReviews++ company.save() } await transaction.commit() await calculateBranchRating(branchId) SERVICES.BranchService.ReloadBranch(branchId) } catch (err) { transaction?.rollback() } // <--- return res.status(200).json({ success: 'success', id: review.id, review: review }) } catch (err) { next(err) } } this.Edit = async (req, res, next) => { logger.child({ path: req.path, request: req.body }).info() let transaction = null try { const { id, rating, text } = req.body if (!isSet(id) || isEmpty(id)) { throw new BadRequest({ lang: req.user?.languageCode }) } if (!isSet(rating)) { throw new BadRequest({ lang: req.user?.languageCode }) } if (!isSet(text) || isEmpty(text)) { throw new BadRequest({ lang: req.user?.languageCode }) } transaction = await db.sequelize.transaction() let review = await db.branchReview.findOne({ where: { id } }) if (rating != review.rating || text != review.text) { review.isVerified = false } review.rating = rating review.text = text await review.save() await transaction.commit() await calculateBranchRating(review.branchId) return res.status(200).json(review) } catch (err) { transaction?.rollback() next(err) } } this.Delete = async (req, res, next) => { logger.child({ path: req.path, request: req.body }).info() let transaction = null try { const { id } = req.body if (!isSet(id) || isEmpty(id)) { throw new BadRequest({ lang: req.user?.languageCode }) } transaction = await db.sequelize.transaction() const review = await db.branchReview.findOne({ where: { id } }) if (!review) { throw new NotFound({ lang: req.user?.languageCode }) } const branchId = review.branchId const isVerified = review.isVerified await review.destroy() // если отзыв был проверен то пересчитать рейтинг if (isVerified) { let branch = await db.branch.findOne({ where: { id: branchId } }) const company = await db.company.findOne({ where: { id: branch.companyId } }) if (company) { company.countBranchReviews-- company.save() } } await transaction.commit() SERVICES.BranchService.ReloadBranch(branchId) await calculateBranchRating(branchId) return res.status(200).json({ status: 'success', id }) } catch (err) { transaction?.rollback() next(err) } } async function calculateBranchRating(branchId) { let branch = await db.branch.findOne({ where: { id: branchId } }) if (!branch) { throw new NotFound({ lang: req.user?.languageCode }) } const sum = await db.branchReview.sum('rating', { where: { branchId: branchId, isVerified: true } } ) const count = await db.branchReview.count( { where: { branchId: branchId, isVerified: true } } ) if (sum > 0) { branch.rating = sum / count await branch.save() } } }