From 73ac65acc70307a395578cd3198e8cad904c6866 Mon Sep 17 00:00:00 2001 From: youngmin Date: Sat, 24 Feb 2024 16:38:07 +0900 Subject: [PATCH] Refactor audit period validation logic and add error handling --- src/middleware/validate_audit_period.ts | 85 +++++++++++++++++++------ 1 file changed, 64 insertions(+), 21 deletions(-) diff --git a/src/middleware/validate_audit_period.ts b/src/middleware/validate_audit_period.ts index 05e28e0..878e97a 100644 --- a/src/middleware/validate_audit_period.ts +++ b/src/middleware/validate_audit_period.ts @@ -9,30 +9,54 @@ export async function validateAuditPeriod( next: NextFunction, ) { sanitizeInput(req); - const today = new Date(); - const year = today.getFullYear(); - const half = today.getMonth() < 8 ? 'spring' : 'fall'; - - const auditPeriod = await AuditPeriod.findOne({ - where: { - year: year, - half: half, - }, - }); - - if (!auditPeriod) { - logger.debug(`audit period does not exist`); - throw new errors.NotFoundError('감사기간이 존재하지 않습니다.'); - } + if (req.params.year && req.params.half) { + const auditPeriod = await AuditPeriod.findOne({ + where: { + year: req.params.year, + half: req.params.half, + }, + }); - if (today < auditPeriod.start || today > auditPeriod.end) { - logger.debug(`today is not in audit period`); - throw new errors.ValidationError('감사기간이 아닙니다.'); - } + if (!auditPeriod) { + logger.debug(`audit period does not exist`); + throw new errors.NotFoundError('감사기간이 존재하지 않습니다.'); + } + + if (today < auditPeriod.start || today > auditPeriod.end) { + logger.debug(`today is not in audit period`); + throw new errors.ValidationError('감사기간이 아닙니다.'); + } + + logger.info(`audit period is validated`); + next(); + } else if (req.params.year || req.params.half) { + logger.debug(`year and half must be used together`); + throw new errors.BadRequestError('년도와 반기는 함께 사용해야 합니다.'); + } else { + const { year, half } = getAuditPeriod(today); + const auditPeriod = await AuditPeriod.findOne({ + where: { + year, + half, + }, + }); + + if (!auditPeriod) { + logger.debug(`audit period does not exist`); + throw new errors.NotFoundError('감사기간이 존재하지 않습니다.'); + } - logger.info(`audit period is validated`); - next(); + if (today < auditPeriod.start || today > auditPeriod.end) { + logger.debug(`today is not in audit period`); + throw new errors.ValidationError( + '현재 감사 기간이 아닙니다. 지난 감사기간에 대해 접근하고 싶다면, 연도와 반기를 요청에 명시하여 주세요.', + ); + } + + logger.info(`audit period is validated`); + next(); + } } export async function findYearAndHalf(req: Request) { @@ -55,6 +79,25 @@ export async function findYearAndHalf(req: Request) { throw new errors.BadRequestError('년도와 반기를 찾을 수 없습니다.'); } +function getAuditPeriod(today: Date) { + // Determine the half based on the month. 0201~0731: spring, 0801~0131: fall. If the month is January, use the previous year's fall. + + let half; + if (today.getMonth() === 0) { + half = 'fall'; + } else if (today.getMonth() < 7) { + half = 'spring'; + } else { + half = 'fall'; + } + + // Determine the year, adjusting for the case when the month is January + const year = + today.getMonth() === 0 ? today.getFullYear() - 1 : today.getFullYear(); + + return { year, half }; +} + function sanitizeInput(req: Request) { if (req.body.income_id && req.body.expense_id) { logger.debug('income_id and expense_id cannot be used together');