Skip to content

Commit

Permalink
fix: income, expense, period APIs does not parse path parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
Byunk committed Jan 13, 2024
1 parent b704fad commit 1fd81f9
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "index.js",
"scripts": {
"start": "NODE_ENV=production ts-node src/index.ts",
"test": "NODE_ENV=test nyc --reporter=text ts-mocha test/**/*.spec.ts --exit",
"test": "NODE_ENV=test nyc --reporter=text ts-mocha test/**/*.spec.ts test/**/**/*.spec.ts --exit",
"dev": "NODE_ENV=development ts-node-dev --respawn src/index.ts"
},
"author": "Kyungho Byoun",
Expand Down
4 changes: 2 additions & 2 deletions src/controller/expense.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class ExpenseController {
updateExpense = async (req: Request, res: Response, next: NextFunction) => {
logger.info('ExpenseController: updateExpense called');

const expenseId = req.params.expenseId;
const expenseId = req.params.expense_id;
const source = req.body.source;
const category = req.body.category;
const project = req.body.project;
Expand All @@ -57,7 +57,7 @@ class ExpenseController {
deleteExpense = async (req: Request, res: Response, next: NextFunction) => {
logger.info('ExpenseController: deleteExpense called');

const expenseId = req.params.expenseId;
const expenseId = req.params.expense_id;
const dto = new DeleteExpenseDto(expenseId);
await this.expenseService.deleteExpense(dto);
res.sendStatus(200);
Expand Down
4 changes: 2 additions & 2 deletions src/controller/income.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class IncomeController {
updateIncome = async (req: Request, res: Response, next: NextFunction) => {
logger.info('IncomeController: updateIncome called');

const incomeId = req.params.incomeId;
const incomeId = req.params.income_id;
const source = req.body.source;
const category = req.body.category;
const content = req.body.content;
Expand All @@ -53,7 +53,7 @@ class IncomeController {
deleteIncome = async (req: Request, res: Response, next: NextFunction) => {
logger.info('IncomeController: deleteIncome called');

const incomeId = req.params.incomeId;
const incomeId = req.params.income_id;
const dto = new DeleteIncomeDto(incomeId);
await this.incomeService.deleteIncome(dto);
res.sendStatus(200);
Expand Down
10 changes: 10 additions & 0 deletions src/routes/budget/period.ts → src/routes/budget/audit_period.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ export function createPeriodRouter() {
const router = express.Router();
router.use(wrapAsync(validateIsAdmin));

router.get('/', async (req: Request, res: Response, next: NextFunction) => {
const auditPeriods = await AuditPeriod.findAll({
order: [
['year', 'ASC'],
['half', 'ASC'],
],
});
res.json(auditPeriods);
});

router.post(
'/:year/:half',
wrapAsync(async (req: Request, res: Response, next: NextFunction) => {
Expand Down
2 changes: 1 addition & 1 deletion src/routes/budget/budget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { wrapAsync } from '../../middleware';
import { validateOrganization } from '../../middleware/auth';
import { createIncomeRouter } from './income';
import { createExpenseRouter } from './expense';
import { createPeriodRouter } from './period';
import { createPeriodRouter } from './audit_period';
import { BudgetController } from '../../controller';

export function createBudgetsRouter() {
Expand Down
15 changes: 15 additions & 0 deletions swagger/routes/period.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
paths:
/budgets/period":
get:
description: 회계기간 목록 조회 (관리자만 호출 가능).
tags:
- 회계기간
responses:
'200':
content:
application/json:
schema:
type: array
items:
$ref: '#/definitions/AuditPeriod'
'401':
description: UnauthorizedError
/budgets/period/:year/:half:
post:
description: 회계기간 생성 (관리자만 호출 가능).
Expand Down
133 changes: 133 additions & 0 deletions test/routes/budget/audit_period.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import chai, { expect } from 'chai';
import chaiHttp from 'chai-http';
import sinon from 'sinon';
import { initDB } from '../../../src/db/utils';
import * as auth from '../../../src/middleware/auth';
import { createApp } from '../../../src/app';
import * as model from '../../../src/model';

chai.use(chaiHttp);

describe('API /budgets/period', function () {
let app: Express.Application;
let stubValidateIsAdmin: sinon.SinonStub;

before(async function () {
await initDB();

stubValidateIsAdmin = sinon
.stub(auth, 'validateIsAdmin')
.callsFake(async (req, res, next) => {
return next();
});

app = createApp();
});

after(async function () {
stubValidateIsAdmin.restore();
});

afterEach(async function () {
const options = {
truncate: true,
cascade: true,
};
await model.AuditPeriod.destroy(options);
});

describe('GET /', function () {
it('모든 감사기간을 조회할 수 있다.', async function () {
await model.AuditPeriod.create({
year: '2024',
half: 'spring',
start: '2024-03-01',
end: '2024-06-30',
});
await model.AuditPeriod.create({
year: '2024',
half: 'fall',
start: '2024-09-01',
end: '2024-12-31',
});
await model.AuditPeriod.create({
year: '2023',
half: 'fall',
start: '2023-09-01',
end: '2023-12-31',
});
await model.AuditPeriod.create({
year: '2023',
half: 'spring',
start: '2023-03-01',
end: '2023-06-30',
});

const res = await chai.request(app).get('/budgets/period');
expect(res).to.have.status(200);
expect(res.body[0].year).to.equal(2023);
expect(res.body[0].half).to.equal('spring');
expect(new Date(res.body[0].start)).eql(new Date('2023-03-01'));
expect(new Date(res.body[0].end)).eql(new Date('2023-06-30'));
expect(res.body[1].year).to.equal(2023);
expect(res.body[1].half).to.equal('fall');
expect(new Date(res.body[1].start)).eql(new Date('2023-09-01'));
expect(new Date(res.body[1].end)).eql(new Date('2023-12-31'));
expect(res.body[2].year).to.equal(2024);
expect(res.body[2].half).to.equal('spring');
expect(new Date(res.body[2].start)).eql(new Date('2024-03-01'));
expect(new Date(res.body[2].end)).eql(new Date('2024-06-30'));
expect(res.body[3].year).to.equal(2024);
expect(res.body[3].half).to.equal('fall');
expect(new Date(res.body[3].start)).eql(new Date('2024-09-01'));
expect(new Date(res.body[3].end)).eql(new Date('2024-12-31'));
});
});

describe('POST /:year/:half', function () {
it('감사기간을 생성할 수 있다.', async function () {
const res = await chai
.request(app)
.post('/budgets/period/2023/spring')
.send({
start: '2023-03-01',
end: '2023-06-30',
});

expect(res).to.have.status(200);
expect(res.body.year).to.equal(2023);
expect(res.body.half).to.equal('spring');
expect(new Date(res.body.start)).eql(new Date('2023-03-01'));
expect(new Date(res.body.end)).eql(new Date('2023-06-30'));
});
});

describe('PUT /:year/:half', function () {
it('감사기간을 변경할 수 있다.', async function () {
await model.AuditPeriod.create({
year: '2023',
half: 'spring',
start: '2023-03-01',
end: '2023-06-30',
});

const res = await chai
.request(app)
.put('/budgets/period/2023/spring')
.send({
start: '2023-03-01',
end: '2023-07-10',
});
expect(res).to.have.status(200);

const updatedPeriod = await model.AuditPeriod.findOne({
where: {
year: '2023',
half: 'spring',
},
});
expect(new Date(updatedPeriod!.start)).eql(new Date('2023-03-01'));
expect(new Date(updatedPeriod!.end)).eql(new Date('2023-07-10'));
});
});
});

0 comments on commit 1fd81f9

Please sign in to comment.