/var/www/verywell.gr/server/delivery/api/controllers
Edit: /var/www/verywell.gr/server/delivery/api/controllers/planConfig.controller.js (3465B)
const logger = require('../../logger')({ service: 'PlanConfigController' })
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 { planId, limit, page } = req.query
let where = {}
if (planId) {
where['planId'] = planId
}
let offset = page > 0 ? (page - 1) * (limit || 10) : 0
const configs = await db.planConfig.findAll({
where,
offset,
limit: parseInt(limit) || 10
})
if (configs.length == 0 || !configs) {
throw new NotFound({ lang: req.user?.languageCode })
}
const time = performance.now() - t1
return res.status(200).json({ list: configs, count: configs.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 config = await db.planConfig.findOne({
where: { uid }
})
if (!config) {
throw new NotFound({ lang: req.user?.languageCode })
}
return res.status(200).json(config)
} catch (err) {
next(err)
}
}
this.Create = async (req, res, next) => {
logger.child({ path: req.path, request: req.body }).info()
try {
const { planId, name, key, description, value } = req.body
if (!isSet(planId) || isEmpty(planId)) {
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(value) || isEmpty(value)) {
throw new BadRequest({ lang: req.user?.languageCode })
}
const config = await db.planConfig.create({
planId,
name,
description,
key,
value,
})
return res.status(200).json({
status: 'success',
uid: config.uid
})
} catch (err) {
next(err)
}
}
this.Edit = async (req, res, next) => {
logger.child({ path: req.path, request: req.body }).info()
try {
const { uid, planId, name, key, description, value } = req.body
const config = await db.planConfig.findOne({
where: { uid }
})
if (!config) {
throw new NotFound({ lang: req.user?.languageCode })
}
config.planId = planId ? planId : config.planId
config.name = name ? name : config.name
config.key = key ? key : config.key
config.description = description ? description : config.description
config.value = value ? value : config.value
await config.save()
return res.status(200).json(config)
} 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 config = await db.planConfig.findOne({
where: { uid }
})
if (!config) {
throw new NotFound({ lang: req.user?.languageCode })
}
await db.planConfig.destroy({ where: { uid } })
return res.status(200).json({ status: 'success', uid })
} catch (err) {
next(err)
}
}
}