Began implementation for getting promotion history
This commit is contained in:
@@ -1,4 +1,8 @@
|
||||
import { BatchPromotion, BatchPromotionMember } from "@app/shared/schemas/promotionSchema";
|
||||
import { PromotionSummary } from "@app/shared/types/rank"
|
||||
import pool from "../db";
|
||||
import { PagedData } from "@app/shared/types/pagination";
|
||||
import { toDateTime } from "@app/shared/utils/time";
|
||||
|
||||
export async function getAllRanks() {
|
||||
const rows = await pool.query(
|
||||
@@ -30,3 +34,78 @@ export async function insertMemberRank(member_id: number, rank_id: number, date?
|
||||
|
||||
await pool.query(sql, params);
|
||||
}
|
||||
|
||||
|
||||
export async function batchInsertMemberRank(promos: BatchPromotionMember[], author: number) {
|
||||
try {
|
||||
var con = await pool.getConnection();
|
||||
console.log(promos);
|
||||
promos.forEach(p => {
|
||||
con.query(`CALL sp_update_member_rank(?, ?, ?, ?, ?, ?)`, [p.member_id, p.rank_id, author, author, "Rank Change", toDateTime(new Date(p.start_date))])
|
||||
});
|
||||
|
||||
con.commit();
|
||||
return
|
||||
} catch (error) {
|
||||
throw error; //pass it up
|
||||
} finally {
|
||||
con.release();
|
||||
}
|
||||
}
|
||||
|
||||
export async function getPromotionHistorySummary(page: number = 1, pageSize: number = 15): Promise<PagedData<PromotionSummary>> {
|
||||
|
||||
const offset = (page - 1) * pageSize;
|
||||
|
||||
let sql = `SELECT
|
||||
DATE(start_date) AS entry_day
|
||||
FROM
|
||||
members_ranks
|
||||
GROUP BY
|
||||
entry_day
|
||||
ORDER BY
|
||||
entry_day DESC
|
||||
LIMIT ? OFFSET ?;`
|
||||
|
||||
let promoList: PromotionSummary[] = await pool.query(sql, [pageSize, offset]) as PromotionSummary[];
|
||||
|
||||
let loaCount = Number((await pool.query(`SELECT
|
||||
COUNT(*) AS total_grouped_days_count
|
||||
FROM
|
||||
(
|
||||
SELECT DISTINCT DATE(start_date)
|
||||
FROM members_ranks
|
||||
) AS grouped_days;`))[0]);
|
||||
|
||||
console.log(loaCount);
|
||||
let pageCount = loaCount / pageSize;
|
||||
|
||||
let output: PagedData<PromotionSummary> = { data: promoList, pagination: { page: page, pageSize: pageSize, total: loaCount, totalPages: pageCount } }
|
||||
return output;
|
||||
}
|
||||
|
||||
export async function getPromotionsOnDay(day: Date): Promise<BatchPromotion[]> {
|
||||
|
||||
// Convert the Date object to a 'YYYY-MM-DD' string for the SQL filter
|
||||
// This assumes pool.query is used with a database that accepts this format for comparison.
|
||||
const dayString = day.toISOString().split('T')[0];
|
||||
|
||||
// SQL query to fetch all records from members_unit for the specified day
|
||||
let sql = `
|
||||
SELECT
|
||||
member_id,
|
||||
unit_id AS rank_id, -- Using unit_id as a proxy for rank_id based on the data structure
|
||||
start_date
|
||||
FROM
|
||||
members_unit
|
||||
WHERE
|
||||
DATE(start_date) = ?
|
||||
ORDER BY
|
||||
start_date ASC;
|
||||
`;
|
||||
|
||||
|
||||
let batchPromotion = await pool.query(sql, [dayString]) as BatchPromotion[];
|
||||
|
||||
return batchPromotion;
|
||||
}
|
||||
Reference in New Issue
Block a user