/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/userCart.controller.js (5827B)
const logger = require('../../logger')({ service: 'api' }) const { Op } = require('sequelize') const { UserModel, UsersListModel } = require('../responseModels') const { NotFound, BadRequest } = require('../responseModels/errors') const { isSet, isEmpty, minLength, isPhone, isEmail } = require('../utils/validator.util') const checkPermission = require('../utils/checkPermission.util') const { performance } = require('perf_hooks') const { BranchModel, BranchListCartModel } = 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() if (req.body.id != req.user.id) { // checkPermission(req, 'user_edit') } const { id } = req.params if (!isSet(id) || isEmpty(id)) { throw new BadRequest({ lang: req.user?.languageCode }) } const user = await db.user.findOne({ where: { id } }) if (!user) { throw new NotFound({ lang: req.user?.languageCode }) } const userCartItems = await db.userCartItem.findAll({ include: [{ model: db.product, include: [db.productImage] }], where: { userId: user.id } }) if (userCartItems.length == 0 || !userCartItems) { throw new NotFound({ lang: req.user.languageCode }) } let branchesIds = [] userCartItems.forEach((item) => { if (!branchesIds.includes(item.branchId)) { branchesIds.push(item.branchId) } }) let branches = await db.branch.findAll({ include: [ { model: db.user, through: db.branchUsers, as: 'users' }, { model: db.company, as: 'company', where: { deletedAt: null } }, { model: db.branchDeliveryTypes, include: [ db.deliveryType, { model: db.branchPayTypes, include: [ db.payType ] } ] }, db.address, ], where: { id: branchesIds } }) for (let branch of branches) { branch.cartItems = userCartItems.map((item) => { if (item.branchId == branch.id) return item }) var filtered = branch.cartItems.filter(function (el) { return el != null; }); branch.cartItems = filtered } const time = performance.now() - t1 return res.status(200).json(new BranchListCartModel({ branches: branches, time: time })) } catch (err) { next(err) } } this.ListByOrder = async (req, res, next) => { logger.child({ path: req.path, request: req.query }).info() try { let t1 = performance.now() if (req.body.id != req.user.id) { // checkPermission(req, 'user_edit') } const { id } = req.params if (!isSet(id) || isEmpty(id)) { throw new BadRequest({ lang: req.user?.languageCode }) } const order = await db.order.findOne({ where: { id } }) if (!order) { throw new NotFound({ lang: req.user?.languageCode }) } const orderItems = await db.orderItems.findAll({ include: [{ model: db.product, include: [db.productImage] }], where: { orderId: order.id } }) if (orderItems.length == 0 || !orderItems) { throw new NotFound({ lang: req.user.languageCode }) } const time = performance.now() - t1 return res.status(200).json({ list: orderItems, time: time }) } catch (err) { next(err) } } this.AddToCart = async (req, res, next) => { logger.child({ path: req.path, request: req.body }).info() try { if (req.body.id != req.user.id) { // checkPermission(req, 'user_edit') } const { id, productId, companyId, branchId, price_json } = req.body if (!isSet(id) || isEmpty(id)) { throw new BadRequest({ lang: req.user?.languageCode }) } const user = await db.user.findOne({ where: { id: id } }) if (!user) { throw new NotFound({ lang: req.user?.languageCode }) } const foundCart = await db.userCartItem.findOne({ where: {userId: user.id, productId: productId, branchId: branchId } }) if (foundCart) { foundCart.price_json = price_json await foundCart.save() return res.status(200).json({ status: 'success', id: foundCart.id }) } const new_product = await db.userCartItem.create({ productId, companyId, branchId, price_json, userId: user.id }) return res.status(200).json({ status: 'success', id: new_product.id }) } catch (err) { next(err) } } this.RemoveFromCart = async (req, res, next) => { logger.child({ path: req.path, request: req.body }).info() try { if (req.body.id != req.user.id) { // checkPermission(req, 'user_edit') } const { id } = req.body if (!isSet(id) || isEmpty(id)) { throw new BadRequest({ code: 400, lang: req.user?.languageCode }) } const userCartItem = await db.userCartItem.findOne({ where: { id: id } }) if (!userCartItem) { throw new NotFound({ lang: req.user?.languageCode }) } await userCartItem.destroy() return res.status(200).json({ status: 'success', id: id }) } catch (err) { next(err) } } this.RemoveFromBranch = async (req, res, next) => { logger.child({ path: req.path, request: req.body }).info() try { if (req.body.userId != req.user.id) { // checkPermission(req, 'user_edit') } const { userId, branchId } = req.body if (!isSet(userId) || isEmpty(userId)) { throw new BadRequest({ code: 400, lang: req.user?.languageCode }) } const userCartItem = await db.userCartItem.findAll({ where: { userId: userId, branchId: branchId } }) if (!userCartItem) { throw new NotFound({ lang: req.user?.languageCode }) } await db.userCartItem.destroy({ where: { userId: userId, branchId: branchId }, truncate: false }) return res.status(200).json({ status: 'success', userId: userId }) } catch (err) { next(err) } } }