'use strict' /** * Copyright (c) 2017~2023, OBCon Inc. * All rights reserved. */ /** * @file * @copyright 2017~2023, OBCon Inc. * @author gye hyun james kim [pnuskgh@gmail.com] */ let View = utils.getClass('include', 'view.js'); //--- SELECT DISTINCT category FROM settings; //--- SELECT category, name, site, moduleName, moduleId, custom FROM settings ORDER BY category ASC, site ASC, moduleName ASC, moduleId ASC; //--- SELECT category, name, site, moduleName, moduleId, custom FROM settings WHERE category = 'Device default' AND site = 1 ORDER BY category ASC, site ASC, moduleName ASC, moduleId ASC; //--- SELECT category, name, site, moduleName, moduleId, custom FROM settings WHERE category = 'Notification Setting' AND site = 1 AND moduleId = 1 ORDER BY category ASC, site ASC, moduleName ASC, moduleId ASC; class SettingsView extends View { constructor() { super(); this._name = 'settings'; this._title = '설정'; this._model = modules.settings.model; this._hasSite = false; } //--- 권한 설정 반환 getRights() { let rights = super.getRights(); rights.get.list = { isLogin: true, isAdmin: true, isGlobalAdmin: true }; rights.get.detail = { isLogin: true, isAdmin: true, isGlobalAdmin: true }; rights.get.create = { isLogin: true, isAdmin: true, isGlobalAdmin: true }; rights.get.update = { isLogin: true, isAdmin: true, isGlobalAdmin: true }; rights.get.upsertNotification = { isLogin: true, isAdmin: true, isGlobalAdmin: false }; rights.ajaxPost.create = { isLogin: true, isAdmin: true, isGlobalAdmin: false }; rights.ajaxPost.update = { isLogin: true, isAdmin: true, isGlobalAdmin: false }; rights.ajaxPost.delete = { isLogin: true, isAdmin: true, isGlobalAdmin: true }; rights.ajaxPost.upsertNotification = { isLogin: true, isAdmin: true, isGlobalAdmin: false }; return rights; } //--- Router 설정 반환 getRouters() { let routers = super.getRouters(); routers.get.upsertNotification = this.upsertNotificationView.bind(this); routers.ajaxPost.upsertNotification = this.ajaxUpsertNotification.bind(this); return routers; } ajaxCreate(req, res, next) { let site = req.body.site; if ((req.session.isGlobalAdmin == false) && (site != req.session.site.id)) { this.errorPage(req, res, 500, '권한이 없습니다.'); return; } super.ajaxCreate(req, res, next); } ajaxUpdate(req, res, next) { let site = req.body.site; if ((req.session.isGlobalAdmin == false) && (site != req.session.site.id)) { this.errorPage(req, res, 500, '권한이 없습니다.'); return; } super.ajaxUpdate(req, res, next); } upsertNotificationView(req, res, next) { //--- 본인과 관리자만 사용자의 SMS Notifications을 설정을 수정 한다. if ((parseInt(req.query.user) == req.session.user.id) || (req.session.user.isAdmin) || (req.session.user.isGlobalAdmin)) { } else { next(); return; } let theme = this.getTheme('upsertNotificationView', req, 'upsertNotificationView', 'upsertNotificationView.ejs'); theme.javascriptFiles.push(this.name + '/js/upsertNotificationView.js'); let userId = req.query.user; let query = { where: { site: req.session.site.id, id: userId, deleted: false } }; logger.error(JSON.stringify(query)); modules.users.model.table.findOne(query) .then(function(user) { if (user == null) { next(); return; } // select site, moduleName, moduleId, category, name from settings; query = { where: { site: req.session.site.id, moduleName: 'user', moduleId: userId, category: 'Notification Setting', name: 'email_sms', deleted: false } }; this.model.table.findOne(query) .then(function(item) { if (item == null) { const custom = { status: '0', emailWork: user.emailWork, //--- 회사 이메일 phoneMobile: user.phoneMobile, //--- 핸드폰 smsOptOut: user.smsOptOut //--- SMS 수신 거부 }; modules.devices.setting.getSettings().types.forEach(item => { custom[`deviceCondition${item.name}`] = ''; }); item = { site: req.session.site.id, moduleName: 'user', moduleId: userId, category: 'Notification Setting', name: 'email_sms', custom: JSON.stringify(custom), description: '', assignedUserId: req.session.user.id, createdBy: req.session.user.id, updatedBy: req.session.user.id }; this.model.table.create(item) .then(function(results) { let data = { item: results }; res.render(utils.getTemplateFullPath(theme.serviceInfo.site.id, 'layoutBlank.ejs'), { theme: theme, baseUrl: req.baseUrl, session: req.session, data: data }); }).catch(utils.obj.loggerError); } else { //--- To-Do: Site 설정에서 SMS, Email 수신 여부가 변경되면, 그에 맞도록 deviceCondition도 변경하여 화면에 표시 하여야 한다. let data = { item: item }; res.render(utils.getTemplateFullPath(theme.serviceInfo.site.id, 'layoutBlank.ejs'), { theme: theme, baseUrl: req.baseUrl, session: req.session, data: data }); } }.bind(this)).catch(utils.obj.loggerError); }.bind(this)).catch(utils.obj.loggerError); } ajaxUpsertNotification(req, res, next) { //--- 본인과 관리자만 사용자의 SMS Notifications을 설정을 수정 한다. if ((parseInt(req.body.moduleId) == req.session.user.id) || (req.session.user.isAdmin) || (req.session.user.isGlobalAdmin)) { } else { next(); return; } let record = req.body.record; let query = { where: { id: record, deleted: false } }; this.model.table.findOne(query) .then(function(item) { if (item == null) { utils.obj.resJson(res, 3, 'Data not found.', {}, {}); return; } // logger.info(JSON.stringify(item)); if (item.moduleId != parseInt(req.body.moduleId)) { utils.obj.resJson(res, 4, 'Not authorized.', {}, {}); return; } let custom = JSON.parse(item.custom); custom.status = req.body.status; modules.devices.setting.getSettings().types.forEach(item => { const typeName = `deviceCondition${item.name}`; custom[typeName] = req.body[typeName]; }); const itemUpdate = { custom: JSON.stringify(custom) } logger.info('modules/settings/view.js :: table update'); this.model.table.update(itemUpdate, { where: { id: record, deleted: false } }) .then(function(results) { utils.obj.resJson(res, 0, 'ok', results, {}); }).catch(function(err) { utils.obj.resJson(res, 2, utils.obj.getErrorMessage(err), {}, err); utils.obj.loggerError(err); }); }.bind(this)).catch(function(err) { utils.obj.resJson(res, 1, utils.obj.getErrorMessage(err), {}, err); utils.obj.loggerError(err); }); } } module.exports = SettingsView;