web/website/node/political-job-notifications.mjs

7 lines
1.9 KiB
JavaScript

import {ensureStaffMailbox} from './internal-mail-staff.mjs';
import {automatedMailTemplate} from './automated-mail-templates.mjs';
export async function drainPoliticalJobNotifications(central,{broker,eligible,sender=ensureStaffMailbox}={}){
const {results}=await central.prepare("SELECT * FROM political_job_outbox WHERE state='queued' ORDER BY created_at,id LIMIT 10").all();if(!results.length)return{sent:0,failed:0};const mailbox=await sender(broker);let sent=0,failed=0;
for(const event of results)try{const person=await central.prepare("SELECT id,username,role FROM site_accounts WHERE id=? AND password_hash NOT LIKE '!%'").bind(event.user_id).first();if(!person||!await eligible(central,person)){await central.prepare("UPDATE political_job_outbox SET state='cancelled' WHERE id=?").bind(event.id).run();continue;}const target=await broker({action:'internal_ensure',userId:person.id,eligible:true}),subject='Political application · '+(event.decision==='accepted'?'Accepted':event.decision==='declined'?'Declined':'Position filled'),message=event.decision==='accepted'?'Your application was accepted. Your political appointment deed is recorded and ends with its issuing office term.':event.decision==='declined'?'Your political application was declined.':'The political vacancy has been filled. Your application is closed.';const mail=automatedMailTemplate('jarvis-application',{recipientName:person.username,subject,message,url:'https://cp.dunerealms.com/political-job?id='+encodeURIComponent(event.listing_id)});const result=await broker({action:'internal_send',userId:mailbox.userId,requestId:event.id,to:[target.address],cc:[],bcc:[],senderName:'J.A.R.V.I.S. · Political appointments',...mail,subject});if(!result.accepted)throw Error('Mail unconfirmed');await central.prepare("UPDATE political_job_outbox SET state='sent' WHERE id=? AND state='queued'").bind(event.id).run();sent++;}catch{failed++;}
return{sent,failed};
}