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

docs: add summary to group endpoints for openapi docs📝 #105

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
28 changes: 26 additions & 2 deletions src/modules/group/group.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
getAllGroupsRepository,
getGroupByIdRepository,
updateGroupRepository,
usersExistsInGroupRepository,
} from "./group.repository";
import type {
IUpdateGroupRoute,
Expand Down Expand Up @@ -308,8 +309,8 @@ export const addUsersToGroup: AppRouteHandler<TAddUsersToGroupRoute> = async (
userIds = [userDetail.userId, ...userIds];
usernames = [userDetail.username, ...usernames];
});
const { groupId } = params;

const { groupId } = params;
const groupExists = await getGroupByIdRepository(groupId);

if (!groupExists) {
Expand All @@ -323,6 +324,27 @@ export const addUsersToGroup: AppRouteHandler<TAddUsersToGroupRoute> = async (
);
}

const usersExistsInGroup = await usersExistsInGroupRepository(
groupId,
userIds,
);

if (usersExistsInGroup.length) {
const existingUserIds = usersExistsInGroup.map(user => user.userId);

logger.error(
`Users with the following ids: ${existingUserIds.join(" ,")} already exists in group`,
);
return c.json(
{
success: false,
message:
"Some or all of the users you are trying to add already exist in the group",
},
HTTPStatusCodes.CONFLICT,
);
}

const addUsersToGroupResp = await addUsersToGroupRepository(groupId, userIds);

if (!addUsersToGroupResp) {
Expand Down Expand Up @@ -352,11 +374,13 @@ export const addUsersToGroup: AppRouteHandler<TAddUsersToGroupRoute> = async (
});
});

const msg = userIds.length > 1 ? "Users" : "User";

logger.debug("Users added to group successfully");
return c.json(
{
success: true,
message: "Users added to group successfully",
message: `${msg} added to group successfully`,
data: groupExists,
},
HTTPStatusCodes.OK,
Expand Down
19 changes: 18 additions & 1 deletion src/modules/group/group.repository.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { SQL } from "drizzle-orm";
import { and, asc, desc, eq, ilike, sql } from "drizzle-orm";
import { and, asc, desc, eq, ilike, inArray, sql } from "drizzle-orm";

import { AuthRoles } from "@/common/enums";
import { db } from "@/db/adapter";
Expand Down Expand Up @@ -117,3 +117,20 @@ export async function getGroupMembersByIdRepository(groupId: string) {

return groupMembers;
}

export async function usersExistsInGroupRepository(
groupId: string,
userIds: string[],
) {
const userExistsInGroup = await db
.select()
.from(usersToGroupsModel)
.where(
and(
eq(usersToGroupsModel.groupId, groupId),
inArray(usersToGroupsModel.userId, userIds),
),
);

return userExistsInGroup;
}
13 changes: 13 additions & 0 deletions src/modules/group/group.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const createGroupRoute = createRoute({
tags,
method: "post",
path: "/groups",
summary: "Create a new group",
middleware: [authMiddleware(), requireAuth()] as const,
request: {
body: jsonContentRequired(
Expand Down Expand Up @@ -80,6 +81,7 @@ export const getAllGroupsRoute = createRoute({
tags,
method: "get",
path: "/groups",
summary: "Get all groups",
middleware: [authMiddleware(), requireAuth()] as const,
request: {
query: groupQuerySchema,
Expand Down Expand Up @@ -107,6 +109,7 @@ export const getGroupById = createRoute({
tags,
method: "get",
path: "/group/:id",
summary: "Get group by ID",
middleware: [authMiddleware(), requireAuth()] as const,
request: {
params: z.object({
Expand Down Expand Up @@ -157,6 +160,7 @@ export const updateGroupRoute = createRoute({
tags,
method: "put",
path: "/group/:id",
summary: "Update group by ID",
middleware: [authMiddleware(), requireAuth()] as const,
request: {
params: z.object({
Expand Down Expand Up @@ -216,6 +220,7 @@ export const deleteGroupRoute = createRoute({
tags,
method: "delete",
path: "groups/:id",
summary: "Delete group by ID",
middleware: [authMiddleware(), requireAuth()] as const,
request: {
params: z.object({
Expand Down Expand Up @@ -265,6 +270,7 @@ export const addUsersToGroupRoute = createRoute({
tags,
method: "post",
path: "/groups/:groupId/users",
summary: "Add users to group",
middleware: [authMiddleware(), requireAuth()] as const,
request: {
body: jsonContentRequired(
Expand Down Expand Up @@ -310,6 +316,13 @@ export const addUsersToGroupRoute = createRoute({
}),
"Group with id does not exist",
),
[HTTPStatusCodes.CONFLICT]: jsonContent(
z.object({
success: z.boolean().default(false),
message: z.string(),
}),
"User(s) already exists in group",
),
[HTTPStatusCodes.UNPROCESSABLE_ENTITY]: jsonContent(
createErrorSchema(insertGroupSchema),
VALIDATION_ERROR_MESSAGE,
Expand Down
43 changes: 43 additions & 0 deletions src/modules/group/group.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,5 +195,48 @@ describe("group Handler", () => {
expect(json.message).toBe(AUTHORIZATION_ERROR_MESSAGE);
}
});

it("should return 409 when adding user that already exists in group", async () => {
await groupClient.groups[":groupId"].users.$post(
{
json: testUsers.map(user => ({
userId: user.id,
username: user.username,
})),
param: { groupId },
},
{
headers: {
session: userSessionToken,
},
},
);

const response = await groupClient.groups[":groupId"].users.$post(
{
json: testUsers.map(user => ({
userId: user.id,
username: user.username,
})),
param: { groupId },
},
{
headers: {
session: userSessionToken,
},
},
);

expect(response.status).toBe(HTTPStatusCodes.CONFLICT);

if (response.status === HTTPStatusCodes.CONFLICT) {
const json = await response.json();

expect(json.success).toBe(false);
expect(json.message).toBe(
"Some or all of the users you are trying to add already exist in the group",
);
}
});
});
});
Loading