kpiplus
Version:
KPI+
1 lines • 9.48 kB
JavaScript
var model=require("../../models/app"),User=require("../../models/user"),usersql=require("../../models/usersql"),Notification=require("../../models/notification"),underscore=require("underscore"),async=require("async"),usersAdmin=require("../../configs").admins,stp=require("stp-utils"),controller=require("../../controllers/controller"),sqlServer=require("../../libs/sqlserver"),permission=require("../../libs/permission"),request=require("request"),fs=require("fs"),path=require("path");let fork=require("child_process").fork;var date2sqltype=function(e){return e.getFullYear()+"-"+(e.getMonth()+1)+"-"+e.getDate()},calcTimeExpired=function(e){e.so_ngay_con_lai=7e3};module.exports=function(e){var n=new controller(e,model,"app",{require_id_app:!1,sort:{name:1}});n.route(),n.found=function(e,n,t,a){t.forEach(function(e){calcTimeExpired(e)}),usersql.find({email:e.email}).lean().exec(function(e,n){if(n){var i=[],r={};n.forEach(function(e){r[e.id_app]=e.id,i.push(e.id_app)}),model.find({_id:{$in:i}}).lean().exec(function(e,n){e&&console.log(e),n&&n.forEach(function(e){var n=underscore.find(t,function(n){return n._id.toString()==e._id.toString()});n||(n=e,t.push(n)),r[n._id.toString()]&&(n.user_db_sql=r[n._id.toString()])}),a(null,t)})}else a(null,t)})},n.view=function(e,n,t){async.parallel({get_name:function(t){async.map(n,function(n,t){n.user_created==e.email&&(n.active=!0),n.participants?n.participants.joinModel2(void 0,User,[{where:{email:"email"},fields:[{name:"name",value:"name"},{name:"picture",value:"picture"}],setFields:(e,n)=>{e.name=n.local.name}}],function(a){a.forEach(function(t){t.picture||(t.picture="/images/avatar.jpg"),t.name||(t.name=t.email),t.email==e.email&&(n.active=t.active)}),t(null)}):t(null)},function(e){t(null)})}},function(e,a){n.forEach(function(e){e.options||(e.options={}),calcTimeExpired(e)}),t(null,n)})},n.getting=function(e,n,t){model.find({_id:n,$or:[{user_created:e.email},{participants:{$elemMatch:{email:e.email}}}]},function(e,n){return e?t(e):0===n.length?t("Not allowed"):t()})},n.finding=function(e,n,t){let a=[{user_created:e.email},{participants:{$elemMatch:{email:e.email,cancel:!1}}}],i=n.$or;delete n.$or,i?n.$and=[{$or:a},{$or:i}]:n.$or=a,t(null,n)},n.creating=function(e,n,t){if(!n.ngay_dn)return t(new Error("Ngày đầu năm tài chính không được trống"));n.ngay_ks||(n.ngay_ks=new Date("2000-01-01")),n.ngay_ky1||(n.ngay_ky1=new Date);var a=new Date;a.setMonth(a.getMonth()+3),n.expire_date=new Date(a),n.nam_bd=n.ngay_ky1.getFullYear(),n.participants&&n.participants.forEach(function(n){n.user_created=e.email}),t(null,n)},n.created=function(e,n,t){(async.map(n.participants,function(e,t){if(e.active||e.cancel)return t();var a=e.email;createNotification(n.user_updated,a,`Bạn đã được thêm vào công ty ${n.name}`,`Bạn đã được thêm vào công ty ${n.name}`,e=>{console.log(e||`Added ${a} to ${n.name}`)},{code:"app",_id:n._id}),t()},function(e){}),n.create_db_sql)?n.server?request.get(n.server+"/createdb?obj="+JSON.stringify(n)).on("response",function(e){t(null,n)}).on("error",function(e){model.remove({_id:n._id},function(n,a){t("Không thể tạo database mới\n"+e)})}):sqlServer.restoreDB("S"+n._id,function(a,i){a?(console.log(a),model.remove({_id:n._id},function(e,n){t("Không thể tạo database mới")})):async.parallel({update_cty_info:function(e){var t="update options set value1=N'"+n.name+"',value2 =N'"+n.name+"' where name='cty_name'";t=(t=(t=t+"\nupdate options set value1=N'"+n.address+"',value2 =N'"+n.address+"' where name='cty_address'")+"\nupdate options set value1=N'"+n.phone+"',value2 =N'"+n.phone+"' where name='cty_phone'")+"\nupdate options set value1=N'"+n.email+"',value2 =N'"+n.email+"' where name='cty_email'",t+="\n update options set value1 ='1',value2 ='1' where name ='dt_autobackup'",t+="\n update options set value1 ='C:STPBackup',value2 ='C:STPBackup' where name ='dt_dirbackup'",t+="\n update command set hide_yn =1 where id='99.03.03'",t+="\n update command set hide_yn=0 where ma_cn ='dmnsd'",t=(t+="\n update versioninfo set id_app ='SE75OL'")+"\nupdate options set value1=N'"+n.ma_so_thue+"',value2 =N'"+n.ma_so_thue+"' where name='ma_so_thue'",sqlServer.query(i,t,function(n,a){n&&console.log(t),e(null)})},update_dmstt:function(e){var t="update dmstt set ngay_dn='"+date2sqltype(n.ngay_dn)+"',ngay_ks='"+date2sqltype(n.ngay_ks)+"',ngay_ky1='"+date2sqltype(n.ngay_ky1)+"'";t=t+",nam_bd="+n.ngay_ky1.getFullYear(),sqlServer.query(i,t,function(n,a){n&&console.log(t),e(null)})},create_user_admin:function(t){var a="delete dmnsd where id='"+n.user_db_sql+"'";a=a+"\ninsert into dmnsd(admin,supper_admin,ma_nv,trang_thai,id,idname) values (1,1,'',1,'"+n.user_db_sql+"',N'"+e.name+"')",a+="\nexec notnull 'dmnsd'",sqlServer.query(i,a,function(e,a){e?model.remove({_id:n._id},function(e,n){t("Không thể khởi tạo công ty này")}):t(null)})}},function(e,a){if(e)return t(e);t(null,n)})}):require("../../libs/initDatabase").init(n._id,function(e){e&&(console.log("Can't init database \n"+e),e="Rất lấy làm tiếc, Chương trình không thể khởi tạo dữ liệu ban đầu cho công ty này, tuy nhiên bạn vẫn có thể sử dụng chương trình"),t(e,n)})},n.updating=function(e,n,t,a){permission.isAdmin(t._id,e.email,function(i,r){if(!r&&!underscore.contains(usersAdmin,e.email)&&!stp.isSupperAdmin(e.email.toLowerCase()))return a(e.email+" have not permission");n.ngay_ky1&&(n.nam_bd=new Date(n.ngay_ky1).getFullYear()),n.participants&&n.participants.forEach(function(n){n.user_created||(n.user_created=e.email),n.user_updated=e.email}),a(null,n,t)})},n.updated=function(e,n,t){async.map(n.participants,function(e,t){if(e.active||e.cancel)return t();var a=e.email;createNotification(n.user_updated,a,`Bạn đã được thêm vào công ty ${n.name}`,`Bạn đã được thêm vào công ty ${n.name}`,e=>{console.log(e||`Added ${a} to ${n.name}`)},{code:"app",_id:n._id}),t()},function(e){}),t(null,n)},n.deleting=function(e,n,t){if(e.email!=n.user_created&&!underscore.contains(usersAdmin,e.email)&&!stp.isSupperAdmin(e.email.toLowerCase()))return t("Bạn không có quyền xóa công ty này");t(null,n)},n.deleted=function(e,n,t){async.map(fs.readdirSync(path.dirname(path.dirname(__dirname))+"/models"),(e,t)=>{let a=require("../../models/"+e);underscore.has(a.schema.paths,"id_app")?a.remove({id_app:n._id},(a,i)=>{console.log("Deleted ",e,"id_app",n._id),t()}):t()},(e,a)=>{t(null,n)})},n.router.route(n.route_name+"/backup/:id").get(function(e,n,t){var a=e.user.email;let i=e.params.id,r=path.dirname(path.dirname(__dirname))+"/workers/backupApp.js",o=fork(r);o.on("message",function(e){if(e.error)return n.status(400).send(e.error);e.file&&n.sendFile(path.dirname(path.dirname(__dirname))+"/backup/"+e.file)}),o.send({id:i,user:a})}),n.router.route(n.route_name+"/restore/:id").post(function(e,n,t){if(!e.files.backup)return n.status(400).send("Không tìm thấy file backup");let a=path.dirname(path.dirname(__dirname))+"/"+e.files.backup.path;var i=e.user.email;let r=e.params.id,o=path.dirname(path.dirname(__dirname))+"/workers/restoreApp.js",s=fork(o);s.on("message",function(e){e.error&&Notification.createNotification(i,i,e.error,e.error),e.result&&Notification.createNotification(i,i,e.result,e.result),e.info&&Notification.createNotification(i,i,e.info,e.info)}),s.send({id:r,user:i,file:a}),n.send("Chương trình đang thực hiện. Xin chờ trong giây lát")}),n.router.route(n.route_name+"/apps/:email_owner").get(function(e,n,t){var a=e.params.email_owner;if(!underscore.contains(usersAdmin,e.user.email)&&!stp.isSupperAdmin(user.email.toLowerCase()))return n.status(403).send("Bạn không có quyền truy cập tính năng này");var i={};"all"!=a.toLowerCase()&&(i.user_created=a),model.find(i).lean().exec(function(e,t){if(e)return n.status(400).send(e);t.forEach(function(e){calcTimeExpired(e)}),n.send(t)})}),n.router.route(n.route_name+"/active/:id").get(function(e,n,t){var a=e.params.id,i=e.user.email;model.findOne({_id:a,participants:{$elemMatch:{email:i}}},function(e,t){return e?n.status(400).send(e):t?(t.participants.forEach(function(e){e.email==i&&(e.active=!0,e.cancel=!1)}),void t.save(function(e){if(e)return n.status(400).send(e);Notification.createNotification(i,t.user_created,"Chấp nhận tham gia "+t.name,"đã chấp nhận tham gia "+t.name),n.send("ok")})):n.send("ok")})}),n.router.route(n.route_name+"/notaccept/:id").get(function(e,n,t){var a=e.params.id,i=e.user.email;model.findOne({_id:a,participants:{$elemMatch:{email:i}}},function(e,t){if(e)return n.status(400).send(e);t.participants.forEach(function(e){e.email==i&&(e.active=!1,e.cancel=!0)}),t.save(function(e){if(e)return n.status(400).send(e);Notification.createNotification(i,t.user_created,"Không chấp nhận tham gia "+t.name,"đã không chấp nhận tham gia "+t.name),n.send("ok")})})});var t=require("crypto");n.router.route(n.route_name+"/getTokenForVoiIP/:id").get(function(e,n,a){var i=e.params.id,r=e.user.email;permission.isAdmin(i,r,function(e,a){return e?n.status(400).send(e):a?void model.findOne({_id:i},function(e,a){if(e)return n.status(400).send(e);if(!a)return n.status(400).send("id app "+i+" is not exists");let r=t.createHash("md5").update((new Date).toISOString()+i).digest("hex");a.voiIPTokens||(a.voiIPTokens=[]),a.voiIPTokens.push(r),a.save(function(e){if(e)return n.status(400).send(e);n.send({token:r})})}):n.status(400).send("Not permission")})})};