/var/www/qr4y.com/server/delivery/api/controllers
Edit: /var/www/qr4y.com/server/delivery/api/controllers/branchApp.controller.js (4686B)
const logger = require('../../logger')({ service: 'api' })
const { NotFound, BadRequest } = require('../responseModels/errors')
const { isSet, isEmpty, minLength, isPhone, isEmail } = require('../utils/validator.util')
// const crypto = require('crypto');
const uuid = require('uuid');
module.exports = function ({ db }) {
this.SendCmd = async (req, res, next) => {
logger.child({ path: req.path, request: req.query }).info()
try {
let t1 = performance.now()
const { appId, cmd, params } = req.body
let where = {}
if (appId) {
where['id'] = appId
}
const app = await db.branchApp.findOne({
where,
})
if (!app) {
throw new BadRequest({ lang: req.user?.languageCode })
}
await SERVICES.WebSocketService.SendToApp(appId, cmd, params)
const time = performance.now() - t1
return res.status(200).json({ success: 'success', id: app.id })
} catch (err) {
next(err)
}
}
this.List = async (req, res, next) => {
logger.child({ path: req.path, request: req.query }).info()
try {
let t1 = performance.now()
const { branchId, limit, page } = req.query
let where = {}
if (branchId) {
where['branchId'] = branchId
}
let offset = page > 0 ? (page - 1) * (limit || 10) : 0
const rows = await db.branchApp.findAll({
// include: db.user,
where,
offset,
order: [
['id', 'DESC']
// ['name', 'ASC'],
],
limit: parseInt(limit) || 10
})
const time = performance.now() - t1
return res.status(200).json({ list: rows, count: rows.length, time, page })
} 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 app = await db.branchApp.findOne({ where: { id } })
return res.status(200).json(app)
} catch (err) {
next(err)
}
}
this.Create = async (req, res, next) => {
logger.child({ path: req.path, request: req.body }).info()
try {
const {id, companyId, branchId, active, name, type } = req.body
var params = {
companyId,
branchId,
active,
name,
type,
}
if (!isSet(id) || isEmpty(id)) {
id = uuid.v4()
}
params['id'] = id
params['key'] = uuid.v4()
params['ver'] = ''
params['platform'] = ''
params['deviceId'] = ''
params['online'] = false
var date = new Date();
date.setDate(date.getDate() + 14);
params['expiredAt'] = date
if (!isSet(companyId) || isEmpty(companyId)) {
throw new BadRequest({ lang: req.user?.languageCode })
}
if (!isSet(branchId) || isEmpty(branchId)) {
throw new BadRequest({ lang: req.user?.languageCode })
}
if (!isSet(type)) {
throw new BadRequest({ lang: req.user?.languageCode })
}
if (!isSet(name)) {
throw new BadRequest({ lang: req.user?.languageCode })
}
// const key = crypto.randomUUID()
const app = await db.branchApp.create(params)
return res.status(200).json({ success: 'success', id: app.id })
} catch (err) {
next(err)
}
}
this.Edit = async (req, res, next) => {
logger.child({ path: req.path, request: req.body }).info()
let transaction = null
try {
const { id, branchId, active, name, type, expiredAt } = req.body
if (!isSet(id) || isEmpty(id)) {
throw new BadRequest({ lang: req.user?.languageCode })
}
// if (!isSet(branchId) || isEmpty(branchId)) {
// throw new BadRequest({ lang: req.user?.languageCode })
// }
if (!isSet(type)) {
throw new BadRequest({ lang: req.user?.languageCode })
}
if (!isSet(name)) {
throw new BadRequest({ lang: req.user?.languageCode })
}
transaction = await db.sequelize.transaction()
let app = await db.branchApp.findOne({ where: { id } })
// app.branchId = branchId
app.type = type
app.name = name
app.active = active || false
// app.expiredAt = expiredAt
await app.save()
await transaction.commit()
return res.status(200).json(app)
} catch (err) {
transaction?.rollback()
next(err)
}
}
this.Delete = async (req, res, next) => {
logger.child({ path: req.path, request: req.body }).info()
let transaction = null
try {
const { id } = req.body
if (!isSet(id) || isEmpty(id)) {
throw new BadRequest({ lang: req.user?.languageCode })
}
transaction = await db.sequelize.transaction()
const app = await db.branchApp.findOne({ where: { id } })
if (!app) {
throw new NotFound({ lang: req.user?.languageCode })
}
await app.destroy()
await transaction.commit()
return res.status(200).json({ status: 'success', id })
} catch (err) {
transaction?.rollback()
next(err)
}
}
}