Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 4 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ DATABASE_HOST=
DATABASE_NAME=
DATABASE_PASSWORD=
DATABASE_PORT=
DATABASE_USER=
DATABASE_USER=
S3_ACCESS_ID=
S3_SECRET_KEY=
S3_REGION=
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@
},
"homepage": "https://github.com/loverduck/Code-High#readme",
"devDependencies": {
"@types/aws-sdk": "^2.7.0",
"@types/axios": "^0.14.0",
"@types/bcrypt": "^5.0.0",
"@types/cookie-parser": "^1.4.2",
"@types/cors": "^2.8.12",
"@types/dotenv": "^8.2.0",
"@types/express": "^4.17.13",
"@types/jsonwebtoken": "^8.5.5",
"@types/multer": "^1.4.7",
"@types/multer-s3": "^2.7.10",
"@types/node": "^16.9.1",
"@types/node-schedule": "^1.3.2",
"@types/nodemailer": "^6.4.4",
Expand All @@ -37,6 +40,7 @@
"typescript": "^4.4.3"
},
"dependencies": {
"aws-sdk": "^2.999.0",
"axios": "^0.21.4",
"bcrypt": "^5.0.1",
"cookie-parser": "^1.4.5",
Expand All @@ -45,6 +49,8 @@
"express": "^4.17.1",
"jsonwebtoken": "^8.5.1",
"moment": "^2.29.1",
"multer": "^1.4.3",
"multer-s3": "^2.9.0",
"mysql": "^2.18.1",
"node-schedule": "^2.0.0",
"nodemailer": "^6.6.3",
Expand Down
4 changes: 4 additions & 0 deletions scripts/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,9 @@ export GOOGLE_CLIENT_SECRET=$(aws ssm get-parameters --region ap-northeast-2 --n
export KAKAO_CLIENT_ID=$(aws ssm get-parameters --region ap-northeast-2 --names KAKAO_CLIENT_ID --query Parameters[0].Value | sed 's/"//g')
export KAKAO_CLIENT_SECRET=$(aws ssm get-parameters --region ap-northeast-2 --names KAKAO_CLIENT_SECRET --query Parameters[0].Value | sed 's/"//g')

export S3_ACCESS_ID=$(aws ssm get-parameters --region ap-northeast-2 --names S3_ACCESS_ID --query Parameters[0].Value | sed 's/"//g')
export S3_SECRET_KEY=$(aws ssm get-parameters --region ap-northeast-2 --names S3_SECRET_KEY --query Parameters[0].Value | sed 's/"//g')
export S3_REGION=$(aws ssm get-parameters --region ap-northeast-2 --names S3_REGION --query Parameters[0].Value | sed 's/"//g')

npm run build
authbind --deep pm2 start dist/src/index.js
33 changes: 29 additions & 4 deletions src/controllers/dashboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ const userActiveStat = async (req: Request, res: Response) => {

// 일별 코드 작성 현황
const dateStat = async (req: Request, res: Response) => {
if (req.body.userRole !== 1) {
return res.status(403).send({ message: 'forbidden user'});
}

const today = dateFormat.today();
const fourDayAgo = dateFormat.calculateDate(-4);
const day = await Stat.find({ date: Between(fourDayAgo, today)});
Expand Down Expand Up @@ -52,11 +56,31 @@ const dateStat = async (req: Request, res: Response) => {
res.status(200).send({ stat });
}

const weekStat = async (req: Request, res: Response) => {
if (req.body.userRole !== 1) {
return res.status(403).send({ message: 'forbidden user'});
}

const week = await Stat.createQueryBuilder()
.select(['CONCAT(YEAR(date), \'-\', Month(date), \' \', WEEK(date)) AS date'])
.groupBy('CONCAT(YEAR(date), \'-\', Month(date), \' \', WEEK(date))')
.getRawMany()

console.log(week)


res.status(200).send('test');
}

const monthStat = async (req: Request, res: Response) => {
if (req.body.userRole !== 1) {
return res.status(403).send({ message: 'forbidden user'});
}

const month = await Stat.createQueryBuilder()
.select(['date', 'SUM(postCount) AS postCount', 'SUM(commentCount) AS commentCount', 'SUM(joinCount) AS joinCount', 'SUM(visitCount) AS visitCount'])
.groupBy('MONTH(date)')
.orderBy('date', 'DESC')
.select(['CONCAT(YEAR(date), \'-\', Month(date)) AS date', 'SUM(postCount) AS postCount', 'SUM(commentCount) AS commentCount', 'SUM(joinCount) AS joinCount', 'SUM(visitCount) AS visitCount'])
.groupBy('CONCAT(YEAR(date), \'-\', Month(date))')
.orderBy('MONTH(date)', 'DESC')
.limit(4)
.getRawMany()

Expand All @@ -71,7 +95,7 @@ const monthStat = async (req: Request, res: Response) => {
}

month.forEach((el) => {
stat.days.push(`${el.date.getMonth() + 1}월`);
stat.days.push(el.date);
stat.postCount.push(el.postCount);
stat.commentCount.push(el.commentCount);
stat.joinCount.push(el.joinCount);
Expand All @@ -84,5 +108,6 @@ const monthStat = async (req: Request, res: Response) => {
export {
userActiveStat,
dateStat,
weekStat,
monthStat
}
16 changes: 16 additions & 0 deletions src/controllers/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,21 @@ const editUser = async (req: Request, res: Response) => {
res.status(200).send({ userInfo: updateInfo, message: 'update success' })
}

const editImage = async (req: Request, res: Response) => {
if (req.body.userRole > 3 ) {
return res.status(403).send({ message: 'forbidden user'})
}

if (!req.file) {
return res.status(422).send('fail');
}

const file: any = req.file;
await User.update({ id: req.body.authUserId}, { image: file.location })

res.status(200).send({ message: 'upload success'})
}

const resetPassword = async (req: Request, res: Response) => {
let password = req.body.password;
if (!req.body.code || !password) {
Expand Down Expand Up @@ -112,5 +127,6 @@ export {
userInfo,
userInfoById,
editUser,
editImage,
deleteUser
};
1 change: 1 addition & 0 deletions src/routes/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ adminRouter.use('/', checkAuth);
adminRouter.use('/', checkRole);

adminRouter.get('/stat/date', dashboardController.dateStat);
adminRouter.get('/stat/week', dashboardController.weekStat);
adminRouter.get('/stat/month', dashboardController.monthStat);

export default adminRouter;
4 changes: 4 additions & 0 deletions src/routes/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ import * as postController from '../controllers/post';
import * as dashboardController from '../controllers/dashboard';
import { checkAuth } from '../middleware/checkAuth'
import { checkRole } from '../middleware/checkRole';
import { upload } from '../utils/multer';

const userRouter = Router();

userRouter.post('/image', upload.single('image'), checkAuth, checkRole, userController.editImage)

userRouter.use('/', checkAuth);
userRouter.use('/', checkRole);

Expand Down
26 changes: 26 additions & 0 deletions src/utils/multer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import * as multer from 'multer';
import * as multerS3 from 'multer-s3';
import * as aws from 'aws-sdk';
import { Request } from 'express';
import 'dotenv/config';

aws.config.update({
"accessKeyId": process.env.S3_ACCESS_ID,
"secretAccessKey": process.env.S3_SECRET_KEY,
"region": process.env.S3_REGION
})

const s3 = new aws.S3();

export const upload = multer({
storage: multerS3({
s3: s3,
contentType: multerS3.AUTO_CONTENT_TYPE,
bucket: 'code-high-image',
acl: 'public-read',

key: (req: Request, file, cb) => {
cb(null, Date.now() + '.' + file.originalname.split('.').pop());
}
})
});
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"sourceMap": true
}
},
//"exclude": ["uploads/"]
}