Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 46f8962742 | |||
| f8b1811b74 | |||
| 7532436b9a | |||
| 0deb2ac316 | |||
| e672159c51 | |||
| cfbab75132 | |||
| b167135038 |
@@ -0,0 +1,45 @@
|
||||
'use strict';
|
||||
|
||||
var dbm;
|
||||
var type;
|
||||
var seed;
|
||||
var fs = require('fs');
|
||||
var path = require('path');
|
||||
var Promise;
|
||||
|
||||
exports.setup = function(options, seedLink) {
|
||||
dbm = options.dbmigrate;
|
||||
type = dbm.dataType;
|
||||
seed = seedLink;
|
||||
Promise = options.Promise;
|
||||
};
|
||||
|
||||
exports.up = function(db) {
|
||||
var filePath = path.join(__dirname, 'sqls', '20260330120000-qualification-sync-up.sql');
|
||||
return new Promise(function(resolve, reject) {
|
||||
fs.readFile(filePath, { encoding: 'utf-8' }, function(err, data) {
|
||||
if (err) return reject(err);
|
||||
console.log('received data: ' + data);
|
||||
resolve(data);
|
||||
});
|
||||
}).then(function(data) {
|
||||
return db.runSql(data);
|
||||
});
|
||||
};
|
||||
|
||||
exports.down = function(db) {
|
||||
var filePath = path.join(__dirname, 'sqls', '20260330120000-qualification-sync-down.sql');
|
||||
return new Promise(function(resolve, reject) {
|
||||
fs.readFile(filePath, { encoding: 'utf-8' }, function(err, data) {
|
||||
if (err) return reject(err);
|
||||
console.log('received data: ' + data);
|
||||
resolve(data);
|
||||
});
|
||||
}).then(function(data) {
|
||||
return db.runSql(data);
|
||||
});
|
||||
};
|
||||
|
||||
exports._meta = {
|
||||
version: 1,
|
||||
};
|
||||
@@ -0,0 +1,45 @@
|
||||
'use strict';
|
||||
|
||||
var dbm;
|
||||
var type;
|
||||
var seed;
|
||||
var fs = require('fs');
|
||||
var path = require('path');
|
||||
var Promise;
|
||||
|
||||
exports.setup = function(options, seedLink) {
|
||||
dbm = options.dbmigrate;
|
||||
type = dbm.dataType;
|
||||
seed = seedLink;
|
||||
Promise = options.Promise;
|
||||
};
|
||||
|
||||
exports.up = function(db) {
|
||||
var filePath = path.join(__dirname, 'sqls', '20260522120000-course-event-challenge-up.sql');
|
||||
return new Promise(function(resolve, reject) {
|
||||
fs.readFile(filePath, { encoding: 'utf-8' }, function(err, data) {
|
||||
if (err) return reject(err);
|
||||
console.log('received data: ' + data);
|
||||
resolve(data);
|
||||
});
|
||||
}).then(function(data) {
|
||||
return db.runSql(data);
|
||||
});
|
||||
};
|
||||
|
||||
exports.down = function(db) {
|
||||
var filePath = path.join(__dirname, 'sqls', '20260522120000-course-event-challenge-down.sql');
|
||||
return new Promise(function(resolve, reject) {
|
||||
fs.readFile(filePath, { encoding: 'utf-8' }, function(err, data) {
|
||||
if (err) return reject(err);
|
||||
console.log('received data: ' + data);
|
||||
resolve(data);
|
||||
});
|
||||
}).then(function(data) {
|
||||
return db.runSql(data);
|
||||
});
|
||||
};
|
||||
|
||||
exports._meta = {
|
||||
version: 1,
|
||||
};
|
||||
@@ -0,0 +1,18 @@
|
||||
ALTER TABLE members_qualifications
|
||||
DROP FOREIGN KEY fk_members_qualifications_course_id,
|
||||
DROP FOREIGN KEY fk_members_qualifications_awarded_by,
|
||||
DROP FOREIGN KEY fk_members_qualifications_revoked_by,
|
||||
DROP FOREIGN KEY fk_members_qualifications_source_event,
|
||||
DROP KEY uq_members_qualifications_member_course,
|
||||
DROP KEY idx_members_qualifications_active,
|
||||
DROP KEY idx_members_qualifications_source_event,
|
||||
DROP COLUMN course_id,
|
||||
DROP COLUMN active,
|
||||
DROP COLUMN awarded_by_id,
|
||||
DROP COLUMN revoked_by_id,
|
||||
DROP COLUMN revoked_reason,
|
||||
DROP COLUMN revoked_at,
|
||||
DROP COLUMN source_course_event_id,
|
||||
ADD COLUMN qualification_id INT(11) DEFAULT 0,
|
||||
ADD KEY fk_members_qualifications_qualifications_id (qualification_id),
|
||||
ADD CONSTRAINT fk_members_qualifications_qualifications_id FOREIGN KEY (qualification_id) REFERENCES qualifications(id) ON UPDATE CASCADE;
|
||||
@@ -0,0 +1,18 @@
|
||||
ALTER TABLE members_qualifications
|
||||
DROP FOREIGN KEY fk_members_qualifications_qualifications_id,
|
||||
DROP KEY fk_members_qualifications_qualifications_id,
|
||||
DROP COLUMN qualification_id,
|
||||
ADD COLUMN course_id INT(11) NOT NULL AFTER member_id,
|
||||
ADD COLUMN active TINYINT(1) NOT NULL DEFAULT 1,
|
||||
ADD COLUMN awarded_by_id INT(11) DEFAULT NULL,
|
||||
ADD COLUMN revoked_by_id INT(11) DEFAULT NULL,
|
||||
ADD COLUMN revoked_reason TEXT DEFAULT NULL,
|
||||
ADD COLUMN revoked_at DATETIME DEFAULT NULL,
|
||||
ADD COLUMN source_course_event_id INT(11) DEFAULT NULL,
|
||||
ADD UNIQUE KEY uq_members_qualifications_member_course (member_id, course_id),
|
||||
ADD KEY idx_members_qualifications_active (member_id, course_id, active),
|
||||
ADD KEY idx_members_qualifications_source_event (source_course_event_id),
|
||||
ADD CONSTRAINT fk_members_qualifications_course_id FOREIGN KEY (course_id) REFERENCES courses(id) ON UPDATE CASCADE,
|
||||
ADD CONSTRAINT fk_members_qualifications_awarded_by FOREIGN KEY (awarded_by_id) REFERENCES members(id) ON UPDATE CASCADE,
|
||||
ADD CONSTRAINT fk_members_qualifications_revoked_by FOREIGN KEY (revoked_by_id) REFERENCES members(id) ON UPDATE CASCADE,
|
||||
ADD CONSTRAINT fk_members_qualifications_source_event FOREIGN KEY (source_course_event_id) REFERENCES course_events(id) ON UPDATE CASCADE;
|
||||
@@ -0,0 +1,2 @@
|
||||
ALTER TABLE course_events
|
||||
DROP COLUMN is_challenge;
|
||||
@@ -0,0 +1,2 @@
|
||||
ALTER TABLE course_events
|
||||
ADD COLUMN is_challenge TINYINT(1) NOT NULL DEFAULT 0 AFTER hasQual;
|
||||
+3
-1
@@ -11,7 +11,9 @@
|
||||
"dev": "tsc && tsc-alias && node ./built/api/src/index.js",
|
||||
"prod": "tsc && tsc-alias && node ./built/api/src/index.js",
|
||||
"build": "tsc && tsc-alias",
|
||||
"seed": "node ./scripts/seed.js"
|
||||
"seed": "node ./scripts/seed.js",
|
||||
"backfill:qualifications:dry": "node ./scripts/backfillQualifications.js --dry-run",
|
||||
"backfill:qualifications": "node ./scripts/backfillQualifications.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"@rsol/hashmig": "^1.0.7",
|
||||
|
||||
@@ -0,0 +1,325 @@
|
||||
const dotenv = require("dotenv");
|
||||
const path = require("path");
|
||||
const mariadb = require("mariadb");
|
||||
|
||||
dotenv.config({ path: path.resolve(process.cwd(), ".env") });
|
||||
|
||||
const {
|
||||
DB_HOST,
|
||||
DB_PORT,
|
||||
DB_USERNAME,
|
||||
DB_PASSWORD,
|
||||
DB_DATABASE,
|
||||
DB_CONNECT_TIMEOUT_MS,
|
||||
DB_SOCKET_TIMEOUT_MS,
|
||||
} = process.env;
|
||||
|
||||
function parseTimeout(value, fallback) {
|
||||
if (value === undefined || value === null || value === "") {
|
||||
return fallback;
|
||||
}
|
||||
|
||||
const parsed = Number(value);
|
||||
if (!Number.isFinite(parsed) || parsed <= 0) {
|
||||
throw new Error(`Invalid timeout value: ${value}`);
|
||||
}
|
||||
|
||||
return parsed;
|
||||
}
|
||||
|
||||
function parseArgs(argv) {
|
||||
const args = {
|
||||
dryRun: false,
|
||||
actorId: null,
|
||||
courseId: null,
|
||||
memberId: null,
|
||||
};
|
||||
|
||||
for (let i = 2; i < argv.length; i++) {
|
||||
const token = argv[i];
|
||||
if (token === "--dry-run") {
|
||||
args.dryRun = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (token.startsWith("--actor=")) {
|
||||
args.actorId = Number(token.split("=")[1]);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (token.startsWith("--course=")) {
|
||||
args.courseId = Number(token.split("=")[1]);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (token.startsWith("--member=")) {
|
||||
args.memberId = Number(token.split("=")[1]);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (args.actorId !== null && Number.isNaN(args.actorId)) {
|
||||
throw new Error("--actor must be a number");
|
||||
}
|
||||
if (args.courseId !== null && Number.isNaN(args.courseId)) {
|
||||
throw new Error("--course must be a number");
|
||||
}
|
||||
if (args.memberId !== null && Number.isNaN(args.memberId)) {
|
||||
throw new Error("--member must be a number");
|
||||
}
|
||||
|
||||
return args;
|
||||
}
|
||||
|
||||
function buildScopeClause(args, tableAlias = "") {
|
||||
const prefix = tableAlias ? `${tableAlias}.` : "";
|
||||
const clauses = [];
|
||||
const params = [];
|
||||
|
||||
if (args.courseId !== null) {
|
||||
clauses.push(`${prefix}course_id = ?`);
|
||||
params.push(args.courseId);
|
||||
}
|
||||
|
||||
if (args.memberId !== null) {
|
||||
clauses.push(`${prefix}member_id = ?`);
|
||||
params.push(args.memberId);
|
||||
}
|
||||
|
||||
if (!clauses.length) {
|
||||
return { sql: "", params };
|
||||
}
|
||||
|
||||
return {
|
||||
sql: ` AND ${clauses.join(" AND ")}`,
|
||||
params,
|
||||
};
|
||||
}
|
||||
|
||||
function key(memberId, courseId) {
|
||||
return `${memberId}:${courseId}`;
|
||||
}
|
||||
|
||||
(async () => {
|
||||
const args = parseArgs(process.argv);
|
||||
|
||||
const conn = await mariadb.createConnection({
|
||||
host: DB_HOST,
|
||||
port: Number(DB_PORT),
|
||||
user: DB_USERNAME,
|
||||
password: DB_PASSWORD,
|
||||
database: DB_DATABASE,
|
||||
multipleStatements: false,
|
||||
connectTimeout: parseTimeout(DB_CONNECT_TIMEOUT_MS, 10000),
|
||||
socketTimeout: parseTimeout(DB_SOCKET_TIMEOUT_MS, 60000),
|
||||
});
|
||||
|
||||
try {
|
||||
const schemaCheck = await conn.query("SHOW COLUMNS FROM members_qualifications LIKE 'course_id';");
|
||||
if (!schemaCheck.length) {
|
||||
throw new Error("members_qualifications.course_id does not exist. Run qualification migration first.");
|
||||
}
|
||||
|
||||
const scoped = buildScopeClause(args);
|
||||
const evidenceScopedClauses = [];
|
||||
const evidenceScopedParams = [];
|
||||
if (args.courseId !== null) {
|
||||
evidenceScopedClauses.push("e.course_id = ?");
|
||||
evidenceScopedParams.push(args.courseId);
|
||||
}
|
||||
if (args.memberId !== null) {
|
||||
evidenceScopedClauses.push("ca.attendee_id = ?");
|
||||
evidenceScopedParams.push(args.memberId);
|
||||
}
|
||||
const evidenceScopedSql = evidenceScopedClauses.length
|
||||
? ` AND ${evidenceScopedClauses.join(" AND ")}`
|
||||
: "";
|
||||
|
||||
const evidenceRows = await conn.query(
|
||||
`SELECT
|
||||
ca.attendee_id AS member_id,
|
||||
e.course_id,
|
||||
e.id AS course_event_id,
|
||||
e.event_date,
|
||||
IFNULL(e.is_challenge, 0) AS is_challenge,
|
||||
IFNULL(e.hasBookwork, 0) AS event_has_bookwork,
|
||||
IFNULL(e.hasQual, 0) AS event_has_qual,
|
||||
ca.passed_bookwork,
|
||||
ca.passed_qual,
|
||||
IFNULL(c.hasBookwork, 0) AS course_has_bookwork,
|
||||
IFNULL(c.hasQual, 0) AS course_has_qual
|
||||
FROM course_events e
|
||||
INNER JOIN courses c ON c.id = e.course_id
|
||||
INNER JOIN course_attendees ca ON ca.course_event_id = e.id
|
||||
WHERE ca.attendee_role_id = 2
|
||||
AND (e.deleted IS NULL OR e.deleted = 0)
|
||||
${evidenceScopedSql}
|
||||
ORDER BY ca.attendee_id ASC, e.course_id ASC, e.event_date DESC, e.id DESC;`,
|
||||
evidenceScopedParams
|
||||
);
|
||||
|
||||
const aggregateByPair = new Map();
|
||||
for (const row of evidenceRows) {
|
||||
const memberId = Number(row.member_id);
|
||||
const courseId = Number(row.course_id);
|
||||
const mapKey = key(memberId, courseId);
|
||||
const bookworkEvidence = Number(row.event_has_bookwork) === 1 && Number(row.passed_bookwork) === 1;
|
||||
const qualEvidence = Number(row.event_has_qual) === 1 && Number(row.passed_qual) === 1;
|
||||
const challengeQualEvidence = Number(row.is_challenge) === 1 && Number(row.event_has_qual) === 1 && Number(row.passed_qual) === 1;
|
||||
|
||||
const existing = aggregateByPair.get(mapKey) || {
|
||||
memberId,
|
||||
courseId,
|
||||
courseHasBookwork: Number(row.course_has_bookwork) === 1,
|
||||
courseHasQual: Number(row.course_has_qual) === 1,
|
||||
hasBookworkPass: false,
|
||||
hasQualPass: false,
|
||||
hasChallengeQualPass: false,
|
||||
latestEvidence: null,
|
||||
};
|
||||
|
||||
existing.hasBookworkPass = existing.hasBookworkPass || bookworkEvidence;
|
||||
existing.hasQualPass = existing.hasQualPass || qualEvidence;
|
||||
existing.hasChallengeQualPass = existing.hasChallengeQualPass || challengeQualEvidence;
|
||||
|
||||
if (bookworkEvidence || qualEvidence || challengeQualEvidence) {
|
||||
const nextLatest = {
|
||||
courseEventId: Number(row.course_event_id),
|
||||
eventDate: row.event_date,
|
||||
};
|
||||
|
||||
if (!existing.latestEvidence) {
|
||||
existing.latestEvidence = nextLatest;
|
||||
} else {
|
||||
const currentDate = new Date(existing.latestEvidence.eventDate).getTime();
|
||||
const nextDate = new Date(nextLatest.eventDate).getTime();
|
||||
if (nextDate > currentDate || (nextDate === currentDate && nextLatest.courseEventId > existing.latestEvidence.courseEventId)) {
|
||||
existing.latestEvidence = nextLatest;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
aggregateByPair.set(mapKey, existing);
|
||||
}
|
||||
|
||||
const latestByPair = new Map();
|
||||
for (const [mapKey, pair] of aggregateByPair.entries()) {
|
||||
const hasBookwork = pair.courseHasBookwork;
|
||||
const hasQual = pair.courseHasQual;
|
||||
|
||||
let qualifies = false;
|
||||
if (hasBookwork && hasQual) {
|
||||
qualifies = (pair.hasBookworkPass && pair.hasQualPass) || pair.hasChallengeQualPass;
|
||||
} else if (hasBookwork && !hasQual) {
|
||||
qualifies = pair.hasBookworkPass;
|
||||
} else if (!hasBookwork && hasQual) {
|
||||
qualifies = pair.hasQualPass;
|
||||
}
|
||||
|
||||
if (qualifies && pair.latestEvidence) {
|
||||
latestByPair.set(mapKey, {
|
||||
memberId: pair.memberId,
|
||||
courseId: pair.courseId,
|
||||
courseEventId: pair.latestEvidence.courseEventId,
|
||||
eventDate: pair.latestEvidence.eventDate,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const currentRows = await conn.query(
|
||||
`SELECT member_id, course_id
|
||||
FROM members_qualifications
|
||||
WHERE active = 1
|
||||
AND (deleted IS NULL OR deleted = 0)
|
||||
${scoped.sql};`,
|
||||
scoped.params
|
||||
);
|
||||
|
||||
const currentKeys = new Set(currentRows.map((r) => key(Number(r.member_id), Number(r.course_id))));
|
||||
const nextKeys = new Set(Array.from(latestByPair.keys()));
|
||||
|
||||
let wouldDeactivate = 0;
|
||||
currentKeys.forEach((k) => {
|
||||
if (!nextKeys.has(k)) {
|
||||
wouldDeactivate++;
|
||||
}
|
||||
});
|
||||
|
||||
const summary = {
|
||||
dryRun: args.dryRun,
|
||||
scope: {
|
||||
memberId: args.memberId,
|
||||
courseId: args.courseId,
|
||||
},
|
||||
actorId: args.actorId,
|
||||
historicalPassingRows: evidenceRows.length,
|
||||
activePairsComputed: latestByPair.size,
|
||||
currentlyActivePairs: currentRows.length,
|
||||
wouldDeactivate,
|
||||
};
|
||||
|
||||
if (args.dryRun) {
|
||||
console.log("Qualification backfill dry run summary:");
|
||||
console.log(JSON.stringify(summary, null, 2));
|
||||
return;
|
||||
}
|
||||
|
||||
await conn.beginTransaction();
|
||||
|
||||
const deactivateReason = "Backfill recompute from historical training reports";
|
||||
|
||||
const deactivateResult = await conn.query(
|
||||
`UPDATE members_qualifications mq
|
||||
SET mq.active = 0,
|
||||
mq.revoked_by_id = ?,
|
||||
mq.revoked_reason = ?,
|
||||
mq.revoked_at = UTC_TIMESTAMP(),
|
||||
mq.updated_at = current_timestamp()
|
||||
WHERE mq.active = 1
|
||||
AND (mq.deleted IS NULL OR mq.deleted = 0)
|
||||
${buildScopeClause(args, "mq").sql};`,
|
||||
[args.actorId, deactivateReason].concat(buildScopeClause(args, "mq").params)
|
||||
);
|
||||
|
||||
let upserts = 0;
|
||||
for (const item of latestByPair.values()) {
|
||||
await conn.query(
|
||||
`INSERT INTO members_qualifications
|
||||
(member_id, course_id, event_date, active, awarded_by_id, revoked_by_id, revoked_reason, revoked_at, source_course_event_id, deleted)
|
||||
VALUES (?, ?, ?, 1, ?, NULL, NULL, NULL, ?, 0)
|
||||
ON DUPLICATE KEY UPDATE
|
||||
event_date = VALUES(event_date),
|
||||
active = 1,
|
||||
awarded_by_id = VALUES(awarded_by_id),
|
||||
revoked_by_id = NULL,
|
||||
revoked_reason = NULL,
|
||||
revoked_at = NULL,
|
||||
source_course_event_id = VALUES(source_course_event_id),
|
||||
deleted = 0,
|
||||
updated_at = current_timestamp();`,
|
||||
[item.memberId, item.courseId, item.eventDate, args.actorId, item.courseEventId]
|
||||
);
|
||||
upserts++;
|
||||
}
|
||||
|
||||
await conn.commit();
|
||||
|
||||
console.log("Qualification backfill complete:");
|
||||
console.log(JSON.stringify({
|
||||
...summary,
|
||||
deactivatedRows: deactivateResult.affectedRows,
|
||||
upserts,
|
||||
}, null, 2));
|
||||
} catch (error) {
|
||||
try {
|
||||
await conn.rollback();
|
||||
} catch (_ignored) {
|
||||
// No-op rollback failure.
|
||||
}
|
||||
console.error("Qualification backfill failed:");
|
||||
console.error(error);
|
||||
process.exitCode = 1;
|
||||
} finally {
|
||||
await conn.end();
|
||||
}
|
||||
})();
|
||||
@@ -1,10 +1,11 @@
|
||||
import { CourseAttendee, CourseEventDetails } from "@app/shared/types/course";
|
||||
import { getAllCourses, getCourseEventAttendees, getCourseEventDetails, getCourseEventRoles, getCourseEvents, insertCourseEvent } from "../services/db/CourseSerivce";
|
||||
import { getAllCourses, getCourseEventAttendees, getCourseEventDetails, getCourseEventRoles, getCourseEvents, insertCourseEvent, updateCourseEvent } from "../services/db/CourseSerivce";
|
||||
import { Request, Response, Router } from "express";
|
||||
import { requireLogin, requireMemberState } from "../middleware/auth";
|
||||
import { requireLogin, requireMemberState, requireRole } from "../middleware/auth";
|
||||
import { MemberState } from "@app/shared/types/member";
|
||||
import { logger } from "../services/logging/logger";
|
||||
import { audit } from "../services/logging/auditLog";
|
||||
import { syncQualificationsForCourseEvent } from "../services/db/qualificationService";
|
||||
|
||||
const cr = Router();
|
||||
const er = Router();
|
||||
@@ -125,9 +126,10 @@ er.post('/', async (req: Request, res: Response) => {
|
||||
data.created_by = posterID;
|
||||
data.event_date = new Date(data.event_date);
|
||||
const id = await insertCourseEvent(data);
|
||||
const syncOutcome = await syncQualificationsForCourseEvent(id, posterID);
|
||||
|
||||
audit.course('report_created', { actorId: posterID, targetId: id });
|
||||
logger.info('app', 'Training report posted', { user: posterID, report: id })
|
||||
audit.course('report_created', { actorId: posterID, targetId: id }, { qualificationSync: syncOutcome });
|
||||
logger.info('app', 'Training report posted', { user: posterID, report: id, qualificationSync: syncOutcome })
|
||||
res.status(201).json(id);
|
||||
} catch (error) {
|
||||
logger.error(
|
||||
@@ -143,5 +145,34 @@ er.post('/', async (req: Request, res: Response) => {
|
||||
}
|
||||
})
|
||||
|
||||
er.put('/:id', [requireLogin, requireMemberState(MemberState.Member), requireRole("dev")], async (req: Request, res: Response) => {
|
||||
const editorID: number = req.user.id;
|
||||
const reportId = Number(req.params.id);
|
||||
|
||||
try {
|
||||
let data: CourseEventDetails = req.body;
|
||||
data.event_date = new Date(data.event_date);
|
||||
|
||||
await updateCourseEvent(reportId, data);
|
||||
const syncOutcome = await syncQualificationsForCourseEvent(reportId, editorID);
|
||||
|
||||
audit.course('report_edited', { actorId: editorID, targetId: reportId }, { qualificationSync: syncOutcome });
|
||||
logger.info('app', 'Training report edited', { user: editorID, report: reportId, qualificationSync: syncOutcome })
|
||||
res.sendStatus(200);
|
||||
} catch (error) {
|
||||
logger.error(
|
||||
'app',
|
||||
'Failed to edit training report',
|
||||
{
|
||||
user: editorID,
|
||||
report: reportId,
|
||||
error: error instanceof Error ? error.message : String(error),
|
||||
stack: error instanceof Error ? error.stack : undefined,
|
||||
}
|
||||
);
|
||||
res.status(500).json("failed to edit training\n" + error)
|
||||
}
|
||||
})
|
||||
|
||||
export const courseRouter = cr;
|
||||
export const eventRouter = er;
|
||||
|
||||
@@ -16,6 +16,7 @@ import { memberCache } from './auth';
|
||||
import { cancelLatestRank } from '../services/db/rankService';
|
||||
import { cancelLatestUnit } from '../services/db/unitService';
|
||||
import { audit } from '../services/logging/auditLog';
|
||||
import { getMemberActiveQualifications } from '../services/db/qualificationService';
|
||||
|
||||
//get all users
|
||||
router.get('/', [requireLogin, requireMemberState(MemberState.Member)], async (req, res) => {
|
||||
@@ -211,6 +212,27 @@ router.post('/full/bulk', async (req: Request, res: Response) => {
|
||||
}
|
||||
})
|
||||
|
||||
router.get('/:id/qualifications', [requireLogin, requireMemberState(MemberState.Member)], async (req: Request, res: Response) => {
|
||||
const memberId = Number(req.params.id);
|
||||
if (Number.isNaN(memberId) || memberId <= 0) {
|
||||
return res.status(400).json({ error: 'Invalid member id' });
|
||||
}
|
||||
|
||||
try {
|
||||
const qualifications = await getMemberActiveQualifications(memberId);
|
||||
return res.status(200).json(qualifications);
|
||||
} catch (error) {
|
||||
logger.error('app', 'Failed to fetch member qualifications', {
|
||||
memberId,
|
||||
caller: req.user?.id,
|
||||
error: error instanceof Error ? error.message : String(error),
|
||||
stack: error instanceof Error ? error.stack : undefined,
|
||||
});
|
||||
|
||||
return res.status(500).json({ error: 'Failed to fetch member qualifications' });
|
||||
}
|
||||
})
|
||||
|
||||
router.post('/cache/user/bust', [requireLogin, requireMemberState(MemberState.Member), requireRole('dev')], async (req: Request, res: Response) => {
|
||||
try {
|
||||
const clearedEntries = memberCache.Clear();
|
||||
|
||||
@@ -86,7 +86,7 @@ export async function insertCourseEvent(event: CourseEventDetails): Promise<numb
|
||||
let course: Course = await getCourseByID(event.course_id);
|
||||
|
||||
await con.beginTransaction();
|
||||
const res = await con.query("INSERT INTO course_events (course_id, event_date, remarks, created_by, hasBookwork, hasQual) VALUES (?, ?, ?, ?, ?, ?);", [event.course_id, toDateTime(event.event_date), event.remarks, event.created_by, course.hasBookwork, course.hasQual]);
|
||||
const res = await con.query("INSERT INTO course_events (course_id, event_date, remarks, created_by, hasBookwork, hasQual, is_challenge) VALUES (?, ?, ?, ?, ?, ?, ?);", [event.course_id, toDateTime(event.event_date), event.remarks, event.created_by, course.hasBookwork, course.hasQual, event.is_challenge ? 1 : 0]);
|
||||
var eventID: number = res.insertId;
|
||||
|
||||
for (const attendee of event.attendees) {
|
||||
@@ -127,6 +127,7 @@ export async function getCourseEvents(sortDir: string, search: string = "", page
|
||||
E.id AS event_id,
|
||||
E.course_id,
|
||||
E.event_date AS date,
|
||||
IFNULL(E.is_challenge, 0) AS is_challenge,
|
||||
E.created_by,
|
||||
C.name AS course_name,
|
||||
C.short_name AS course_shortname,
|
||||
@@ -160,4 +161,50 @@ export async function getCourseEventRoles(): Promise<CourseAttendeeRole[]> {
|
||||
const sql = "SELECT * FROM course_attendee_roles;"
|
||||
const roles: CourseAttendeeRole[] = await pool.query(sql);
|
||||
return roles;
|
||||
}
|
||||
|
||||
export async function updateCourseEvent(eventId: number, event: CourseEventDetails): Promise<void> {
|
||||
try {
|
||||
var con = await pool.getConnection();
|
||||
|
||||
let course: Course = await getCourseByID(event.course_id);
|
||||
|
||||
await con.beginTransaction();
|
||||
|
||||
await con.query(
|
||||
`UPDATE course_events
|
||||
SET course_id = ?,
|
||||
event_date = ?,
|
||||
remarks = ?,
|
||||
hasBookwork = ?,
|
||||
hasQual = ?,
|
||||
is_challenge = ?
|
||||
WHERE id = ?;`,
|
||||
[event.course_id, toDateTime(event.event_date), event.remarks, course.hasBookwork, course.hasQual, event.is_challenge ? 1 : 0, eventId]
|
||||
);
|
||||
|
||||
await con.query(`DELETE FROM course_attendees WHERE course_event_id = ?;`, [eventId]);
|
||||
|
||||
for (const attendee of event.attendees) {
|
||||
await con.query(
|
||||
`INSERT INTO course_attendees (
|
||||
attendee_id,
|
||||
course_event_id,
|
||||
attendee_role_id,
|
||||
passed_bookwork,
|
||||
passed_qual,
|
||||
remarks
|
||||
)
|
||||
VALUES (?, ?, ?, ?, ?, ?);`,
|
||||
[attendee.attendee_id, eventId, attendee.attendee_role_id, attendee.passed_bookwork, attendee.passed_qual, attendee.remarks]
|
||||
);
|
||||
}
|
||||
|
||||
await con.commit();
|
||||
} catch (error) {
|
||||
if (con) await con.rollback();
|
||||
throw error;
|
||||
} finally {
|
||||
if (con) await con.release();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,310 @@
|
||||
import pool from "../../db";
|
||||
import { CourseAttendee } from "@app/shared/types/course";
|
||||
import { MemberQualificationView } from "@app/shared/types/qualification";
|
||||
|
||||
interface EventQualificationContext {
|
||||
courseId: number;
|
||||
eventDate: Date;
|
||||
hasBookwork: boolean;
|
||||
hasQual: boolean;
|
||||
attendees: CourseAttendee[];
|
||||
}
|
||||
|
||||
interface SyncOutcome {
|
||||
courseId: number | null;
|
||||
awarded: number;
|
||||
deactivated: number;
|
||||
impactedMembers: number;
|
||||
}
|
||||
|
||||
async function getEventQualificationContext(con: any, courseEventId: number): Promise<EventQualificationContext | null> {
|
||||
const eventRows = await con.query(
|
||||
`SELECT e.event_date, c.hasBookwork, c.hasQual, c.id AS course_id
|
||||
FROM course_events e
|
||||
INNER JOIN courses c ON c.id = e.course_id
|
||||
WHERE e.id = ?;`,
|
||||
[courseEventId]
|
||||
);
|
||||
|
||||
if (!eventRows.length) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const attendeeRows = await con.query(
|
||||
`SELECT
|
||||
ca.passed_bookwork,
|
||||
ca.passed_qual,
|
||||
ca.attendee_id,
|
||||
ca.course_event_id,
|
||||
ca.attendee_role_id,
|
||||
ca.created_at,
|
||||
ca.updated_at,
|
||||
ca.remarks,
|
||||
mem.name AS attendee_name,
|
||||
ar.id AS role_id,
|
||||
ar.name AS role_name,
|
||||
ar.description AS role_description,
|
||||
ar.deleted AS role_deleted,
|
||||
ar.created_at AS role_created_at,
|
||||
ar.updated_at AS role_updated_at
|
||||
FROM course_attendees ca
|
||||
LEFT JOIN members mem ON ca.attendee_id = mem.id
|
||||
LEFT JOIN course_attendee_roles ar ON ca.attendee_role_id = ar.id
|
||||
WHERE ca.course_event_id = ?;`,
|
||||
[courseEventId]
|
||||
);
|
||||
|
||||
const attendees: CourseAttendee[] = attendeeRows.map((row: any) => ({
|
||||
passed_bookwork: !!row.passed_bookwork,
|
||||
passed_qual: !!row.passed_qual,
|
||||
attendee_id: row.attendee_id,
|
||||
course_event_id: row.course_event_id,
|
||||
attendee_role_id: row.attendee_role_id,
|
||||
created_at: new Date(row.created_at),
|
||||
updated_at: new Date(row.updated_at),
|
||||
remarks: row.remarks,
|
||||
attendee_name: row.attendee_name,
|
||||
role: row.role_id
|
||||
? {
|
||||
id: row.role_id,
|
||||
name: row.role_name,
|
||||
description: row.role_description,
|
||||
deleted: !!row.role_deleted,
|
||||
created_at: row.role_created_at ? new Date(row.role_created_at) : null,
|
||||
updated_at: row.role_updated_at ? new Date(row.role_updated_at) : null,
|
||||
}
|
||||
: null,
|
||||
}));
|
||||
|
||||
return {
|
||||
courseId: Number(eventRows[0].course_id),
|
||||
eventDate: new Date(eventRows[0].event_date),
|
||||
hasBookwork: !!eventRows[0].hasBookwork,
|
||||
hasQual: !!eventRows[0].hasQual,
|
||||
attendees,
|
||||
};
|
||||
}
|
||||
|
||||
async function getLatestPassingCourseEvent(
|
||||
con: any,
|
||||
memberId: number,
|
||||
courseId: number,
|
||||
hasBookwork: boolean,
|
||||
hasQual: boolean
|
||||
): Promise<{ course_event_id: number, event_date: Date } | null> {
|
||||
const evidenceRows = await con.query(
|
||||
`SELECT
|
||||
MAX(CASE WHEN IFNULL(e.hasBookwork, 0) = 1 AND ca.passed_bookwork = 1 THEN 1 ELSE 0 END) AS has_bookwork_pass,
|
||||
MAX(CASE WHEN IFNULL(e.hasQual, 0) = 1 AND ca.passed_qual = 1 THEN 1 ELSE 0 END) AS has_qual_pass,
|
||||
MAX(CASE WHEN IFNULL(e.is_challenge, 0) = 1 AND IFNULL(e.hasQual, 0) = 1 AND ca.passed_qual = 1 THEN 1 ELSE 0 END) AS has_challenge_qual_pass
|
||||
FROM course_events e
|
||||
INNER JOIN course_attendees ca ON ca.course_event_id = e.id
|
||||
WHERE e.course_id = ?
|
||||
AND ca.attendee_id = ?
|
||||
AND ca.attendee_role_id = 2
|
||||
AND (e.deleted IS NULL OR e.deleted = 0);`,
|
||||
[courseId, memberId]
|
||||
);
|
||||
|
||||
if (!evidenceRows.length) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const hasBookworkPass = Number(evidenceRows[0].has_bookwork_pass) === 1;
|
||||
const hasQualPass = Number(evidenceRows[0].has_qual_pass) === 1;
|
||||
const hasChallengeQualPass = Number(evidenceRows[0].has_challenge_qual_pass) === 1;
|
||||
|
||||
let hasPassingRecord = false;
|
||||
if (hasBookwork && hasQual) {
|
||||
hasPassingRecord = (hasBookworkPass && hasQualPass) || hasChallengeQualPass;
|
||||
} else if (hasBookwork && !hasQual) {
|
||||
hasPassingRecord = hasBookworkPass;
|
||||
} else if (!hasBookwork && hasQual) {
|
||||
hasPassingRecord = hasQualPass;
|
||||
}
|
||||
|
||||
if (!hasPassingRecord) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const latestRows = await con.query(
|
||||
`SELECT e.id AS course_event_id, e.event_date
|
||||
FROM course_events e
|
||||
INNER JOIN course_attendees ca ON ca.course_event_id = e.id
|
||||
WHERE e.course_id = ?
|
||||
AND ca.attendee_id = ?
|
||||
AND ca.attendee_role_id = 2
|
||||
AND (e.deleted IS NULL OR e.deleted = 0)
|
||||
AND (
|
||||
(IFNULL(e.hasBookwork, 0) = 1 AND ca.passed_bookwork = 1)
|
||||
OR (IFNULL(e.hasQual, 0) = 1 AND ca.passed_qual = 1)
|
||||
OR (IFNULL(e.is_challenge, 0) = 1 AND IFNULL(e.hasQual, 0) = 1 AND ca.passed_qual = 1)
|
||||
)
|
||||
ORDER BY e.event_date DESC, e.id DESC
|
||||
LIMIT 1;`,
|
||||
[courseId, memberId]
|
||||
);
|
||||
|
||||
if (!latestRows.length) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return {
|
||||
course_event_id: Number(latestRows[0].course_event_id),
|
||||
event_date: new Date(latestRows[0].event_date),
|
||||
};
|
||||
}
|
||||
|
||||
export async function syncQualificationsForCourseEvent(courseEventId: number, actorId: number, existingConnection: any | null = null): Promise<SyncOutcome> {
|
||||
let con = existingConnection;
|
||||
let ownConnection = false;
|
||||
|
||||
if (!con) {
|
||||
con = await pool.getConnection();
|
||||
ownConnection = true;
|
||||
}
|
||||
|
||||
try {
|
||||
if (ownConnection) {
|
||||
await con.beginTransaction();
|
||||
}
|
||||
|
||||
const context = await getEventQualificationContext(con, courseEventId);
|
||||
if (!context) {
|
||||
if (ownConnection) {
|
||||
await con.commit();
|
||||
}
|
||||
return { courseId: null, awarded: 0, deactivated: 0, impactedMembers: 0 };
|
||||
}
|
||||
|
||||
if (!context.hasBookwork && !context.hasQual) {
|
||||
if (ownConnection) {
|
||||
await con.commit();
|
||||
}
|
||||
return { courseId: context.courseId, awarded: 0, deactivated: 0, impactedMembers: 0 };
|
||||
}
|
||||
|
||||
const expectedMembers = new Set<number>();
|
||||
for (const attendee of context.attendees) {
|
||||
if (attendee.attendee_role_id === 2) {
|
||||
expectedMembers.add(attendee.attendee_id);
|
||||
}
|
||||
}
|
||||
|
||||
const existingAwardedByThisEvent = await con.query(
|
||||
`SELECT member_id
|
||||
FROM members_qualifications
|
||||
WHERE course_id = ?
|
||||
AND source_course_event_id = ?
|
||||
AND active = 1
|
||||
AND (deleted IS NULL OR deleted = 0);`,
|
||||
[context.courseId, courseEventId]
|
||||
);
|
||||
|
||||
const existingMembers = new Set<number>(existingAwardedByThisEvent.map((row: any) => Number(row.member_id)));
|
||||
|
||||
const expectedMemberList = Array.from(expectedMembers);
|
||||
const impactedMembers = new Set<number>(expectedMemberList.concat(Array.from(existingMembers)));
|
||||
const impactedList = Array.from(impactedMembers);
|
||||
|
||||
let awarded = 0;
|
||||
let deactivated = 0;
|
||||
|
||||
for (let i = 0; i < impactedList.length; i++) {
|
||||
const memberId = impactedList[i];
|
||||
const latestPass = await getLatestPassingCourseEvent(
|
||||
con,
|
||||
memberId,
|
||||
context.courseId,
|
||||
context.hasBookwork,
|
||||
context.hasQual
|
||||
);
|
||||
|
||||
if (latestPass) {
|
||||
await con.query(
|
||||
`INSERT INTO members_qualifications
|
||||
(member_id, course_id, event_date, active, awarded_by_id, revoked_by_id, revoked_reason, revoked_at, source_course_event_id, deleted)
|
||||
VALUES (?, ?, ?, 1, ?, NULL, NULL, NULL, ?, 0)
|
||||
ON DUPLICATE KEY UPDATE
|
||||
event_date = VALUES(event_date),
|
||||
active = 1,
|
||||
awarded_by_id = VALUES(awarded_by_id),
|
||||
revoked_by_id = NULL,
|
||||
revoked_reason = NULL,
|
||||
revoked_at = NULL,
|
||||
source_course_event_id = VALUES(source_course_event_id),
|
||||
deleted = 0,
|
||||
updated_at = current_timestamp();`,
|
||||
[memberId, context.courseId, latestPass.event_date, actorId, latestPass.course_event_id]
|
||||
);
|
||||
awarded++;
|
||||
} else {
|
||||
const result = await con.query(
|
||||
`UPDATE members_qualifications
|
||||
SET active = 0,
|
||||
revoked_by_id = ?,
|
||||
revoked_reason = ?,
|
||||
revoked_at = UTC_TIMESTAMP(),
|
||||
updated_at = current_timestamp()
|
||||
WHERE member_id = ?
|
||||
AND course_id = ?
|
||||
AND active = 1;`,
|
||||
[actorId, 'No passing record remains for this course after report reconciliation', memberId, context.courseId]
|
||||
);
|
||||
|
||||
if (result.affectedRows > 0) {
|
||||
deactivated++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (ownConnection) {
|
||||
await con.commit();
|
||||
}
|
||||
|
||||
return {
|
||||
courseId: context.courseId,
|
||||
awarded,
|
||||
deactivated,
|
||||
impactedMembers: impactedMembers.size,
|
||||
};
|
||||
} catch (error) {
|
||||
if (ownConnection) {
|
||||
await con.rollback();
|
||||
}
|
||||
throw error;
|
||||
} finally {
|
||||
if (ownConnection && con) {
|
||||
await con.release();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export async function getMemberActiveQualifications(memberId: number): Promise<MemberQualificationView[]> {
|
||||
const rows = await pool.query(
|
||||
`SELECT
|
||||
mq.course_id,
|
||||
c.name AS course_name,
|
||||
c.short_name AS course_short_name,
|
||||
mq.event_date AS awarded_on,
|
||||
mq.source_course_event_id,
|
||||
mq.active
|
||||
FROM members_qualifications mq
|
||||
INNER JOIN courses c ON c.id = mq.course_id
|
||||
WHERE mq.member_id = ?
|
||||
AND mq.active = 1
|
||||
AND (mq.deleted IS NULL OR mq.deleted = 0)
|
||||
AND (c.deleted IS NULL OR c.deleted = 0)
|
||||
ORDER BY mq.event_date DESC, c.name ASC;`,
|
||||
[memberId]
|
||||
);
|
||||
|
||||
return rows.map((row: any) => ({
|
||||
course_id: row.course_id,
|
||||
course_name: row.course_name,
|
||||
course_short_name: row.course_short_name,
|
||||
awarded_on: row.awarded_on ? new Date(row.awarded_on) : null,
|
||||
active: !!row.active,
|
||||
source_course_event_id: row.source_course_event_id,
|
||||
}));
|
||||
}
|
||||
@@ -11,6 +11,7 @@ export const courseEventAttendeeSchema = z.object({
|
||||
export const trainingReportSchema = z.object({
|
||||
id: z.number().int().positive().optional(),
|
||||
course_id: z.number({ invalid_type_error: "Must select a training" }).int(),
|
||||
is_challenge: z.boolean().default(false),
|
||||
event_date: z
|
||||
.string()
|
||||
.refine(
|
||||
|
||||
@@ -18,6 +18,7 @@ export interface CourseEventDetails {
|
||||
course_id: number | null; // FK → courses.id
|
||||
event_type: number | null; // FK → event_types.id
|
||||
event_date: Date; // datetime (not nullable)
|
||||
is_challenge?: boolean;
|
||||
|
||||
guilded_event_id: number | null;
|
||||
|
||||
@@ -88,4 +89,5 @@ export interface CourseEventSummary {
|
||||
date: string;
|
||||
created_by: number;
|
||||
created_by_name: string;
|
||||
is_challenge?: boolean;
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
export interface MemberQualification {
|
||||
id: number;
|
||||
member_id: number;
|
||||
course_id: number;
|
||||
event_date: Date | null;
|
||||
active: boolean;
|
||||
awarded_by_id: number | null;
|
||||
revoked_by_id: number | null;
|
||||
revoked_reason: string | null;
|
||||
revoked_at: Date | null;
|
||||
source_course_event_id: number | null;
|
||||
created_at: Date;
|
||||
updated_at: Date;
|
||||
}
|
||||
|
||||
export interface MemberQualificationView {
|
||||
course_id: number;
|
||||
course_name: string;
|
||||
course_short_name: string;
|
||||
awarded_on: Date | null;
|
||||
active: boolean;
|
||||
source_course_event_id: number | null;
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
import { Discharge } from "@shared/schemas/dischargeSchema";
|
||||
import { memberSettings, Member, MemberLight, MemberCardDetails, PaginatedMembers, MemberState, UserCacheBustResult } from "@shared/types/member";
|
||||
import { MemberQualificationView } from "@shared/types/qualification";
|
||||
|
||||
// @ts-ignore
|
||||
const addr = import.meta.env.VITE_APIHOST;
|
||||
@@ -117,6 +118,18 @@ export async function getFullMembers(ids: number[]): Promise<MemberCardDetails[]
|
||||
return response.json();
|
||||
}
|
||||
|
||||
export async function getMemberQualifications(memberID: number): Promise<MemberQualificationView[]> {
|
||||
const response = await fetch(`${addr}/members/${memberID}/qualifications`, {
|
||||
credentials: 'include',
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error("Failed to fetch member qualifications");
|
||||
}
|
||||
|
||||
return response.json();
|
||||
}
|
||||
|
||||
/**
|
||||
* Requests for the given member to be discharged
|
||||
* @param data discharge packet
|
||||
|
||||
@@ -91,3 +91,19 @@ export async function postTrainingReport(report: CourseEventDetails) {
|
||||
|
||||
return res.json(); // expected to return the inserted report or new ID
|
||||
}
|
||||
|
||||
export async function putTrainingReport(reportId: number, report: CourseEventDetails): Promise<void> {
|
||||
const res = await fetch(`${addr}/courseEvent/${reportId}`, {
|
||||
method: "PUT",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify(report),
|
||||
credentials: 'include',
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
const errorText = await res.text();
|
||||
throw new Error(`Failed to update training report: ${res.status} ${errorText}`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,6 +53,7 @@ const reasonOptions = [
|
||||
{ label: 'Leadership Vote', value: 'leadership_vote' },
|
||||
{ label: 'Appointment', value: 'appointment' },
|
||||
{ label: 'Step Down', value: 'step_down' },
|
||||
{ label: 'Completed Basic', value: 'completed_basic' },
|
||||
{ label: 'Custom', value: 'custom' },
|
||||
]
|
||||
|
||||
|
||||
@@ -34,6 +34,7 @@ const { handleSubmit, resetForm, errors, values, setFieldValue } = useForm({
|
||||
validateOnMount: false,
|
||||
initialValues: {
|
||||
course_id: null,
|
||||
is_challenge: false,
|
||||
event_date: "",
|
||||
remarks: "",
|
||||
attendees: [],
|
||||
@@ -59,6 +60,17 @@ watch(() => values.course_id, (newCourseId, oldCourseId) => {
|
||||
});
|
||||
});
|
||||
|
||||
watch(() => values.is_challenge, (isChallenge) => {
|
||||
if (!isChallenge) {
|
||||
return;
|
||||
}
|
||||
|
||||
values.attendees.forEach((a, index) => {
|
||||
// @ts-ignore
|
||||
setFieldValue(`attendees[${index}].passed_bookwork`, false);
|
||||
});
|
||||
});
|
||||
|
||||
const submitForm = handleSubmit(onSubmit);
|
||||
|
||||
function toMySQLDateTime(date: Date): string {
|
||||
@@ -92,6 +104,15 @@ async function onSubmit(vals) {
|
||||
const { remove, push, fields } = useFieldArray('attendees');
|
||||
|
||||
const selectedCourse = computed<Course | undefined>(() => { return trainings.value?.find(c => c.id == values.course_id) })
|
||||
const bookworkDisabledMessage = computed(() => {
|
||||
if (values.is_challenge) {
|
||||
return "Bookwork is waived for challenge reports";
|
||||
}
|
||||
if (!selectedCourse.value?.hasBookwork) {
|
||||
return "This course does not have bookwork";
|
||||
}
|
||||
return "";
|
||||
});
|
||||
|
||||
const trainings = ref<Course[] | null>(null);
|
||||
const members = ref<MemberLight[] | null>(null);
|
||||
@@ -195,6 +216,24 @@ const filteredMembers = computed(() => {
|
||||
</VeeField>
|
||||
</FieldGroup>
|
||||
</div>
|
||||
|
||||
<div class="w-[190px]">
|
||||
<FieldGroup>
|
||||
<VeeField name="is_challenge">
|
||||
<Field>
|
||||
<FieldLabel class="scroll-m-20 text-lg tracking-tight">Challenge</FieldLabel>
|
||||
<div class="h-9 px-2 border rounded flex items-center gap-2">
|
||||
<Checkbox :model-value="values.is_challenge"
|
||||
@update:model-value="(v) => setFieldValue('is_challenge', !!v)"></Checkbox>
|
||||
<p class="text-sm text-muted-foreground">Mark report as challenge</p>
|
||||
</div>
|
||||
<FieldDescription class="mt-2 text-xs">
|
||||
Challenge waives bookwork, but qualification pass is still required.
|
||||
</FieldDescription>
|
||||
</Field>
|
||||
</VeeField>
|
||||
</FieldGroup>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<VeeFieldArray name="attendees" v-slot="{ fields, push, remove }">
|
||||
@@ -335,9 +374,9 @@ const filteredMembers = computed(() => {
|
||||
<VeeField v-slot="{ field }" :name="`attendees[${index}].passed_bookwork`" type="checkbox"
|
||||
:value="false" :unchecked-value="true">
|
||||
<div class="flex flex-col items-center">
|
||||
<Tooltip :open="!selectedCourse?.hasBookwork"
|
||||
message="This course does not have bookwork">
|
||||
<Checkbox :disabled="!selectedCourse?.hasBookwork"
|
||||
<Tooltip :open="!!bookworkDisabledMessage"
|
||||
:message="bookworkDisabledMessage">
|
||||
<Checkbox :disabled="!selectedCourse?.hasBookwork || values.is_challenge"
|
||||
:name="`attendees[${index}].passed_bookwork`" :model-value="!field.checked"
|
||||
@update:model-value="field['onUpdate:modelValue']">
|
||||
</Checkbox>
|
||||
|
||||
@@ -34,6 +34,7 @@ import {
|
||||
} from '@/components/ui/pagination'
|
||||
import Tooltip from '@/components/tooltip/Tooltip.vue';
|
||||
import { CopyLink } from '@/lib/copyLink';
|
||||
import Badge from '@/components/ui/badge/Badge.vue';
|
||||
|
||||
enum sidePanelState { view, create, closed };
|
||||
|
||||
@@ -187,6 +188,10 @@ function formatDate(date: Date | string): string {
|
||||
});
|
||||
}
|
||||
|
||||
function isChallengeReport(report: { is_challenge?: boolean | number | null }): boolean {
|
||||
return report?.is_challenge === true || Number(report?.is_challenge || 0) === 1;
|
||||
}
|
||||
|
||||
function setPageSize(size: number) {
|
||||
pageSize.value = size
|
||||
pageNum.value = 1;
|
||||
@@ -246,9 +251,12 @@ const expanded = ref<number>(null);
|
||||
<button v-for="report in trainingReports" :key="`mobile-report-${report.event_id}`"
|
||||
class="w-full rounded-lg border bg-card px-3 py-2 text-left transition-colors hover:bg-muted/40"
|
||||
@click="openTrainingReport(report.event_id)">
|
||||
<p class="font-semibold text-foreground">
|
||||
{{ report.course_name.length > 35 ? report.course_shortname : report.course_name }}
|
||||
</p>
|
||||
<div class="flex items-center gap-2">
|
||||
<p class="font-semibold text-foreground">
|
||||
{{ report.course_name.length > 35 ? report.course_shortname : report.course_name }}
|
||||
</p>
|
||||
<Badge v-if="isChallengeReport(report)" variant="outline" class="uppercase text-xs">Challenge</Badge>
|
||||
</div>
|
||||
<p class="mt-1 text-sm text-muted-foreground">{{ formatDate(report.date) }}</p>
|
||||
<div class="mt-2 flex items-center gap-2 text-xs text-muted-foreground">
|
||||
<span class="font-medium">Posted by:</span>
|
||||
@@ -274,8 +282,12 @@ const expanded = ref<number>(null);
|
||||
<TableBody v-if="loaded">
|
||||
<TableRow class="cursor-pointer" v-for="report in trainingReports" :key="report.event_id"
|
||||
@click="openTrainingReport(report.event_id)">
|
||||
<TableCell class="font-medium">{{ report.course_name.length > 30 ? report.course_shortname :
|
||||
report.course_name }}</TableCell>
|
||||
<TableCell class="font-medium">
|
||||
<div class="flex items-center gap-2">
|
||||
<span>{{ report.course_name.length > 30 ? report.course_shortname : report.course_name }}</span>
|
||||
<Badge v-if="isChallengeReport(report)" variant="outline" class="uppercase text-[10px]">Challenge</Badge>
|
||||
</div>
|
||||
</TableCell>
|
||||
<TableCell>{{ report.date.split('T')[0] }}</TableCell>
|
||||
<TableCell class="text-right">
|
||||
<MemberCard v-if="report.created_by" :member-id="report.created_by"></MemberCard>
|
||||
@@ -341,8 +353,10 @@ const expanded = ref<number>(null);
|
||||
</div>
|
||||
<div v-if="TRLoaded" :class="isMobile ? 'my-3 pb-8' : 'my-5 max-h-[70vh] overflow-y-auto overflow-x-hidden scrollbar-themed'">
|
||||
<div class="flex flex-col mb-5 border rounded-lg bg-muted/70 p-2 py-3 px-4">
|
||||
<p class="scroll-m-20 text-xl font-semibold tracking-tight">{{ focusedTrainingReport.course_name }}
|
||||
</p>
|
||||
<div class="flex items-center gap-2">
|
||||
<p class="scroll-m-20 text-xl font-semibold tracking-tight">{{ focusedTrainingReport.course_name }}</p>
|
||||
<Badge v-if="isChallengeReport(focusedTrainingReport)" variant="outline" class="uppercase text-xs">Challenge</Badge>
|
||||
</div>
|
||||
<div class="mt-2 flex flex-col gap-2 text-sm sm:flex-row sm:items-center sm:gap-10">
|
||||
<p class="text-muted-foreground">{{ formatDate(focusedTrainingReport.event_date) }}</p>
|
||||
<div class="flex gap-2 items-center">Created by:
|
||||
|
||||
Reference in New Issue
Block a user