UNPKG

flexbiz-server

Version:

Flexible Server

7 lines (6 loc) 2.38 kB
'use strict';const 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_receiver:1}),notificationSchema.index({email_sender:1}),notificationSchema.index({"exfields.id_app":1}),notificationSchema.index({email_receiver:1,email_owner:1,read:-1}),notificationSchema.index({date_created:-1}),notificationSchema.index({title:"text",content:"text"}),notificationSchema.index({key:1},{unique:!0}),notificationSchema.index({title:1}),notificationSchema.index({content:1}), notificationSchema.index({status:1}),notificationSchema.index({user_created:1,visible_to:1,visible_to_users:1}));const Notification=mongoose.models.notification||mongoose.model("notification",notificationSchema); Notification.createNotification=async function($email_sender$$,$email_receiver$$,$title$$,$content$$,$fn$$,$exfields$$,$emitEvent$$){$content$$="object"==typeof $content$$?JSON.stringify($content$$):($content$$||"").toString();let $key$$=($exfields$$||{}).key||`${$email_receiver$$}-${(new Date).getTime()}`;var $ex_notifi_re$$;($exfields$$||{}).key&&($ex_notifi_re$$=await Notification.findOne({key:$key$$}));$ex_notifi_re$$||($ex_notifi_re$$=new Notification,$ex_notifi_re$$.email_owner=$email_receiver$$, $ex_notifi_re$$.email_sender=$email_sender$$,$ex_notifi_re$$.email_receiver=$email_receiver$$,$ex_notifi_re$$.title=$title$$,$ex_notifi_re$$.content=$content$$,$ex_notifi_re$$.exfields=$exfields$$,$ex_notifi_re$$.exfields&&($ex_notifi_re$$.exfields.app_info=void 0),$ex_notifi_re$$.key=$key$$,$ex_notifi_re$$.user_created=$email_sender$$,$ex_notifi_re$$.user_updated=$email_sender$$,$ex_notifi_re$$.save(function($err$$,$m$$){$err$$?(console.error($err$$),$fn$$&&$fn$$($err$$)):(global.alertNotification($email_receiver$$), global.sendNotification($email_receiver$$,$m$$._id,$m$$.toObject(),!1!==$emitEvent$$),$fn$$&&$fn$$(null,$m$$,$m$$))}))};module.exports=Notification;