Skip to content

Commit

Permalink
Fix/321 members are not deleted even though the 204 response is retur…
Browse files Browse the repository at this point in the history
…ned (#324)

* feat: add deleteUserFromGroup

* fix: update method to delete users who don't exist in updatedGroup

* refactor: utilize concurrency

* feat: add test for deleting user from a group

* fix: delete container info from Group

* fix: test

* fix: missing sending command statement
  • Loading branch information
taniguchiiqqq authored Mar 21, 2024
1 parent a634b87 commit ba18ce3
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 5 deletions.
34 changes: 29 additions & 5 deletions backend/main/src/Group/Repositories/GroupRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,35 @@ export class GroupRepository implements IGroupRepository {
* @param Group
*/
async update(Group: Group): Promise<undefined> {
await this.nishikiDbClient.saveGroup(Group.id.id, {
groupName: Group.name,
userIds: Group.userIds.map((userId) => userId.id),
containerIds: Group.containerIds.map((containerId) => containerId.id),
});
// find users who are not in the updated group
const currentGroupUsers = await this.nishikiDbClient.listOfUsersInGroup(
Group.id.id,
);
const deletingUsers = currentGroupUsers.filter(
(user) => !Group.userIds.some((userId) => userId.id === user.userId),
);

// find containers which are not in the updated group
const currentGroupContainers = await this.nishikiDbClient.listOfContainers(
Group.id.id,
);
const deletingContainers = currentGroupContainers.filter(
(containerId) => !Group.containerIds.some((id) => id.id === containerId),
);

await Promise.all([
deletingUsers.map((user) =>
this.nishikiDbClient.deleteUserFromGroup(Group.id.id, user.userId),
),
deletingContainers.map((containerId) =>
this.nishikiDbClient.deleteContainerFromGroup(Group.id.id, containerId),
),
this.nishikiDbClient.saveGroup(Group.id.id, {
groupName: Group.name,
userIds: Group.userIds.map((userId) => userId.id),
containerIds: Group.containerIds.map((containerId) => containerId.id),
}),
]);
}

/**
Expand Down
38 changes: 38 additions & 0 deletions backend/main/src/Shared/Adapters/DB/NishikiTableClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,44 @@ export class NishikiDynamoDBClient {
);
}

/**
* Delete a user from the group.
* This function deletes the relation between the user and the group.
* @param groupId
* @param userId
*/
async deleteUserFromGroup(groupId: string, userId: string) {
const deleteUserFromGroupInput: DeleteItemInput = {
TableName: this.tableName,
Key: marshall({
PK: userId,
SK: `Group#${groupId}`,
}),
};

const command = new DeleteItemCommand(deleteUserFromGroupInput);
await this.dynamoClient.send(command);
}

/**
* Delete a container from the group.
* This function deletes the relation between the container and the group.
* @param groupId
* @param containerId
*/
async deleteContainerFromGroup(groupId: string, containerId: string) {
const deleteContainerFromGroupInput: DeleteItemInput = {
TableName: this.tableName,
Key: marshall({
PK: groupId,
SK: `Container#${containerId}`,
}),
};

const command = new DeleteItemCommand(deleteContainerFromGroupInput);
await this.dynamoClient.send(command);
}

/**
* Add a link and hash data to the Table.
* This function takes the link expiry Datetime as a parameter.
Expand Down
24 changes: 24 additions & 0 deletions backend/main/test/Shared/Adapters/NishikiTableClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,30 @@ describe.sequential("DynamoDB test client", () => {
expect(result).toEqual([]);
});
});

it("delete a user from a group", async () => {
const groupId = groupData.groupData[0].groupId;
const userId = groupData.groupData[0].users![0];

await nishikiClient.deleteUserFromGroup(groupId, userId);

const result = await nishikiClient.getUser({
userId,
groupId,
});

expect(result).toBeFalsy();
});

it("delete a container from a group", async () => {
const groupId = groupData.groupData[0].groupId;
const containerId = groupData.groupData[0].containerIds![0];

await nishikiClient.deleteContainerFromGroup(groupId, containerId);
const result = await nishikiClient.listOfContainers(groupId);

expect(result).not.toContain(containerId);
});
});

describe.sequential("join link expiry datetime", () => {
Expand Down

0 comments on commit ba18ce3

Please # to comment.