Skip to content

Commit a6587cc

Browse files
committed
Add new endpoint GET /taas-teams/me
1 parent 187d79d commit a6587cc

File tree

6 files changed

+146
-20
lines changed

6 files changed

+146
-20
lines changed

docs/Topcoder-bookings-api.postman_collection.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4656,6 +4656,36 @@
46564656
}
46574657
},
46584658
"response": []
4659+
},
4660+
{
4661+
"name": "GET /taas-teams/me",
4662+
"request": {
4663+
"method": "GET",
4664+
"header": [
4665+
{
4666+
"key": "Authorization",
4667+
"type": "text",
4668+
"value": "Bearer {{token_member}}"
4669+
}
4670+
],
4671+
"url": {
4672+
"raw": "{{URL}}/taas-teams/me?enrich=true",
4673+
"host": [
4674+
"{{URL}}"
4675+
],
4676+
"path": [
4677+
"taas-teams",
4678+
"me"
4679+
],
4680+
"query": [
4681+
{
4682+
"key": "enrich",
4683+
"value": "true"
4684+
}
4685+
]
4686+
}
4687+
},
4688+
"response": []
46594689
}
46604690
]
46614691
},

docs/swagger.yaml

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1881,6 +1881,59 @@ paths:
18811881
application/json:
18821882
schema:
18831883
$ref: '#/components/schemas/Error'
1884+
1885+
/taas-teams/me:
1886+
get:
1887+
tags:
1888+
- Teams
1889+
description: |
1890+
Return details about the current user.
1891+
security:
1892+
- bearerAuth: []
1893+
parameters:
1894+
- in: query
1895+
name: enrich
1896+
required: false
1897+
schema:
1898+
type: boolean
1899+
description: whether to return enriched data or not.
1900+
responses:
1901+
'200':
1902+
description: OK
1903+
content:
1904+
application/json:
1905+
schema:
1906+
$ref: '#/components/schemas/UbahnUser'
1907+
'400':
1908+
description: Bad request
1909+
content:
1910+
application/json:
1911+
schema:
1912+
$ref: '#/components/schemas/Error'
1913+
'401':
1914+
description: Not authenticated
1915+
content:
1916+
application/json:
1917+
schema:
1918+
$ref: '#/components/schemas/Error'
1919+
'403':
1920+
description: Forbidden
1921+
content:
1922+
application/json:
1923+
schema:
1924+
$ref: '#/components/schemas/Error'
1925+
'404':
1926+
description: Not Found
1927+
content:
1928+
application/json:
1929+
schema:
1930+
$ref: '#/components/schemas/Error'
1931+
'500':
1932+
description: Internal Server Error
1933+
content:
1934+
application/json:
1935+
schema:
1936+
$ref: '#/components/schemas/Error'
18841937
/health:
18851938
get:
18861939
tags:
@@ -2697,10 +2750,13 @@ components:
26972750
example: 'xxx@xxx.com'
26982751
ProjectMember:
26992752
type: object
2700-
example: {"id": 14329, "userId": 40159097, "role": "customer", "createdAt": "2021-02-24T12:34:45.074Z", "updatedAt": "2021-02-24T12:34:45.075Z", "createdBy": -101, "updatedBy": -101, "handle": "tester1234", "photoURL": null, "workingHourStart": "9:00", "workingHourEnd": "17:00", "timeZone": "Asia/Kolkata", "email": "sathya.jayabal@gmail.com"}
2753+
example: {"id": 14329, "userId": 40159097, "role": "customer", "createdAt": "2021-02-24T12:34:45.074Z", "updatedAt": "2021-02-24T12:34:45.075Z", "createdBy": -101, "updatedBy": -101, "handle": "tester1234", "photoURL": null, "workingHourStart": "9:00", "workingHourEnd": "17:00", "timeZone": "Asia/Kolkata", "email": "xxx@xxx.com"}
27012754
ProjectMemberInvite:
27022755
type: object
27032756
example: {"createdAt": "2021-02-24T11:02:12.673Z", "deletedAt": null, "role": "customer", "updatedBy": -101, "createdBy": -101, "id": 3686, "projectId": 16705, "userId": 23008602, "email": null, "deletedBy": null, "updatedAt": "2021-02-24T11:02:12.674Z", "status": "pending"}
2757+
UbahnUser:
2758+
type: object
2759+
example: {"lastName": "DeLaurentis", "updatedBy": "tcAdmin", "achievements": [{"certifierId": "certifierId", "updatedBy": "tcAdmin", "createdBy": "tc-user", "certifiedDate": "2020-05-04T07:36:28.036Z", "created": "2020-05-13T08:44:27.244Z", "name": "Topcoder", "id": "a49e1013-fd42-4c08-bc12-492510cadb96", "achievementsProviderId": "ce05133f-129e-484d-9ef9-72bf51ff81f9", "uri": "http://www.google.com/xx", "updated": "2021-01-05T10:58:32.429Z", "userId": "0bcb0d86-09bb-410a-b2b1-fba90d1a7699", "achievementprovider": {"updatedBy": "tcAdmin", "createdBy": "tc-user", "created": "2020-05-13T08:42:41.877Z", "name": "achievementsProviders_02", "id": "ce05133f-129e-484d-9ef9-72bf51ff81f9", "updated": "2021-01-05T10:58:32.341Z"}}], "created": "2020-05-05T10:18:03.882Z", "handle": "lazybaer", "skills": [{"certifierId": null, "skillId": "d67f35c3-fa42-4866-a0f9-0a4b84fcf4a9", "updatedBy": "tcAdmin", "createdBy": "lazybaer", "certifiedDate": null, "created": "2020-10-23T16:22:11.208Z", "skill": {"updatedBy": "tcAdmin", "skillprovider": {"updatedBy": "tcAdmin", "createdBy": "TonyJ", "created": "2020-08-31T12:30:00.543Z", "name": "Wipro Digital", "id": "26fb37b1-5f9f-4727-baa9-f3c87de84ab1", "updated": "2021-01-05T10:58:32.836Z"}, "createdBy": "0", "created": "2020-09-01T21:59:21.554Z", "skillProviderId": "26fb37b1-5f9f-4727-baa9-f3c87de84ab1", "name": "GitHub", "externalId": null, "id": "d67f35c3-fa42-4866-a0f9-0a4b84fcf4a9", "uri": null, "updated": "2021-01-05T10:58:33.332Z"}, "metricValue": null, "id": "8a84c1b4-1884-4a3c-90b2-eb86bf469bb6", "updated": "2021-01-05T10:58:34.080Z", "userId": "0bcb0d86-09bb-410a-b2b1-fba90d1a7699"}], "firstName": "Christopher", "externalProfiles": [{"organizationId": "0d2320f9-be61-4ba4-973e-edc3bb682a69", "updatedBy": "tcAdmin", "createdBy": "TonyJ", "isInactive": false, "created": "2020-08-31T12:30:38.495Z", "organization": {"updatedBy": "tcAdmin", "createdBy": "TonyJ", "created": "2020-08-31T12:29:58.081Z", "name": "Wipro Digital", "skillProviders": [{"organizationId": "0d2320f9-be61-4ba4-973e-edc3bb682a69", "updatedBy": "tcAdmin", "createdBy": "TonyJ", "created": "2020-08-31T12:30:08.410Z", "skillProviderId": "26fb37b1-5f9f-4727-baa9-f3c87de84ab1", "id": "5b26cdd3-fe68-4b30-85c5-ceaf280bd688", "updated": "2021-01-05T10:58:32.919Z"}], "id": "0d2320f9-be61-4ba4-973e-edc3bb682a69", "updated": "2021-01-05T10:58:32.261Z"}, "externalId": "8547899", "id": "870af97b-8c3b-4659-92d6-cac126bbe9de", "uri": null, "updated": "2021-01-05T10:58:32.724Z", "userId": "0bcb0d86-09bb-410a-b2b1-fba90d1a7699"}], "createdBy": "tc-Copilot", "attributes": [{"attributeId": "d709276a-80c3-491c-9b29-a4f065b2a56f", "updatedBy": "tcAdmin", "createdBy": "tc-Admin", "created": "2020-05-13T08:19:13.709Z", "id": "21de9324-900d-41ea-b127-f297dfb9a873", "attribute": {"updatedBy": "tcAdmin", "attributegroup": {"organizationId": "36ed815b-3da1-49f1-a043-aaed0a4e81ad", "updatedBy": "tc-Admin", "createdBy": "tc-Admin", "created": "2020-05-13T07:15:01.215Z", "name": "group 03", "id": "84634bbd-8191-40cf-a03e-9962d7e39fda", "updated": "2020-05-13T07:16:20.636Z"}, "createdBy": "tc-Admin", "created": "2020-05-13T07:32:03.128Z", "name": "Billing Account", "id": "d709276a-80c3-491c-9b29-a4f065b2a56f", "attributeGroupId": "84634bbd-8191-40cf-a03e-9962d7e39fda", "updated": "2021-01-05T10:58:32.604Z"}, "value": "74314457", "updated": "2021-01-05T10:58:33.739Z", "userId": "0bcb0d86-09bb-410a-b2b1-fba90d1a7699"}], "id": "0bcb0d86-09bb-410a-b2b1-fba90d1a7699", "updated": "2021-01-05T10:58:32.113Z"}
27042760
Error:
27052761
required:
27062762
- message

src/common/helper.js

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -595,17 +595,18 @@ function encodeQueryString (queryObj, nesting = '') {
595595
}
596596

597597
/**
598-
* Function to get user ids
599-
* @param {Integer} userId user id from jwt token
600-
* @returns {String} user id.
598+
* Function to list users by external id.
599+
* @param {Integer} externalId the legacy user id
600+
* @param {Boolean} enrich whether to return enriched data or not
601+
* @returns {Array} the users found
601602
*/
602-
async function getUserIds (userId) {
603+
async function listUsersByExternalId (externalId, enrich = false) {
603604
const token = await getM2MUbahnToken()
604605
const q = {
605-
enrich: true,
606+
enrich,
606607
externalProfile: {
607608
organizationId: config.ORG_ID,
608-
externalId: userId
609+
externalId
609610
}
610611
}
611612
const url = `${config.TC_API}/users?${encodeQueryString(q)}`
@@ -614,21 +615,22 @@ async function getUserIds (userId) {
614615
.set('Authorization', `Bearer ${token}`)
615616
.set('Content-Type', 'application/json')
616617
.set('Accept', 'application/json')
617-
localLogger.debug({ context: 'getUserIds', message: `response body: ${JSON.stringify(res.body)}` })
618+
localLogger.debug({ context: 'listUserByExternalId', message: `response body: ${JSON.stringify(res.body)}` })
618619
return res.body
619620
}
620621

621622
/**
622-
* Function to get user id
623-
* @param {Integer} userId user id from jwt token
624-
* @returns {String} user id.
623+
* Function to get user by external id.
624+
* @param {Integer} externalId the legacy user id
625+
* @param {Boolean} enrich whether to return enriched data or not
626+
* @returns {Object} the user
625627
*/
626-
async function getUserId (userId) {
627-
const ids = await getUserIds(userId)
628-
if (_.isEmpty(ids)) {
629-
throw new errors.NotFoundError(`userId: ${userId} "user" not found`)
628+
async function getUserByExternalId (externalId, enrich) {
629+
const users = await listUsersByExternalId(externalId, enrich)
630+
if (_.isEmpty(users)) {
631+
throw new errors.NotFoundError(`externalId: ${externalId} "user" not found`)
630632
}
631-
return ids[0].id
633+
return users[0]
632634
}
633635

634636
/**
@@ -880,7 +882,7 @@ async function getSkillById (skillId) {
880882
}
881883

882884
/**
883-
* Encapsulate the getUserId function.
885+
* Encapsulate the getUserByExternalId function.
884886
* Make sure a user exists in ubahn(/v5/users) and return the id of the user.
885887
*
886888
* In the case the user does not exist in /v5/users but can be found in /v3/users
@@ -891,7 +893,7 @@ async function getSkillById (skillId) {
891893
*/
892894
async function ensureUbahnUserId (currentUser) {
893895
try {
894-
return await getUserId(currentUser.userId)
896+
return (await getUserByExternalId(currentUser.userId, true)).id
895897
} catch (err) {
896898
if (!(err instanceof errors.NotFoundError)) {
897899
throw err
@@ -1135,6 +1137,7 @@ module.exports = {
11351137
}
11361138
return ensureUbahnUserId({ userId })
11371139
},
1140+
getUserByExternalId,
11381141
getM2MToken,
11391142
getM2MUbahnToken,
11401143
postEvent,

src/controllers/TeamController.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,15 @@ async function deleteMember (req, res) {
8383
res.status(HttpStatus.NO_CONTENT).end()
8484
}
8585

86+
/**
87+
* Return details about the current user.
88+
* @param req the request
89+
* @param res the response
90+
*/
91+
async function getMe (req, res) {
92+
res.send(await service.getMe(req.authUser, req.query))
93+
}
94+
8695
module.exports = {
8796
searchTeams,
8897
getTeam,
@@ -91,5 +100,6 @@ module.exports = {
91100
addMembers,
92101
searchMembers,
93102
searchInvites,
94-
deleteMember
103+
deleteMember,
104+
getMe
95105
}

src/routes/TeamRoutes.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ module.exports = {
2828
scopes: [constants.Scopes.READ_TAAS_TEAM]
2929
}
3030
},
31+
'/taas-teams/me': {
32+
get: {
33+
controller: 'TeamController',
34+
method: 'getMe',
35+
auth: 'jwt',
36+
scopes: [constants.Scopes.READ_TAAS_TEAM]
37+
}
38+
},
3139
'/taas-teams/:id': {
3240
get: {
3341
controller: 'TeamController',

src/services/TeamService.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,24 @@ deleteMember.schema = Joi.object().keys({
513513
projectMemberId: Joi.number().integer().required()
514514
}).required()
515515

516+
/**
517+
* Return details about the current user.
518+
*
519+
* @param {Object} currentUser the user who perform this operation.
520+
* @params {Object} criteria the search criteria
521+
* @returns {Object} the user data for current user
522+
*/
523+
async function getMe (currentUser, criteria) {
524+
return helper.getUserByExternalId(currentUser.userId, criteria.enrich)
525+
}
526+
527+
getMe.schema = Joi.object().keys({
528+
currentUser: Joi.object().required(),
529+
criteria: Joi.object().keys({
530+
enrich: Joi.boolean()
531+
}).required()
532+
}).required()
533+
516534
module.exports = {
517535
searchTeams,
518536
getTeam,
@@ -521,5 +539,6 @@ module.exports = {
521539
addMembers,
522540
searchMembers,
523541
searchInvites,
524-
deleteMember
542+
deleteMember,
543+
getMe
525544
}

0 commit comments

Comments
 (0)