/
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/productReview.controller.js
(6164B)
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, productId, limit, page } = req.query let where = {} if (userId) { where['userId'] = userId } if (productId) { where['productId'] = productId } if (typeof isVerified === 'undefined') { where['isVerified'] = true } else { where['isVerified'] = isVerified } let offset = page > 0 ? (page - 1) * (limit || 10) : 0 const { count, rows } = await db.productReview.findAndCountAll({ 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, 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.productReview.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 if(!isSet(id) || isEmpty(id)){ throw new BadRequest({ lang: req.user?.languageCode }) } transaction = await db.sequelize.transaction() const review = await db.productReview.findOne({ where: { id } }) review.isVerified = true await review.save() await calculateProductRating(review.productId) await transaction.commit() 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 { productId, text, rating } = req.body if(!isSet(productId) || isEmpty(productId)){ throw new BadRequest({ lang: req.user?.languageCode }) } if(!isSet(text) || isEmpty(text)){ throw new BadRequest({ lang: req.user?.languageCode }) } if(!isSet(rating)){ throw new BadRequest({ lang: req.user?.languageCode }) } let product = await db.product.findOne({ // include: db.company, where: { id: productId } }) const review = await db.productReview.create({ productId: product.id, userId: req.user.id, text, rating }) // Расчет рейтинга не нужен. Расчет будет после подтверждения. // ---> Пока что принимаем все подряд let transaction = null try { transaction = await db.sequelize.transaction() let id = review.id; const review2 = await db.productReview.findOne({ where: { id } }) review2.isVerified = true await review2.save() // company counters const company = await db.company.findOne({ where: { id: product.companyId } }) if (company) { company.countProductReviews++ company.save() } await transaction.commit() await calculateProductRating(review2.productId) } 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(text) || isEmpty(text)){ throw new BadRequest({ lang: req.user?.languageCode }) } if(!isSet(rating)){ throw new BadRequest({ lang: req.user?.languageCode }) } transaction = await db.sequelize.transaction() let review = await db.productReview.findOne({ where: { id } }) if (rating != review.rating || text != review.text) { review.isVerified = false } review.rating = rating review.text = text await review.save() await calculateProductRating(review.productId) await transaction.commit() 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.productReview.findOne({ where: { id } }) const productId = review.productId const isVerified = review.isVerified await review.destroy() // если отзыв был проверен то пересчитать рейтинг if (isVerified) { let product = await db.product.findOne({ where: { id: productId } }) const company = await db.company.findOne({ where: { id: product.companyId } }) if (company) { company.countProductReviews-- company.save() } } await transaction.commit() await calculateBranchRating(branchId) return res.status(200).json({ status: 'success', id }) } catch (err) { transaction?.rollback() next(err) } } async function calculateProductRating(productId) { let product = await db.product.findOne({ where: { id: productId } }) if(!product){ throw new NotFound({ lang: req.user?.languageCode }) } const sum = await db.productReview.sum('rating', { where: { productId: productId, isVerified: true } } ) const count = await db.productReview.count( { where: { productId: productId, isVerified: true } } ) if (sum > 0) { product.rating = sum / count await product.save() } } }
Save
cmd:
run