flexbiz-server
Version:
Flexible Server
9 lines (8 loc) • 2.94 kB
JavaScript
const {runWithoutSession}=require("../libs/sessionContext"),notificationSchema=new Schema({key:String,email_sender:{type:String,required:!0},email_receiver:{type:String,required:!0},email_owner:{type:String,required:!0},title:{type:String,required:!0},content:{type:String},exfields:Schema.Types.Mixed,read:{type:Boolean,default:!1},status:{type:Boolean,default:!1},date_created:{type:Date,default:Date.now},date_updated:{type:Date,default:Date.now},user_created:{type:String,default:""},user_updated:{type:String,
default:""}});
(global.configs||{}).createIndexes&&(notificationSchema.index({email_owner:1}),notificationSchema.index({email_owner:1,email_receiver:1}),notificationSchema.index({email_owner:1,email_sender:1}),notificationSchema.index({"exfields.id_app":1}),notificationSchema.index({"exfields.id_app":1,email_owner:1}),notificationSchema.index({"exfields.id_app":1,email_owner:1,read:1}),notificationSchema.index({"exfields.id_app":1,email_owner:1,date_created:-1}),notificationSchema.index({"exfields.id_app":1,email_receiver:1,
email_owner:1,read:-1}),notificationSchema.index({key:1},{unique:!0}),notificationSchema.index({email_owner:1,user_created:1,visible_to:1,visible_to_users:1}));const Notification=mongoose.models.notification||mongoose.model("notification",notificationSchema);
Notification.createNotification=function($email_sender$$,$email_receiver$$,$title$$,$content$$,$fn$$,$exfields$$,$emitEvent$$){runWithoutSession(async()=>{if(!$email_sender$$||!$email_receiver$$){if($fn$$){$fn$$("Kh\u00f4ng c\u00f3 \u0111\u1ee7 th\u00f4ng tin ng\u01b0\u1eddi nh\u1eadn v\u00e0 ng\u01b0\u1eddi g\u1eedi");return}throw"Kh\u00f4ng c\u00f3 \u0111\u1ee7 th\u00f4ng tin ng\u01b0\u1eddi nh\u1eadn v\u00e0 ng\u01b0\u1eddi g\u1eedi";}$content$$=typeof $content$$=="object"?JSON.stringify($content$$):
($content$$||"").toString();let $key$$=($exfields$$||{}).key||`${$email_receiver$$}-${(new Date).getTime()}`,$ex$$;($exfields$$||{}).key&&($ex$$=await Notification.findOne({key:$key$$}));if(!$ex$$){const $notifi_re$$=new Notification;$notifi_re$$.email_owner=$email_receiver$$;$notifi_re$$.email_sender=$email_sender$$;$notifi_re$$.email_receiver=$email_receiver$$;$notifi_re$$.title=$title$$;$notifi_re$$.content=$content$$;$notifi_re$$.exfields=$exfields$$;$notifi_re$$.exfields&&($notifi_re$$.exfields.app_info&&
$notifi_re$$.exfields.app_info._id&&!$notifi_re$$.exfields.id_app&&($notifi_re$$.exfields.id_app=$notifi_re$$.exfields.app_info._id.toString()),$notifi_re$$.exfields.app_info=void 0);$notifi_re$$.key=$key$$;$notifi_re$$.user_created=$email_sender$$;$notifi_re$$.user_updated=$email_sender$$;$notifi_re$$.save(function($err$$,$m$$){$err$$?(Logger.error($err$$),$fn$$&&$fn$$($err$$)):(global.alertNotification($email_receiver$$,$notifi_re$$.exfields?.id_app),global.sendNotification($email_receiver$$,$m$$._id,
$m$$.toObject(),$emitEvent$$!==!1),$fn$$&&$fn$$(null,$m$$,$m$$))})}})};module.exports=Notification;