Skip to content

Commit

Permalink
Merge pull request #381 from privacy-scaling-explorations/fix/member-…
Browse files Browse the repository at this point in the history
…wrong-order

Proper ordering of group members
  • Loading branch information
vplasencia authored Feb 18, 2024
2 parents 7d06735 + 0826c3b commit 5c8341a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 27 deletions.
13 changes: 9 additions & 4 deletions apps/api/src/app/groups/groups.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -694,25 +694,30 @@ describe("GroupsService", () => {
)
})

it("Should add members to an existing group manually", async () => {
it("Should add members to an existing group manually in order", async () => {
const { members } = await groupsService.addMembersManually(
group.id,
["123123", "456456", "789789"],
["789789", "122121", "456456"],
"admin"
)

expect(members).toHaveLength(3)
expect(members.map((m) => m.id)).toEqual([
"789789",
"122121",
"456456"
])
})

it("Should not add members if they already exists", async () => {
const fun = groupsService.addMembersManually(
group.id,
["123123", "456456", "789789"],
["123123", "456456", "789790"],
"admin"
)

await expect(fun).rejects.toThrow(
`Member '123123' already exists in the group '${group.id}'`
`Member '456456' already exists in the group '${group.id}'`
)
})

Expand Down
55 changes: 32 additions & 23 deletions apps/api/src/app/groups/groups.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,32 +394,31 @@ export class GroupsService {
async addMembers(groupId: string, memberIds: string[]): Promise<Group> {
const group = await this.getGroup(groupId)

await Promise.all(
memberIds.map(async (memberId) => {
const member = group.members.find((m) => m.id === memberId)

if (!member) {
let newMember: Member
for (const memberId of memberIds) {
const member = group.members.find((m) => m.id === memberId)

// Check if the member is already a member of another group.
const anotherGroupMember =
await this.memberRepository.findOne({
where: { id: memberId }
})
if (!member) {
let newMember: Member

if (!anotherGroupMember) {
newMember = new Member()
newMember.id = memberId
// Check if the member is already a member of another group.
// eslint-disable-next-line
const anotherGroupMember = await this.memberRepository.findOne({
where: { id: memberId }
})

await this.memberRepository.save(newMember)
} else {
newMember = anotherGroupMember
}
if (!anotherGroupMember) {
newMember = new Member()
newMember.id = memberId

group.members.push(newMember)
// eslint-disable-next-line
await this.memberRepository.save(newMember)
} else {
newMember = anotherGroupMember
}
})
)

group.members.push(newMember)
}
}

await this.groupRepository.save(group)

Expand Down Expand Up @@ -606,7 +605,12 @@ export class GroupsService {

return this.groupRepository.find({
relations: { members: true },
where
where,
order: {
members: {
createdAt: "ASC"
}
}
})
}

Expand All @@ -618,7 +622,12 @@ export class GroupsService {
async getGroup(groupId: string): Promise<Group> {
const group = await this.groupRepository.findOne({
relations: { members: true, oAuthAccounts: true },
where: { id: groupId }
where: { id: groupId },
order: {
members: {
createdAt: "ASC"
}
}
})

if (!group) {
Expand Down

0 comments on commit 5c8341a

Please sign in to comment.