/var/www/verywell.gr/server/delivery/api/controllers
NameSizeModeActions
account/-0755rm
tableReserv/-0755rm
auth.controller.js131870644editdlrm
branch.controller.js324430644editdlrm
branchReview.controller.js59750644editdlrm
category.controller.js55210644editdlrm
company.controller.js226320644editdlrm
companyReview.controller.js63960644editdlrm
companyTypes.controller.js27650644editdlrm
countryCodes.controller.js26480644editdlrm
delivery.controller.js76240644editdlrm
deliveryTypes.controller.js23970644editdlrm
driver.controller.js46030644editdlrm
language.controller.js27090644editdlrm
order.controller.js403260644editdlrm
orderCancelCause.controller.js34600644editdlrm
payTypes.controller.js23290644editdlrm
permission.controller.js26050644editdlrm
plan.controller.js36300644editdlrm
planConfig.controller.js34650644editdlrm
product.controller.js257930644editdlrm
productGroups.controller.js66180644editdlrm
productReview.controller.js59920644editdlrm
productTypeFeatures.controller.js27130644editdlrm
productTypes.controller.js41540644editdlrm
role.controller.js40120644editdlrm
room.controller.js34570644editdlrm
table.controller.js154950644editdlrm
user.controller.js222130644editdlrm
userCart.controller.js72310644editdlrm
waiter.controller.js61730644editdlrm
Edit: /var/www/verywell.gr/server/delivery/api/controllers/userCart.controller.js (7231B)
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.uid != req.user.uid) { // checkPermission(req, 'user_edit') } const { uid } = req.params if (!isSet(uid) || isEmpty(uid)) { throw new BadRequest({ lang: req.user?.languageCode }) } const user = await db.user.findOne({ where: { uid } }) if (!user) { throw new NotFound({ lang: req.user?.languageCode }) } const userCartItems = await db.userCartItem.findAll({ include: [{ model: db.product, include: [db.branchProduct, db.productImage, db.productVariant, db.productExtra, db.productIngridient] }], 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 } }, db.workTime, db.shippingRate, db.deliveryType, db.address ], where: { id: branchesIds } }) for (let branch of branches) { let branchPayTypes = await db.branchPayTypes.findAll({ include: [{ model: db.payType }, { model: db.deliveryType }], where: { branchId: branch.id } }) for (let dt of branch.deliveryTypes) { dt.dataValues.payTypes = branchPayTypes.map((bpt) => { if (bpt.deliveryTypeId == dt.id) return bpt.payType }) var filtered = dt.dataValues.payTypes.filter(function (el) { return el != null; }); dt.dataValues.payTypes = filtered } 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.uid != req.user.uid) { // checkPermission(req, 'user_edit') } const { uid } = req.params if (!isSet(uid) || isEmpty(uid)) { throw new BadRequest({ lang: req.user?.languageCode }) } const order = await db.order.findOne({ where: { uid } }) if (!order) { throw new NotFound({ lang: req.user?.languageCode }) } const orderItems = await db.orderItems.findAll({ include: [{ model: db.product, include: [db.branchProduct, db.productImage, db.productVariant, db.productExtra, db.productIngridient] }], 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.uid != req.user.uid) { // checkPermission(req, 'user_edit') } const { uid, productId, companyId, branchId, qty, productVariant, productExtra, productIngridient } = req.body if (!isSet(uid) || isEmpty(uid)) { throw new BadRequest({ lang: req.user?.languageCode }) } const user = await db.user.findOne({ where: { uid: uid } }) if (!user) { throw new NotFound({ lang: req.user?.languageCode }) } const foundCart = await db.userCartItem.findOne({ where: { productId: productId, branchId: branchId } }) if (foundCart) { foundCart.qty = foundCart.qty * 1 + qty await foundCart.save() return res.status(200).json({ status: 'success', uid: foundCart.uid }) } const new_product = await db.userCartItem.create({ productId, companyId, branchId, qty, productVariant, productExtra, productIngridient, userId: user.id }) return res.status(200).json({ status: 'success', uid: new_product.uid }) } catch (err) { next(err) } } this.SubFromCart = async (req, res, next) => { logger.child({ path: req.path, request: req.body }).info() try { if (req.body.uid != req.user.uid) { // checkPermission(req, 'user_edit') } const { uid } = req.body if (!isSet(uid) || isEmpty(uid)) { throw new BadRequest({ code: 400, lang: req.user?.languageCode }) } const userCartItem = await db.userCartItem.findOne({ where: { uid: uid } }) if (!userCartItem) { throw new NotFound({ lang: req.user?.languageCode }) } userCartItem.qty = userCartItem.qty * 1 - 1 if (userCartItem.qty == 0) { await userCartItem.destroy() } else { await userCartItem.save() } return res.status(200).json({ status: 'success', uid: uid }) } catch (err) { next(err) } } this.RemoveFromCart = async (req, res, next) => { logger.child({ path: req.path, request: req.body }).info() try { if (req.body.uid != req.user.uid) { // checkPermission(req, 'user_edit') } const { uid } = req.body if (!isSet(uid) || isEmpty(uid)) { throw new BadRequest({ code: 400, lang: req.user?.languageCode }) } const userCartItem = await db.userCartItem.findOne({ where: { uid: uid } }) if (!userCartItem) { throw new NotFound({ lang: req.user?.languageCode }) } await userCartItem.destroy() return res.status(200).json({ status: 'success', uid: uid }) } 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) } } }