/var/www/verywell.gr/server/delivery/api/controllers/tableReserv
Edit: /var/www/verywell.gr/server/delivery/api/controllers/tableReserv/create.js (1971B)
const { NotFound, BadRequest } = require('../../responseModels/errors')
const { isSet, isEmpty } = require('../../utils/validator.util')
const checkPermission = require('../../utils/checkPermission.util')
module.exports = function ({ db, logger }) {
return async (req, res, next) => {
logger.child({ path: req.path, request: req.body }).info()
let t1 = performance.now()
try {
// проверка параметров запроса
var { branchUid, tableUid, userUid, date, duration, comment, places } = req.body
if (!isSet(branchUid) || isEmpty(branchUid))
throw new BadRequest({ code: 1025, lang: req.user?.languageCode })
if (!isSet(userUid) || isEmpty(userUid))
throw new BadRequest({ code: 1026, lang: req.user?.languageCode })
if (!isSet(date) || isEmpty(date))
throw new BadRequest({ code: 1099, lang: req.user?.languageCode })
if (!isSet(places) || isEmpty(places)) places = 1
// запрос
const result = await Promise.all([
db.user.findOne({ where: { uid: userUid } }),
db.branch.findOne({ where: { uid: branchUid } }),
db.table.findOne({ where: { uid: tableUid } })
])
// проверка результата
if (!result) {
throw new NotFound({ lang: req.user?.languageCode })
}
if (!result[0]) {
throw new NotFound({ code: 1028, lang: req.user?.languageCode })
}
if (!result[1]) {
throw new NotFound({ code: 1029, lang: req.user?.languageCode })
}
const userId = result[0].id
const branchId = result[1].id
const tableId = result[2] != null ? result[2].id : 0;
// создание
const tableReserv = await db.tableReserv.create({
userId,
branchId,
tableId,
duration,
comment,
places,
date
})
await SERVICES.ReservService.AddReserv(tableReserv)
const time = performance.now() - t1
return res.status(200).json({ status: 'success', uid: tableReserv.uid, time })
} catch (err) {
next(err)
}
}
}