Skip to content

Commit 2a32209

Browse files
committed
1. Updated the node version to 16.x
2. Updated the codebase engine job to pick name from github secrets 3. Added the validator middlewares for the APIs
1 parent 80ab72b commit 2a32209

File tree

9 files changed

+129
-104
lines changed

9 files changed

+129
-104
lines changed

.github/job.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ const codeRatingModel = require('../models/codeRatingModel')
33
const mongoose = require('mongoose')
44
const fs = require('fs')
55

6+
require('dotenv').config()
7+
68
// function to read the code file and sends the username in callback function
79
const getUserNameFromCodeBaseFile = (fileName) => {
810
return new Promise((resolve, reject) => {
@@ -27,7 +29,6 @@ const getUserNameFromCodeBaseFile = (fileName) => {
2729

2830
// function to connect to mongodb using mongoose
2931
const connectMongoDB = () => {
30-
require('dotenv').config()
3132
mongoose.connect(process.env.MONGODB_URI, {
3233
useNewUrlParser: true,
3334
useUnifiedTopology: true,
@@ -58,13 +59,13 @@ const insertDataInMongo = (records) => {
5859
const callGithubCodeBaseAPI = () => {
5960
axios
6061
.get(
61-
'https://api.github.com/repos/TechOUs/HacktoberFest21Community/contents/CodeBase'
62+
`https://api.github.com/repos/TECHOUS/${process.env.CODE_SRC}/contents/CodeBase`
6263
)
6364
.then((res) => {
6465
// get all the names from the files
6566
const filePromises = res.data.map((codeObject) => {
6667
return getUserNameFromCodeBaseFile(
67-
`HacktoberFest21Community/${codeObject.path}`
68+
`${process.env.CODE_SRC}/${codeObject.path}`
6869
)
6970
})
7071

.github/job.sh

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
# /bin/bash
22
# this shell script it to read all the code from the community project and
33
# upload to mongodb run this script locally only from root
4-
# bash .github/job.sh
54

6-
git clone "https://github.com/TechOUs/HacktoberFest21Community.git"
5+
git clone "https://github.com/TECHOUS/$CODE_SRC.git"
76

87
node .github/job.js
98

109
# cleanup
1110
echo "Cleanup..."
12-
rm -rf HacktoberFest21Community
11+
rm -rf $CODE_SRC
1312
echo "Cleanup...Done"

.github/workflows/Vercel Deployment.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
- uses: actions/checkout@v2
1414
- uses: actions/setup-node@v2
1515
with:
16-
node-version: '14.x'
16+
node-version: '16.x'
1717
- run: yarn install
1818
- run: yarn run test
1919
env:

.github/workflows/codebase-engine.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ jobs:
99
- uses: actions/checkout@v2
1010
- uses: actions/setup-node@v2
1111
with:
12-
node-version: '14.x'
13-
- run: npm install
12+
node-version: '16.x'
13+
- run: yarn install
1414
- run: bash .github/job.sh
1515
env:
1616
MONGODB_URI: ${{ secrets.MONGODB_URI }}
1717
RATE_LIMIT_MINUTES: ${{ secrets.RATE_LIMIT_MINUTES }}
1818
RATE_LIMIT_MAX_REQUEST: ${{ secrets.RATE_LIMIT_MAX_REQUEST }}
19-
CACHE_STORAGE_SECONDS: ${{ secrets.CACHE_STORAGE_SECONDS }}
19+
CACHE_STORAGE_SECONDS: ${{ secrets.CACHE_STORAGE_SECONDS }}
20+
CODE_SRC: ${{secrets.CODE_SRC}}

.vercelignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,7 @@ node_modules
55
test
66
.env
77
.gitignore
8+
.prettierignore
9+
.prettierrc.json
810
README.md
9-
HacktoberFest21Community
11+
coderatefest

app.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,6 @@ app.use(express.json())
3434
app.use(express.urlencoded({extended: false}))
3535

3636
// middleware for handling sample api routes
37-
app.use('/api/v1', require('./routes/api/API'))
37+
app.use('/api/v1', require('./routes/api/v1/API'))
3838

3939
module.exports = app

package.json

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,42 @@
11
{
2-
"name": "coderatingengine",
3-
"version": "1.0.0",
4-
"engines": {
5-
"node": "14.x",
6-
"yarn": "1.x"
7-
},
8-
"description": "Engine to handle the rating mechanisms for the clean code",
9-
"main": "bin/www",
10-
"scripts": {
11-
"start": "node ./bin/www",
12-
"dev": "nodemon ./bin/www",
13-
"test": "mocha --exit --timeout 15000"
14-
},
15-
"author": "Gaurav Walia",
16-
"license": "ISC",
17-
"dependencies": {
18-
"axios": "^0.21.1",
19-
"cors": "^2.8.5",
20-
"express": "^4.17.1",
21-
"express-rate-limit": "^5.3.0",
22-
"mongoose": "^5.13.3",
23-
"uuid": "^8.3.2"
24-
},
25-
"devDependencies": {
26-
"dotenv": "^10.0.0",
27-
"husky": "^7.0.4",
28-
"lint-staged": "^12.3.8",
29-
"mocha": "^9.0.2",
30-
"nodemon": "^2.0.12",
31-
"prettier": "^2.6.2",
32-
"supertest": "^6.1.3"
33-
},
34-
"lint-staged": {
35-
"*.js": "prettier --write"
36-
},
37-
"husky":{
38-
"hooks": {
39-
"pre-commit": "lint-staged"
2+
"name": "coderatingengine",
3+
"version": "1.0.0",
4+
"engines": {
5+
"node": "16.x",
6+
"yarn": "1.x"
7+
},
8+
"description": "Engine to handle the rating mechanisms for the clean code",
9+
"main": "bin/www",
10+
"scripts": {
11+
"start": "node ./bin/www",
12+
"dev": "nodemon ./bin/www",
13+
"test": "mocha --exit --timeout 15000"
14+
},
15+
"author": "Gaurav Walia",
16+
"license": "ISC",
17+
"dependencies": {
18+
"axios": "^0.21.1",
19+
"cors": "^2.8.5",
20+
"express": "^4.17.1",
21+
"express-rate-limit": "^5.3.0",
22+
"mongoose": "^5.13.3",
23+
"uuid": "^8.3.2"
24+
},
25+
"devDependencies": {
26+
"dotenv": "^10.0.0",
27+
"husky": "^7.0.4",
28+
"lint-staged": "^12.3.8",
29+
"mocha": "^9.0.2",
30+
"nodemon": "^2.0.12",
31+
"prettier": "^2.6.2",
32+
"supertest": "^6.1.3"
33+
},
34+
"lint-staged": {
35+
"*.js": "prettier --write"
36+
},
37+
"husky": {
38+
"hooks": {
39+
"pre-commit": "lint-staged"
40+
}
4041
}
41-
}
4242
}

routes/api/API.js renamed to routes/api/v1/API.js

Lines changed: 37 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,18 @@ const {
99
getCodeBaseFileForUser,
1010
generateAndSaveToken,
1111
getCodeBaseFilesRating,
12-
} = require('../../src/engine')
13-
const {authenticateAPI} = require('../../src/auth')
12+
} = require('../../../src/engine')
1413
const {
1514
checkRandomCodesCacheExpiry,
1615
getRandomCodesFromCache,
1716
setRandomCodesInCache,
1817
checkSearchUserCacheExpiry,
1918
getSearchUserFromCache,
20-
setSearchUserInCache
21-
} = require('../../src/cache')
19+
setSearchUserInCache,
20+
} = require('../../../src/cache')
21+
// import middlewares
22+
const {authenticateAPI} = require('../../../src/auth')
23+
const {updateValidator, searchValidator} = require('../../../src/validator')
2224

2325
/**
2426
* @endpoint /api/v1/randomCodes
@@ -122,51 +124,35 @@ router.get('/randomCodes', async (req, res) => {
122124
* "message": ""
123125
* }
124126
**/
125-
router.put('/rateCode', authenticateAPI, async (req, res) => {
127+
router.put('/rateCode', authenticateAPI, updateValidator, async (req, res) => {
126128
const {codeId1, codeId2, winner} = req.body
127129

128-
if (
129-
codeId1 === undefined ||
130-
codeId2 === undefined ||
131-
winner === undefined
132-
) {
133-
res.status(400).json({
134-
status: 400,
135-
message: 'Invalid request parameters !!',
136-
})
137-
} else if (winner !== 1 && winner !== 2) {
138-
res.status(400).json({
130+
const codeBaseFiles = await getCodeBaseFilesRating(codeId1, codeId2)
131+
let codeRating1 = 0
132+
let codeRating2 = 0
133+
codeBaseFiles.map((codeBaseFile) => {
134+
if (codeBaseFile.codeId === codeId1) {
135+
codeRating1 = codeBaseFile.codeRating
136+
} else if (codeBaseFile.codeId === codeId2) {
137+
codeRating2 = codeBaseFile.codeRating
138+
}
139+
})
140+
const updateResult = await rateCodeAndUpdate({
141+
codeId1,
142+
codeId2,
143+
codeRating1,
144+
codeRating2,
145+
winner,
146+
}).catch((err) => {
147+
return res.status(400).json({
139148
status: 400,
140-
message: 'Winner can either be 1 or 2 only',
141-
})
142-
} else {
143-
const codeBaseFiles = await getCodeBaseFilesRating(codeId1, codeId2)
144-
let codeRating1 = 0
145-
let codeRating2 = 0
146-
codeBaseFiles.map((codeBaseFile) => {
147-
if (codeBaseFile.codeId === codeId1) {
148-
codeRating1 = codeBaseFile.codeRating
149-
} else if (codeBaseFile.codeId === codeId2) {
150-
codeRating2 = codeBaseFile.codeRating
151-
}
152-
})
153-
const updateResult = await rateCodeAndUpdate({
154-
codeId1,
155-
codeId2,
156-
codeRating1,
157-
codeRating2,
158-
winner,
159-
}).catch((err) => {
160-
return res.status(400).json({
161-
status: 400,
162-
message: 'Bad Request!!!',
163-
})
149+
message: 'Bad Request!!!',
164150
})
151+
})
165152

166-
updateResult.status = 200
167-
updateResult.message = 'Code Ratings are updated'
168-
res.status(200).json(updateResult)
169-
}
153+
updateResult.status = 200
154+
updateResult.message = 'Code Ratings are updated'
155+
res.status(200).json(updateResult)
170156
})
171157

172158
/**
@@ -187,15 +173,13 @@ router.put('/rateCode', authenticateAPI, async (req, res) => {
187173
* userCodeBaseFiles: []
188174
* }
189175
**/
190-
router.get('/searchUser', authenticateAPI, async (req, res) => {
191-
const {username, sendContent} = req.query
176+
router.get(
177+
'/searchUser',
178+
authenticateAPI,
179+
searchValidator,
180+
async (req, res) => {
181+
const {username, sendContent} = req.query
192182

193-
if (username === undefined) {
194-
res.status(400).json({
195-
status: 400,
196-
message: 'Bad Request !! Please send username in the query',
197-
})
198-
} else {
199183
// send the cache data according to username and sendContent
200184
if (checkSearchUserCacheExpiry(username, sendContent)) {
201185
return res.status(200).json(getSearchUserFromCache())
@@ -251,6 +235,6 @@ router.get('/searchUser', authenticateAPI, async (req, res) => {
251235
res.status(200).json(getSearchUserFromCache())
252236
}
253237
}
254-
})
238+
)
255239

256240
module.exports = router

src/validator.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
function updateValidator(req, res, next) {
2+
const {codeId1, codeId2, winner} = req.body
3+
4+
if (
5+
typeof codeId1 !== 'string' ||
6+
typeof codeId2 !== 'string' ||
7+
typeof winner !== 'number'
8+
) {
9+
res.status(400).json({
10+
status: 400,
11+
message: 'Invalid request parameters !!',
12+
})
13+
} else if (winner !== 1 && winner !== 2) {
14+
res.status(400).json({
15+
status: 400,
16+
message: 'Winner can either be 1 or 2 only',
17+
})
18+
} else {
19+
next()
20+
}
21+
}
22+
23+
function searchValidator(req, res, next) {
24+
const {username} = req.query
25+
if (username === undefined) {
26+
res.status(400).json({
27+
status: 400,
28+
message: 'Bad Request !! Please send username in the query',
29+
})
30+
} else {
31+
next()
32+
}
33+
}
34+
35+
module.exports = {
36+
updateValidator,
37+
searchValidator,
38+
}

0 commit comments

Comments
 (0)