json-object-editor
Version:
JOE the Json Object Editor | Platform Edition
110 lines (107 loc) • 5.57 kB
JavaScript
var notification ={
info:"A notification is an automated message, sent when an action is taken (ex: new subscriber email). A notification can be an email, sms, or system.",
default_schema:true,
title: '${name}',
methods:{
sendTest: async function(notificationId){
try{
var recipients = _jco(true).test_recipients || [];
if(!recipients.length){ alert('Please add at least one recipient'); return; }
var body = { notification: notificationId, recipients: recipients };
var res = await fetch('/API/plugin/notifier/testNotification', {
method:'POST', headers:{'Content-Type':'application/json'}, body: JSON.stringify(body)
});
var json = await res.json();
if(!res.ok || (json && (json.error || (json.errors && json.errors.length)))){
var errMsg = (json && (json.error || (json.errors && json.errors[0] && (json.errors[0].error||json.errors[0])))) || ('HTTP '+res.status);
alert('Send failed: '+(errMsg.message||errMsg));
return;
}
if(json && json.status === 'partial'){
(_joe.toast && _joe.toast('Test partially sent')) || alert('Test partially sent');
return;
}
(_joe.toast && _joe.toast('Test sent')) || alert('Test sent');
}catch(e){ alert('Failed: '+e); }
}
},
// Curated summary for agents
summary:{
description:'Automated outbound message configuration (email/SMS/system).',
purpose:'Defines templates and routing for notifications triggered by events. Use to send emails or system notices with dynamic content.',
labelField:'name',
defaultSort:{ field:'joeUpdated', dir:'desc' },
searchableFields:['name','info','notification_code','subject','to','from','_id'],
allowedSorts:['joeUpdated','created','name','notification_code'],
relationships:{
outbound:[
{ field:'dataset', targetSchema:'<schemaName>', cardinality:'one' }
],
inbound:{ graphRef:'server/relationships.graph.json' }
},
joeManagedFields:['created','joeUpdated'],
fields:[
{ name:'_id', type:'string', required:true },
{ name:'itemtype', type:'string', required:true, const:'notification' },
{ name:'name', type:'string', required:true },
{ name:'info', type:'string' },
{ name:'notification_type', type:'string', enumValues:['email'] },
{ name:'notification_code', type:'string' },
{ name:'from', type:'string' },
{ name:'to', type:'string' },
{ name:'dataset', type:'string' },
{ name:'subject', type:'string' },
{ name:'content', type:'string' },
{ name:'text', type:'string' },
{ name:'joeUpdated', type:'string', format:'date-time', required:true },
{ name:'created', type:'string', format:'date-time', required:true }
]
},
fields:[
'name',
'info',
{name:'notification_type',type:'select',values:['email'],width:'50%'},
{name:'notification_code', display:'Notification Code',comment:'a human readable unique code for this notification',width:'50%'},
'from',
'to',
'dataset',
'subject',
'content',
'text:rendering',
{sidebar_start:'right',collapsed:false},
{section_start:'test',display:'Send Test',collapsed:false},
{name:'test_recipients', display:'Recipient(s)', type:'objectReference', idprop:'_id',
template:function(recipient){
return `<joe-title>${recipient.name}</joe-title>
<joe-subtext>${recipient.fullname||''}</joe-subtext>
<joe-subtext>${recipient.email||recipient._id}</joe-subtext>`},
values:function(note){
try{
if(note && note.dataset){
return _joe.getDataset(note.dataset)||[];
}
}catch(e){}
return _joe.getDataset('user')||[];
},
comment:'Search and add one or more recipients from the configured dataset'
},
{name:'send_test', type:'button', display:'Send Test', icon:'email',
onclick:function(note){
if(!note || !note._id){ return ''; }
return `_joe.schemas.notification.methods.sendTest('${note._id}');`;
}
},
{section_end:'test'},
{sidebar_end:'right'},
'_id','created','itemtype'
],
idprop: '_id',
listView:{
listWindowTitle: 'Notifications',
title: '<joe-title>${name}</joe-title><joe-subtitle>${info}</joe-subtitle>'+
'<joe-subtext>${subject}</joe-subtext>'+
'<joe-subtext style="color:orangered;">${notification_code}</joe-subtext>',
},
menuicon:'<svg xmlns="http://www.w3.org/2000/svg" viewBox="-8 -8 112 112"><path d="M48 13.1c-17.7 0-32 13-32 29.1S30.3 71.3 48 71.3V82.9s32-15.6 32-40.7C80 26.1 65.7 13.1 48 13.1ZM44 24.4h7v9.3L49.6 46.4H46.3L44 33.6Zm8 35H44v-8h8Z"/></svg>'
};
module.exports = notification;