Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[50기 최현수 - ADD : Api/용돈조회등록수정삭제 및 잔여 용돈 조회 및 testcode] #7

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
ADD: 용돈조회등록수정삭제 amount => allowance로 get 요청 res.body key 수정 및 delete …
…method target req.body에서 req.query 수신으로 수정 및 testcode 추가
chs991209 committed Nov 21, 2023
commit d53ad8a1e3555a66e767a8c1eca4bef71a63d164
38 changes: 16 additions & 22 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,24 @@
const http = require('http');
const express = require('express');
const cors = require('cors');
const dotenv = require('dotenv');
const morgan = require('morgan')
const routes = require('./src/routes');
const app = express()

dotenv.config()
app.use(cors())
app.use(express.json())
app.use(routes)
const createApp = () => {
const app = express();

app.get('/ping',(req,res) => {
res.status(200).json({
message: 'pong'
})
})

const server = http.createServer(app)
app.use(cors())
app.use(express.json())
app.use(morgan('combined'))
app.use(routes)

const start = async () => {
try {
server.listen(process.env.TYPEORM_SERVER_PORT, () => console.log(
`Server is listening on ${process.env.TYPEORM_SERVER_PORT}`))
} catch (err) {
console.error(err)
}
app.get('/ping',(req,res) => {
res.status(200).json({
message: 'pong'
})
})
return app
}

start()
module.exports = {
createApp
}
27 changes: 27 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const http = require('http');
const dotenv = require('dotenv');

dotenv.config()

const { createApp } = require('./app');
const { appDataSource } = require('./src/utils/dataSource');

const startServer = async () => {
const app = createApp();
appDataSource.initialize()
.then(()=> {
console.log('Data Source has been initialized!')
})
.catch((err) => {
console.error('Error occured during Data Source initialization', err)
})

const server = http.createServer(app)
const PORT = process.env.TYPEORM_SERVER_PORT;

server.listen(PORT, () => {
console.log(`Server is listening on Port ${PORT}`)
})
};

startServer();
13 changes: 8 additions & 5 deletions src/controllers/allowanceController.js
Original file line number Diff line number Diff line change
@@ -37,23 +37,23 @@ const getAllowancesByCondition = async (req, res) => { // 일반 유저도 가
const userId = await usersFamilyService.getAuthenticUserId(familyId, userName); // familyId 정보와 유저의 이름으로 유저 id를 찾습니다.
if (!year && !month) { // 연도, 월의 조건이 없는 경우 => 해당 유저의 용돈을 모두 찾습니다.
const allowances = await allowanceService.getAllowancesByUserId(userId);
return res.status(200).json({message: 'GET_SUCCESS', 'budget': allowances});
return res.status(200).json({message: 'GET_SUCCESS', allowances: allowances});
}
else if (year && !month) { // 연도 조건만 있고, 월 조건은 없는 경우 => 해당 유저의 해당 연도의 모든 용돈을 찾습니다..
const allowances = await allowanceService.getAllowancesByUserIdByYear(userId, year);
return res.status(200).json({message: 'GET_SUCCESS', 'budget': allowances});
return res.status(200).json({message: 'GET_SUCCESS', allowances: allowances});
}
const allowance = await allowanceService.getAllowanceByUserIdByYearMonth(userId, year, month); // 해당 유저의 해당 연, 월의 용돈을 찾습니다.
return res.status(200).json({message: 'GET_SUCCESS', allowances: allowance});
}
else {
if (!year && !month) { // 연도, 월의 조건이 없는 경우 => 가족 구성원의 용돈을 모두 찾습니다.
const allowances = await allowanceService.getAllowances(familyUsersIds);
return res.status(200).json({message: 'GET_SUCCESS', 'budget': allowances});
return res.status(200).json({message: 'GET_SUCCESS', allowances: allowances});
}
else if (year && !month) { // 연도 조건만 있고, 월 조건은 없는 경우 => 가족 구성원의 해당 연도의 모든 용돈을 찾습니다.
const allowances = await allowanceService.getAllowancesByYear(familyUsersIds, year);
return res.status(200).json({message: 'GET_SUCCESS', 'budget': allowances});
return res.status(200).json({message: 'GET_SUCCESS', allowances: allowances});
}
const allowances = await allowanceService.getAllowanceByUserIdByYearMonth(familyUsersIds, year, month); // 가족 구성원의 해당 연, 월의 용돈을 찾습니다.
return res.status(200).json({message: 'GET_SUCCESS', allowances: allowances});
@@ -77,7 +77,9 @@ const getRestAllowance = async (req, res) => {
}
const userId = await usersFamilyService.getAuthenticUserId(familyId, userName);
const allowance = await allowanceService.getAllowanceByUserIdByYearMonthAndGetAmount(userId, year, month); // 해당 유저의 해당 연, 월의 용돈을 찾습니다.
console.log('>>>>>>>>', allowance)
const sumOfUsage = await moneyFlowService.getUsedMoneyFlowsByYearMonthAndGetSum(userId, year, month);
console.log(sumOfUsage)
const restAllowance = allowance - sumOfUsage
return res.status(200).json({message: 'GET_SUCCESS', restAllowance: restAllowance});
} catch (err) {
@@ -93,6 +95,7 @@ const updateAllowance = async (req, res) => { // 관리자만 가능
error.throwErr(400, 'NOT_INCLUDED_IN_FAMILY_OR_NOT_AN_ADMIN');
}
const { userName, allowance, year, month } = req.body;
console.log(userName, allowance, year, month)
if (!userName || !allowance || !year || !month) {
error.throwErr(400, 'KEY_ERROR');
}
@@ -111,7 +114,7 @@ const deleteAllowance = async (req, res) => { // 관리자만 가능
if (!familyId || !roleId) {
error.throwErr(400, 'NOT_INCLUDED_IN_FAMILY_OR_NOT_AN_ADMIN');
}
const { userName, year, month } = req.body;
const { userName, year, month } = req.query;
if (!userName || !year || !month) {
error.throwErr(400, 'KEY_ERROR');
}
6 changes: 3 additions & 3 deletions src/models/allowanceDao.js
Original file line number Diff line number Diff line change
@@ -18,7 +18,7 @@ const postAllowance = async (userId, amount, year, month) => {
const getAllowance = async (userId) => { // 최신 순
return await appDataSource.query(
`
SELECT allowances.id, users.name as userName, allowances.amount, allowances.year, allowances.month
SELECT allowances.id, users.name as userName, allowances.amount as allowance as allowance, allowances.year, allowances.month
FROM allowances
JOIN users
ON allowances.user_id = users.id
@@ -32,7 +32,7 @@ const getAllowance = async (userId) => { // 최신 순
const getAllowanceByYear = async (userId, year) => { // 최신 순
return await appDataSource.query(
`
SELECT allowances.id, users.name as userName, allowances.amount, allowances.year, allowances.month
SELECT allowances.id, users.name as userName, allowances.amount as allowance, allowances.year, allowances.month
FROM allowances
JOIN users
ON allowances.user_id = users.id
@@ -47,7 +47,7 @@ const getAllowanceByYear = async (userId, year) => { // 최신 순
const getAllowanceByYearMonth = async (userId, year, month) => {
return await appDataSource.query(
`
SELECT allowances.id, users.name as userName, allowances.amount, allowances.year, allowances.month
SELECT allowances.id, users.name as userName, allowances.amount as allowance, allowances.year, allowances.month
FROM allowances
JOIN users
ON allowances.user_id = users.id
2 changes: 1 addition & 1 deletion src/services/allowanceService.js
Original file line number Diff line number Diff line change
@@ -46,7 +46,7 @@ const getAllowanceByUserIdByYearMonth = async (userId, year, month) => { // 단

const getAllowanceByUserIdByYearMonthAndGetAmount = async (userId, year, month) => {
const allowance = await allowanceDao.getAllowanceByYearMonth(userId, year, month);
return await allowance.reduce((acc, allowance) => acc + allowance.amount, 0);
return await allowance.reduce((acc, allowance) => acc + allowance.allowance, 0);
}

const updateAllowance = async (userId, allowance, year, month) => { // userName, year, month(수정 전)가 수정 전의 지표인 함수
16 changes: 3 additions & 13 deletions src/utils/dataSource.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { DataSource } = require('typeorm');
const {DataSource} = require('typeorm');
const dotenv = require('dotenv');
dotenv.config();

@@ -9,16 +9,6 @@ const appDataSource = new DataSource({
username: process.env.TYPEORM_USERNAME,
password: process.env.TYPEORM_PASSWORD,
database: process.env.TYPEORM_DATABASE
})
});

appDataSource.initialize()
.then(() => {
console.log('Data Source has been initialized!');
})
.catch((err) => {
console.error('Error occured during Data Source initialization', err);
})

module.exports = {
appDataSource
}
module.exports = { appDataSource }
2 changes: 1 addition & 1 deletion src/utils/error.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const throwErr = (code, message) => {
const error = new Error(message);
error.status = code;
error.statusCode = code;
throw error;
};

423 changes: 423 additions & 0 deletions test/flow.test.js

Large diffs are not rendered by default.

1,199 changes: 1,199 additions & 0 deletions test/testSupplies.js

Large diffs are not rendered by default.