/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/product.controller.js (13402B)
const logger = require('../../logger')({ service: 'api' }) const checkPermission = require('../utils/checkPermission.util') const { NotFound, BadRequest } = require('../responseModels/errors') const { isSet, isEmpty, minLength } = require('../utils/validator.util') const { performance } = require('perf_hooks') const { ProductListModel } = require('../responseModels') 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 { all, active, companyId, productGroupId, productTypeId, limit, page } = req.query let where = {} if (isSet(all) && !isEmpty(all)) { } else { if (isSet(active) && !isEmpty(active)) { where['active'] = active } else { where['active'] = true } } if (isSet(companyId) && !isEmpty(companyId)) { where['companyId'] = companyId } if (isSet(productGroupId) && !isEmpty(productGroupId)) { where['productGroupId'] = productGroupId } if (isSet(productTypeId) && !isEmpty(productTypeId)) { where['productTypeId'] = productTypeId } // let whereProductImage = { isDefault: true } let whereProductImage = {} let offset = page > 0 ? (page - 1) * (limit || 10) : 0 const count = await db.product.count({ where, }); const rows = await db.product.findAll({ include: [ { model: db.productFilter }, { model: db.productImage, where: whereProductImage, required: false }, ], where, order: [ ['position', 'ASC'] ], offset, limit: parseInt(limit) || 10 }) if (rows.length == 0 || !rows) { throw new NotFound({ lang: req.user?.languageCode }) } const time = performance.now() - t1 // return res.status(200).json(new ProductListModel({ rows, time, page })) return res.status(200).json({ list: rows, time, count }) } catch (err) { next(err) } } this.Details = async (req, res, next) => { logger.child({ path: req.path, request: req.params }).info() try { let t1 = performance.now() const { id } = req.params if (!id) { throw new BadRequest({ lang: req.user?.languageCode }) } const product = await db.product.findOne({ include: [ { model: db.productImage }, { model: db.productFilter } ], where: { id: id } }) if (!product) { throw new NotFound({ lang: req.user?.languageCode }) } const time = performance.now() - t1 return res.status(200).json(product) } catch (err) { next(err) } } this.ListFavorite = async (req, res, next) => { logger.child({ path: req.path, request: req.params }).info() try { let t1 = performance.now() const id = req.user.id const { limit, page } = req.query let offset = page ? (page - 1) * (limit || 10) : 0 let whereProductImage = {} let rows = await db.product.findAll({ include: [ { model: db.productFilter }, { model: db.productImage, where: whereProductImage, required: false }, { model: db.company, as: 'company', paranoid: false, include: { model: db.branch, as: "branches" }, where: { deletedAt: null }, }, { model: db.user, through: 'favorite_products', as: 'favoriteUsers', where: { id } } ], // where: {'$company.deletedAt$': null }, offset, limit: parseInt(limit) }) if (!rows || rows.length == 0) { throw new NotFound({ lang: req.user?.languageCode }) } const time = performance.now() - t1 return res.status(200).json(new ProductListModel({ rows, time, page })) } catch (err) { next(err) } } this.AddFavorite = async (req, res, next) => { logger.child({ path: req.path, request: req.body }).info() try { const { productId } = req.body if (!isSet(productId) || isEmpty(productId)) { throw new BadRequest({ lang: req.user?.languageCode }) } const userId = req.user.id const product = await db.product.findOne({ where: { id: productId } }) const user = await db.user.findOne({ where: { id: userId } }) if (!user || !product) { throw new NotFound({ lang: req.user?.languageCode }) } user.addFavoriteProduct(product) 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 { const { productId } = req.body if (!isSet(productId) || isEmpty(productId)) { throw new BadRequest({ lang: req.user?.languageCode }) } const userId = req.user.id const product = await db.product.findOne({ where: { id: productId } }) const user = await db.user.findOne({ where: { id: userId } }) if (!user || !product) { throw new NotFound({ lang: req.user?.languageCode }) } user.removeFavoriteProduct(product) return res.status(200).json({ status: 'success' }) } catch (err) { next(err) } } this.Create = async (req, res, next) => { logger.child({ path: req.path, request: req.body }).info() try { let t1 = performance.now() checkPermission(req, 'product_edit') if (!isSet(req.body.companyId) || isEmpty(req.body.companyId)) { throw new BadRequest({ lang: req.user?.languageCode }) } // if (!isSet(req.body.productTypeId) || isEmpty(req.body.productTypeId)) { // throw new BadRequest({ lang: req.user?.languageCode }) // } if (!isSet(req.body.productGroupId) || isEmpty(req.body.productGroupId)) { throw new BadRequest({ lang: req.user?.languageCode }) } if (!isSet(req.body.productFilters) || !Array.isArray(req.body.productFilters)) { req.body.productFilters = [] } if (!isSet(!req.body.productImages) || !Array.isArray(req.body.productImages)) { req.body.productImages = [] } const { position, active, active_delivery, article, remote_id, description, price_json, tag, companyId, // productTypeId, productGroupId, productFilters, productImages, } = req.body const product = await db.product.create( { position, active, active_delivery, article, remote_id, description, price_json, tag, companyId, // productTypeId, productGroupId, productFilters, productImages, }, { include: [ { model: db.productImage }, { model: db.productFilter } ] } ) const time = performance.now() - t1 return res.status(200).json({ status: 'success', id: product.id }) } catch (err) { next(err) } } this.Edit = async (req, res, next) => { logger.child({ path: req.path, request: req.body }).info() const transaction = await db.sequelize.transaction() try { let t1 = performance.now() checkPermission(req, 'product_edit') if (!isSet(req.body.id) || isEmpty(req.body.id)) { throw new BadRequest({ lang: req.user?.languageCode }) } if (!isSet(req.body.companyId) || isEmpty(req.body.companyId)) { throw new BadRequest({ lang: req.user?.languageCode }) } if (!isSet(req.body.productTypeId) || isEmpty(req.body.productTypeId)) { throw new BadRequest({ code: 1058, lang: req.user?.languageCode }) } // if (!isSet(req.body.productGroupId) || isEmpty(req.body.productGroupId)) { // throw new BadRequest({ lang: req.user?.languageCode }) // } const { id, position, active, active_delivery, article, remote_id, description, price_json, tag, companyId, productTypeId, productGroupId, productFilters, productImages, } = req.body let product = await db.product.findOne({ include: [ { model: db.productImage }, { model: db.productFilter } ], where: { id: id } }) if (!product) { throw new NotFound({ lang: req.user?.languageCode }) } product.tag = tag product.position = position product.active = active product.active_delivery = active_delivery product.article = article product.remote_id = remote_id product.description = description product.price_json = price_json product.companyId = companyId product.productTypeId = productTypeId product.productGroupId = productGroupId // создать новые или обновить характеристики if (isSet(productFilters) && Array.isArray(productFilters)) { for (const filter of productFilters) { let idx = product.productFilters.findIndex((p) => p.id == filter?.id) if (idx === -1) { await db.productFilter.create(filter) } else { await db.productFilter.update(filter, { where: { id: filter.id } }) } } } // удалить характеристики if ( isSet(product.productFilters) && Array.isArray(product.productFilters) && isSet(productFilters) && Array.isArray(productFilters) ) { for (const filter of product.productFilters) { let idx = productFilters.findIndex((p) => p.id == filter?.id) if (idx === -1) { await db.productFilter.destroy({ where: { id: filter.id } }) } } } // создать новые или обновить изображения if (isSet(productImages) && Array.isArray(productImages)) { for (const image of productImages) { let idx = product.productImages.findIndex((p) => p.id == image?.id) if (idx === -1) { await db.productImage.create(image) } else { await db.productImage.update(image, { where: { id: image.id } }) } } } // удалить изображения if ( isSet(product.productImages) && Array.isArray(product.productImages) && isSet(productImages) && Array.isArray(productImages) ) { for (const image of product.productImages) { let idx = productImages.findIndex((p) => p.id == image?.id) // будет работать //let idx = product.productImages.findIndex((p) => p.id == image?.id) // не будет работать if (idx === -1) { await db.productImage.destroy({ where: { id: image.id } }) // удалить изображение с сервера // ... } } } await product.save() await transaction.commit() // await product.reload() db.company.increment( { dataVersion: +1 }, { where: { id: product.companyId } } ) const time = performance.now() - t1 return res.status(200).json(product) } catch (err) { await transaction.rollback() next(err) } } this.Delete = async (req, res, next) => { logger.child({ path: req.path, request: req.body }).info() try { let t1 = performance.now() checkPermission(req, 'product_edit') const { id } = req.body if (!isSet(id) || isEmpty(id)) { throw new BadRequest({ lang: req.user?.languageCode }) } const product = await db.product.findOne({ where: { id: id } }) if (!product) { throw new NotFound({ lang: req.user?.languageCode }) } await db.product.destroy({ where: { id: id } }) const time = performance.now() - t1 return res.status(200).json({ status: 'success', id: product.id }) } catch (err) { next(err) } } this.AddImage = async (req, res, next) => { logger.child({ path: req.path, request: req.body }).info() try { let t1 = performance.now() checkPermission(req, 'product_edit') const { productId, productImage } = req.body if (!isSet(productId) || isEmpty(productId)) { throw new BadRequest({ lang: req.user?.languageCode }) } if (!isSet(productImage) || isEmpty(productImage)) { throw new BadRequest({ lang: req.user?.languageCode }) } const product = await db.product.findOne({ where: { id: productId } }) if (!product) { throw new NotFound({ lang: req.user?.languageCode }) } let image = JSON.parse(productImage) image.productId = product.id const result = await db.productImage.create(image) const time = performance.now() - t1 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 { let t1 = performance.now() checkPermission(req, 'product_edit') const { imageId } = req.body if (!isSet(imageId) || isEmpty(imageId)) { throw new BadRequest({ lang: req.user?.languageCode }) } await db.productImage.destroy({ where: { id: imageId } }) const time = performance.now() - t1 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 { let t1 = performance.now() checkPermission(req, 'product_edit') const { imageId } = req.body if (!isSet(imageId) || isEmpty(imageId)) { throw new BadRequest({ lang: req.user?.languageCode }) } let product_image = await db.productImage.findOne({ where: { id: imageId } }) await db.productImage.update( { isDefault: false }, { where: { productId: product_image.productId } } ) product_image.isDefault = true product_image.save() const time = performance.now() - t1 return res.status(200).json({ status: 'success', id: imageId }) } catch (err) { next(err) } } }