/var/www/myprog.com.ua/server/node/api/controllers
Edit: /var/www/myprog.com.ua/server/node/api/controllers/web_app_controller.js (9548B)
const log = require('../../logs/log')
const CustomError = require('../../objects/custom_error')
// const Company = require('../../objects/company')
// const Category = require('../../objects/category')
// const Product = require('../../objects/product')
exports.get_companies = async (req, res) => {
log.debug('api', req.originalUrl, req.params)
try {
let { city_id, group_id, client_id, ids, limit } = req.query
if (req.token.roles.includes(1)) {
client_id = 0
} else {
client_id = req.token.client_id
}
// res.status(200).json(await core.context.get_companies({ city_id, group_id, client_id, ids, limit }))
res.status(200).json(await core.context.get_user_places(client_id))
} catch (e) {
log.error('api', req.originalUrl, e, req.query)
res.status(500).json(e.message)
}
}
exports.get_company = async (req, res) => {
log.debug('api', req.originalUrl, req.params)
try {
let { id } = req.params
res.status(200).json(await core.context.get_company({ id }))
} catch (e) {
log.error('api', req.originalUrl, e, req.params)
res.status(500).json(e.message)
}
}
exports.create_company = async (req, res) => {
log.debug('api', req.originalUrl, req.body)
try {
// if (!req.token.roles.includes(1)) {
// throw new CustomError(403)
// }
let { city_id, name, address, image, description, la, lo, radius, start_time, end_time } = req.body
if (!name) {
throw new CustomError(400, null, { name })
}
let company = new Company({ city_id, name, address, image, description, la, lo, radius, start_time, end_time })
res.status(200).json(await core.context.create_company(company));
} catch (e) {
log.error('api', req.originalUrl, e, req.body)
res.status(500).json(e.message)
}
}
exports.edit_company = async (req, res) => {
log.debug('api', req.originalUrl, req.body)
try {
if (!req.body.id) {
throw new CustomError(400, null, { id: req.body.id })
}
if(!req.token.roles.includes(1)){
if(req.token.roles.includes(2)){
if(!req.token.companies.includes(req.body.id)){
throw new CustomError(403)
}
}else{
throw new CustomError(403)
}
}
let company = new Company(await core.context.get_company({ id: req.body.id }))
if (!company) {
throw new CustomError(404, 'Company not found')
}
company.update(req.body)
res.status(200).json(await core.context.update_company(company));
} catch (e) {
log.error('api', req.originalUrl, e, req.body)
res.status(500).json(e.message)
}
}
exports.add_company_client = async (req, res) => {
log.debug('api', req.originalUrl, req.body)
try {
let { company_id, client_id } = req.body
if (!company_id) {
throw new CustomError(400, null, { company_id })
}
if (!client_id) {
throw new CustomError(400, null, { client_id })
}
if(!req.token.roles.includes(1)){
if(req.token.roles.includes(2)){
if(!req.token.companies.includes(company_id)){
throw new CustomError(403)
}
}else{
throw new CustomError(403)
}
}
let company = await core.context.get_company({ id: company_id })
if (!company) {
throw new CustomError(404, 'Company not found', { company_id })
}
let client = await core.context.get_client({ client_id })
if (!client) {
throw new CustomError(404, 'Client not found', { client_id })
}
await core.context.add_company_client({ company_id, client_id })
res.status(200).json({ id: company.id })
} catch (e) {
log.error('api', req.originalUrl, e, req.body)
res.status(500).json(e.message)
}
}
exports.delete_company_client = async (req, res) => {
log.debug('api', req.originalUrl, req.body)
try {
let { company_id, client_id } = req.body
if (!company_id) {
throw new CustomError(400, null, { company_id })
}
if (!client_id) {
throw new CustomError(400, null, { client_id })
}
let company = await core.context.get_company({ id: company_id })
if (!company) {
throw new CustomError(404, 'Company not found', { company_id })
}
let client = await core.context.get_client({ client_id })
if (!client) {
throw new CustomError(404, 'Client not found', { client_id })
}
await core.context.delete_company_client({ company_id, client_id })
res.status(200).json({ id: company.id })
} catch (e) {
log.error('api', req.originalUrl, e, req.body)
res.status(500).json(e.message)
}
}
exports.get_categories = async (req, res) => {
log.debug('api', req.originalUrl, req.query)
try {
let { parent_id, company_id, limit } = req.query
res.status(200).json(await core.context.get_categories({ parent_id, company_id, limit }))
} catch (e) {
log.error('api', req.originalUrl, e, req.query)
res.status(500).json(e.message)
}
}
exports.get_category = async (req, res) => {
log.debug('api', req.originalUrl, req.params)
try {
let { id } = req.params
let category = new Category(await core.context.get_category({ id }))
let model = {}
for (let i of category.get_db_fields()) {
model[i] = category[i]
}
res.status(200).json(model)
} catch (e) {
log.error('api', req.originalUrl, e, req.params)
res.status(500).json(e.message)
}
}
exports.create_category = async (req, res) => {
log.debug('api', req.originalUrl, req.params)
try {
let { name, description, parent_id, company_id, image, position, icon, color } = req.body
let category = new Category({ name, description, parent_id, company_id, image, position, icon, color })
category = await core.context.create_category(category)
let model = {}
for (let i of category.get_db_fields()) {
model[i] = category[i]
}
res.status(200).json(model);
} catch (e) {
log.error('api', req.originalUrl, e, req.params)
res.status(500).json(e.message)
}
}
exports.edit_category = async (req, res) => {
log.debug('api', req.originalUrl, req.params)
try {
if (!req.body.id) {
throw new CustomError(400, null, { id: req.body.id })
}
let category = new Category(await core.context.get_category({ id: req.body.id }))
if (!category) {
throw new CustomError(404, 'Category not found')
}
category.update(req.body)
res.status(200).json(await core.context.update_category(category));
} catch (e) {
log.error('api', req.originalUrl, e, req.params)
res.status(500).json(e.message)
}
}
exports.get_products = async (req, res) => {
log.debug('api', req.originalUrl, req.params)
try {
let { company_id, category_id, limit } = req.query
res.status(200).json(await core.context.get_products({ company_id, category_id, limit }))
} catch (e) {
log.error('api', req.originalUrl, e, req.query)
res.status(500).json(e.message)
}
}
exports.get_product = async (req, res) => {
log.debug('api', req.originalUrl, req.params)
try {
let { id } = req.params
let product = new Product(await core.context.get_product({ id }))
let model = {}
for (let i of product.get_db_fields()) {
model[i] = product[i]
}
res.status(200).json(model)
} catch (e) {
log.error('api', req.originalUrl, e, req.params)
res.status(500).json(e.message)
}
}
exports.create_product = async (req, res) => {
log.debug('api', req.originalUrl, req.params)
try {
let { name, description, article, type_id, company_id, category_id, image, position, qty, cost } = req.body
let product = new Product({ name, description, article, type_id, company_id, category_id, image, position, qty, cost })
product.id = await core.context.create_product(product)
let model = {}
for (let i of product.get_db_fields()) {
model[i] = product[i]
}
res.status(200).json(model);
} catch (e) {
log.error('api', req.originalUrl, e, req.params)
res.status(500).json(e.message)
}
}
exports.edit_product = async (req, res) => {
log.debug('api', req.originalUrl, req.params)
try {
if (!req.body.id) {
throw new CustomError(400, null, { id: req.body.id })
}
let product = new Product(await core.context.get_product({ id: req.body.id }))
if (!product) {
throw new CustomError(404, 'product not found')
}
product.update(req.body)
product = await core.context.update_product(product)
let model = {}
for (let i of product.get_db_fields()) {
model[i] = product[i]
}
res.status(200).json(model);
} catch (e) {
log.error('api', req.originalUrl, e, req.params)
res.status(500).json(e.message)
}
}