Skip to content

Commit

Permalink
feat: add route for delete many
Browse files Browse the repository at this point in the history
  • Loading branch information
rasyadanfz committed May 19, 2024
1 parent 7615326 commit 26ccae3
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 5 deletions.
83 changes: 83 additions & 0 deletions src/controllers/user-unsubscribe.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import {
deleteUserUnsubscribeCategoryRoute,
} from '~/routes/user-unsubscribe.route';
import {
DeleteListUserUnsubscribeCategoryResponseSchema,
DeleteUserUnsubscribeCategorySchema,
GetUserUnsubscribeCategoryResponseSchema,
PostUserUnsubscribeCategorySchema,
} from '~/types/user-unsubscribe.types';
Expand Down Expand Up @@ -232,3 +234,84 @@ userUnsubscribeRouter.openapi(deleteUserUnsubscribeCategoryRoute, async (c) => {
return c.json({ error: 'Something went wrong!' }, 500);
}
});

userUnsubscribeRouter.openapi(deleteListUserUnsubscribeRoute, async (c) => {
const { id } = c.var.user;
const categoryIds = c.req.valid('json').categoryId;

try {
const existingCategories: string[] = [];
const checkExistingCategoryPromises: Array<
Promise<false | z.infer<typeof CategorySchema>>
> = [];

categoryIds.forEach((categoryId) => {
checkExistingCategoryPromises.push(isCategoryExists(db, categoryId));
});

const res = await Promise.allSettled(checkExistingCategoryPromises);
res.forEach((result) => {
if (result.status === 'fulfilled') {
if (result.value) {
existingCategories.push(result.value.id);
}
}
});

if (existingCategories.length === 0) {
return c.json({ error: 'All categories does not exist!' }, 400);
}

const notFoundCategories = categoryIds.filter(
(categoryId) => !existingCategories.includes(categoryId),
);

const deletePromises: Array<
Promise<z.infer<typeof DeleteUserUnsubscribeCategorySchema>>
> = [];

existingCategories.forEach((categoryId) => {
deletePromises.push(
deleteUserUnsubscribeCategory(
db,
{
userId: id,
categoryId,
},
id,
),
);
});

const resDelete = await Promise.allSettled(deletePromises);
const deletedCategories: string[] = [];
const alreadySubscribedCategories: string[] = [];

resDelete.forEach((result) => {
if (result.status === 'fulfilled') {
if (result.value) {
deletedCategories.push(result.value.categoryId);
}
}
});

existingCategories.forEach((categoryId) => {
if (!deletedCategories.includes(categoryId)) {
alreadySubscribedCategories.push(categoryId);
}
});

const returnObj: z.infer<
typeof DeleteListUserUnsubscribeCategoryResponseSchema
> = {
userId: id,
categoryId: deletedCategories,
categoriesNotFound: notFoundCategories,
categoriesAlreadySubscribed: alreadySubscribedCategories,
};

return c.json(returnObj, 201);
} catch (e) {
return c.json({ error: 'Something went wrong!' }, 500);
}
});
9 changes: 4 additions & 5 deletions src/routes/user-unsubscribe.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@ import {
ServerErrorSchema,
} from '~/types/responses.type';
import {
DeleteListUserUnsubscribeCategorySchema,
DeleteListUserUnsubscribeCategoryParamsSchema,
DeleteListUserUnsubscribeCategoryResponseSchema,
DeleteUserUnsubscribeCategoryParamsSchema,
DeleteUserUnsubscribeCategorySchema,
GetListUserUnsubscribeCategorySchema,
GetUserUnsubscribeCategoryParamsSchema,
GetUserUnsubscribeCategoryResponseSchema,
GetUserUnsubscribeCategorySchema,
PostListUserUnsubscribeCategoryParamsSchema,
PostListUserUnsubscribeCategoryResponseSchema,
PostListUserUnsubscribeCategorySchema,
PostUserUnsubscribeCategoryParamsSchema,
PostUserUnsubscribeCategorySchema,
} from '~/types/user-unsubscribe.types';
Expand Down Expand Up @@ -225,7 +224,7 @@ export const deleteListUserUnsubscribeRoute = createRoute({
body: {
content: {
'application/json': {
schema: DeleteUserUnsubscribeCategoryParamsSchema,
schema: DeleteListUserUnsubscribeCategoryParamsSchema,
},
},
required: true,
Expand All @@ -236,7 +235,7 @@ export const deleteListUserUnsubscribeRoute = createRoute({
description: 'User is subscribed to all given categories',
content: {
'application/json': {
schema: DeleteListUserUnsubscribeCategorySchema,
schema: DeleteListUserUnsubscribeCategoryResponseSchema,
},
},
},
Expand Down
14 changes: 14 additions & 0 deletions src/types/user-unsubscribe.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ export const DeleteUserUnsubscribeCategoryParamsSchema = z.object({
categoryId: z.string().openapi({ example: '1' }),
});

export const DeleteListUserUnsubscribeCategoryParamsSchema = z.object({
categoryId: z.array(z.string()).openapi({ example: ['1', '2', '3'] }),
});

// Response types
export const GetUserUnsubscribeCategoryResponseSchema = z.object({
...GetUserUnsubscribeCategorySchema.shape,
Expand All @@ -84,3 +88,13 @@ export const PostListUserUnsubscribeCategoryResponseSchema = z.object({
example: ['5', '6'],
}),
});

export const DeleteListUserUnsubscribeCategoryResponseSchema = z.object({
...DeleteListUserUnsubscribeCategorySchema.shape,
categoriesNotFound: z.array(z.string()).openapi({
example: ['3', '4'],
}),
categoriesAlreadySubscribed: z.array(z.string()).openapi({
example: ['5', '6'],
}),
});

0 comments on commit 26ccae3

Please sign in to comment.