Skip to content

Commit 0dadc3f

Browse files
swarajpureprakashchoudhary07Ankita2002-FrtapansawantCartmanishere
authored
Sync main with develop (#411)
* Removed generateUrl method * Define CRUD operation for feature flag * Added integration and unit tests for the feature flag * Generated api schema for feature flags * Added small changes in routes/featureFlags.js * added .js extensions * Update package-lock.json * Added wiki link to the backend setup video * Fixed all the validators to strictly follow the schema as it is by default converting the string to number * change self task api ready (#408) * api schema ready * checking for conflicts * minor line changes * api ready * unnecessary files deleted * percentCompleted field can also be changed now * status and percentCompleted made optional in validators * response code changes to 200 * using strint for validators Co-authored-by: Prakash <[email protected]> Co-authored-by: Ankita Bannore <[email protected]> Co-authored-by: tapansawant <[email protected]> Co-authored-by: Pranav Gajjewar <[email protected]> Co-authored-by: Ankush Dharkar <[email protected]> Co-authored-by: lakshayman <[email protected]> Co-authored-by: Akshay Shinde <[email protected]>
1 parent 95d4c00 commit 0dadc3f

28 files changed

+16146
-2433
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,6 @@ $ npm run validate-setup
4646
- Write JS Doc on top of your routes using YAML based annotations in OPEN API 3.0 format.
4747
- Run `npm run generate-api-schema` to generate the API schema. A file `public/apiSchema.json` will be created/updated.
4848

49-
Read more about contributing to the project: [CONTRIBUTING](CONTRIBUTING.md)
49+
Check out our video on how to setup the backend here: [Wiki link](https://github.com/Real-Dev-Squad/website-backend/wiki/Backend-setup-and-understanding-the-flow)
50+
51+
Read more about contributing to the project: [CONTRIBUTING](CONTRIBUTING.md)

controllers/featureFlags.js

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
const featureFlagQuery = require('../models/featureFlags')
2+
3+
/**
4+
* Fetches all the featureFlag
5+
*
6+
* @param req {Object} - Express request object
7+
* @param res {Object} - Express response object
8+
*/
9+
10+
const getFeatureFlags = async (req, res) => {
11+
try {
12+
const allFeatureFlags = await featureFlagQuery.fetchFeatureFlag()
13+
return res.json({
14+
message: 'FeatureFlags returned successfully!',
15+
featureflags: allFeatureFlags.length > 0 ? allFeatureFlags : []
16+
})
17+
} catch (err) {
18+
logger.error(`Error while fetching tasks ${err}`)
19+
return res.boom.badImplementation('An internal server error occurred')
20+
}
21+
}
22+
23+
/**
24+
* Posts the data of the featureFlag
25+
*
26+
* @param req {Object} - Express request object
27+
* @param res {Object} - Express response object
28+
*/
29+
30+
const addFeatureFlag = async (req, res) => {
31+
try {
32+
const featureFlag = await featureFlagQuery.addFeatureFlags(req.body, req.userData.username)
33+
return res.json({
34+
message: 'FeatureFlag added successfully!',
35+
data: featureFlag
36+
})
37+
} catch (err) {
38+
logger.error(`Error while adding featureFlag info: ${err}`)
39+
return res.boom.badImplementation('Something went wrong please contact admin')
40+
}
41+
}
42+
43+
/**
44+
* Update the data of the featureFlag
45+
*
46+
* @param req {Object} - Express request object
47+
* @param res {Object} - Express response object
48+
*/
49+
50+
const updateFeatureFlag = async (req, res) => {
51+
try {
52+
const result = await featureFlagQuery.updateFeatureFlags(req.body, req.params.id)
53+
if (result.isUpdated) {
54+
return res.status(204).send()
55+
}
56+
return res.boom.notFound('FeatureFlag doesn\'t exist')
57+
} catch (err) {
58+
logger.error(`Error while updating featureFlag info: ${err}`)
59+
return res.boom.badImplementation('Something went wrong please contact admin')
60+
}
61+
}
62+
63+
/**
64+
* Delete featureFlag
65+
*
66+
* @param req {Object} - Express request object
67+
* @param res {Object} - Express response object
68+
*/
69+
70+
const deleteFeatureFlag = async (req, res) => {
71+
try {
72+
const result = await featureFlagQuery.deleteFeatureFlag(req.params.id)
73+
if (result.isDeleted) {
74+
return res.json({
75+
message: 'FeatureFlag deleted successfully!!'
76+
})
77+
}
78+
return res.boom.notFound('featureFlag doesn\'t exist')
79+
} catch (err) {
80+
logger.error(`Error while deleting featureFlag info: ${err}`)
81+
return res.boom.badImplementation('Something went wrong please contact admin')
82+
}
83+
}
84+
85+
module.exports = {
86+
getFeatureFlags,
87+
addFeatureFlag,
88+
updateFeatureFlag,
89+
deleteFeatureFlag
90+
}

controllers/tasks.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,33 @@ const updateTask = async (req, res) => {
123123
}
124124
}
125125

126+
/**
127+
* Updates self task status
128+
* @param req {Object} - Express request object
129+
* @param res {Object} - Express response object
130+
*/
131+
const updateTaskStatus = async (req, res) => {
132+
try {
133+
const taskId = req.params.id
134+
const { id: userId } = req.userData
135+
const task = await tasks.fetchSelfTask(taskId, userId)
136+
137+
if (task.taskNotFound) return res.boom.notFound('Task doesn\'t exist')
138+
if (task.notAssignedToYou) return res.boom.forbidden('This task is not assigned to you')
139+
140+
await tasks.updateTask(req.body, taskId)
141+
return res.json({ message: 'Task updated successfully!' })
142+
} catch (err) {
143+
logger.error(`Error while updating task status : ${err}`)
144+
return res.boom.badImplementation('An internal server error occured')
145+
}
146+
}
147+
126148
module.exports = {
127149
addNewTask,
128150
fetchTasks,
129151
updateTask,
130152
getSelfTasks,
131-
getUserTasks
153+
getUserTasks,
154+
updateTaskStatus
132155
}

docs/swaggerDefinition.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,36 @@ const swaggerOptions = {
590590
}
591591
}
592592
},
593+
featureFlag: {
594+
type: 'object',
595+
properties: {
596+
name: {
597+
type: 'string'
598+
},
599+
id: {
600+
type: 'string'
601+
},
602+
title: {
603+
type: 'string'
604+
},
605+
created_at: {
606+
type: 'number'
607+
},
608+
updated_at: {
609+
type: 'number'
610+
},
611+
config: {
612+
type: 'object'
613+
},
614+
owner: {
615+
type: 'string'
616+
},
617+
launched_at: {
618+
type: 'number'
619+
}
620+
621+
}
622+
},
593623
errors: {
594624
unAuthorized: {
595625
type: 'object',

firebase.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88
"port": 4000
99
}
1010
}
11-
}
11+
}

middlewares/validators/auctions.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const joi = require('joi')
22

33
const createAuction = async (req, res, next) => {
4-
const schema = joi.object().keys({
4+
const schema = joi.object().strict().keys({
55
item_type: joi.string().required(),
66
quantity: joi.number().required(),
77
initial_price: joi.number().required(),
@@ -17,7 +17,7 @@ const createAuction = async (req, res, next) => {
1717
}
1818

1919
const placeBid = async (req, res, next) => {
20-
const schema = joi.object().keys({
20+
const schema = joi.object().strict().keys({
2121
bid: joi.number().required()
2222
})
2323
try {
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
const joi = require('joi')
2+
3+
const validateFeatureFlag = async (req, res, next) => {
4+
const schema = joi.object().strict().keys({
5+
name: joi.string().required(),
6+
title: joi.string().required(),
7+
created_at: joi.number().optional(),
8+
updated_at: joi.number().optional(),
9+
config: joi.object({
10+
enabled: joi.boolean().required()
11+
}).required(),
12+
launched_at: joi.number().optional()
13+
})
14+
15+
try {
16+
await schema.validateAsync(req.body)
17+
next()
18+
} catch (error) {
19+
logger.error(`Error in validating featureFlag data: ${error}`)
20+
res.boom.badRequest(error.details[0].message)
21+
}
22+
}
23+
24+
const updateFeatureFlags = async (req, res, next) => {
25+
const schema = joi.object().strict().keys({
26+
title: joi.string().optional(),
27+
config: joi.object({
28+
enabled: joi.boolean().required()
29+
}).required()
30+
})
31+
try {
32+
await schema.validateAsync(req.body)
33+
next()
34+
} catch (error) {
35+
logger.error(`Error in validating featureFlag data: ${error}`)
36+
res.boom.badRequest(error.details[0].message)
37+
}
38+
}
39+
40+
module.exports = {
41+
validateFeatureFlag,
42+
updateFeatureFlags
43+
}

middlewares/validators/recruiter.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const joi = require('joi')
22

33
const validateRecruiter = async (req, res, next) => {
4-
const schema = joi.object().keys({
4+
const schema = joi.object().strict().keys({
55
company: joi.string().required(),
66
first_name: joi.string().required(),
77
last_name: joi.string().required(),

middlewares/validators/stocks.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const joi = require('joi')
22

33
const createStock = async (req, res, next) => {
4-
const schema = joi.object().keys({
4+
const schema = joi.object().strict().keys({
55
name: joi.string().required(),
66
quantity: joi.number().required(),
77
price: joi.number().required()

middlewares/validators/tasks.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const joi = require('joi')
22
const { DINERO, NEELAM } = require('../../constants/wallets')
33

44
const createTask = async (req, res, next) => {
5-
const schema = joi.object().keys({
5+
const schema = joi.object().strict().keys({
66
title: joi.string().required(),
77
purpose: joi.string().optional(),
88
featureUrl: joi.string().optional(),
@@ -36,7 +36,7 @@ const createTask = async (req, res, next) => {
3636
}
3737

3838
const updateTask = async (req, res, next) => {
39-
const schema = joi.object().keys({
39+
const schema = joi.object().strict().keys({
4040
title: joi.string().optional(),
4141
purpose: joi.string().optional(),
4242
featureUrl: joi.string().optional(),
@@ -68,7 +68,22 @@ const updateTask = async (req, res, next) => {
6868
}
6969
}
7070

71+
const updateSelfTask = async (req, res, next) => {
72+
const schema = joi.object().strict().keys({
73+
status: joi.string().optional(),
74+
percentCompleted: joi.number().optional()
75+
})
76+
try {
77+
await schema.validateAsync(req.body)
78+
next()
79+
} catch (error) {
80+
logger.error(`Error validating updateSelfTask payload : ${error}`)
81+
res.boom.badRequest(error.details[0].message)
82+
}
83+
}
84+
7185
module.exports = {
7286
createTask,
73-
updateTask
87+
updateTask,
88+
updateSelfTask
7489
}

0 commit comments

Comments
 (0)