Skip to content

Commit 13acb25

Browse files
YiFeiZhang2pierreTklein
authored andcommitted
Feature/sponsor get self (#299)
* Create /self route for sponsor * docs and add a authentication test * Fix PR comments * Add roles in post_roles to respect the tiers of sponsors
1 parent f4a6b31 commit 13acb25

File tree

10 files changed

+346
-32
lines changed

10 files changed

+346
-32
lines changed

constants/general.constant.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,11 @@ const REQUEST_TYPES = {
6969
//Define names of the roles specifically associated with permission to create an account
7070
const POST_ROLES = {};
7171
POST_ROLES[HACKER] = "postHacker";
72-
POST_ROLES[SPONSOR] = "postSponsor";
72+
POST_ROLES[SPONSOR_T1] = "postSponsor";
73+
POST_ROLES[SPONSOR_T2] = "postSponsor";
74+
POST_ROLES[SPONSOR_T3] = "postSponsor";
75+
POST_ROLES[SPONSOR_T4] = "postSponsor";
76+
POST_ROLES[SPONSOR_T5] = "postSponsor";
7377
POST_ROLES[VOLUNTEER] = "postVolunteer";
7478
POST_ROLES[STAFF] = "postStaff";
7579

constants/role.constant.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ const sponsorT1Role = {
6969
"routes": [
7070
Constants.Routes.sponsorRoutes.post,
7171
Constants.Routes.sponsorRoutes.getSelfById,
72+
Constants.Routes.sponsorRoutes.getSelf,
7273
]
7374
};
7475

@@ -78,6 +79,7 @@ const sponsorT2Role = {
7879
"routes": [
7980
Constants.Routes.sponsorRoutes.post,
8081
Constants.Routes.sponsorRoutes.getSelfById,
82+
Constants.Routes.sponsorRoutes.getSelf,
8183
]
8284
};
8385

@@ -87,6 +89,7 @@ const sponsorT3Role = {
8789
"routes": [
8890
Constants.Routes.sponsorRoutes.post,
8991
Constants.Routes.sponsorRoutes.getSelfById,
92+
Constants.Routes.sponsorRoutes.getSelf,
9093
]
9194
};
9295

@@ -96,6 +99,7 @@ const sponsorT4Role = {
9699
"routes": [
97100
Constants.Routes.sponsorRoutes.post,
98101
Constants.Routes.sponsorRoutes.getSelfById,
102+
Constants.Routes.sponsorRoutes.getSelf,
99103
]
100104
};
101105

@@ -105,6 +109,7 @@ const sponsorT5Role = {
105109
"routes": [
106110
Constants.Routes.sponsorRoutes.post,
107111
Constants.Routes.sponsorRoutes.getSelfById,
112+
Constants.Routes.sponsorRoutes.getSelf,
108113
]
109114
};
110115

constants/routes.constant.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ const hackerRoutes = {
127127
};
128128

129129
const sponsorRoutes = {
130+
"getSelf": {
131+
requestType: Constants.REQUEST_TYPES.GET,
132+
uri: "/api/sponsor/self/",
133+
},
130134
"getSelfById": {
131135
requestType: Constants.REQUEST_TYPES.GET,
132136
uri: "/api/sponsor/" + Constants.ROLE_CATEGORIES.SELF,

docs/api/api_data.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1797,6 +1797,67 @@ define({
17971797
"url": "https://api.mchacks.ca/api/hacker/resume/:id"
17981798
}]
17991799
},
1800+
{
1801+
"type": "get",
1802+
"url": "/sponsor/self",
1803+
"title": "get information about logged in sponsor",
1804+
"name": "self",
1805+
"group": "Hacker",
1806+
"version": "1.4.1",
1807+
"success": {
1808+
"fields": {
1809+
"Success 200": [{
1810+
"group": "Success 200",
1811+
"type": "String",
1812+
"optional": false,
1813+
"field": "message",
1814+
"description": "<p>Success message</p>"
1815+
},
1816+
{
1817+
"group": "Success 200",
1818+
"type": "Object",
1819+
"optional": false,
1820+
"field": "data",
1821+
"description": "<p>Sponsor object</p>"
1822+
}
1823+
]
1824+
},
1825+
"examples": [{
1826+
"title": "Success-Response: ",
1827+
"content": "{\n \"message\": \"Successfully retrieved sponsor information\", \n \"data\": {...}\n }",
1828+
"type": "object"
1829+
}]
1830+
},
1831+
"error": {
1832+
"fields": {
1833+
"Error 4xx": [{
1834+
"group": "Error 4xx",
1835+
"type": "String",
1836+
"optional": false,
1837+
"field": "message",
1838+
"description": "<p>Error message</p>"
1839+
},
1840+
{
1841+
"group": "Error 4xx",
1842+
"type": "Object",
1843+
"optional": false,
1844+
"field": "data",
1845+
"description": "<p>empty</p>"
1846+
}
1847+
]
1848+
},
1849+
"examples": [{
1850+
"title": "Error-Response: ",
1851+
"content": "{\"message\": \"Sponsor not found\", \"data\": {}}",
1852+
"type": "object"
1853+
}]
1854+
},
1855+
"filename": "routes/api/sponsor.js",
1856+
"groupTitle": "Hacker",
1857+
"sampleRequest": [{
1858+
"url": "https://api.mchacks.ca/api/sponsor/self"
1859+
}]
1860+
},
18001861
{
18011862
"type": "get",
18021863
"url": "/hacker/self",

docs/api/api_data.json

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1796,6 +1796,67 @@
17961796
"url": "https://api.mchacks.ca/api/hacker/resume/:id"
17971797
}]
17981798
},
1799+
{
1800+
"type": "get",
1801+
"url": "/sponsor/self",
1802+
"title": "get information about logged in sponsor",
1803+
"name": "self",
1804+
"group": "Hacker",
1805+
"version": "1.4.1",
1806+
"success": {
1807+
"fields": {
1808+
"Success 200": [{
1809+
"group": "Success 200",
1810+
"type": "String",
1811+
"optional": false,
1812+
"field": "message",
1813+
"description": "<p>Success message</p>"
1814+
},
1815+
{
1816+
"group": "Success 200",
1817+
"type": "Object",
1818+
"optional": false,
1819+
"field": "data",
1820+
"description": "<p>Sponsor object</p>"
1821+
}
1822+
]
1823+
},
1824+
"examples": [{
1825+
"title": "Success-Response: ",
1826+
"content": "{\n \"message\": \"Successfully retrieved sponsor information\", \n \"data\": {...}\n }",
1827+
"type": "object"
1828+
}]
1829+
},
1830+
"error": {
1831+
"fields": {
1832+
"Error 4xx": [{
1833+
"group": "Error 4xx",
1834+
"type": "String",
1835+
"optional": false,
1836+
"field": "message",
1837+
"description": "<p>Error message</p>"
1838+
},
1839+
{
1840+
"group": "Error 4xx",
1841+
"type": "Object",
1842+
"optional": false,
1843+
"field": "data",
1844+
"description": "<p>empty</p>"
1845+
}
1846+
]
1847+
},
1848+
"examples": [{
1849+
"title": "Error-Response: ",
1850+
"content": "{\"message\": \"Sponsor not found\", \"data\": {}}",
1851+
"type": "object"
1852+
}]
1853+
},
1854+
"filename": "routes/api/sponsor.js",
1855+
"groupTitle": "Hacker",
1856+
"sampleRequest": [{
1857+
"url": "https://api.mchacks.ca/api/sponsor/self"
1858+
}]
1859+
},
17991860
{
18001861
"type": "get",
18011862
"url": "/hacker/self",

docs/api/api_project.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
define({
2-
"name": "hackerAPI",
3-
"version": "0.0.8",
4-
"description": "Documentation for the API used for mchacks",
5-
"defaultVersion": "0.0.8",
6-
"title": "hackerAPI documentation",
7-
"url": "https://api.mchacks.ca/api",
8-
"sampleUrl": "https://api.mchacks.ca/api",
9-
"apidoc": "0.3.0",
10-
"generator": {
11-
"name": "apidoc",
12-
"time": "2019-01-10T23:24:21.462Z",
13-
"url": "http://apidocjs.com",
14-
"version": "0.17.7"
15-
}
1+
define({
2+
"name": "hackerAPI",
3+
"version": "0.0.8",
4+
"description": "Documentation for the API used for mchacks",
5+
"defaultVersion": "0.0.8",
6+
"title": "hackerAPI documentation",
7+
"url": "https://api.mchacks.ca/api",
8+
"sampleUrl": "https://api.mchacks.ca/api",
9+
"apidoc": "0.3.0",
10+
"generator": {
11+
"name": "apidoc",
12+
"time": "2019-01-18T19:02:31.941Z",
13+
"url": "http://apidocjs.com",
14+
"version": "0.17.7"
15+
}
1616
});

docs/api/api_project.json

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
{
2-
"name": "hackerAPI",
3-
"version": "0.0.8",
4-
"description": "Documentation for the API used for mchacks",
5-
"defaultVersion": "0.0.8",
6-
"title": "hackerAPI documentation",
7-
"url": "https://api.mchacks.ca/api",
8-
"sampleUrl": "https://api.mchacks.ca/api",
9-
"apidoc": "0.3.0",
10-
"generator": {
11-
"name": "apidoc",
12-
"time": "2019-01-10T23:24:21.462Z",
13-
"url": "http://apidocjs.com",
14-
"version": "0.17.7"
15-
}
1+
{
2+
"name": "hackerAPI",
3+
"version": "0.0.8",
4+
"description": "Documentation for the API used for mchacks",
5+
"defaultVersion": "0.0.8",
6+
"title": "hackerAPI documentation",
7+
"url": "https://api.mchacks.ca/api",
8+
"sampleUrl": "https://api.mchacks.ca/api",
9+
"apidoc": "0.3.0",
10+
"generator": {
11+
"name": "apidoc",
12+
"time": "2019-01-18T19:02:31.941Z",
13+
"url": "http://apidocjs.com",
14+
"version": "0.17.7"
15+
}
1616
}

middlewares/sponsor.middleware.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,39 @@ async function validateConfirmedStatus(req, res, next) {
8787
}
8888
}
8989

90+
/**
91+
* Finds the sponsor information of the logged in user
92+
* @param {{user: {id: string, accountType: string}}} req
93+
* @param {*} res
94+
* @param {(err?)=>void} next
95+
*/
96+
async function findSelf(req, res, next) {
97+
if (!Constants.General.SPONSOR_TIERS.includes(req.user.accountType)) {
98+
return next({
99+
status: 409,
100+
message: Constants.Error.ACCOUNT_TYPE_409_MESSAGE,
101+
error: {
102+
id: req.user.id
103+
}
104+
});
105+
}
106+
107+
const sponsor = await Services.Sponsor.findByAccountId(req.user.id);
108+
109+
if (!!sponsor) {
110+
req.body.sponsor = sponsor;
111+
return next();
112+
} else {
113+
return next({
114+
status: 404,
115+
message: Constants.Error.SPONSOR_404_MESSAGE,
116+
error: {
117+
id: req.user.id
118+
}
119+
});
120+
}
121+
}
122+
90123
/**
91124
* @async
92125
* @function findById
@@ -157,6 +190,7 @@ async function checkDuplicateAccountLinks(req, res, next) {
157190
module.exports = {
158191
parsePatch: parsePatch,
159192
parseSponsor: parseSponsor,
193+
findSelf: Middleware.Util.asyncMiddleware(findSelf),
160194
findById: Middleware.Util.asyncMiddleware(findById),
161195
createSponsor: Middleware.Util.asyncMiddleware(createSponsor),
162196
checkDuplicateAccountLinks: Middleware.Util.asyncMiddleware(checkDuplicateAccountLinks),

routes/api/sponsor.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,41 @@ module.exports = {
2525
activate: function (apiRouter) {
2626
const sponsorRouter = new express.Router();
2727

28+
/**
29+
* @api {get} /sponsor/self get information about logged in sponsor
30+
* @apiName self
31+
* @apiGroup Hacker
32+
* @apiVersion 1.4.1
33+
*
34+
* @apiSuccess {String} message Success message
35+
* @apiSuccess {Object} data Sponsor object
36+
* @apiSuccessExample {object} Success-Response:
37+
* {
38+
"message": "Successfully retrieved sponsor information",
39+
"data": {
40+
"id": "5bff4d736f86be0a41badb91",
41+
"accountId": "5bff4d736f86be0a41badb99",
42+
"tier": 3,
43+
"company": "companyName",
44+
"contractURL": "https://www.contractHere.com",
45+
"nominees": ["5bff4d736f86be0a41badb93","5bff4d736f86be0a41badb94"]
46+
}
47+
}
48+
49+
* @apiError {String} message Error message
50+
* @apiError {Object} data empty
51+
* @apiErrorExample {object} Error-Response:
52+
* {"message": "Sponsor not found", "data": {}}
53+
* @apiPermission: Sponsor
54+
*/
55+
sponsorRouter.route("/self").get(
56+
Middleware.Auth.ensureAuthenticated(),
57+
Middleware.Auth.ensureAuthorized(),
58+
59+
Middleware.Sponsor.findSelf,
60+
Controllers.Sponsor.showSponsor
61+
);
62+
2863
/**
2964
* @api {get} /sponsor/:id get a sponsor's information
3065
* @apiName getSponsor

0 commit comments

Comments
 (0)