/var/www/qr4y.com/server/delivery/api/controllers
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)
}
}
}