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