Skip to content

Commit

Permalink
Merge pull request #202 from TogetherCrew/201-others-category-issue-i…
Browse files Browse the repository at this point in the history
…n-member-breakdown-tables

201 others category issue in member breakdown tables
  • Loading branch information
cyri113 authored Aug 2, 2023
2 parents bc52c39 + ded9f06 commit 9c18d01
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
10 changes: 10 additions & 0 deletions __tests__/fixtures/guildMember.fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 <Type>(guildMembers: Array<Type>, connection: Connection) {
await connection.models.GuildMember.insertMany(guildMembers.map((guildMember) => (guildMember)));

Expand Down
22 changes: 11 additions & 11 deletions __tests__/integration/memberActivity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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}"})
Expand Down Expand Up @@ -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(`
Expand Down Expand Up @@ -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(`
Expand Down Expand Up @@ -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);")
Expand Down Expand Up @@ -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);")
Expand Down Expand Up @@ -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`)
Expand Down Expand Up @@ -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`)
Expand Down Expand Up @@ -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`)
Expand Down
8 changes: 7 additions & 1 deletion src/services/guildMember.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')) {
Expand Down Expand Up @@ -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([
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 9c18d01

Please sign in to comment.