Skip to content

Commit 8907b7e

Browse files
authored
Merge pull request #54 from 9roomMoa/refactor6
[REFACTOR#6] 스키마 수정 및 우선순위 변경 + 인증 미들웨어 모든 라우터에 적용
2 parents e44a96e + 6c09af9 commit 8907b7e

15 files changed

Lines changed: 93 additions & 61 deletions

src/controllers/comment-controller.js

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ const commentValidation = require('../validation/comment-validation.js');
44

55
exports.searchComments = async (req, res) => {
66
try {
7-
const { userId } = req.body;
87
const { tid } = req.params;
98
const { keyword } = req.query;
9+
const userId = req.user?.sub;
1010

1111
if (!userId || !tid) {
1212
return res.status(StatusCodes.BAD_REQUEST).json({
@@ -15,12 +15,12 @@ exports.searchComments = async (req, res) => {
1515
});
1616
}
1717

18-
if (!keyword || keyword.trim() === '') {
19-
return res.status(StatusCodes.BAD_REQUEST).json({
20-
success: false,
21-
message: 'there is no keyword',
22-
});
23-
}
18+
// if (!keyword || keyword.trim() === '') {
19+
// return res.status(StatusCodes.BAD_REQUEST).json({
20+
// success: false,
21+
// message: 'there is no keyword',
22+
// });
23+
// }
2424

2525
const comments = await commentService.searchComments(keyword, tid, userId);
2626

@@ -52,6 +52,7 @@ exports.createComment = async (req, res) => {
5252
const { error, value } = commentValidation.creatingSchema.validate(
5353
req.body
5454
);
55+
const userId = req.user?.sub;
5556

5657
if (!tid) {
5758
return res.status(StatusCodes.BAD_REQUEST).json({
@@ -67,7 +68,7 @@ exports.createComment = async (req, res) => {
6768
});
6869
}
6970

70-
const commentData = { taskId: tid, ...value };
71+
const commentData = { commenterId: userId, taskId: tid, ...value };
7172

7273
const comment = await commentService.createComment(commentData);
7374

@@ -88,7 +89,7 @@ exports.createComment = async (req, res) => {
8889
exports.getComments = async (req, res) => {
8990
try {
9091
const { tid } = req.params;
91-
const { userId } = req.body;
92+
const userId = req.user?.sub;
9293

9394
if (!userId || !tid) {
9495
return res.status(StatusCodes.BAD_REQUEST).json({
@@ -119,9 +120,9 @@ exports.updateComment = async (req, res) => {
119120
const { error, value } = commentValidation.updatingSchema.validate(
120121
req.body
121122
);
123+
const userId = req.user?.sub;
122124

123125
if (!tid || !cid) {
124-
console.log(tid, cid);
125126
return res.status(StatusCodes.BAD_REQUEST).json({
126127
success: false,
127128
message: 'taskId or commentId omitted',
@@ -137,7 +138,7 @@ exports.updateComment = async (req, res) => {
137138
});
138139
}
139140

140-
const { userId, ...updateData } = value;
141+
const updateData = value;
141142
const result = await commentService.updateComment(
142143
userId,
143144
tid,
@@ -162,7 +163,8 @@ exports.updateComment = async (req, res) => {
162163
exports.deleteComment = async (req, res) => {
163164
try {
164165
const { tid } = req.params;
165-
const { userId, commentId } = req.body;
166+
const { commentId } = req.body;
167+
const userId = req.user?.sub;
166168

167169
if (!tid || !userId) {
168170
res.status(StatusCodes.BAD_REQUEST).json({

src/controllers/docs-controller.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@ const docsService = require('../services/docs-service');
44
exports.postDocument = async (req, res) => {
55
try {
66
const { tid } = req.params;
7-
const { userId } = req.body;
7+
const { userId } = req.user?.sub;
88

99
if (!tid || !userId) {
1010
return res.status(StatusCodes.BAD_REQUEST).json({
1111
success: false,
1212
message: 'TaskId or userId omitted',
1313
});
1414
}
15-
console.log('✅ Received File:', req.file);
1615

1716
if (!req.file) {
1817
return res.status(StatusCodes.BAD_REQUEST).json({
@@ -40,7 +39,7 @@ exports.postDocument = async (req, res) => {
4039
exports.getDocuments = async (req, res) => {
4140
try {
4241
const { tid } = req.params;
43-
const { userId } = req.body;
42+
const userId = req.user?.sub;
4443

4544
if (!tid) {
4645
return res.status(StatusCodes.BAD_REQUEST).json({
@@ -83,14 +82,23 @@ exports.getDocuments = async (req, res) => {
8382
exports.downloadDocument = async (req, res) => {
8483
try {
8584
const { did } = req.params;
85+
const userId = req.user?.sub;
86+
if (!userId) {
87+
return res.status(StatusCodes.BAD_REQUEST).json({
88+
success: false,
89+
message: 'userId omitted',
90+
});
91+
}
8692
if (!did) {
8793
return res.status(StatusCodes.BAD_REQUEST).json({
8894
success: false,
8995
message: 'documentId omitted',
9096
});
9197
}
9298

93-
return await docsService.downloadDocument(did, res);
99+
const data = { userId, did };
100+
101+
return await docsService.downloadDocument(data, res);
94102
} catch (err) {
95103
console.error('❌ Error in downloadDocument Controller:', err.message);
96104
return res.status(StatusCodes.INTERNAL_SERVER_ERROR).json({
@@ -104,7 +112,7 @@ exports.searchDocuments = async (req, res) => {
104112
try {
105113
const { tid } = req.params;
106114
const { keyword } = req.query;
107-
const { userId } = req.body;
115+
const userId = req.user?.sub;
108116

109117
if (!tid) {
110118
return res.status(StatusCodes.BAD_REQUEST).json({

src/controllers/project-controller.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ exports.createProject = async (req, res) => {
1212
if (error) {
1313
return res.status(StatusCodes.BAD_REQUEST).json({
1414
success: false,
15-
message: 'Invalid input data: ' + error.details,
15+
message:
16+
'Invalid input data: ' +
17+
error.details.map((d) => d.message).join(', '),
1618
});
1719
}
1820

1921
const projectData = {
20-
createdBy: req.user?.id || value.createdBy,
22+
createdBy: req.user?.sub,
2123
...value,
2224
};
2325

@@ -52,7 +54,8 @@ exports.patchProject = async (req, res) => {
5254
const { error, value } =
5355
projectValidation.updateProjectValidationSchema.validate(req.body);
5456

55-
const { userId, ...updateData } = value;
57+
const userId = req.user?.sub;
58+
const updateData = value;
5659

5760
if (error) {
5861
return res.status(StatusCodes.BAD_REQUEST).json({

src/middlewares/verify-token.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ exports.verifyToken = (req, res, next) => {
1313
try {
1414
const decoded = jwt.verify(token, process.env.JWT_SECRET);
1515
req.user = decoded;
16-
console.log(req.user);
1716
next();
1817
} catch (err) {
1918
console.error(err.message);

src/models/Project.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ const projectSchema = new mongoose.Schema(
1313
startDate: { type: Date, required: true },
1414
dueDate: { type: Date, required: true },
1515
scope: { type: String, enum: ['Public', 'Team'], default: 'Team' },
16+
priority: { type: Number, min: 0, max: 4, default: 0 },
1617
status: {
1718
type: String,
18-
enum: ['To Do', 'In Progress', 'Done'],
19+
enum: ['To Do', 'In Progress', 'Done', 'Issue'],
1920
default: 'To Do',
2021
},
2122
},

src/models/Task.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ const taskSchema = new mongoose.Schema(
66
description: { type: String, required: true },
77
status: {
88
type: String,
9-
enum: ['To Do', 'In Progress', 'Done'],
9+
enum: ['To Do', 'In Progress', 'Done', 'Issue'],
1010
default: 'To Do',
1111
},
1212
priority: {
1313
type: Number,
14-
min: 1,
15-
max: 5,
16-
default: 5,
14+
min: 0,
15+
max: 4,
16+
default: 0,
1717
},
1818
project: {
1919
type: mongoose.Schema.Types.ObjectId,

src/models/User.js

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,8 @@ const userSchema = new mongoose.Schema(
3333
role: { type: String, enum: ['Admin', 'Member'], default: 'Member' },
3434
rank: {
3535
type: String,
36-
enum: [
37-
'Intern',
38-
'Junior',
39-
'Mid',
40-
'Senior',
41-
'Lead',
42-
'Manager',
43-
'Director',
44-
'VP',
45-
'CEO',
46-
], // 직급 정의
47-
default: 'Junior', // 기본값 설정
36+
enum: ['인턴', '사원', '주임', '대리', '과장', '차장', '부장', '팀장'], // 직급 정의
37+
default: '인턴', // 기본값 설정
4838
},
4939
},
5040
{ timestamps: true }

src/routes/docs-route.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@ const express = require('express');
22

33
const router = express.Router();
44
const docsController = require('../controllers/docs-controller');
5+
const authMiddleware = require('../middlewares/verify-token');
56

6-
router.get('/downloads/:did', docsController.downloadDocument);
7+
router.get(
8+
'/downloads/:did',
9+
authMiddleware.verifyToken,
10+
docsController.downloadDocument
11+
);
712

813
module.exports = router;

src/routes/project-route.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
const express = require('express');
22
const router = express.Router();
33
const projectController = require('../controllers/project-controller');
4+
const authMiddleware = require('../middlewares/verify-token');
45

5-
router.post('/', projectController.createProject);
6+
router.post('/', authMiddleware.verifyToken, projectController.createProject);
67

7-
router.patch('/:pid', projectController.patchProject);
8+
router.patch(
9+
'/:pid',
10+
authMiddleware.verifyToken,
11+
projectController.patchProject
12+
);
813

914
module.exports = router;

src/services/comment-service.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ exports.searchComments = async (keyword, taskId, userId) => {
1010
if (!isAccessible) {
1111
throw new Error('You dont have privilege to access to comments');
1212
}
13-
console.log(keyword);
13+
1414
const comments = await Comment.find({
1515
taskId: taskId,
1616
content: { $regex: keyword, $options: 'i' },
@@ -25,6 +25,18 @@ exports.searchComments = async (keyword, taskId, userId) => {
2525

2626
exports.createComment = async (commentData) => {
2727
try {
28+
const isExistingTask = await taskUtil.isExistingResource(
29+
Task,
30+
commentData.taskId
31+
);
32+
if (!isExistingTask) {
33+
throw new Error('Invalid TaskId');
34+
}
35+
if (
36+
!(await taskUtil.scopeChecker(commentData.commenterId, isExistingTask))
37+
) {
38+
throw new Error('You dont have privilege to create comment');
39+
}
2840
if (commentData.parentId) {
2941
const isChildComment = await Comment.findById(commentData.parentId);
3042
if (!isChildComment) {

0 commit comments

Comments
 (0)