-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from osamhack2022-v2/web_orgtree
Pouring Web orgtree into Web for finishing organizational tree backend setting
- Loading branch information
Showing
13 changed files
with
434 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
const asyncHandler = require("express-async-handler"); | ||
const UnitM = require("../models/unitModel"); | ||
const Chart = require("../models/chartModel"); | ||
|
||
//@description Get organizational chart as array list | ||
//@route GET /api/chart | ||
//@access Protected | ||
const getChart = asyncHandler(async (req, res) => { | ||
const currentUser = req.user; | ||
const currentUnit = await UnitM.findOne({ _id: currentUser.Unit }); | ||
|
||
if (!currentUnit) { | ||
res.status(400); | ||
throw new Error('사용자의 부대가 설정되지 않았습니다.'); | ||
} | ||
|
||
const unitOrganization = await Chart.find({ Unit: { $eq: currentUnit.Unitname } }); | ||
if (unitOrganization.length === 0) { | ||
await Chart.create({ | ||
Name: currentUser.Name, | ||
Rank: currentUser.Rank, | ||
Unit: currentUnit.Unitname | ||
}) | ||
} | ||
res.send(unitOrganization); | ||
}); | ||
|
||
//@description Add organizational chart | ||
//@route POST /api/chart/add | ||
//@access Protected | ||
const addChart = asyncHandler(async (req, res) => { | ||
const { | ||
Name, | ||
Rank, | ||
Unit, | ||
Position, | ||
DoDID, | ||
Number, | ||
MilNumber, | ||
Email, | ||
Parent | ||
} = req.body; | ||
|
||
if (!Name || !Rank || !Unit) { | ||
res.status(400); | ||
throw new Error("모든 정보를 입력하세요."); | ||
} | ||
|
||
const newChart = await Chart.create({ | ||
Name, | ||
Rank, | ||
Unit, | ||
Position, | ||
DoDID, | ||
Number, | ||
MilNumber, | ||
Email, | ||
Parent, | ||
}); | ||
|
||
if (newChart) { | ||
res.status(201).json({ | ||
_id: newChart._id, | ||
Name, | ||
Rank, | ||
Unit, | ||
Position, | ||
DoDID, | ||
Number, | ||
MilNumber, | ||
Email, | ||
Parent: Parent ?? null, | ||
}) | ||
} | ||
else { | ||
res.send(400); | ||
throw new Error('조직도를 추가/수정할 수 없습니다.') | ||
} | ||
}); | ||
|
||
//@description Edit organizational chart | ||
//@route POST /api/chart/edit | ||
//@access Protected | ||
const editChart = asyncHandler(async (req, res) => { | ||
const { | ||
_id, | ||
Name, | ||
Rank, | ||
Unit, | ||
Position, | ||
DoDID, | ||
Number, | ||
MilNumber, | ||
Email, | ||
Parent | ||
} = req.body; | ||
|
||
if (!_id || !Name || !Rank || !Unit) { | ||
res.status(400); | ||
throw new Error("모든 정보를 입력하세요."); | ||
} | ||
|
||
const updatedChart = await Chart.findByIdAndUpdate(_id, { | ||
Name, | ||
Rank, | ||
Position, | ||
DoDID, | ||
Number, | ||
MilNumber, | ||
Email, | ||
Parent, | ||
Unit | ||
}); | ||
|
||
if (updatedChart) { | ||
res.status(200).json({ | ||
Name, | ||
Rank, | ||
Position, | ||
DoDID, | ||
Number, | ||
MilNumber, | ||
Email, | ||
Parent, | ||
Unit | ||
}) | ||
} | ||
else { | ||
res.send(400); | ||
throw new Error('조직도를 추가/수정할 수 없습니다.') | ||
} | ||
}); | ||
|
||
//@description Delete organizational chart | ||
//@route POST /api/chart/delete | ||
//@access Protected | ||
const deleteChart = asyncHandler(async (req, res) => { | ||
const { _id } = req.body; | ||
|
||
if (!_id) { | ||
res.status(400); | ||
throw new Error("모든 정보를 입력하세요."); | ||
} | ||
|
||
const deleteChart = await Chart.findByIdAndDelete(_id); | ||
if (deleteChart) { | ||
res.send(200); | ||
} | ||
else { | ||
res.send(400); | ||
throw new Error('조직도를 추가/수정할 수 없습니다.') | ||
} | ||
}); | ||
|
||
module.exports = { | ||
getChart, | ||
addChart, | ||
editChart, | ||
deleteChart | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
const mongoose = require("mongoose"); | ||
|
||
const chartModel = mongoose.Schema({ | ||
Name: { | ||
type: String, | ||
required: true | ||
}, | ||
Rank: { | ||
type: String, | ||
required: true | ||
}, | ||
Unit: { | ||
type: String, | ||
required: true | ||
}, | ||
Position: { | ||
type: String | ||
}, | ||
DoDID: { | ||
type: String | ||
}, | ||
Number: { | ||
type: String | ||
}, | ||
MilNumber: { | ||
type: String | ||
}, | ||
Email: { | ||
type: String | ||
}, | ||
Parent: { | ||
type: String, | ||
default: null | ||
} | ||
}, { | ||
timestamps: true | ||
}); | ||
|
||
const Chart = mongoose.model("Chart", chartModel); | ||
|
||
module.exports = Chart; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
const express = require("express"); | ||
const { | ||
getChart, | ||
addChart, | ||
editChart, | ||
deleteChart | ||
} = require('../controllers/chartController'); | ||
const { | ||
protect, | ||
onlyAdmin | ||
} = require("../middleware/authMiddleware"); | ||
|
||
const router = express.Router(); | ||
|
||
router.route("/").get(protect, getChart); | ||
router.route("/add").post(onlyAdmin, addChart); | ||
router.route("/edit").post(onlyAdmin, editChart); | ||
router.route("/delete").post(onlyAdmin, deleteChart); | ||
|
||
module.exports = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.