forked from unimatrix/web
36 lines
5 KiB
JavaScript
36 lines
5 KiB
JavaScript
import {randomUUID} from 'node:crypto';
|
|
import {JarvisError} from './jarvis-listings.mjs';
|
|
export const JARVIS_CLAN_REQUEST_SCHEMA=[
|
|
`CREATE TABLE IF NOT EXISTS site_jarvis_clan_applications(clan_id CHAR(36) NOT NULL,user_id CHAR(36) NOT NULL,listing_id CHAR(36) NOT NULL,message TEXT NOT NULL,status VARCHAR(16) NOT NULL,created_at BIGINT NOT NULL,updated_at BIGINT NOT NULL,PRIMARY KEY(clan_id,user_id))`,
|
|
`CREATE TABLE IF NOT EXISTS site_jarvis_application_events(id CHAR(36) PRIMARY KEY,listing_id CHAR(36) NOT NULL,clan_id CHAR(36) NOT NULL,user_id CHAR(36) NOT NULL,decision VARCHAR(16) NOT NULL,listing_closed INT NOT NULL DEFAULT 0,state VARCHAR(16) NOT NULL,attempts INT NOT NULL DEFAULT 0,next_attempt BIGINT NOT NULL,created_at BIGINT NOT NULL)`
|
|
];
|
|
// These statements are appended to the EXISTING clan request/review transaction.
|
|
// Membership, capacity and role checks stay in createClans, never in a second engine.
|
|
export function createJarvisClanRequests(central,store){return {
|
|
async request({user,record,body,now}){
|
|
if(!body.jarvisListingId)return [central.prepare('DELETE FROM site_jarvis_clan_applications WHERE clan_id=? AND user_id=?').bind(record.id,user.id)];
|
|
if(!store)throw new JarvisError('The recruitment register is unavailable.',503);
|
|
if(!/^[a-f0-9]{8}(?:-[a-f0-9]{4}){3}-[a-f0-9]{12}$/i.test(body.jarvisListingId))throw new JarvisError('Choose a current recruitment listing.');
|
|
const listing=await store.first("SELECT id,organisation_kind,organisation_id,state FROM jarvis_listings WHERE id=?",[body.jarvisListingId]);
|
|
if(!listing||listing.organisation_kind!=='clan'||listing.organisation_id!==record.id||listing.state!=='open')throw new JarvisError('This recruitment listing is no longer available.',409);
|
|
const message=body.applicationMessage??'';if(typeof message!=='string'||message.length>1000||/[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]/.test(message))throw new JarvisError('Application messages can contain up to 1,000 characters.');
|
|
return [central.prepare("INSERT INTO site_jarvis_clan_applications(clan_id,user_id,listing_id,message,status,created_at,updated_at) VALUES(?,?,?,?,'pending',?,?) ON CONFLICT(clan_id,user_id) DO UPDATE SET listing_id=excluded.listing_id,message=excluded.message,status='pending',created_at=excluded.created_at,updated_at=excluded.updated_at").bind(record.id,user.id,listing.id,message.trim(),now,now)];
|
|
},
|
|
async review({record,target,decision,now}){
|
|
const link=await central.prepare("SELECT listing_id FROM site_jarvis_clan_applications WHERE clan_id=? AND user_id=? AND status='pending'").bind(record.id,target.user_id).first();if(!link)return [];
|
|
const statements=[],event=(userId,outcome)=>central.prepare("INSERT INTO site_jarvis_application_events(id,listing_id,clan_id,user_id,decision,state,next_attempt,created_at) VALUES(?,?,?,?,?,'queued',?,?)").bind(randomUUID(),link.listing_id,record.id,userId,outcome,now,now);
|
|
statements.push(central.prepare("UPDATE site_jarvis_clan_applications SET status=?,updated_at=? WHERE clan_id=? AND user_id=? AND status='pending'").bind(decision==='accept'?'accepted':'declined',now,record.id,target.user_id),event(target.user_id,decision==='accept'?'accepted':'declined'));
|
|
if(decision==='accept'){
|
|
const {results:others}=await central.prepare("SELECT j.user_id FROM site_jarvis_clan_applications j JOIN site_clan_requests r ON r.clan_id=j.clan_id AND r.user_id=j.user_id WHERE j.listing_id=? AND j.user_id<>? AND j.status='pending' AND r.status='pending'").bind(link.listing_id,target.user_id).all();
|
|
for(const other of others){statements.push(central.prepare("UPDATE site_clan_requests SET status='declined',updated_at=? WHERE clan_id=? AND user_id=? AND status='pending'").bind(now,record.id,other.user_id),central.prepare("UPDATE site_jarvis_clan_applications SET status='filled',updated_at=? WHERE clan_id=? AND user_id=? AND status='pending'").bind(now,record.id,other.user_id),event(other.user_id,'filled'));}
|
|
}
|
|
return statements;
|
|
},
|
|
withdraw({user,record,now}){return [central.prepare("UPDATE site_jarvis_clan_applications SET status='withdrawn',updated_at=? WHERE clan_id=? AND user_id=? AND status='pending'").bind(now,record.id,user.id)];}
|
|
};}
|
|
export async function closeFilledJarvisListings(central,store,{clock=Date.now}={}){
|
|
const {results:events}=await central.prepare("SELECT DISTINCT listing_id FROM site_jarvis_application_events WHERE decision='accepted' AND listing_closed=0 ORDER BY listing_id LIMIT 100").all();
|
|
for(const event of events)await store.transaction(async tx=>{await tx.first('SELECT id FROM jarvis_listings WHERE id=? FOR UPDATE',[event.listing_id]);await tx.query("UPDATE jarvis_listings SET state='filled',revision=revision+1,updated_at=? WHERE id=? AND state='open'",[clock(),event.listing_id]);await tx.query("UPDATE jarvis_promotion_jobs SET state='cancelled' WHERE listing_id=? AND state='queued'",[event.listing_id]);});
|
|
for(const event of events)await central.prepare("UPDATE site_jarvis_application_events SET listing_closed=1 WHERE listing_id=? AND decision='accepted'").bind(event.listing_id).run();
|
|
return events.length;
|
|
}
|

Staff