implemented integrations and events system
This commit is contained in:
@@ -2,10 +2,19 @@ import { ApplicationListRow, ApplicationRow, CommentRow } from "@app/shared/type
|
||||
import pool from "../../db";
|
||||
import { error } from "console";
|
||||
|
||||
export async function createApplication(memberID: number, appVersion: number, app: string) {
|
||||
/**
|
||||
* Create an application in the db
|
||||
* @param memberID
|
||||
* @param appVersion
|
||||
* @param app
|
||||
* @returns ID of the created application
|
||||
*/
|
||||
export async function createApplication(memberID: number, appVersion: number, app: string): Promise<number> {
|
||||
const sql = `INSERT INTO applications (member_id, app_version, app_data) VALUES (?, ?, ?);`;
|
||||
const params = [memberID, appVersion, JSON.stringify(app)]
|
||||
return await pool.query(sql, params);
|
||||
|
||||
let result = await pool.query(sql, params);
|
||||
return Number(result.insertId);
|
||||
}
|
||||
|
||||
export async function getMemberApplication(memberID: number): Promise<ApplicationRow> {
|
||||
|
||||
56
api/src/services/events/eventBus.ts
Normal file
56
api/src/services/events/eventBus.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
import { randomUUID } from "crypto";
|
||||
import { logger } from "../logging/logger";
|
||||
|
||||
interface Event {
|
||||
id: string
|
||||
type: string
|
||||
occurredAt: string
|
||||
payload?: Record<string, any>
|
||||
}
|
||||
|
||||
type EventHandler = (event: Event) => void | Promise<void>;
|
||||
|
||||
class EventBus {
|
||||
private handlers: Map<string, EventHandler[]> = new Map();
|
||||
|
||||
/**
|
||||
* Register event listener
|
||||
* @param type
|
||||
* @param handler
|
||||
*/
|
||||
on(type: string, handler: EventHandler) {
|
||||
const handlers = this.handlers.get(type) ?? [];
|
||||
handlers.push(handler);
|
||||
this.handlers.set(type, handlers);
|
||||
}
|
||||
|
||||
/**
|
||||
* Emit event of given type
|
||||
* @param type
|
||||
* @param payload
|
||||
*/
|
||||
async emit(type: string, payload?: Record<string, any>) {
|
||||
const event: Event = {
|
||||
id: randomUUID(),
|
||||
type,
|
||||
occurredAt: new Date().toISOString(),
|
||||
payload
|
||||
}
|
||||
|
||||
const handlers = this.handlers.get(type) ?? []
|
||||
|
||||
for (const h of handlers) {
|
||||
try {
|
||||
await h(event)
|
||||
} catch (error) {
|
||||
logger.error('app', 'Event handler failed', {
|
||||
type: event.type,
|
||||
id: event.id,
|
||||
error: error instanceof Error ? error.message : String(error),
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const bus = new EventBus();
|
||||
32
api/src/services/integrations/discord.ts
Normal file
32
api/src/services/integrations/discord.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import { bus } from "../events/eventBus";
|
||||
|
||||
export function initializeDiscordIntegrations() {
|
||||
bus.on('application.create', async (event) => {
|
||||
let applicantName = event.payload.member_discord_id || event.payload.member_name;
|
||||
if (event.payload.member_discord_id) {
|
||||
applicantName = `<@${event.payload.member_discord_id}>`;
|
||||
}
|
||||
const link = `${process.env.CLIENT_URL}/administration/applications/${event.payload.application}`;
|
||||
|
||||
const embed = {
|
||||
title: "Application Posted",
|
||||
description: `[View Application](${link})`,
|
||||
color: 0x00ff00, // optional: green color
|
||||
timestamp: new Date().toISOString(), // <-- Discord expects ISO8601
|
||||
fields: [
|
||||
{
|
||||
name: "Submitted By",
|
||||
value: applicantName,
|
||||
inline: false,
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
// send to Discord webhook
|
||||
await fetch(process.env.DISCORD_APPLICATIONS_WEBHOOK!, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ embeds: [embed] }),
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user