Files
milsim-site-v4/api/src/services/applicationService.ts

71 lines
2.2 KiB
TypeScript

import { ApplicationListRow, ApplicationRow, CommentRow } from "@app/shared/types/application";
import pool from "../db";
export async function createApplication(memberID: number, appVersion: number, app: string) {
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);
}
export async function getMemberApplication(memberID: number): Promise<ApplicationRow> {
const sql = `SELECT app.*,
member.name AS member_name
FROM applications AS app
INNER JOIN members AS member ON member.id = app.member_id
WHERE app.member_id = ?;`;
let app: ApplicationRow[] = await pool.query(sql, [memberID]);
return app[0];
}
export async function getApplicationByID(appID: number): Promise<ApplicationRow> {
const sql =
`SELECT app.*,
member.name AS member_name
FROM applications AS app
INNER JOIN members AS member ON member.id = app.member_id
WHERE app.id = ?;`;
let app: ApplicationRow[] = await pool.query(sql, [appID]);
return app[0];
}
export async function getApplicationList(): Promise<ApplicationListRow[]> {
const sql = `SELECT
member.name AS member_name,
app.id,
app.member_id,
app.submitted_at,
app.app_status
FROM applications AS app
LEFT JOIN members AS member
ON member.id = app.member_id;`
const rows: ApplicationListRow[] = await pool.query(sql);
return rows;
}
export async function approveApplication(id) {
const sql = `
UPDATE applications
SET approved_at = NOW()
WHERE id = ?
AND approved_at IS NULL
AND denied_at IS NULL
`;
const result = await pool.execute(sql, id);
return result;
}
export async function getApplicationComments(appID: number): Promise<CommentRow[]> {
return await pool.query(`SELECT app.id AS comment_id,
app.post_content,
app.poster_id,
app.post_time,
app.last_modified,
member.name AS poster_name
FROM application_comments AS app
INNER JOIN members AS member ON member.id = app.poster_id
WHERE app.application_id = ?;`,
[appID]);
}