/var/www/verywell.gr/server/delivery/api/controllers
Edit: /var/www/verywell.gr/server/delivery/api/controllers/plan.controller.js (3630B)
const logger = require('../../logger')({ service: 'PlanController' })
const checkPermission = require('../utils/checkPermission.util')
const { isSet, isEmpty, minLength, isPhone, isEmail } = require('../utils/validator.util')
const { NotFound, BadRequest } = require('../responseModels/errors')
module.exports = function ({ db }) {
this.List = async (req, res, next) => {
logger.child({ path: req.path, request: req.query }).info()
let t1 = performance.now()
try {
const { limit, page } = req.query
let where = {}
let offset = page > 0 ? (page - 1) * (limit || 10) : 0
const plans = await db.plan.findAll({
include: db.planConfig,
where,
offset,
limit: parseInt(limit) || 10
})
if (plans.length == 0 || !plans) {
throw new NotFound({ lang: req.user?.languageCode })
}
const time = performance.now() - t1
return res.status(200).json({ list: plans, count: plans.length, time, page })
} catch (err) {
next(err)
}
}
this.Details = async (req, res, next) => {
logger.child({ path: req.path, request: req.params }).info()
try {
const { uid } = req.params
if (!isSet(uid) || isEmpty(uid)) {
throw new BadRequest({ lang: req.user?.languageCode })
}
const plan = await db.plan.findOne({
include: db.planConfig,
where: { uid }
})
if (!plan) {
throw new NotFound({ lang: req.user?.languageCode })
}
return res.status(200).json(plan)
} catch (err) {
next(err)
}
}
this.Create = async (req, res, next) => {
logger.child({ path: req.path, request: req.body }).info()
try {
const { active, name, key, description, image, cost, costYear } = req.body
if (!isSet(active) || isEmpty(active)) {
throw new BadRequest({ lang: req.user?.languageCode })
}
if (!isSet(name) || isEmpty(name)) {
throw new BadRequest({ lang: req.user?.languageCode })
}
if (!isSet(key) || isEmpty(key)) {
throw new BadRequest({ lang: req.user?.languageCode })
}
if (!isSet(cost) || isEmpty(cost)) {
throw new BadRequest({ lang: req.user?.languageCode })
}
if (!isSet(costYear) || isEmpty(costYear)) {
throw new BadRequest({ lang: req.user?.languageCode })
}
const plan = await db.plan.create({
active,
name,
description,
image,
key,
cost,
costYear,
})
return res.status(200).json({
status: 'success',
uid: plan.uid
})
} catch (err) {
next(err)
}
}
this.Edit = async (req, res, next) => {
logger.child({ path: req.path, request: req.body }).info()
try {
const { uid, active, name, key, description, image, cost, costYear } = req.body
const plan = await db.plan.findOne({
where: { uid }
})
if (!plan) {
throw new NotFound({ lang: req.user?.languageCode })
}
plan.active = active ? active : plan.active
plan.name = name ? name : plan.name
plan.key = key ? key : plan.key
plan.description = description ? description : plan.description
plan.image = image ? image : plan.image
plan.cost = cost ? cost : plan.cost
plan.costYear = costYear ? costYear : plan.costYear
await plan.save()
return res.status(200).json(plan)
} catch (err) {
next(err)
}
}
this.Delete = async (req, res, next) => {
logger.child({ path: req.path, request: req.body }).info()
try {
const { uid } = req.body
const plan = await db.plan.findOne({
where: { uid }
})
if (!plan) {
throw new NotFound({ lang: req.user?.languageCode })
}
await db.plan.destroy({ where: { uid } })
return res.status(200).json({ status: 'success', uid })
} catch (err) {
next(err)
}
}
}