feat: apply datetime and self-approve admin requests
This commit is contained in:
@@ -271,6 +271,22 @@ async function retryPassDelivery(dbQuery: QueryFn, deliveryId: number) {
|
||||
return updated.rows[0];
|
||||
}
|
||||
|
||||
function canSelfApprove(user: UserRow): boolean {
|
||||
return user.roles.includes('ADMIN') || user.roles.includes('SECURITY');
|
||||
}
|
||||
|
||||
async function approveVisit(dbQuery: QueryFn, visitId: number, actor: UserRow, comment: string | null): Promise<VisitRow> {
|
||||
await dbQuery("UPDATE visit_requests SET status = 'APPROVED', updated_at = now() WHERE id = $1", [visitId]);
|
||||
await dbQuery(
|
||||
"INSERT INTO approvals (created_at, updated_at, visit_request_id, approver_id, decision, comment, decided_at) VALUES (now(), now(), $1, $2, 'APPROVED', $3, now())",
|
||||
[visitId, Number(actor.id), comment],
|
||||
);
|
||||
await audit(dbQuery, actor, 'APPROVE', 'VISIT_REQUEST', visitId, '승인');
|
||||
const visit = (await visitById(dbQuery, visitId))!;
|
||||
await sendPassDelivery(dbQuery, visit);
|
||||
return visit;
|
||||
}
|
||||
|
||||
async function resolvePurpose(dbQuery: QueryFn, code: string | undefined, purpose: string, detail?: string) {
|
||||
if (code) {
|
||||
const result = await dbQuery<{ code: string; name: string; custom_allowed: boolean; active: boolean }>(
|
||||
@@ -548,7 +564,12 @@ export function createBusinessRouter(deps: BusinessRouterDeps = { query }): Rout
|
||||
if (zones.length === 0) throw new ApiError(400, '출입 구역을 선택하세요.');
|
||||
const created = [];
|
||||
for (const zone of zones) {
|
||||
created.push(await createOneVisit(dbQuery, actor, req.body, zone));
|
||||
const visit = await createOneVisit(dbQuery, actor, req.body, zone);
|
||||
if (canSelfApprove(actor)) {
|
||||
created.push(toVisit(await approveVisit(dbQuery, visit.id, actor, '본인 등록 자동승인')));
|
||||
} else {
|
||||
created.push(visit);
|
||||
}
|
||||
}
|
||||
ok(res, created);
|
||||
}));
|
||||
@@ -568,14 +589,7 @@ export function createBusinessRouter(deps: BusinessRouterDeps = { query }): Rout
|
||||
router.post('/approvals/:id/approve', asyncRoute(async (req, res) => {
|
||||
const actor = await requireAdmin(dbQuery, req);
|
||||
const id = Number(req.params.id);
|
||||
await dbQuery("UPDATE visit_requests SET status = 'APPROVED', updated_at = now() WHERE id = $1", [id]);
|
||||
await dbQuery(
|
||||
"INSERT INTO approvals (created_at, updated_at, visit_request_id, approver_id, decision, comment, decided_at) VALUES (now(), now(), $1, $2, 'APPROVED', $3, now())",
|
||||
[id, Number(actor.id), req.body?.comment ? String(req.body.comment) : null],
|
||||
);
|
||||
await audit(dbQuery, actor, 'APPROVE', 'VISIT_REQUEST', id, '승인');
|
||||
const visit = (await visitById(dbQuery, id))!;
|
||||
await sendPassDelivery(dbQuery, visit);
|
||||
const visit = await approveVisit(dbQuery, id, actor, req.body?.comment ? String(req.body.comment) : null);
|
||||
ok(res, toVisit(visit));
|
||||
}));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user