UNPKG

kpiplus

Version:

KPI+

1 lines 6.17 kB
"use strict";var underscore=require("underscore"),fs=require("fs"),path=require("path"),permission=require("../libs/permission"),excelReport=require("excel-report"),app=require("../models/app"),log=require("../models/log"),moment=require("moment");class controllerRPT{constructor(e,t,a,n={}){this.module=t,this.router=e;let s=this.module;this.base_path,!1===n.require_id_app?this.base_path="/":this.base_path="/:id_app/",e.route(`${this.base_path+this.module}`).get((e,r,i)=>{e.query.tu_ngay&&(e.query.tu_ngay=moment(new Date(e.query.tu_ngay)).startOf("date").toDate()),e.query.den_ngay&&(e.query.den_ngay=moment(new Date(e.query.den_ngay)).endOf("date").toDate());for(let t in e.query)e.query[t]||delete e.query[t];let o;delete e.query.t;let u=t;if(!1!==n.require_id_app&&(u+=e.params.id_app),n.cache&&!n.stream&&cacheDatas[u]&&(new Date).getTime()-cacheDatas[u].time.getTime()<3e4){let n=cacheDatas[u].data;n&&underscore.isEqual(cacheDatas[u].query,e.query)?(console.log(t,"get data from cache"),o=function(e,t){t(null,n)}):(delete cacheDatas[u],o=a)}else delete cacheDatas[u],o=a;let d=function(){let a,s=[];o(e,(i,o,d)=>{if(i)return r.status(400).send(i);n.stream?"begin"===d?(console.log("begin stream"),e.query.jsonstream?r.setHeader("Content-Type","application/jsonstream"):(r.setHeader("Content-Type","application/json"),r.setHeader("status",200),r.write("["))):o?(s.push(o),e.query.jsonstream?r.write(JSON.stringify(o)+"$$end%"):a?r.write(","+JSON.stringify(o)):r.write(JSON.stringify(o)),a=o):"end"==d&&(console.log("end stream"),log.create({id_app:e.user.current_id_app,id_func:t,action:"VIEWRPT",data:{condition:JSON.stringify(e.query)}},e.user.email,e.header("user-agent"),e),cacheDatas[u]||(delete e.query.jsonstream,cacheDatas[u]={data:s,time:new Date,query:e.query}),e.query.jsonstream?r.end():r.end("]")):(log.create({id_app:e.user.current_id_app,id_func:t,action:"VIEWRPT",data:{condition:JSON.stringify(e.query)}},e.user.email,e.header("user-agent"),e),cacheDatas[u]||(cacheDatas[u]={data:o,time:new Date,query:e.query}),r.send(o))})};if(!1===n.require_id_app)d();else{let t=e.params.id_app;permission.hasRight(t,e.user.email,s,"view",(e,t,a)=>{if(t){if(!permission.checkUseTime(a))return r.status(403).send("OFD - Chương trình cho "+a.name+" đã hết thời gian sử dụng. Để tiếp tục sử dụng, bạn hãy mua thêm thời gian sử dụng.");d()}else r.status(403).send("Bạn không có quyền xem báo cáo này")},{notNeedRight:n.notNeedRight})}}),e.route(`${this.base_path+this.module}/excel`).get((e,r,i)=>{for(let t in e.query)e.query[t]||delete e.query[t];let o;delete e.query.t,e.query.tu_ngay&&(e.query.tu_ngay=moment(new Date(e.query.tu_ngay)).startOf("date").toDate()),e.query.den_ngay&&(e.query.den_ngay=moment(new Date(e.query.den_ngay)).endOf("date").toDate()),n.onExport||(n.onExport=function(e,t){t()});let u=t;if(!1!==n.require_id_app&&(u+=e.params.id_app),n.stream&&!cacheDatas[u])return r.status(400).send("Bạn phải chạy báo cáo trước khi xuất dữ liệu ra excel");if(cacheDatas[u]&&(new Date).getTime()-cacheDatas[u].time.getTime()<6e5)if(underscore.isEqual(cacheDatas[u].query,e.query))o=function(e,t){n.stream?(t(null,null,"begin"),t(null,cacheDatas[u].data,"data"),t(null,null,"end")):t(null,cacheDatas[u].data)};else{if(n.stream)return r.status(400).send("Bạn phải chạy báo cáo trước khi xuất dữ liệu ra excel");delete cacheDatas[u],o=a}else{if(n.stream)return r.status(400).send("Bạn phải chạy báo cáo trước khi xuất dữ liệu ra excel");delete cacheDatas[u],o=a}let d=function(a){let s=[];o(e,function(i,o,u){if(i)return r.status(400).send(i);if(n.stream?o&&(underscore.isArray(o)?s=o:s.push(o)):s=o,!n.stream||"end"===u){if(log.create({id_app:e.user.current_id_app,id_func:t,action:"EXCELRPT",data:{condition:JSON.stringify(e.query)}},e.user.email,e.header("user-agent"),e),0===s.length)return r.status(400).send("Không có dữ liệu");delete a.participants,delete a.create_db_sql,delete a.expire_date,delete a.sale_online,underscore.extend(a,e.query),delete a.access_token,a.detail=s;for(let e in a){let t=a[e];underscore.isDate(t)&&(a[e+"_string"]=moment(t).format("DD/MM/YYYY"))}e.query.json?r.json(a):n.onExport(a,function(){console.log("begin export",new Date),process.nextTick(()=>{excelReport(c,a,function(e,a){if(e)return r.status(400).send(e);console.log("end export",new Date),r.setHeader("Content-Type","application/vnd.openxmlformats"),r.setHeader("Content-Disposition","attachment; filename="+t+".xlsx"),r.end(a,"binary"),console.log("end send export",new Date)})})})}})},c=`${path.dirname(__dirname)}/templates/excels/reports/${s}.xlsx`;if(!1===n.require_id_app)d();else{let t=e.params.id_app;permission.hasRight(t,e.user.email,s,"view",function(e,t,a){if(t){if(!permission.checkUseTime(a))return r.status(403).send("OFD - Chương trình cho "+a.name+" đã hết thời gian sử dụng. Để tiếp tục sử dụng, bạn hãy mua thêm thời gian sử dụng.");d(a)}else r.status(403).send("Bạn không có quyền xuất excel báo cáo này")},{notNeedRight:n.notNeedRight})}})}createRoute(e,t,a={method:"GET"}){a.method?a.method=a.method.toUpperCase():a.method="GET";let n=function(e,n,s){if(!1===options.require_id_app)t(e,function(e,t){if(e)return n.status(400).send(e);n.send(t)});else{let s=e.params.id_app;permission.hasRight(s,e.user.email,this.module,"view",function(a,s,r){if(s){if(!permission.checkUseTime(r))return n.status(403).send(`OFD - Chương trình cho ${r.name} đã hết thời gian sử dụng. Để tiếp tục sử dụng, bạn hãy mua thêm thời gian sử dụng.`);t(e,function(e,t){if(e)return n.status(400).send(e);n.send(t)})}else n.status(403).send("Bạn không có quyền xem báo cáo này")},{notNeedRight:options.notNeedRight||a.notNeedRight})}};"DELETE"==a.method?this.router.route(`${this.base_path+this.module}/${e}`).delete(n):"POST"==a.method?this.router.route(`${this.base_path+this.module}/${e}`).post(n):"PUT"==a.method?this.router.route(`${this.base_path+this.module}/${e}`).put(n):this.router.route(`${this.base_path+this.module}/${e}`).get(n)}}module.exports=controllerRPT;