forked from ryntab/Directus-Mailer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
1 lines (1 loc) · 1.09 KB
/
index.js
1
"use strict";module.exports=(t,{services:e,env:a})=>{const{AssetsService:n,MailService:s}=e;t.post("/",(async(t,e,l)=>{const c=new s({accountability:t.accountability,schema:l}),i=new n({accountability:t.accountability,schema:t.schema}),{body:u}=t;(async n=>{if(!(null!=a.EMAIL_ALLOW_GUEST_SEND&&a.EMAIL_ALLOW_GUEST_SEND||null!=t.accountability.user&&null!=t.accountability.role))return e.status(400).send({message:"User not authorized, enable guest sending or include a token"});await c.send(n).then((()=>{e.send("sent")}))})(await(async t=>{let e=new Object;if(e.to=t.to,e.template=t.template,e.subject=t.subject,e.attachments=t.attachments,e.list=t.list,null==e.attachments&&(e.attachments=new Array),null!=t.files&&t.files.length>0){let a=await(async t=>{let e=new Array,a=new Array;return a=await Promise.allSettled(t.map((function(t){return i.getAsset(t,{})}))),a.filter((t=>"fulfilled"===t.status)).forEach((t=>{const{stream:a,file:n}=t.value;let s=new Object;s.contentType=n.type,s.filename=n.filename_download,s.content=a,e.push(s)})),e})(t.files);e.attachments=e.attachments.concat(a)}return e})(u))}))};