Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proper ordering of group members #381

Merged
merged 2 commits into from
Feb 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading