Skip to content

Commit

Permalink
Refactor audit period validation logic and add error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
yym0329 committed Feb 24, 2024
1 parent b5fe1e8 commit 73ac65a
Showing 1 changed file with 64 additions and 21 deletions.
85 changes: 64 additions & 21 deletions src/middleware/validate_audit_period.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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');
Expand Down

0 comments on commit 73ac65a

Please sign in to comment.