UNPKG

flexbiz-server

Version:

Flexible Server

9 lines (8 loc) 3.81 kB
const model=global.getModel("notification"),User=global.getModel("user"),underscore=require("underscore"),controller=require("../../controllers/controller"),async=require("async"),{onAfterCommit}=require("../../libs/sessionContext"); module.exports=function($contr_router$$){const $usersAdmin$$=configs.admins;$contr_router$$=new controller($contr_router$$,model,"notification",{require_id_app:!1,sort:{date_created:-1}});$contr_router$$.route();$contr_router$$.view=function($user$$,$result$$,$fn$$){$result$$.joinModel(void 0,User,[{akey:"email_sender",bkey:"email",fields:[{name:"name_sender",value:"name"},{name:"picture_sender",value:"picture"}]},{akey:"email_receiver",bkey:"email",fields:[{name:"name_receiver",value:"name"},{name:"picture_receiver", value:"picture"}]}],function(){$result$$.forEach(function($r$$){$r$$.picture_sender||($r$$.picture_sender="/images/avatar.jpg");$r$$.name_sender||($r$$.name_sender=$r$$.email_sender);$r$$.picture_receiver||($r$$.picture_receiver="/images/avatar.jpg");$r$$.name_receiver||($r$$.name_receiver=$r$$.email_receiver)});$fn$$(null,$result$$)})};$contr_router$$.getting=function($user$$,$id$$,$next$$,$obj$$){if($obj$$.email_owner!=$user$$.email)return $next$$("B\u1ea1n kh\u00f4ng c\u00f3 quy\u1ec1n th\u1ef1c hi\u1ec7n thao t\u00e1c n\u00e0y"); $next$$()};$contr_router$$.finding=function($user$$,$condition$$,$next$$){$condition$$.$or=[{email_owner:$user$$.email}];$next$$(null,$condition$$)};$contr_router$$.creating=function($user$$,$obj$$,$next$$){$obj$$.email_sender=$user$$.email;$obj$$.email_owner=$user$$.email;$obj$$.read=!0;$next$$(null,$obj$$)};$contr_router$$.created=function($user$$,$obj$$,$next$$){onAfterCommit(()=>{const $obj_receiver$$=new model;$obj_receiver$$.email_sender=$obj$$.email_sender;$obj_receiver$$.email_receiver=$obj$$.email_receiver; $obj_receiver$$.email_owner=$obj$$.email_receiver;$obj_receiver$$.content=$obj$$.content;$obj_receiver$$.save(function($error$$){if($error$$)return Logger.info($error$$);global.alertNotification($obj$$.email_receiver,$obj$$.exfields?.id_app);User.emitEvent($obj$$.email_receiver,"notify:new",{_id:$obj$$._id,sender:$obj$$.email_sender,content:$obj$$.content})})});$next$$(null,$obj$$)};$contr_router$$.updated=function($user$$,$obj$$,$next$$){global.alertNotification($obj$$.email_receiver,$obj$$.exfields?.id_app); User.emitEvent($obj$$.email_receiver,"notify:update",{_id:$obj$$._id,sender:$obj$$.email_sender,content:$obj$$.content});$next$$(null,$obj$$)};$contr_router$$.deleting=function($user$$,$obj$$,$next$$){$next$$(null,$obj$$)};$contr_router$$.deleted=function($user$$,$obj$$,$next$$){global.alertNotification($obj$$.email_owner,$obj$$.exfields?.id_app);$next$$(null,$obj$$)};$contr_router$$.createRoute("broadcast/all",function($q_req$$,$callback$$){let $body$$=$q_req$$.body;if(!$body$$||!$body$$.title|| !$body$$.message)return $callback$$("error: Notification is empty");if(!underscore.contains($usersAdmin$$,$q_req$$.user.email))return $callback$$("Not permission");$q_req$$={};$body$$.to&&($q_req$$.email={$in:$body$$.to.split(",")});User.find($q_req$$,{email:1}).lean().then(function($us$$){async.mapLimit($us$$,20,($u$$,$callback$$)=>{model.createNotification("SYSTEM",$u$$.email,$body$$.title,$body$$.message,$e$$=>{$callback$$($e$$)},!0)},$e$$=>{if($e$$)return $callback$$($e$$);$callback$$(null,"broadcast is sent")})}).catch($e$$=> {$callback$$($e$$)})},{method:"POST"});$contr_router$$.createRoute("read/all",async function($req$$,$callback$$){try{let $query$$={email_owner:$req$$.user.email,read:{$ne:!0}};$req$$.query.id_app&&($query$$["exfields.id_app"]=$req$$.query.id_app);await model.updateMany($query$$,{read:!0});global.alertNotification($req$$.user.email,$req$$.query.id_app);$callback$$(null,{result:"ok"})}catch($e$$){$callback$$($e$$)}},{method:"GET"})};