UNPKG

flexbiz-server

Version:

Flexible Server

9 lines (8 loc) 3.66 kB
'use strict';const model=global.getModel("notification"),User=global.getModel("user"),underscore=require("underscore"),controller=require("../../controllers/controller"),async=require("async"); 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($obj_receiver_user$$,$obj$$,$next$$){$obj_receiver_user$$=new model;$obj_receiver_user$$.email_sender=$obj$$.email_sender;$obj_receiver_user$$.email_receiver= $obj$$.email_receiver;$obj_receiver_user$$.email_owner=$obj$$.email_receiver;$obj_receiver_user$$.content=$obj$$.content;$obj_receiver_user$$.save(function($error$$){if($error$$)return console.log($error$$);global.alertNotification($obj$$.email_receiver);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); 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);$next$$(null,$obj$$)};$contr_router$$.createRoute("broadcast/all",function($q_req$$,$callback$jscomp$0$$){let $body$$=$q_req$$.body;if(!$body$$||!$body$$.title||!$body$$.message)return $callback$jscomp$0$$("error: Notification is empty"); if(!underscore.contains($usersAdmin$$,$q_req$$.user.email))return $callback$jscomp$0$$("Not permission");$q_req$$={};$body$$.to&&($q_req$$.email={$in:$body$$.to.split(",")});User.find($q_req$$,{email:1}).lean().exec(function($e$$,$us$$){if($e$$)return $callback$jscomp$0$$($e$$);async.mapSeries($us$$,($u$$,$callback$$)=>{model.createNotification("SYSTEM",$u$$.email,$body$$.title,$body$$.message,$e$$=>{$callback$$($e$$)},!0)},$e$$=>{if($e$$)return $callback$jscomp$0$$($e$$);$callback$jscomp$0$$(null, "broadcast is sent")})})},{method:"POST"});$contr_router$$.createRoute("read/all",async function($req$$,$callback$$){try{await model.updateMany({email_owner:$req$$.user.email,read:{$ne:!0}},{read:!0}),global.alertNotification($req$$.user.email),$callback$$(null,{result:"ok"})}catch($e$$){$callback$$($e$$)}},{method:"GET"})};