Merge branch 'main' into database-view-updates
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { ApplicationListRow, ApplicationRow, CommentRow } from "@app/shared/types/application";
|
||||
import pool from "../db";
|
||||
import { error } from "console";
|
||||
|
||||
export async function createApplication(memberID: number, appVersion: number, app: string) {
|
||||
const sql = `INSERT INTO applications (member_id, app_version, app_data) VALUES (?, ?, ?);`;
|
||||
@@ -12,12 +13,13 @@ export async function getMemberApplication(memberID: number): Promise<Applicatio
|
||||
member.name AS member_name
|
||||
FROM applications AS app
|
||||
INNER JOIN members AS member ON member.id = app.member_id
|
||||
WHERE app.member_id = ?;`;
|
||||
WHERE app.member_id = ? ORDER BY submitted_at DESC LIMIT 1;`;
|
||||
|
||||
let app: ApplicationRow[] = await pool.query(sql, [memberID]);
|
||||
return app[0];
|
||||
}
|
||||
|
||||
|
||||
export async function getApplicationByID(appID: number): Promise<ApplicationRow> {
|
||||
const sql =
|
||||
`SELECT app.*,
|
||||
@@ -44,7 +46,20 @@ export async function getApplicationList(): Promise<ApplicationListRow[]> {
|
||||
return rows;
|
||||
}
|
||||
|
||||
export async function approveApplication(id) {
|
||||
export async function getAllMemberApplications(memberID: number): Promise<ApplicationListRow[]> {
|
||||
const sql = `SELECT
|
||||
app.id,
|
||||
app.member_id,
|
||||
app.submitted_at,
|
||||
app.app_status
|
||||
FROM applications AS app WHERE app.member_id = ? ORDER BY submitted_at DESC;`;
|
||||
|
||||
const rows: ApplicationListRow[] = await pool.query(sql, [memberID])
|
||||
return rows;
|
||||
}
|
||||
|
||||
|
||||
export async function approveApplication(id: number) {
|
||||
const sql = `
|
||||
UPDATE applications
|
||||
SET approved_at = NOW()
|
||||
@@ -57,15 +72,38 @@ export async function approveApplication(id) {
|
||||
return result;
|
||||
}
|
||||
|
||||
export async function getApplicationComments(appID: number): Promise<CommentRow[]> {
|
||||
export async function denyApplication(id: number) {
|
||||
const sql = `
|
||||
UPDATE applications
|
||||
SET denied_at = NOW()
|
||||
WHERE id = ?
|
||||
AND approved_at IS NULL
|
||||
AND denied_at IS NULL
|
||||
`;
|
||||
|
||||
const result = await pool.execute(sql, id);
|
||||
|
||||
if (result.affectedRows == 1) {
|
||||
return
|
||||
} else {
|
||||
throw new Error(`"Something went wrong denying application with ID ${id}`);
|
||||
}
|
||||
}
|
||||
|
||||
export async function getApplicationComments(appID: number, admin: boolean = false): Promise<CommentRow[]> {
|
||||
const excludeAdmin = ' AND app.admin_only = false';
|
||||
|
||||
const whereClause = `WHERE app.application_id = ?${!admin ? excludeAdmin : ''}`;
|
||||
|
||||
return await pool.query(`SELECT app.id AS comment_id,
|
||||
app.post_content,
|
||||
app.poster_id,
|
||||
app.post_time,
|
||||
app.last_modified,
|
||||
app.admin_only,
|
||||
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 = ?;`,
|
||||
${whereClause}`,
|
||||
[appID]);
|
||||
}
|
||||
98
api/src/services/loaService.ts
Normal file
98
api/src/services/loaService.ts
Normal file
@@ -0,0 +1,98 @@
|
||||
import { toDateTime } from "@app/shared/utils/time";
|
||||
import pool from "../db";
|
||||
import { LOARequest, LOAType } from '@app/shared/types/loa'
|
||||
|
||||
export async function getLoaTypes(): Promise<LOAType[]> {
|
||||
return await pool.query('SELECT * FROM leave_of_absences_types;');
|
||||
}
|
||||
|
||||
export async function getAllLOA(page = 1, pageSize = 20): Promise<LOARequest[]> {
|
||||
const offset = (page - 1) * pageSize;
|
||||
|
||||
const sql = `
|
||||
SELECT loa.*, members.name, t.name AS type_name
|
||||
FROM leave_of_absences AS loa
|
||||
LEFT JOIN members ON loa.member_id = members.id
|
||||
LEFT JOIN leave_of_absences_types AS t ON loa.type_id = t.id
|
||||
ORDER BY
|
||||
CASE
|
||||
WHEN loa.closed IS NULL
|
||||
AND NOW() > COALESCE(loa.extended_till, loa.end_date) THEN 1
|
||||
WHEN loa.closed IS NULL
|
||||
AND NOW() BETWEEN loa.start_date AND COALESCE(loa.extended_till, loa.end_date) THEN 2
|
||||
WHEN loa.closed IS NULL AND NOW() < loa.start_date THEN 3
|
||||
WHEN loa.closed IS NOT NULL THEN 4
|
||||
END,
|
||||
loa.start_date DESC
|
||||
LIMIT ? OFFSET ?;
|
||||
`;
|
||||
|
||||
let res: LOARequest[] = await pool.query(sql, [pageSize, offset]) as LOARequest[];
|
||||
return res;
|
||||
}
|
||||
|
||||
export async function getUserLOA(userId: number): Promise<LOARequest[]> {
|
||||
const result: LOARequest[] = await pool.query(`
|
||||
SELECT loa.*, members.name, t.name AS type_name
|
||||
FROM leave_of_absences AS loa
|
||||
LEFT JOIN members ON loa.member_id = members.id
|
||||
LEFT JOIN leave_of_absences_types AS t ON loa.type_id = t.id
|
||||
WHERE member_id = ?
|
||||
ORDER BY
|
||||
CASE
|
||||
WHEN loa.closed IS NULL
|
||||
AND NOW() > COALESCE(loa.extended_till, loa.end_date) THEN 1
|
||||
WHEN loa.closed IS NULL
|
||||
AND NOW() BETWEEN loa.start_date AND COALESCE(loa.extended_till, loa.end_date) THEN 2
|
||||
WHEN loa.closed IS NULL AND NOW() < loa.start_date THEN 3
|
||||
WHEN loa.closed IS NOT NULL THEN 4
|
||||
END,
|
||||
loa.start_date DESC
|
||||
`, [userId])
|
||||
return result;
|
||||
}
|
||||
|
||||
export async function getUserActiveLOA(userId: number): Promise<LOARequest[]> {
|
||||
const sql = `SELECT *
|
||||
FROM leave_of_absences
|
||||
WHERE member_id = ?
|
||||
AND closed IS NULL
|
||||
AND UTC_TIMESTAMP() BETWEEN start_date AND end_date;`
|
||||
const LOAData = await pool.query(sql, [userId]);
|
||||
return LOAData;
|
||||
}
|
||||
|
||||
export async function createNewLOA(data: LOARequest) {
|
||||
const sql = `INSERT INTO leave_of_absences
|
||||
(member_id, filed_date, start_date, end_date, type_id, reason)
|
||||
VALUES (?, ?, ?, ?, ?, ?)`;
|
||||
await pool.query(sql, [data.member_id, toDateTime(data.filed_date), toDateTime(data.start_date), toDateTime(data.end_date), data.type_id, data.reason])
|
||||
return;
|
||||
}
|
||||
|
||||
export async function closeLOA(id: number, closer: number) {
|
||||
const sql = `UPDATE leave_of_absences
|
||||
SET closed = 1,
|
||||
closed_by = ?
|
||||
WHERE leave_of_absences.id = ?`;
|
||||
let out = await pool.query(sql, [closer, id]);
|
||||
console.log(out);
|
||||
return out;
|
||||
}
|
||||
|
||||
export async function getLOAbyID(id: number): Promise<LOARequest> {
|
||||
let res = await pool.query(`SELECT * FROM leave_of_absences WHERE id = ?`, [id]);
|
||||
console.log(res);
|
||||
if (res.length != 1)
|
||||
throw new Error(`LOA with id ${id} not found`);
|
||||
return res[0];
|
||||
}
|
||||
|
||||
export async function setLOAExtension(id: number, extendTo: Date) {
|
||||
let res = await pool.query(`UPDATE leave_of_absences
|
||||
SET extended_till = ?
|
||||
WHERE leave_of_absences.id = ? `, [toDateTime(extendTo), id]);
|
||||
if (res.affectedRows != 1)
|
||||
throw new Error(`Could not extend LOA`);
|
||||
return res[0];
|
||||
}
|
||||
Reference in New Issue
Block a user