/var/www/p4ela.kz/server/delivery/api/controllers
Edit: /var/www/p4ela.kz/server/delivery/api/controllers/countryCodes.controller.js (2648B)
const logger = require('../../logger')({ service: 'CountryCodeController' })
const checkPermission = require('../utils/checkPermission.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()
try {
const countryCodes = await db.countryCode.findAll()
if (countryCodes.length == 0 || !countryCodes) {
throw new NotFound({ lang: req.user?.languageCode })
}
return res.status(200).json(
countryCodes.map((countryCode) => {
const { id, name, code, dialCode } = countryCode
return { id, name, code, dialCode }
})
)
} catch (err) {
next(err)
}
}
this.Details = async (req, res, next) => {
logger.child({ path: req.path, request: req.params }).info()
try {
const { id } = req.params
const countryCode = await db.countryCode.findOne({
where: { id: id }
})
if (!countryCode) {
throw new NotFound({ lang: req.user?.languageCode })
}
return res.status(200).json(countryCode)
} catch (err) {
next(err)
}
}
this.Create = async (req, res, next) => {
logger.child({ path: req.path, request: req.body }).info()
try {
checkPermission(req, 'country_code_edit')
const { name, code, dialCode } = req.body
const countryCode = await db.countryCode.create({
name,
code,
dialCode
})
return res.status(200).json({
status: 'success',
id: countryCode.id
})
} catch (err) {
next(err)
}
}
this.Edit = async (req, res, next) => {
logger.child({ path: req.path, request: req.body }).info()
try {
checkPermission(req, 'country_code_edit')
const { id, name, code, dialCode } = req.body
const countryCode = await db.countryCode.findOne({
where: { id: id }
})
if (!countryCode) {
throw new NotFound({ lang: req.user?.languageCode })
}
countryCode.name = name
countryCode.code = code
countryCode.dialCode = dialCode
await countryCode.save()
return res.status(200).json(countryCode)
} catch (err) {
next(err)
}
}
this.Delete = async (req, res, next) => {
logger.child({ path: req.path, request: req.body }).info()
try {
checkPermission(req, 'country_code_edit')
const { id } = req.body
const countryCode = await db.countryCode.findOne({
where: { id: id }
})
if (!countryCode) {
throw new NotFound({ lang: req.user?.languageCode })
}
await db.countryCode.destroy({ where: { id: id } })
return res.status(200).json({ status: 'success', id: countryCode.id })
} catch (err) {
next(err)
}
}
}