Skip to content

Commit

Permalink
Merge pull request #229 from TogetherCrew/227-refinement-and-enhancem…
Browse files Browse the repository at this point in the history
…ent-of-api-endpoints-for-roles-and-member-breakdown

227 refinement and enhancement of api endpoints for roles and member breakdown
  • Loading branch information
cyri113 authored Oct 7, 2023
2 parents 03387d0 + aff3eeb commit 5e731ef
Show file tree
Hide file tree
Showing 9 changed files with 313 additions and 47 deletions.
160 changes: 155 additions & 5 deletions __tests__/integration/guild.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -647,26 +647,50 @@ describe('Guild routes', () => {
beforeEach(async () => {
await connection.dropDatabase();
});
test('should return 200 and array of roles of the guild if data is ok', async () => {
test('should return 200 and apply the default query options', async () => {
await insertUsers([userOne]);
await insertGuilds([guildFive]);
await insertRoles([role1, role2, role3, role4], connection);

const res = await request(app)
.get(`/api/v1/guilds/${guildFive.guildId}/roles`)
.set('Authorization', `Bearer ${userOneAccessToken}`)
.send()
.expect(httpStatus.OK);

expect(res.body).toHaveLength(3);
expect(res.body[0].roleId).toBe(role1.roleId);
expect(res.body[1].roleId).toBe(role2.roleId);
expect(res.body[2].roleId).toBe(role3.roleId);

expect(res.body).toEqual({
results: expect.any(Array),
page: 1,
limit: 10,
totalPages: 1,
totalResults: 3,
});
expect(res.body.results).toHaveLength(3);

expect(res.body.results[0]).toEqual({
roleId: role1.roleId,
name: role1.name,
color: role1.color,
});
expect(res.body.results[1]).toEqual({
roleId: role3.roleId,
name: role3.name,
color: role3.color,
});

expect(res.body.results[2]).toEqual({
roleId: role2.roleId,
name: role2.name,
color: role2.color,
});
})

test('should return 401 if access token is missing', async () => {
await insertUsers([userOne]);
await request(app)
.get(`/api/v1/guilds/${guildFive.guildId}/roles`)
.send()
.expect(httpStatus.UNAUTHORIZED);
})
test('should return 400 if guild id is not valid', async () => {
Expand All @@ -684,6 +708,132 @@ describe('Guild routes', () => {
.send()
.expect(440);
})

test('should correctly apply filter on name field', async () => {
await insertUsers([userOne]);
await insertGuilds([guildFive]);
await insertRoles([role1, role2, role3, role4], connection);

const res = await request(app)
.get(`/api/v1/guilds/${guildFive.guildId}/roles`)
.set('Authorization', `Bearer ${userOneAccessToken}`)
.query({ name: "Member" })
.send()
.expect(httpStatus.OK);


expect(res.body).toEqual({
results: expect.any(Array),
page: 1,
limit: 10,
totalPages: 1,
totalResults: 1,
});
expect(res.body.results).toHaveLength(1);
expect(res.body.results[0].roleId).toBe(role3.roleId);

})

test('should correctly sort the returned array if descending sort param is specified', async () => {
await insertUsers([userOne]);
await insertGuilds([guildFive]);
await insertRoles([role1, role2, role3, role4], connection);

const res = await request(app)
.get(`/api/v1/guilds/${guildFive.guildId}/roles`)
.set('Authorization', `Bearer ${userOneAccessToken}`)
.query({ sortBy: 'name:desc' })
.send()
.expect(httpStatus.OK);


expect(res.body).toEqual({
results: expect.any(Array),
page: 1,
limit: 10,
totalPages: 1,
totalResults: 3,
});
expect(res.body.results).toHaveLength(3);
expect(res.body.results[0].roleId).toBe(role2.roleId);
expect(res.body.results[1].roleId).toBe(role3.roleId);
expect(res.body.results[2].roleId).toBe(role1.roleId);
})

test('should correctly sort the returned array if ascending sort param is specified', async () => {
await insertUsers([userOne]);
await insertGuilds([guildFive]);
await insertRoles([role1, role2, role3, role4], connection);

const res = await request(app)
.get(`/api/v1/guilds/${guildFive.guildId}/roles`)
.set('Authorization', `Bearer ${userOneAccessToken}`)
.query({ sortBy: 'name:asc' })
.send()
.expect(httpStatus.OK);


expect(res.body).toEqual({
results: expect.any(Array),
page: 1,
limit: 10,
totalPages: 1,
totalResults: 3,
});
expect(res.body.results).toHaveLength(3);
expect(res.body.results[0].roleId).toBe(role1.roleId);
expect(res.body.results[1].roleId).toBe(role3.roleId);
expect(res.body.results[2].roleId).toBe(role2.roleId);
})

test('should limit returned array if limit param is specified', async () => {
await insertUsers([userOne]);
await insertGuilds([guildFive]);
await insertRoles([role1, role2, role3, role4], connection);

const res = await request(app)
.get(`/api/v1/guilds/${guildFive.guildId}/roles`)
.set('Authorization', `Bearer ${userOneAccessToken}`)
.query({ limit: 2 })
.send()
.expect(httpStatus.OK);


expect(res.body).toEqual({
results: expect.any(Array),
page: 1,
limit: 2,
totalPages: 2,
totalResults: 3,
});
expect(res.body.results).toHaveLength(2);
expect(res.body.results[0].roleId).toBe(role1.roleId);
expect(res.body.results[1].roleId).toBe(role3.roleId);
})

test('should correctly sort the returned array if page and limit are specified', async () => {
await insertUsers([userOne]);
await insertGuilds([guildFive]);
await insertRoles([role1, role2, role3, role4], connection);

const res = await request(app)
.get(`/api/v1/guilds/${guildFive.guildId}/roles`)
.set('Authorization', `Bearer ${userOneAccessToken}`)
.query({ page: 2, limit: 2 })
.send()
.expect(httpStatus.OK);


expect(res.body).toEqual({
results: expect.any(Array),
page: 2,
limit: 2,
totalPages: 2,
totalResults: 3,
});
expect(res.body.results).toHaveLength(1);
expect(res.body.results[0].roleId).toBe(role2.roleId);
})
})

});
6 changes: 3 additions & 3 deletions __tests__/integration/memberActivity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -366,13 +366,13 @@ describe('member-activity routes', () => {
expect(res.body).toHaveLength(2)
expect(res.body).toEqual(expect.arrayContaining([({
from: { avatar: null, id: "123456789", discordId: "123456789", joinedAt: "2023-03-07T00:00:00.000Z", ngu: "Behzad", radius: 3, roles: [], stats: "SENDER", username: "behzad_rabiei" },
to: { avatar: "AvatarLink", id: "987654321", discordId: '987654321', joinedAt: "2023-03-31T00:00:00.000Z", ngu: "Daniel", radius: 3, roles: [], stats: "RECEIVER", username: "mrjackalop"},
to: { avatar: "AvatarLink", id: "987654321", discordId: '987654321', joinedAt: "2023-03-31T00:00:00.000Z", ngu: "Daniel", radius: 3, roles: [], stats: "RECEIVER", username: "mrjackalop" },
width: 1
})
]))
expect(res.body).toEqual(expect.arrayContaining([({
from: { avatar: "AvatarLink", id: "987654321", discordId: "987654321", joinedAt: "2023-03-31T00:00:00.000Z", ngu: "Daniel", radius: 3, roles: [], stats: "RECEIVER", username: "mrjackalop"},
to: { avatar: null, id: "123456789", discordId: '123456789', joinedAt: "2023-03-07T00:00:00.000Z", ngu: "Behzad", radius: 3, roles: [], stats: "SENDER", username: "behzad_rabiei"},
from: { avatar: "AvatarLink", id: "987654321", discordId: "987654321", joinedAt: "2023-03-31T00:00:00.000Z", ngu: "Daniel", radius: 3, roles: [], stats: "RECEIVER", username: "mrjackalop" },
to: { avatar: null, id: "123456789", discordId: '123456789', joinedAt: "2023-03-07T00:00:00.000Z", ngu: "Behzad", radius: 3, roles: [], stats: "SENDER", username: "behzad_rabiei" },
width: 2
})
]))
Expand Down
6 changes: 3 additions & 3 deletions __tests__/integration/twitter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ describe('Twitter routes', () => {
MERGE (t2) -[:MENTIONED {createdAt: ${threeDaysAgoTimestamp}}] -> (a2)
MERGE (t3) -[:MENTIONED {createdAt: ${fourDaysAgoTimestamp}}] -> (a2)
`

await insertUsers([userOne]);
await Neo4j.write("match (n) detach delete (n);")
await Neo4j.write(numberOfPostsMockData)
Expand All @@ -123,13 +123,14 @@ describe('Twitter routes', () => {
.get(`/api/v1/twitter/metrics/activity`)
.set('Authorization', `Bearer ${userOneAccessToken}`)
.expect(httpStatus.OK);

expect(res.body.posts).toEqual(7)
expect(res.body.replies).toEqual(0)
expect(res.body.retweets).toEqual(0)
expect(res.body.likes).toEqual(1)
expect(res.body.mentions).toEqual(2)
})


test("should return 400 if user has not connected his Twitter account yet!", async () => {
await insertUsers([userTwo]);
Expand Down Expand Up @@ -378,7 +379,6 @@ describe('Twitter routes', () => {
.set('Authorization', `Bearer ${userOneAccessToken}`)
.expect(httpStatus.OK);

console.log(res.body)
expect(res.body.hqla).toEqual(2)
expect(res.body.hqhe).toEqual(0)
expect(res.body.lqla).toEqual(0)
Expand Down
4 changes: 3 additions & 1 deletion src/controllers/guild.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ const getRoles = catchAsync(async function (req: IAuthRequest, res: Response) {
if (! await guildService.getGuild({ guildId: req.params.guildId, user: req.user.discordId })) {
throw new ApiError(440, 'Oops, something went wrong! Could you please try logging in');
}
const filter = pick(req.query, ['name']);
const options = pick(req.query, ['sortBy', 'limit', 'page']);
const connection = databaseService.connectionFactory(req.params.guildId, config.mongoose.botURL);
const roles = await roleService.getRoles(connection, { deletedAt: null });
const roles = await roleService.QueryRoles(connection, { deletedAt: null, ...filter }, options);
await closeConnection(connection)
res.send(roles)
});
Expand Down
1 change: 1 addition & 0 deletions src/controllers/memberActivity.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ const activeMembersCompositionTable = catchAsync(async function (req: IAuthReque
const activityCompostionFields = memberActivityService.getActivityCompositionOfActiveMembersComposition()
const memberActivity = await memberActivityService.getLastDocumentForTablesUsage(connection, activityCompostionFields);
const guildMembers = await guildMemberService.queryGuildMembers(connection, filter, options, memberActivity, activityCompostionsTypes.activeMembersComposition);

const roles = await roleService.getRoles(connection, {});
if (guildMembers) {
guildMembers.results.forEach((guildMember) => {
Expand Down
85 changes: 58 additions & 27 deletions src/docs/guild.doc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,7 @@ paths:
get:
tags:
- [Guild]
summary: Get guild roles from discord api
description: Used [this](https://discord.com/developers/docs/resources/guild#get-guild-roles) Discord API
summary: Get guild roles
security:
- bearerAuth: []
parameters:
Expand All @@ -227,28 +226,69 @@ paths:
schema:
type: string
description: Guild Id
- in: query
name: name
required: false
schema:
type: string
example: "Member"
- in: query
name: sortBy
required: false
schema:
type: string
example: "name:desc"
- in: query
name: limit
required: false
schema:
type: integer
format: int32
example: 10
default: 10
- in: query
name: page
required: false
schema:
type: integer
format: int32
example: 2
default: 1
responses:
"200":
description: OK
content:
application/json:
schema:
type: array
items:
type: object
properties:
roleId:
type: string
format: Snowflake
name:
type: string
color:
type: integer
description: integer representation of hexadecimal color code | color with value of 0 means no color
example:
roleId: "80351110224678914"
name: "Member"
color: 2067276
type: object
properties:
results:
type: array
items:
type: object
properties:
roleId:
type: string
format: Snowflake
example: "652345789987654321"
name:
type: string
example: "Member"
color:
type: integer
description: integer representation of hexadecimal color code | color with value of 0 means no color
limit:
type: integer
example: 10
page:
type: integer
example: 1
totalPages:
type: integer
example: 2
totalResults:
type: integer
example: 20
"400":
description: Bad Request
$ref: "#/components/responses/BadRequest"
Expand All @@ -258,9 +298,6 @@ paths:
"401":
description: Unauthorized
$ref: "#/components/responses/Unauthorized"
"590":
description: Can not fetch from discord API
$ref: "#/components/responses/Can_Not_Fetch_From_Discord_API"

/api/v1/guilds/{guildId}/channels:
get:
Expand Down Expand Up @@ -323,9 +360,6 @@ paths:
"401":
description: Unauthorized
$ref: "#/components/responses/Unauthorized"
"590":
description: Can not fetch from discord API
$ref: "#/components/responses/Can_Not_Fetch_From_Discord_API"

/api/v1/guilds/{guildId}/selected-channels:
get:
Expand Down Expand Up @@ -385,9 +419,6 @@ paths:
"401":
description: Unauthorized
$ref: "#/components/responses/Unauthorized"
"590":
description: Can not fetch from discord API
$ref: "#/components/responses/Can_Not_Fetch_From_Discord_API"

/api/v1/guilds/connect:
get:
Expand Down
Loading

0 comments on commit 5e731ef

Please sign in to comment.