/var/www/myprog.com.ua/server/node/api/controllers
NameSizeModeActions
clients_controller.js187470644editdlrm
roles_controller.js14480644editdlrm
web_app_controller.js95480644editdlrm
Edit: /var/www/myprog.com.ua/server/node/api/controllers/clients_controller.js (18747B)
const log = require('../../logs/log') const bcrypt = require('bcrypt') const jwt = require('jsonwebtoken') const Client = require('../../objects/client') const CustomError = require('../../objects/custom_error') exports.login = async (req, res) => { try { let roles = [] let companies = [] let response = {} let decoded_token, token let { phone, password, app_id } = req.body phone = await core.normalize_phone(phone) app_id = parseInt(app_id) if (!phone) { throw new CustomError(400, 'phone required') } if (!password) { throw new CustomError(400, 'password required') } let client = await core.service_clients.get_client({ phone }) const match = await bcrypt.compare(password, client.get_password()) if (!match) { throw new CustomError(401, 'Password error') } if (!client) { throw new CustomError(401, 'Client not found') } if (!client.get_activated()) { throw new CustomError(401, 'Client not activated') } if (!client.get_enabled()) { throw new CustomError(401, 'Client disabled') } token = await core.context.get_token({ client_id: client.get_id(), app_id: app_id }) if (token) { try { decoded_token = jwt.verify(token, config.JWT_KEY) } catch (e) { await core.context.delete_token({ token }) token = null; // switch (e.name) { // case 'TokenExpiredError': // throw new CustomError(401, e.message) // case 'JsonWebTokenError': // throw new CustomError(401, e.message) // case 'NotBeforeError': // throw new CustomError(401, e.message) // } // e = { // name: 'TokenExpiredError', // message: 'jwt expired', // expiredAt: 1408621000 // } // e = { // name: 'JsonWebTokenError', // message: 'jwt malformed' // } // e = { // name: 'NotBeforeError', // message: 'jwt not active', // date: 2018 - 10 - 04T16: 10: 44.000Z // } } } if (!token) { let rows = await core.context.get_client_roles({ client_id: client.get_id() }) for (let r of rows) { roles.push(r.get_id()) } let rows2 = await core.context.get_user_places_devices(client.get_id()) for(let c of rows2){ companies.push(c.id) } token = jwt.sign( { client_id: client.get_id(), company_id: client.get_company_id(), city_id: client.get_city_id(), app_id: parseInt(app_id), roles: roles, companies: companies, issuer: 'homehead-service', }, config.JWT_KEY, { expiresIn: config.JWT_EXPIRESIN } ) decoded_token = jwt.verify(token, config.JWT_KEY) // сохранить токен клиента await core.context.insert_token({ client_id: client.get_id(), token: token, app_id: app_id }) } if (!decoded_token) { throw new CustomError(401, 'jwt error') } response.token = token switch (app_id) { case WHO.USER.KEY: // response.roles = roles // response.companies = companies break case WHO.ANDROID.KEY: case WHO.IOS.KEY: response.expiresIn = decoded_token.exp break case WHO.WEB.KEY: break } res.status(200).json(response) } catch (e) { log.error('api', req.originalUrl, e, req.body) if (e instanceof CustomError) { res.status(e.code).json(e.message) } else { res.status(500).json(e.message) } } } exports.logout = async (req, res) => { log.debug('api', req.originalUrl, req.params) try { res.status(501).json("Not Implemented"); } catch (e) { log.error('api', req.originalUrl, e, req.body) if (e instanceof CustomError) { res.status(e.code).json(e.message) } else { res.status(500).json(e.message) } } } exports.registration = async (req, res) => { log.debug('api', req.originalUrl, req.body) try { let { phone, password, name, email, company_id, app_id } = req.body if (!phone) { throw new CustomError(400, 'phone required') } if (!name) { throw new CustomError(400, 'name required') } if (!password) { throw new CustomError(400, 'password required') } if (password.length < 6) { throw new CustomError(400, 'minimum password length 6 characters') } let old_client = await core.service_clients.get_client({ phone }) if (old_client) { throw new CustomError(400, 'user with this number is already registered') } let client = await core.service_clients.create_client({ phone, password, name, email, company_id }) if (!client) { throw new CustomError(500, 'user creation error') } type = 1 // проверять в конфиге let code = await core.service_clients.activation({ phone, app_id, type }) if (!code) { throw new CustomError(500, 'user activation error') } res.status(200).json({ type, code }) } catch (e) { log.error('api', req.originalUrl, e, req.body) if (e instanceof CustomError) { res.status(e.code).json(e.message) } else { res.status(500).json(e.message) } } } exports.activation = async (req, res) => { log.debug('api', req.originalUrl, req.body) try { let { phone, app_id, type } = req.body let code = await core.service_clients.activation({ phone, app_id, type }) res.status(200).json({ type, code }) } catch (e) { log.error('api', req.originalUrl, e, req.body) if (e instanceof CustomError) { res.status(e.code).json(e.message) } else { res.status(500).json(e.message) } } } exports.activation_confirm = async (req, res) => { log.debug('api', req.originalUrl, req.body) try { let { phone, code } = req.body await core.service_clients.activation_confirm({ phone, code }) res.status(200).json({ status: 'OK' }) } catch (e) { log.error('api', req.originalUrl, e, req.body) if (e instanceof CustomError) { res.status(e.code).json(e.message) } else { res.status(500).json(e.message) } } } exports.password_change = async (req, res) => { log.debug('api', req.originalUrl, req.body) try { let { client_id, password, password_old } = req.body if (!client_id) { throw new CustomError(400, 'client_id required') } if (!password || password.length < 6) { throw new CustomError(400, 'minimum password length 6 characters') } if (!password_old) { throw new CustomError(400, 'password_old required') } let client = await core.service_clients.get_client({ client_id }) if (!client) { throw new CustomError(404, 'client not found') } const match = await bcrypt.compare(password_old, client.get_password()) if (!match) { throw new CustomError(400, 'bad password_old') } client.set_password(await bcrypt.hash(password, 10)) await core.context.update_client(client) res.status(200).json({ status: 'OK' }) } catch (e) { log.error('api', req.originalUrl, e, req.body) if (e instanceof CustomError) { res.status(e.code).json(e.message) } else { res.status(500).json(e.message) } } } // exports.password_reset = async (req, res) => { // log.debug('api', req.originalUrl, req.params) // try { // let { phone, password } = req.body // phone = await core.normalize_phone(phone) // if (!phone) { // throw new CustomError(400, 'phone required') // } // if (!password || password.length < 6) { // throw new CustomError(400, 'minimum password length 6 characters') // } // let client = await core.context.get_client({ client_id }) // if (!client) { // throw new CustomError(404, 'client not found') // } // } catch (e) { // log.error('api', req.originalUrl, e, req.body) // if (e instanceof CustomError) { // res.status(e.code).json(e.message) // } else { // res.status(500).json(e.message) // } // } // } exports.create = async (req, res) => { log.debug('api', req.originalUrl, req.params) try { let { phone, password, name, email, company_id } = req.body if (!phone) { res.status(400).json('phone required') } if (!name) { res.status(400).json('name required') } let client = await core.core.service_clients.create_client({ phone, password, name, email, company_id }) res.status(200).json({ id: client.get_id() }) } catch (e) { log.error('api', req.originalUrl, e, req.params) if (e instanceof CustomError) { res.status(e.code).json(e.message) } else { res.status(500).json(e.message) } } } exports.get = async (req, res) => { log.debug('api', req.originalUrl, req.params) try { let { client_id, phone } = req.params let client = await core.service_clients.get_client({ client_id, phone }) if (client instanceof Client) { res.status(200).json(client.get_object()) } else { res.status(404).json('Not Found') } } catch (e) { log.error('api', req.originalUrl, e, req.params) if (e instanceof CustomError) { res.status(e.code).json(e.message) } else { res.status(500).json(e.message) } } } exports.get_all = async (req, res) => { log.debug('api', req.originalUrl, req.query) try { let { limit } = req.query let clients = [] let result = await core.context.get_clients({ limit }) for (let r of result) { clients.push(r.get_object()) } res.status(200).json(clients) } catch (e) { log.error('api', req.originalUrl, e, req.query) if (e instanceof CustomError) { res.status(e.code).json(e.message) } else { res.status(500).json(e.message) } } } exports.get_client_roles = async (req, res) => { log.debug('api', req.originalUrl, req.params) try { let { limit, client_id } = req.params res.status(200).json(await core.context.get_client_roles({ limit, client_id })); } catch (e) { log.error('api', req.originalUrl, e, req.params) if (e instanceof CustomError) { res.status(e.code).json(e.message) } else { res.status(500).json(e.message) } } } exports.get_client_phones = async (req, res) => { log.debug('api', req.originalUrl, req.params) try { let { limit, client_id } = req.params res.status(200).json(await core.context.get_client_phones({ limit, client_id })); } catch (e) { log.error('api', req.originalUrl, e, req.params) if (e instanceof CustomError) { res.status(e.code).json(e.message) } else { res.status(500).json(e.message) } } } exports.client_add_phone = async (req, res) => { log.debug('api', req.originalUrl, req.body) try { let phone_id let { client_id, phone } = req.body phone = await core.normalize_phone(phone) if (!phone || !client_id) { res.status(400).json("Bad Request") return } let client = await core.service_clients.get_client({ client_id }) if (!client) { res.status(404).json("Not found") return } let client_phone = await core.context.get_phone({ phone }) if (client_phone) { if (phone.client_id == client.get_id()) { phone_id = client_phone.id } else { res.status(400).json("Phone is already in use") return } } if (!phone_id) { phone_id = await core.context.client_add_phone({ client_id, phone }) } res.status(200).json({ id: phone_id }) } catch (e) { log.error('api', req.originalUrl, e, req.body) if (e instanceof CustomError) { res.status(e.code).json(e.message) } else { res.status(500).json(e.message) } } } exports.client_delete_phone = async (req, res) => { log.debug('api', req.originalUrl, req.body) try { let { client_id, phone } = req.body phone = await core.normalize_phone(phone) if (!phone || !client_id) { res.status(400).json("Bad Request") return } let client = await core.service_clients.get_client({ client_id }) if (!client) { res.status(404).json("Not found client") return } let client_phones = await core.context.get_client_phones({ client_id }) if (client_phones && client_phones.length == 1) { res.status(400).json("Phones min limit") return } let client_phone = client_phones.find(p => p.phone == phone) if (!client_phone) { res.status(404).json("Not found client phone") return } await core.context.client_delete_phone({ client_id, phone }) res.status(200).json({ id: client_phone.id }) } catch (e) { log.error('api', req.originalUrl, e, req.body) if (e instanceof CustomError) { res.status(e.code).json(e.message) } else { res.status(500).json(e.message) } } } exports.client_add_role = async (req, res) => { log.debug('api', req.originalUrl, req.body) try { const { client_id, role_id } = req.body if (!client_id || !role_id) { res.status(400).json("Bad Request") return } let client = await core.service_clients.get_client({ client_id }) if (!client) { res.status(404).json("Not found client") return } let client_roles = await core.context.get_client_roles({ client_id }) let role = client_roles.find(p => p.id == role_id) if (role) { res.status(200).json({ id: role.id }) return } role = await core.context.get_role({ id: role_id }) if (!role) { res.status(404).json("Not found role") return } res.status(200).json({ id: await core.context.client_add_role({ client_id, role_id }) }) } catch (e) { log.error('api', req.originalUrl, e, req.body) if (e instanceof CustomError) { res.status(e.code).json(e.message) } else { res.status(500).json(e.message) } } } exports.client_delete_role = async (req, res) => { log.debug('api', req.originalUrl, req.body) try { const { client_id, role_id } = req.body if (!client_id || !role_id) { res.status(400).json("Bad Request") return } let client = await core.service_clients.get_client({ client_id }) if (!client) { res.status(404).json("Not found client") return } let client_roles = await core.context.get_client_roles({ client_id }) let role = client_roles.find(p => p.id == role_id) if (!role) { res.status(404).json("Not found role") return } let resdb = await core.context.client_delete_role({ client_id, role_id }) res.status(200).json({ id: role.id }) } catch (e) { log.error('api', req.originalUrl, e, req.body) if (e instanceof CustomError) { res.status(e.code).json(e.message) } else { res.status(500).json(e.message) } } } exports.get_config = async (req, res) => { log.debug('api', req.originalUrl, req.params) try { let client = await core.service_clients.get_client({ client_id: req.token.client_id }) if (!client) { throw new CustomError(404, 'Client not found') } let roles = [] let companies = [] let conf = {} let rows = await core.context.get_client_roles({ client_id: client.get_id() }) for (let r of rows) { roles.push(r.get_id()) } let rows2 = await core.context.get_user_places_devices(client.get_id()) for(let c of rows2){ companies.push(c.id) } switch (parseInt(req.token.app_id)) { case WHO.USER.KEY: conf.valuta = config.valuta ? config.valuta : '' conf.prefix = config.prefix ? config.prefix : '' conf.company_id = client.get_company_id() conf.client_name = client.get_name() conf.client_id = client.get_id() conf.city_id = client.get_city_id() conf.roles = roles conf.companies = companies break case WHO.WEB.KEY: break case WHO.ANDROID.KEY: case WHO.IOS.KEY: break } res.status(200).json(conf) } catch (e) { log.error('api', req.originalUrl, e, req.params) if (e instanceof CustomError) { res.status(e.code).json(e.message) } else { res.status(500).json(e.message) } } }