UNPKG

flexbiz-server

Version:

Flexible Server

12 lines (11 loc) 5.36 kB
'use strict';const model=global.getModel("schedule"),controller=require("../../controllers/controller"),underscore=require("underscore"),permission=require("../../libs/permission"); module.exports=function($contr_router$$){$contr_router$$=new controller($contr_router$$,model,"schedule",{require_id_app:!1,sort:{trang_thai:1,time_run:1},onFinding:($user$$,$condition$$,$next$$)=>{const $adminUsers$$=configs.admins.concat(configs.subAdmins||[]);underscore.contains($adminUsers$$,$user$$.email)?$next$$(null,$condition$$):$next$$("B\u1ea1n kh\u00f4ng c\u00f3 quy\u1ec1n truy c\u1eadp th\u00f4ng tin n\u00e0y")}});$contr_router$$.deleting=function($user$$,$obj$$,$fn$$){const $adminUsers$$= configs.admins.concat(configs.subAdmins||[]);underscore.contains($adminUsers$$,$user$$.email)?$fn$$(null,$obj$$):$fn$$("B\u1ea1n kh\u00f4ng th\u1ec3 x\u00f3a n\u1ed9i dung n\u00e0y")};$contr_router$$.deleted=async function($user$$,$obj$$,$next$$){try{let $schedules$$=await model.loadFromRedis();$schedules$$=$schedules$$.filter($s$$=>$s$$._id.toString()!==$obj$$._id.toString());await model.saveToRedis($schedules$$)}catch($e$$){console.error("error remove schedule",$e$$)}$next$$(null,$obj$$)};$contr_router$$.creating= function($user$$,$data$$,$fn$$){const $adminUsers$$=configs.admins.concat(configs.subAdmins||[]);underscore.contains($adminUsers$$,$user$$.email)?($data$$.next_run=null,$fn$$(null,$data$$)):$fn$$("B\u1ea1n kh\u00f4ng th\u1ec3 t\u1ea1o n\u1ed9i dung n\u00e0y")};$contr_router$$.created=async function($user$$,$schedule$$,$next$$){try{$schedule$$.toObject&&($schedule$$=$schedule$$.toObject());const $schedules$$=await model.loadFromRedis();$schedules$$.push($schedule$$);await model.saveToRedis($schedules$$); console.log("added a schedule, time state at",$schedule$$.time_run);$next$$(null,$schedule$$)}catch($e$$){$next$$($e$$)}};$contr_router$$.updating=function($user$$,$data$$,$obj$$,$fn$$){const $adminUsers$$=configs.admins.concat(configs.subAdmins||[]);underscore.contains($adminUsers$$,$user$$.email)?$fn$$(null,$data$$,$obj$$):$fn$$("B\u1ea1n kh\u00f4ng th\u1ec3 c\u1eadp nh\u1eadt n\u1ed9i dung n\u00e0y")};$contr_router$$.updated=async function($user$$,$schedule$$,$next$$){try{$schedule$$.toObject&& ($schedule$$=$schedule$$.toObject());let $schedules$$=await model.loadFromRedis();$schedules$$=$schedules$$.filter($s$$=>$s$$._id.toString()!==$schedule$$._id.toString());$schedules$$.push($schedule$$);await model.saveToRedis($schedules$$);console.log("updated a schedule, time state at",$schedule$$.time_run);$next$$(null,$schedule$$)}catch($e$$){$next$$($e$$)}};$contr_router$$.createRoute("add/notify",async($req_sche$$,$next$$)=>{let $obj$$=$req_sche$$.body;if(!$obj$$._id)return $next$$("D\u1eef li\u1ec7u thi\u1ebfu th\u00f4ng tin _id c\u1ee7a \u0111\u1ed1i t\u01b0\u1ee3ng li\u00ean k\u1ebft (_id)"); if(!$obj$$.code)return $next$$("D\u1eef li\u1ec7u thi\u1ebfu th\u00f4ng tin m\u00e3 \u0111\u1ed1i t\u01b0\u1ee3ng li\u00ean k\u1ebft (code)");if(!$obj$$.time_run)return $next$$("D\u1eef li\u1ec7u thi\u1ebfu th\u1eddi gian b\u1eaft \u0111\u1ea7u ch\u1ea1y (time_run)");if(!$obj$$.users_will_receive||0===$obj$$.users_will_receive.length)return $next$$("D\u1eef li\u1ec7u thi\u1ebfu th\u00f4ng tin danh s\u00e1ch ng\u01b0\u1eddi nh\u1eadn");if(!$obj$$.title)return $next$$("D\u1eef li\u1ec7u thi\u1ebfu ti\u00eau \u0111\u1ec1 c\u1ee7a th\u00f4ng b\u00e1o"); let $data$$={...$obj$$};await model.removeScheduleLink($obj$$._id.toString());let $time_run$$=new Date($obj$$.time_run);$time_run$$.setMinutes($time_run$$.getMinutes()-($obj$$.advance_remider||$obj$$.nhac_nho_truoc||30));$req_sche$$={id_link:$data$$.id_link||$data$$._id.toString(),id_app:$data$$.id_app,action:"sendNotification",data:$data$$,title:$data$$.title,time_run:$time_run$$,trang_thai:"0",repeat:$data$$.repeat||0,finish_run:$data$$.finish_run,status:!0,user_created:$req_sche$$.user.email,user_updated:$req_sche$$.user.email}; model.addSchedule($req_sche$$);$next$$(null,$req_sche$$)},{method:"POST"});$contr_router$$.createRoute("add/updateData",async($req$$,$next$$)=>{let $obj$$=$req$$.body;if(!$obj$$.data)return $next$$("D\u1eef li\u1ec7u thi\u1ebfu data");if(!$obj$$.data.module)return $next$$("D\u1eef li\u1ec7u thi\u1ebfu data.module");if(!$obj$$.time_run)return $next$$("D\u1eef li\u1ec7u thi\u1ebfu th\u1eddi gian b\u1eaft \u0111\u1ea7u ch\u1ea1y (time_run)");let $data$$={...$obj$$.data};permission.hasRight($data$$.id_app, $req$$.user.email,$data$$.module,"add",async($e$jscomp$3_sche$$,$right$$)=>{$right$$?(await model.removeScheduleLink($obj$$.id_link||$data$$._id.toString()),$e$jscomp$3_sche$$={id_link:$obj$$.id_link||$data$$._id.toString(),id_app:$data$$.id_app,action:"updateData",data:$data$$,title:$obj$$.title||"C\u1eadp nh\u1eadt d\u1eef li\u1ec7u cho module "+$data$$.module,time_run:new Date($obj$$.time_run),trang_thai:"0",repeat:0,status:!0,user_created:$req$$.user.email,user_updated:$req$$.user.email},model.addSchedule($e$jscomp$3_sche$$), $next$$(null,$e$jscomp$3_sche$$)):$next$$($e$jscomp$3_sche$$||"Kh\u00f4ng th\u1ec3 t\u1ea1o l\u1ecbch c\u1eadp nh\u1eadt d\u1eef li\u1ec7u cho module "+$obj$$.data.module)})},{method:"POST"});$contr_router$$.createRoute("delete/:id_link",async($req$$,$next$$)=>{await model.removeScheduleLink($req$$.params.id_link);$next$$(null,{result:"OK"})},{method:"GET"});$contr_router$$.route()};