From 5dbf3248ac549f5a2098b8e68af422b07e59d74c Mon Sep 17 00:00:00 2001 From: Behzad Rabiei <53224485+Behzad-rabiei@users.noreply.github.com> Date: Wed, 2 Aug 2023 23:17:03 +0400 Subject: [PATCH 1/2] [TEST]: update tests --- __tests__/fixtures/guildMember.fixture.ts | 10 +++++++++ __tests__/integration/memberActivity.test.ts | 22 ++++++++++---------- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/__tests__/fixtures/guildMember.fixture.ts b/__tests__/fixtures/guildMember.fixture.ts index 008e002b..a49851d3 100644 --- a/__tests__/fixtures/guildMember.fixture.ts +++ b/__tests__/fixtures/guildMember.fixture.ts @@ -40,6 +40,16 @@ export const guildMemberFour = { discriminator: "4321" }; +export const guildMemberFive = { + discordId: "444444444", + username: "User4", + avatar: "AvatarLink", + roles: ["652345789987654321"], + joinedAt: new Date("2023-04-08"), + isBot: false, + discriminator: "4321" +}; + export const insertGuildMembers = async function (guildMembers: Array, connection: Connection) { await connection.models.GuildMember.insertMany(guildMembers.map((guildMember) => (guildMember))); diff --git a/__tests__/integration/memberActivity.test.ts b/__tests__/integration/memberActivity.test.ts index 0b717757..48750bce 100644 --- a/__tests__/integration/memberActivity.test.ts +++ b/__tests__/integration/memberActivity.test.ts @@ -6,7 +6,7 @@ import dateUtils from '../../src/utils/date'; import { userOne, insertUsers } from '../fixtures/user.fixture'; import { userOneAccessToken } from '../fixtures/token.fixture'; import { memberActivityOne, memberActivityTwo, memberActivityThree, memberActivityFour, insertMemberActivities } from '../fixtures/memberActivity.fixture'; -import { guildMemberOne, guildMemberTwo, guildMemberThree, guildMemberFour, insertGuildMembers } from '../fixtures/guildMember.fixture'; +import { guildMemberOne, guildMemberTwo, guildMemberThree, guildMemberFour, guildMemberFive, insertGuildMembers } from '../fixtures/guildMember.fixture'; import { guildOne, guildTwo, insertGuilds } from '../fixtures/guilds.fixture'; import { databaseService } from '@togethercrew.dev/db'; import { role1, role2, role3, insertRoles } from '../fixtures/discord.roles.fixture'; @@ -330,10 +330,10 @@ describe('member-activity routes', () => { await insertGuildMembers([guildMemberOne, guildMemberTwo, guildMemberThree, guildMemberFour], connection); const yesterdayTimestamp = dateUtils.getYesterdayUTCtimestamp() - + const date = new Date(); date.setDate(date.getDate() - 2); - const twodaysAgoTimestamp = date.setHours(10,0,0,0); + const twodaysAgoTimestamp = date.setHours(10, 0, 0, 0); await Neo4j.write("match (n) detach delete (n);") await Neo4j.write(`CREATE (a:DiscordAccount) -[:IS_MEMBER]->(g:Guild {guildId: "${guildOne.guildId}"}) @@ -408,7 +408,7 @@ describe('member-activity routes', () => { const date = new Date(); date.setDate(date.getDate() - 2); - const twodaysAgoTimestamp = date.setHours(0,0,0,0); + const twodaysAgoTimestamp = date.setHours(0, 0, 0, 0); await Neo4j.write("match (n) detach delete (n);") await Neo4j.write(` @@ -482,7 +482,7 @@ describe('member-activity routes', () => { const date = new Date(); date.setDate(date.getDate() - 2); - const twodaysAgoTimestamp = date.setHours(0,0,0,0); + const twodaysAgoTimestamp = date.setHours(0, 0, 0, 0); await Neo4j.write("match (n) detach delete (n);") await Neo4j.write(` @@ -574,7 +574,7 @@ describe('member-activity routes', () => { const date = new Date(); date.setDate(date.getDate() - 2); - const twodaysAgoTimestamp = date.setHours(0,0,0,0); + const twodaysAgoTimestamp = date.setHours(0, 0, 0, 0); // TODO: write neo4j queries in other file await Neo4j.write("match (n) detach delete (n);") @@ -645,12 +645,12 @@ describe('member-activity routes', () => { await insertGuilds([guildOne]); await insertGuilds([guildTwo]); await insertGuildMembers([guildMemberOne, guildMemberTwo, guildMemberThree, guildMemberFour], connection); - + const yesterdayTimestamp = dateUtils.getYesterdayUTCtimestamp() const date = new Date(); date.setDate(date.getDate() - 2); - const twodaysAgoTimestamp = date.setHours(0,0,0,0); + const twodaysAgoTimestamp = date.setHours(0, 0, 0, 0); // TODO: write neo4j queries in other file await Neo4j.write("match (n) detach delete (n);") @@ -737,7 +737,7 @@ describe('member-activity routes', () => { await insertUsers([userOne]); await insertGuilds([guildOne]); await insertMemberActivities([memberActivityOne, memberActivityTwo], connection); - await insertGuildMembers([guildMemberOne, guildMemberTwo, guildMemberThree, guildMemberFour], connection); + await insertGuildMembers([guildMemberOne, guildMemberTwo, guildMemberThree, guildMemberFour, guildMemberFive], connection); await insertRoles([role1, role2, role3], connection); const res = await request(app) .get(`/api/v1/member-activity/${guildOne.guildId}/active-members-composition-table`) @@ -1116,7 +1116,7 @@ describe('member-activity routes', () => { await insertUsers([userOne]); await insertGuilds([guildOne]); await insertMemberActivities([memberActivityOne, memberActivityTwo], connection); - await insertGuildMembers([guildMemberOne, guildMemberTwo, guildMemberThree, guildMemberFour], connection); + await insertGuildMembers([guildMemberOne, guildMemberTwo, guildMemberThree, guildMemberFour, guildMemberFive], connection); await insertRoles([role1, role2, role3], connection); const res = await request(app) .get(`/api/v1/member-activity/${guildOne.guildId}/active-members-onboarding-table`) @@ -1496,7 +1496,7 @@ describe('member-activity routes', () => { await insertUsers([userOne]); await insertGuilds([guildOne]); await insertMemberActivities([memberActivityOne, memberActivityTwo], connection); - await insertGuildMembers([guildMemberOne, guildMemberTwo, guildMemberThree, guildMemberFour], connection); + await insertGuildMembers([guildMemberOne, guildMemberTwo, guildMemberThree, guildMemberFour, guildMemberFive], connection); await insertRoles([role1, role2, role3], connection); const res = await request(app) .get(`/api/v1/member-activity/${guildOne.guildId}/disengaged-members-composition-table`) From ded9f061710021e0b415e30f7c72400c011861d3 Mon Sep 17 00:00:00 2001 From: Behzad Rabiei <53224485+Behzad-rabiei@users.noreply.github.com> Date: Wed, 2 Aug 2023 23:17:37 +0400 Subject: [PATCH 2/2] [FEATURE]: ignore users who joined after memberActivity computation --- src/services/guildMember.service.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/services/guildMember.service.ts b/src/services/guildMember.service.ts index 56292fe3..0344bf01 100644 --- a/src/services/guildMember.service.ts +++ b/src/services/guildMember.service.ts @@ -34,6 +34,8 @@ async function queryGuildMembers(connection: Connection, filter: Filter, options let matchStage: any = {}; let allActivityIds: string[] = []; + const memberActivityDate = await connection.models.MemberActivity.findOne().sort({ date: -1 }).select({ date: 1, _id: 0 }); + if (activityComposition && activityComposition.length > 0) { // If 'others' is in activityComposition, we exclude all IDs that are part of other activities if (activityComposition.includes('others')) { @@ -65,6 +67,10 @@ async function queryGuildMembers(connection: Connection, filter: Filter, options matchStage.roles = { $in: roles }; } + if (memberActivityDate) { + matchStage.joinedAt = { $lte: memberActivityDate.date }; + } + const totalResults = await connection.models.GuildMember.countDocuments(matchStage); const results = await connection.models.GuildMember.aggregate([ @@ -114,7 +120,7 @@ async function queryGuildMembers(connection: Connection, filter: Filter, options } } /** - * handel guild member roles and username + * handel guild member roles, ngu, and username * @param {Array} guildMembers - guild members array. * @param {Array} roles - roles array. * @param {Any} memberActivity - The document containing the last member activity.