From c3052989275c300fd0262d0a2f000728a3b7555e Mon Sep 17 00:00:00 2001 From: Kevin Puig <119972216+k-puig@users.noreply.github.com> Date: Sat, 27 Sep 2025 21:42:43 -0400 Subject: [PATCH] code made pretty --- .../src/controller/categoryController.ts | 41 +- .../src/controller/channelController.ts | 40 +- .../src/controller/instanceController.ts | 8 +- .../src/controller/messageController.ts | 40 +- concord-server/src/routes/categoryRoutes.ts | 530 ++++++------ concord-server/src/routes/channelRoutes.ts | 526 ++++++------ concord-server/src/routes/instanceRoutes.ts | 110 +-- concord-server/src/routes/messageRoutes.ts | 229 ++--- concord-server/src/services/channelService.ts | 799 +++++++++--------- .../src/services/instanceService.ts | 94 ++- concord-server/src/services/messageService.ts | 32 +- concord-server/src/services/userService.ts | 1 - .../src/validators/categoryValidator.ts | 86 +- .../src/validators/channelValidator.ts | 76 +- .../src/validators/instanceValidator.ts | 46 +- .../src/validators/messageValidator.ts | 26 +- .../src/validators/userValidator.ts | 2 +- 17 files changed, 1394 insertions(+), 1292 deletions(-) diff --git a/concord-server/src/controller/categoryController.ts b/concord-server/src/controller/categoryController.ts index 7177e5f..c2b5a1b 100644 --- a/concord-server/src/controller/categoryController.ts +++ b/concord-server/src/controller/categoryController.ts @@ -1,39 +1,40 @@ -import{ - createCategory, - getCategory, - getCategoriesByInstance, - updateCategory, - deleteCategory, - deleteAllCategoriesFromInstance, +import { + createCategory, + getCategory, + getCategoriesByInstance, + updateCategory, + deleteCategory, + deleteAllCategoriesFromInstance, } from "../services/channelService"; -import{ - CreateCategoryInput, - UpdateCategoryInput, - DeleteCategoryInput, - DeleteCategoriesByInstanceIdInput +import { + CreateCategoryInput, + UpdateCategoryInput, + DeleteCategoryInput, + DeleteCategoriesByInstanceIdInput, } from "../validators/categoryValidator"; export async function createNewCategory(data: CreateCategoryInput) { - return await createCategory(data); + return await createCategory(data); } export async function fetchCategoryData(id: string) { - return await getCategory(id); + return await getCategory(id); } export async function fetchCategoriesByInstance(instanceId: string) { - return await getCategoriesByInstance(instanceId); + return await getCategoriesByInstance(instanceId); } export async function updateExistingCategory(data: UpdateCategoryInput) { - return await updateCategory(data); + return await updateCategory(data); } export async function deleteExistingCategory(data: DeleteCategoryInput) { - return await deleteCategory(data); + return await deleteCategory(data); } -export async function deleteAllCategoriesByInstance(data: DeleteCategoriesByInstanceIdInput) { - return await deleteAllCategoriesFromInstance(data); +export async function deleteAllCategoriesByInstance( + data: DeleteCategoriesByInstanceIdInput, +) { + return await deleteAllCategoriesFromInstance(data); } - diff --git a/concord-server/src/controller/channelController.ts b/concord-server/src/controller/channelController.ts index b1c6cce..1c09a84 100644 --- a/concord-server/src/controller/channelController.ts +++ b/concord-server/src/controller/channelController.ts @@ -1,38 +1,40 @@ -import{ - createChannel, - getChannel, - getChannelsByCategory, - updateChannel, - deleteChannel, - deleteAllChannelsFromCategory +import { + createChannel, + getChannel, + getChannelsByCategory, + updateChannel, + deleteChannel, + deleteAllChannelsFromCategory, } from "../services/channelService"; import { - CreateChannelInput, - UpdateChannelInput, - DeleteChannelInput, - DeleteChannelsByCategoryIdInput + CreateChannelInput, + UpdateChannelInput, + DeleteChannelInput, + DeleteChannelsByCategoryIdInput, } from "../validators/channelValidator"; export async function createNewChannel(data: CreateChannelInput) { - return await createChannel(data); + return await createChannel(data); } export async function fetchChannelData(id: string) { - return await getChannel(id); + return await getChannel(id); } export async function fetchChannelsByCategory(categoryId: string) { - return await getChannelsByCategory(categoryId); + return await getChannelsByCategory(categoryId); } export async function updateExistingChannel(data: UpdateChannelInput) { - return await updateChannel(data); + return await updateChannel(data); } export async function deleteExistingChannel(data: DeleteChannelInput) { - return await deleteChannel(data); + return await deleteChannel(data); } -export async function deleteAllChannelsByCategory(data: DeleteChannelsByCategoryIdInput) { - return await deleteAllChannelsFromCategory(data); -} \ No newline at end of file +export async function deleteAllChannelsByCategory( + data: DeleteChannelsByCategoryIdInput, +) { + return await deleteAllChannelsFromCategory(data); +} diff --git a/concord-server/src/controller/instanceController.ts b/concord-server/src/controller/instanceController.ts index 97578fd..a586e35 100644 --- a/concord-server/src/controller/instanceController.ts +++ b/concord-server/src/controller/instanceController.ts @@ -1,10 +1,10 @@ import { createInstance, getAllInstances } from "../services/instanceService"; import { CreateInstanceRequest } from "../validators/instanceValidator"; -export async function createInstanceReq(data:CreateInstanceRequest) { - return await createInstance(data); +export async function createInstanceReq(data: CreateInstanceRequest) { + return await createInstance(data); } export async function getAllInstancesReq() { - return await getAllInstances(); -} \ No newline at end of file + return await getAllInstances(); +} diff --git a/concord-server/src/controller/messageController.ts b/concord-server/src/controller/messageController.ts index e96b404..483b184 100644 --- a/concord-server/src/controller/messageController.ts +++ b/concord-server/src/controller/messageController.ts @@ -1,25 +1,29 @@ -import { getMessageInformation, getMessagesBefore, sendMessageToChannel } from "../services/messageService"; +import { + getMessageInformation, + getMessagesBefore, + sendMessageToChannel, +} from "../services/messageService"; -export async function fetchMessageData(id:string) { - return await getMessageInformation(id); +export async function fetchMessageData(id: string) { + return await getMessageInformation(id); } -export async function fetchMessagesBefore(date:string, channelId:string) { - return getMessagesBefore(date, channelId); +export async function fetchMessagesBefore(date: string, channelId: string) { + return getMessagesBefore(date, channelId); } export async function sendMessage( - channelId: string, - userId: string, - content: string, - token: string, - repliedMessageId: string | null + channelId: string, + userId: string, + content: string, + token: string, + repliedMessageId: string | null, ) { - return await sendMessageToChannel( - channelId, - userId, - content, - token, - repliedMessageId - ); -} \ No newline at end of file + return await sendMessageToChannel( + channelId, + userId, + content, + token, + repliedMessageId, + ); +} diff --git a/concord-server/src/routes/categoryRoutes.ts b/concord-server/src/routes/categoryRoutes.ts index de396cf..9b48532 100644 --- a/concord-server/src/routes/categoryRoutes.ts +++ b/concord-server/src/routes/categoryRoutes.ts @@ -1,297 +1,315 @@ import { - createNewCategory, - fetchCategoryData, - fetchCategoriesByInstance, - updateExistingCategory, - deleteExistingCategory, - deleteAllCategoriesByInstance, + createNewCategory, + fetchCategoryData, + fetchCategoriesByInstance, + updateExistingCategory, + deleteExistingCategory, + deleteAllCategoriesByInstance, } from "../controller/categoryController"; import { - createCategorySchema, - getCategorySchema, - getCategoriesByInstanceIdSchema, - updateCategorySchema, - deleteCategorySchema, - deleteCategoriesByInstanceIdSchema, - CreateCategoryInput, - GetCategoryInput, - GetCategoriesByInstanceIdInput, - UpdateCategoryInput, - DeleteCategoryInput, - DeleteCategoriesByInstanceIdInput, + createCategorySchema, + getCategorySchema, + getCategoriesByInstanceIdSchema, + updateCategorySchema, + deleteCategorySchema, + deleteCategoriesByInstanceIdSchema, + CreateCategoryInput, + GetCategoryInput, + GetCategoriesByInstanceIdInput, + UpdateCategoryInput, + DeleteCategoryInput, + DeleteCategoriesByInstanceIdInput, } from "../validators/categoryValidator"; import { zValidator } from "@hono/zod-validator"; import { Hono } from "hono"; import { describeRoute, resolver } from "hono-openapi"; -const categoryRoutes = new Hono() +const categoryRoutes = new Hono(); // Create a new category categoryRoutes.post( - "/", - describeRoute({ - description: "Create a new category", - responses: { - 200: { - description: "Success creating category", - content: { - "application/json": { schema: resolver(createCategorySchema) }, - }, - }, - 400: { - description: "Bad Request - Invalid input data", - content: { - "application/json": { schema: resolver(createCategorySchema)}, - }, - }, - 401: { - description: "Unauthorized - Admin access required", - content: { - "application/json": { schema: resolver(createCategorySchema)}, - }, - }, - 404: { - description: "User Id not found", - content: { - "application/json": { schema: resolver(createCategorySchema)}, - }, - }, + "/", + describeRoute({ + description: "Create a new category", + responses: { + 200: { + description: "Success creating category", + content: { + "application/json": { schema: resolver(createCategorySchema) }, }, - }), - zValidator("json", createCategorySchema), - async (c) => { - const data = c.req.valid("json") as CreateCategoryInput; - const categoryData = await createNewCategory(data); - if (categoryData) { - return c.json(categoryData); - } else { - return c.json({ error: "Failed to create category" }, 400); - } + }, + 400: { + description: "Bad Request - Invalid input data", + content: { + "application/json": { schema: resolver(createCategorySchema) }, + }, + }, + 401: { + description: "Unauthorized - Admin access required", + content: { + "application/json": { schema: resolver(createCategorySchema) }, + }, + }, + 404: { + description: "User Id not found", + content: { + "application/json": { schema: resolver(createCategorySchema) }, + }, + }, + }, + }), + zValidator("json", createCategorySchema), + async (c) => { + const data = c.req.valid("json") as CreateCategoryInput; + const categoryData = await createNewCategory(data); + if (categoryData) { + return c.json(categoryData); + } else { + return c.json({ error: "Failed to create category" }, 400); } -) + }, +); // Get a category by ID categoryRoutes.get( - "/:id", - describeRoute({ - description: "Get category by id", - responses: { - 200: { - description: "Success getting category", - content: { - "application/json": { schema: resolver(getCategorySchema) }, - }, - }, - 404: { - description: "Category id not found", - content: { - "application/json": { schema: resolver(getCategorySchema) }, - }, - }, + "/:id", + describeRoute({ + description: "Get category by id", + responses: { + 200: { + description: "Success getting category", + content: { + "application/json": { schema: resolver(getCategorySchema) }, }, - }), - async (c) => { - const id = c.req.param("id"); - const categoryData = await fetchCategoryData(id); - if (categoryData) { - return c.json(categoryData); - } else { - return c.json({ error: "Category not found" }, 404); - } + }, + 404: { + description: "Category id not found", + content: { + "application/json": { schema: resolver(getCategorySchema) }, + }, + }, + }, + }), + async (c) => { + const id = c.req.param("id"); + const categoryData = await fetchCategoryData(id); + if (categoryData) { + return c.json(categoryData); + } else { + return c.json({ error: "Category not found" }, 404); } + }, ); // Get all categories by instance ID categoryRoutes.get( - "/instance/:instanceId", - describeRoute({ - description: "Get all categories by instance id", - responses: { - 200: { - description: "Success getting all categories in instance", - content: { - "application/json": { schema: resolver(getCategoriesByInstanceIdSchema) }, - }, - }, - 400: { - description: "Bad Request - Missing instance ID", - content: { - "application/json": { schema: resolver(getCategoriesByInstanceIdSchema) }, - }, - }, + "/instance/:instanceId", + describeRoute({ + description: "Get all categories by instance id", + responses: { + 200: { + description: "Success getting all categories in instance", + content: { + "application/json": { + schema: resolver(getCategoriesByInstanceIdSchema), + }, }, - }), - async (c) => { - const instanceId = c.req.param("instanceId"); - if (!instanceId) { - return c.json({ error: "No instance id provided" }, 400); - } - - const categoryData = await fetchCategoriesByInstance(instanceId); - if (categoryData) { - return c.json(categoryData); - } else { - return c.json({ error: "Error getting all categories from instance" }, 500); - } + }, + 400: { + description: "Bad Request - Missing instance ID", + content: { + "application/json": { + schema: resolver(getCategoriesByInstanceIdSchema), + }, + }, + }, + }, + }), + async (c) => { + const instanceId = c.req.param("instanceId"); + if (!instanceId) { + return c.json({ error: "No instance id provided" }, 400); } + + const categoryData = await fetchCategoriesByInstance(instanceId); + if (categoryData) { + return c.json(categoryData); + } else { + return c.json( + { error: "Error getting all categories from instance" }, + 500, + ); + } + }, ); // Update a category categoryRoutes.put( - "/:id", - describeRoute({ - description: "Update an existing category", - responses: { - 200: { - description: "Success updating category", - content: { - "application/json": { schema: resolver(updateCategorySchema) }, - }, - }, - 400: { - description: "Bad Request - Invalid input data", - content: { - "application/json": { schema: resolver(updateCategorySchema)}, - }, - }, - 401: { - description: "Unauthorized - Admin access required", - content: { - "application/json": { schema: resolver(updateCategorySchema)}, - }, - }, - 404: { - description: "Category id or User Id not found", - content: { - "application/json": { schema: resolver(updateCategorySchema)}, - }, - }, + "/:id", + describeRoute({ + description: "Update an existing category", + responses: { + 200: { + description: "Success updating category", + content: { + "application/json": { schema: resolver(updateCategorySchema) }, }, - }), - zValidator("json", updateCategorySchema), - async (c) => { - const id = c.req.param("id"); - const data = c.req.valid("json") as UpdateCategoryInput; - - // Ensure the ID in the path matches the one in the body - if (data.id && data.id !== id) { - return c.json({ error: "ID in path does not match ID in body" }, 400); - } - - // Set ID from path if not in body - if (!data.id) { - data.id = id; - } - - const categoryData = await updateExistingCategory(data); - if (categoryData) { - return c.json(categoryData); - } else { - return c.json({ error: "Failed to update category" }, 400); - } + }, + 400: { + description: "Bad Request - Invalid input data", + content: { + "application/json": { schema: resolver(updateCategorySchema) }, + }, + }, + 401: { + description: "Unauthorized - Admin access required", + content: { + "application/json": { schema: resolver(updateCategorySchema) }, + }, + }, + 404: { + description: "Category id or User Id not found", + content: { + "application/json": { schema: resolver(updateCategorySchema) }, + }, + }, + }, + }), + zValidator("json", updateCategorySchema), + async (c) => { + const id = c.req.param("id"); + const data = c.req.valid("json") as UpdateCategoryInput; + + // Ensure the ID in the path matches the one in the body + if (data.id && data.id !== id) { + return c.json({ error: "ID in path does not match ID in body" }, 400); } + + // Set ID from path if not in body + if (!data.id) { + data.id = id; + } + + const categoryData = await updateExistingCategory(data); + if (categoryData) { + return c.json(categoryData); + } else { + return c.json({ error: "Failed to update category" }, 400); + } + }, ); // Delete a specific category categoryRoutes.delete( - "/:id", - describeRoute({ - description: "Delete an existing category", - responses: { - 200: { - description: "Success deleting category", - content: { - "application/json": { schema: resolver(deleteCategorySchema) }, - }, - }, - 400: { - description: "Bad Request - Invalid input data", - content: { - "application/json": { schema: resolver(deleteCategorySchema)}, - }, - }, - 401: { - description: "Unauthorized - Admin access required", - content: { - "application/json": { schema: resolver(deleteCategorySchema)}, - }, - }, - 404: { - description: "Category id or User Id not found", - content: { - "application/json": { schema: resolver(deleteCategorySchema)}, - }, - }, + "/:id", + describeRoute({ + description: "Delete an existing category", + responses: { + 200: { + description: "Success deleting category", + content: { + "application/json": { schema: resolver(deleteCategorySchema) }, }, - }), - zValidator("json", deleteCategorySchema), - async (c) => { - const id = c.req.param("id"); - const data = c.req.valid("json") as DeleteCategoryInput; - - // Ensure the ID in the path matches the one in the body - if (data.id !== id) { - return c.json({ error: "ID in path does not match ID in body" }, 400); - } - - const categoryData = await deleteExistingCategory(data); - if (categoryData) { - return c.json(categoryData); - } else { - return c.json({ error: "Failed to delete category" }, 400); - } + }, + 400: { + description: "Bad Request - Invalid input data", + content: { + "application/json": { schema: resolver(deleteCategorySchema) }, + }, + }, + 401: { + description: "Unauthorized - Admin access required", + content: { + "application/json": { schema: resolver(deleteCategorySchema) }, + }, + }, + 404: { + description: "Category id or User Id not found", + content: { + "application/json": { schema: resolver(deleteCategorySchema) }, + }, + }, + }, + }), + zValidator("json", deleteCategorySchema), + async (c) => { + const id = c.req.param("id"); + const data = c.req.valid("json") as DeleteCategoryInput; + + // Ensure the ID in the path matches the one in the body + if (data.id !== id) { + return c.json({ error: "ID in path does not match ID in body" }, 400); } -) + + const categoryData = await deleteExistingCategory(data); + if (categoryData) { + return c.json(categoryData); + } else { + return c.json({ error: "Failed to delete category" }, 400); + } + }, +); // Delete all categories by instance ID categoryRoutes.delete( - "/instance/:instanceId", - describeRoute({ - description: "Delete all categories by instance id", - responses: { - 200: { - description: "Success deleting all categories in instance", - content: { - "application/json": { schema: resolver(deleteCategoriesByInstanceIdSchema) }, - }, - }, - 400: { - description: "Bad Request - Invalid input data", - content: { - "application/json": { schema: resolver(deleteCategoriesByInstanceIdSchema)}, - }, - }, - 401: { - description: "Unauthorized - Admin access required", - content: { - "application/json": { schema: resolver(deleteCategoriesByInstanceIdSchema)}, - }, - }, - 404: { - description: "Instance id or User Id not found", - content: { - "application/json": { schema: resolver(deleteCategoriesByInstanceIdSchema)}, - }, - }, + "/instance/:instanceId", + describeRoute({ + description: "Delete all categories by instance id", + responses: { + 200: { + description: "Success deleting all categories in instance", + content: { + "application/json": { + schema: resolver(deleteCategoriesByInstanceIdSchema), + }, }, - }), - zValidator("json", deleteCategoriesByInstanceIdSchema), - async (c) => { - const instanceId = c.req.param("instanceId"); - const data = c.req.valid("json") as DeleteCategoriesByInstanceIdInput; - - // Ensure the instanceId in the path matches the one in the body - if (data.instanceId !== instanceId) { - return c.json({ error: "Instance ID in path does not match Instance ID in body" }, 400); - } - - const categoryData = await deleteAllCategoriesByInstance(data); - if (categoryData) { - return c.json(categoryData); - } else { - return c.json({ error: "Failed to delete categories" }, 400); - } - } -) + }, + 400: { + description: "Bad Request - Invalid input data", + content: { + "application/json": { + schema: resolver(deleteCategoriesByInstanceIdSchema), + }, + }, + }, + 401: { + description: "Unauthorized - Admin access required", + content: { + "application/json": { + schema: resolver(deleteCategoriesByInstanceIdSchema), + }, + }, + }, + 404: { + description: "Instance id or User Id not found", + content: { + "application/json": { + schema: resolver(deleteCategoriesByInstanceIdSchema), + }, + }, + }, + }, + }), + zValidator("json", deleteCategoriesByInstanceIdSchema), + async (c) => { + const instanceId = c.req.param("instanceId"); + const data = c.req.valid("json") as DeleteCategoriesByInstanceIdInput; -export { categoryRoutes }; \ No newline at end of file + // Ensure the instanceId in the path matches the one in the body + if (data.instanceId !== instanceId) { + return c.json( + { error: "Instance ID in path does not match Instance ID in body" }, + 400, + ); + } + + const categoryData = await deleteAllCategoriesByInstance(data); + if (categoryData) { + return c.json(categoryData); + } else { + return c.json({ error: "Failed to delete categories" }, 400); + } + }, +); + +export { categoryRoutes }; diff --git a/concord-server/src/routes/channelRoutes.ts b/concord-server/src/routes/channelRoutes.ts index e650baf..43f38ad 100644 --- a/concord-server/src/routes/channelRoutes.ts +++ b/concord-server/src/routes/channelRoutes.ts @@ -1,299 +1,313 @@ import { - createNewChannel, - fetchChannelData, - fetchChannelsByCategory, - updateExistingChannel, - deleteExistingChannel, - deleteAllChannelsByCategory, + createNewChannel, + fetchChannelData, + fetchChannelsByCategory, + updateExistingChannel, + deleteExistingChannel, + deleteAllChannelsByCategory, } from "../controller/channelController"; import { - createChannelSchema, - getChannelSchema, - getChannelsByCategoryIdSchema, - updateChannelSchema, - deleteChannelSchema, - deleteChannelsByCategoryIdSchema, - - CreateChannelInput, - GetChannelInput, - GetChannelsByCategoryIdInput, - UpdateChannelInput, - DeleteChannelInput, - DeleteChannelsByCategoryIdInput, + createChannelSchema, + getChannelSchema, + getChannelsByCategoryIdSchema, + updateChannelSchema, + deleteChannelSchema, + deleteChannelsByCategoryIdSchema, + CreateChannelInput, + GetChannelInput, + GetChannelsByCategoryIdInput, + UpdateChannelInput, + DeleteChannelInput, + DeleteChannelsByCategoryIdInput, } from "../validators/channelValidator"; import { zValidator } from "@hono/zod-validator"; import { Hono } from "hono"; import { describeRoute, resolver } from "hono-openapi"; -const channelRoutes = new Hono() +const channelRoutes = new Hono(); // Create a new channel channelRoutes.post( - "/", - describeRoute({ - description: "Create a new channel", - responses: { - 200: { - description: "Success creating channel", - content: { - "application/json": { schema: resolver(createChannelSchema) }, - }, - }, - 400: { - description: "Bad Request - Invalid input data", - content: { - "application/json": { schema: resolver(createChannelSchema)}, - }, - }, - 401: { - description: "Unauthorized - Admin access required", - content: { - "application/json": { schema: resolver(createChannelSchema)}, - }, - }, - 404: { - description: "User Id not found", - content: { - "application/json": { schema: resolver(createChannelSchema)}, - }, - }, + "/", + describeRoute({ + description: "Create a new channel", + responses: { + 200: { + description: "Success creating channel", + content: { + "application/json": { schema: resolver(createChannelSchema) }, }, - }), - zValidator("json", createChannelSchema), - async (c) => { - const data = c.req.valid("json") as CreateChannelInput; - const channelData = await createNewChannel(data); - if (channelData) { - return c.json(channelData); - } else { - return c.json({ error: "Failed to create channel" }, 400); - } + }, + 400: { + description: "Bad Request - Invalid input data", + content: { + "application/json": { schema: resolver(createChannelSchema) }, + }, + }, + 401: { + description: "Unauthorized - Admin access required", + content: { + "application/json": { schema: resolver(createChannelSchema) }, + }, + }, + 404: { + description: "User Id not found", + content: { + "application/json": { schema: resolver(createChannelSchema) }, + }, + }, + }, + }), + zValidator("json", createChannelSchema), + async (c) => { + const data = c.req.valid("json") as CreateChannelInput; + const channelData = await createNewChannel(data); + if (channelData) { + return c.json(channelData); + } else { + return c.json({ error: "Failed to create channel" }, 400); } -) + }, +); // Get a channel by ID channelRoutes.get( - "/:id", - describeRoute({ - description: "Get channel by id", - responses: { - 200: { - description: "Success getting channel", - content: { - "application/json": { schema: resolver(getChannelSchema) }, - }, - }, - 404: { - description: "Channel id not found", - content: { - "application/json": { schema: resolver(getChannelSchema) }, - }, - }, + "/:id", + describeRoute({ + description: "Get channel by id", + responses: { + 200: { + description: "Success getting channel", + content: { + "application/json": { schema: resolver(getChannelSchema) }, }, - }), - async (c) => { - const id = c.req.param("id"); - const channelData = await fetchChannelData(id); - if (channelData) { - return c.json(channelData); - } else { - return c.json({ error: "Channel not found" }, 404); - } + }, + 404: { + description: "Channel id not found", + content: { + "application/json": { schema: resolver(getChannelSchema) }, + }, + }, + }, + }), + async (c) => { + const id = c.req.param("id"); + const channelData = await fetchChannelData(id); + if (channelData) { + return c.json(channelData); + } else { + return c.json({ error: "Channel not found" }, 404); } + }, ); // Get all channels by category ID channelRoutes.get( - "/category/:categoryId", - describeRoute({ - description: "Get all channels by category id", - responses: { - 200: { - description: "Success getting all channels in category", - content: { - "application/json": { schema: resolver(getChannelsByCategoryIdSchema) }, - }, - }, - 400: { - description: "Bad Request - Missing category ID", - content: { - "application/json": { schema: resolver(getChannelsByCategoryIdSchema) }, - }, - }, + "/category/:categoryId", + describeRoute({ + description: "Get all channels by category id", + responses: { + 200: { + description: "Success getting all channels in category", + content: { + "application/json": { + schema: resolver(getChannelsByCategoryIdSchema), + }, }, - }), - async (c) => { - const categoryId = c.req.param("categoryId"); - if (!categoryId) { - return c.json({ error: "No category id provided" }, 400); - } - - const channels = await fetchChannelsByCategory(categoryId); - if (channels) { - return c.json(channels); - } else { - return c.json({ error: "Error getting channels from category" }, 500); - } + }, + 400: { + description: "Bad Request - Missing category ID", + content: { + "application/json": { + schema: resolver(getChannelsByCategoryIdSchema), + }, + }, + }, + }, + }), + async (c) => { + const categoryId = c.req.param("categoryId"); + if (!categoryId) { + return c.json({ error: "No category id provided" }, 400); } + + const channels = await fetchChannelsByCategory(categoryId); + if (channels) { + return c.json(channels); + } else { + return c.json({ error: "Error getting channels from category" }, 500); + } + }, ); // Update a channel channelRoutes.put( - "/:id", - describeRoute({ - description: "Update an existing channel", - responses: { - 200: { - description: "Success updating channel", - content: { - "application/json": { schema: resolver(updateChannelSchema) }, - }, - }, - 400: { - description: "Bad Request - Invalid input data", - content: { - "application/json": { schema: resolver(updateChannelSchema)}, - }, - }, - 401: { - description: "Unauthorized - Admin access required", - content: { - "application/json": { schema: resolver(updateChannelSchema)}, - }, - }, - 404: { - description: "Channel id or User Id not found", - content: { - "application/json": { schema: resolver(updateChannelSchema)}, - }, - }, + "/:id", + describeRoute({ + description: "Update an existing channel", + responses: { + 200: { + description: "Success updating channel", + content: { + "application/json": { schema: resolver(updateChannelSchema) }, }, - }), - zValidator("json", updateChannelSchema), - async (c) => { - const id = c.req.param("id"); - const data = c.req.valid("json") as UpdateChannelInput; - - // Ensure the ID in the path matches the one in the body - if (data.id && data.id !== id) { - return c.json({ error: "ID in path does not match ID in body" }, 400); - } - - // Set ID from path if not in body - if (!data.id) { - data.id = id; - } - - const result = await updateExistingChannel(data); - if (result) { - return c.json(result); - } else { - return c.json({ error: "Failed to update channel" }, 400); - } + }, + 400: { + description: "Bad Request - Invalid input data", + content: { + "application/json": { schema: resolver(updateChannelSchema) }, + }, + }, + 401: { + description: "Unauthorized - Admin access required", + content: { + "application/json": { schema: resolver(updateChannelSchema) }, + }, + }, + 404: { + description: "Channel id or User Id not found", + content: { + "application/json": { schema: resolver(updateChannelSchema) }, + }, + }, + }, + }), + zValidator("json", updateChannelSchema), + async (c) => { + const id = c.req.param("id"); + const data = c.req.valid("json") as UpdateChannelInput; + + // Ensure the ID in the path matches the one in the body + if (data.id && data.id !== id) { + return c.json({ error: "ID in path does not match ID in body" }, 400); } + + // Set ID from path if not in body + if (!data.id) { + data.id = id; + } + + const result = await updateExistingChannel(data); + if (result) { + return c.json(result); + } else { + return c.json({ error: "Failed to update channel" }, 400); + } + }, ); // Delete a specific channel channelRoutes.delete( - "/:id", - describeRoute({ - description: "Delete an existing channel", - responses: { - 200: { - description: "Success deleting channel", - content: { - "application/json": { schema: resolver(deleteChannelSchema) }, - }, - }, - 400: { - description: "Bad Request - Invalid input data", - content: { - "application/json": { schema: resolver(deleteChannelSchema)}, - }, - }, - 401: { - description: "Unauthorized - Admin access required", - content: { - "application/json": { schema: resolver(deleteChannelSchema)}, - }, - }, - 404: { - description: "Channel id or User Id not found", - content: { - "application/json": { schema: resolver(deleteChannelSchema)}, - }, - }, + "/:id", + describeRoute({ + description: "Delete an existing channel", + responses: { + 200: { + description: "Success deleting channel", + content: { + "application/json": { schema: resolver(deleteChannelSchema) }, }, - }), - zValidator("json", deleteChannelSchema), - async (c) => { - const id = c.req.param("id"); - const data = c.req.valid("json") as DeleteChannelInput; - - // Ensure the ID in the path matches the one in the body - if (data.id !== id) { - return c.json({ error: "ID in path does not match ID in body" }, 400); - } - - const result = await deleteExistingChannel(data); - if (result) { - return c.json({ success: true }); - } else { - return c.json({ error: "Failed to delete channel" }, 400); - } + }, + 400: { + description: "Bad Request - Invalid input data", + content: { + "application/json": { schema: resolver(deleteChannelSchema) }, + }, + }, + 401: { + description: "Unauthorized - Admin access required", + content: { + "application/json": { schema: resolver(deleteChannelSchema) }, + }, + }, + 404: { + description: "Channel id or User Id not found", + content: { + "application/json": { schema: resolver(deleteChannelSchema) }, + }, + }, + }, + }), + zValidator("json", deleteChannelSchema), + async (c) => { + const id = c.req.param("id"); + const data = c.req.valid("json") as DeleteChannelInput; + + // Ensure the ID in the path matches the one in the body + if (data.id !== id) { + return c.json({ error: "ID in path does not match ID in body" }, 400); } + + const result = await deleteExistingChannel(data); + if (result) { + return c.json({ success: true }); + } else { + return c.json({ error: "Failed to delete channel" }, 400); + } + }, ); // Delete all channels by category ID channelRoutes.delete( - "/category/:categoryId", - describeRoute({ - description: "Delete all channels by category id", - responses: { - 200: { - description: "Success deleting all channels in category", - content: { - "application/json": { schema: resolver(deleteChannelsByCategoryIdSchema) }, - }, - }, - 400: { - description: "Bad Request - Invalid input data", - content: { - "application/json": { schema: resolver(deleteChannelsByCategoryIdSchema)}, - }, - }, - 401: { - description: "Unauthorized - Admin access required", - content: { - "application/json": { schema: resolver(deleteChannelsByCategoryIdSchema)}, - }, - }, - 404: { - description: "Category id or User Id not found", - content: { - "application/json": { schema: resolver(deleteChannelsByCategoryIdSchema)}, - }, - }, + "/category/:categoryId", + describeRoute({ + description: "Delete all channels by category id", + responses: { + 200: { + description: "Success deleting all channels in category", + content: { + "application/json": { + schema: resolver(deleteChannelsByCategoryIdSchema), + }, }, - }), - zValidator("json", deleteChannelsByCategoryIdSchema), - async (c) => { - const categoryId = c.req.param("categoryId"); - const data = c.req.valid("json") as DeleteChannelsByCategoryIdInput; - - // Ensure the categoryId in the path matches the one in the body - if (data.categoryId !== categoryId) { - return c.json({ error: "Category ID in path does not match Category ID in body" }, 400); - } - - const result = await deleteAllChannelsByCategory(data); - if (result) { - return c.json({ success: true }); - } else { - return c.json({ error: "Failed to delete channels" }, 400); - } - } -) + }, + 400: { + description: "Bad Request - Invalid input data", + content: { + "application/json": { + schema: resolver(deleteChannelsByCategoryIdSchema), + }, + }, + }, + 401: { + description: "Unauthorized - Admin access required", + content: { + "application/json": { + schema: resolver(deleteChannelsByCategoryIdSchema), + }, + }, + }, + 404: { + description: "Category id or User Id not found", + content: { + "application/json": { + schema: resolver(deleteChannelsByCategoryIdSchema), + }, + }, + }, + }, + }), + zValidator("json", deleteChannelsByCategoryIdSchema), + async (c) => { + const categoryId = c.req.param("categoryId"); + const data = c.req.valid("json") as DeleteChannelsByCategoryIdInput; -export { channelRoutes }; \ No newline at end of file + // Ensure the categoryId in the path matches the one in the body + if (data.categoryId !== categoryId) { + return c.json( + { error: "Category ID in path does not match Category ID in body" }, + 400, + ); + } + + const result = await deleteAllChannelsByCategory(data); + if (result) { + return c.json({ success: true }); + } else { + return c.json({ error: "Failed to delete channels" }, 400); + } + }, +); + +export { channelRoutes }; diff --git a/concord-server/src/routes/instanceRoutes.ts b/concord-server/src/routes/instanceRoutes.ts index 5fea872..4e27d75 100644 --- a/concord-server/src/routes/instanceRoutes.ts +++ b/concord-server/src/routes/instanceRoutes.ts @@ -1,63 +1,77 @@ import { Hono } from "hono"; import { describeRoute, resolver } from "hono-openapi"; -import { createInstanceRequestSchema, getAllInstancesResponseSchema } from "../validators/instanceValidator"; +import { + createInstanceRequestSchema, + getAllInstancesResponseSchema, +} from "../validators/instanceValidator"; import { zValidator } from "@hono/zod-validator"; -import { createInstanceReq, getAllInstancesReq } from "../controller/instanceController"; +import { + createInstanceReq, + getAllInstancesReq, +} from "../controller/instanceController"; const instanceRoutes = new Hono(); instanceRoutes.post( - "", - describeRoute({ - description: "Create instance", - responses: { - 200: { - description: "Instance created", - content: { - "application/json": { schema: resolver(createInstanceRequestSchema) } - }, - }, - 400: { - description: "Invalid request", - }, - } - }), - zValidator('json', createInstanceRequestSchema), - async (c) => { - const data = await c.req.json(); - if (!data) { - return c.json({ error: "could not parse data" }, 400); - } - - const instance = await createInstanceReq(data); - return c.json(instance, 201); + "", + describeRoute({ + description: "Create instance", + responses: { + 200: { + description: "Instance created", + content: { + "application/json": { schema: resolver(createInstanceRequestSchema) }, + }, + }, + 400: { + description: "Invalid request", + }, + }, + }), + zValidator("json", createInstanceRequestSchema), + async (c) => { + const data = await c.req.json(); + if (!data) { + return c.json({ error: "could not parse data" }, 400); } + + const instance = await createInstanceReq(data); + return c.json(instance, 201); + }, ); instanceRoutes.get( - "", - describeRoute({ - description: "Get all instances", - responses: { - 200: { - description: "List of all instances", - content: { - "application/json": { schema: resolver(getAllInstancesResponseSchema) } - }, - }, - 500: { - description: "Server error", - }, - } - }), - async (c) => { - const instances = await getAllInstancesReq(); - if (instances.success) { - return c.json(instances, 200); - } else { - return c.json({ success: false, error: instances.error || "Failed to fetch instances" }, 500); - } + "", + describeRoute({ + description: "Get all instances", + responses: { + 200: { + description: "List of all instances", + content: { + "application/json": { + schema: resolver(getAllInstancesResponseSchema), + }, + }, + }, + 500: { + description: "Server error", + }, + }, + }), + async (c) => { + const instances = await getAllInstancesReq(); + if (instances.success) { + return c.json(instances, 200); + } else { + return c.json( + { + success: false, + error: instances.error || "Failed to fetch instances", + }, + 500, + ); } + }, ); export default instanceRoutes; diff --git a/concord-server/src/routes/messageRoutes.ts b/concord-server/src/routes/messageRoutes.ts index 2733231..17a576e 100644 --- a/concord-server/src/routes/messageRoutes.ts +++ b/concord-server/src/routes/messageRoutes.ts @@ -1,121 +1,146 @@ import { Hono } from "hono"; import { describeResponse, describeRoute, resolver } from "hono-openapi"; -import { getMessageByIdSchema, getMessagesBeforeDate, sendMessageSchema } from "../validators/messageValidator"; +import { + getMessageByIdSchema, + getMessagesBeforeDate, + sendMessageSchema, +} from "../validators/messageValidator"; import { zValidator } from "@hono/zod-validator"; -import { fetchMessageData, fetchMessagesBefore, sendMessage } from "../controller/messageController"; +import { + fetchMessageData, + fetchMessagesBefore, + sendMessage, +} from "../controller/messageController"; const messageRoutes = new Hono(); messageRoutes.get( - "/:id", - describeRoute({ - description: "Get message by id", - responses: { - 200: { - description: "Success getting message", - content: { - "application/json": { schema: resolver(getMessageByIdSchema) } - } - }, - 404: { - description: "Message id not found", - content: { - "application/json": { schema: resolver(getMessageByIdSchema) } - } - } - } - }), - zValidator("param", getMessageByIdSchema), - async (c) => { - const id = c.req.param("id"); - const messageData = await fetchMessageData(id); + "/:id", + describeRoute({ + description: "Get message by id", + responses: { + 200: { + description: "Success getting message", + content: { + "application/json": { schema: resolver(getMessageByIdSchema) }, + }, + }, + 404: { + description: "Message id not found", + content: { + "application/json": { schema: resolver(getMessageByIdSchema) }, + }, + }, + }, + }), + zValidator("param", getMessageByIdSchema), + async (c) => { + const id = c.req.param("id"); + const messageData = await fetchMessageData(id); - if (messageData) { - return c.json(messageData, 200); - } else { - return c.json({ error: "Message not found" }, 404); - } + if (messageData) { + return c.json(messageData, 200); + } else { + return c.json({ error: "Message not found" }, 404); } -) + }, +); messageRoutes.get( - "", - describeRoute({ - description: "Get up to 50 messages prior to given datetime", - responses: { - 200: { - description: "Success getting up to 50 messages", - content: { - "application/json": { schema: resolver(getMessagesBeforeDate) } - } - } - } - }), - zValidator("query", getMessagesBeforeDate), - async (c) => { - const date = c.req.query("date"); - if (!date) { - return c.json({ error: "date not provided" }, 400); - } - - const channelId = c.req.query("channelId"); - if (!channelId) { - return c.json({ error: "channelId not provided" }, 400); - } - - const messagesArr = await fetchMessagesBefore(date, channelId); - - if (messagesArr) { - return c.json(messagesArr, 200); - } else { - return c.json({ error: "Failed to fetch messages" }, 500); - } + "", + describeRoute({ + description: "Get up to 50 messages prior to given datetime", + responses: { + 200: { + description: "Success getting up to 50 messages", + content: { + "application/json": { schema: resolver(getMessagesBeforeDate) }, + }, + }, + }, + }), + zValidator("query", getMessagesBeforeDate), + async (c) => { + const date = c.req.query("date"); + if (!date) { + return c.json({ error: "date not provided" }, 400); } -) + + const channelId = c.req.query("channelId"); + if (!channelId) { + return c.json({ error: "channelId not provided" }, 400); + } + + const messagesArr = await fetchMessagesBefore(date, channelId); + + if (messagesArr) { + return c.json(messagesArr, 200); + } else { + return c.json({ error: "Failed to fetch messages" }, 500); + } + }, +); messageRoutes.post( - "", - describeRoute({ - description: "Send a message to a channel", - responses: { - 201: { - description: "Message sent successfully", - content: { - "application/json": { schema: resolver(sendMessageSchema) } - } + "", + describeRoute({ + description: "Send a message to a channel", + responses: { + 201: { + description: "Message sent successfully", + content: { + "application/json": { schema: resolver(sendMessageSchema) }, + }, + }, + 401: { + description: "Unauthorized - invalid token or user credentials", + content: { + "application/json": { + schema: { + type: "object", + properties: { error: { type: "string" } }, }, - 401: { - description: "Unauthorized - invalid token or user credentials", - content: { - "application/json": { schema: { type: "object", properties: { error: { type: "string" } } } } - } + }, + }, + }, + 500: { + description: "Server error", + content: { + "application/json": { + schema: { + type: "object", + properties: { error: { type: "string" } }, }, - 500: { - description: "Server error", - content: { - "application/json": { schema: { type: "object", properties: { error: { type: "string" } } } } - } - } - } - }), - zValidator("json", sendMessageSchema), - async (c) => { - const { channelId, userId, content, token, repliedMessageId } = await c.req.json(); - - const result = await sendMessage( - channelId, - userId, - content, - token, - repliedMessageId || null - ); + }, + }, + }, + }, + }), + zValidator("json", sendMessageSchema), + async (c) => { + const { channelId, userId, content, token, repliedMessageId } = + await c.req.json(); - if (result) { - return c.json(result, 201); - } else { - return c.json({ error: "Failed to send message. Check your credentials and try again." }, 401); - } + const result = await sendMessage( + channelId, + userId, + content, + token, + repliedMessageId || null, + ); + + if (result) { + return c.json(result, 201); + } else { + return c.json( + { + error: + "Failed to send message. Check your credentials and try again.", + }, + 401, + ); } -) + }, +); -export default messageRoutes; \ No newline at end of file +export default messageRoutes; diff --git a/concord-server/src/services/channelService.ts b/concord-server/src/services/channelService.ts index 17c2b0f..4e12c51 100644 --- a/concord-server/src/services/channelService.ts +++ b/concord-server/src/services/channelService.ts @@ -1,450 +1,457 @@ -import{ - Channel, - Category -} from '@prisma/client'; +import { Channel, Category } from "@prisma/client"; import { PrismaClient } from "@prisma/client"; -import { getUserInformation, getUserCredentials } from './userService'; +import { getUserInformation, getUserCredentials } from "./userService"; import { - CreateChannelInput, - UpdateChannelInput, - DeleteChannelInput, - DeleteChannelsByCategoryIdInput -} from '../validators/channelValidator'; -import{ - UpdateCategoryInput, - DeleteCategoryInput, - DeleteCategoriesByInstanceIdInput, - CreateCategoryInput -} from '../validators/categoryValidator'; + CreateChannelInput, + UpdateChannelInput, + DeleteChannelInput, + DeleteChannelsByCategoryIdInput, +} from "../validators/channelValidator"; +import { + UpdateCategoryInput, + DeleteCategoryInput, + DeleteCategoriesByInstanceIdInput, + CreateCategoryInput, +} from "../validators/categoryValidator"; const prisma = new PrismaClient(); +export async function createCategory( + data: CreateCategoryInput, +): Promise { + try { + //Confirm if user exists and is admin + const requestingUser = await getUserInformation(data.requestingUserId); + const requestingUserCredentials = await getUserCredentials( + data.requestingUserId, + ); -export async function createCategory(data: CreateCategoryInput): Promise{ - try{ - - //Confirm if user exists and is admin - const requestingUser = await getUserInformation(data.requestingUserId); - const requestingUserCredentials = await getUserCredentials( - data.requestingUserId, - ) - - if ( - !requestingUser || - !requestingUserCredentials || - !requestingUser.admin || - requestingUserCredentials.token == null || - data.requestingUserToken != requestingUserCredentials.token - ) { - return null; - } - - const newCategory = await prisma.category.create({ - data: { - name: data.name, - position: data.position - } - }); - - if(!newCategory){ - throw new Error("could not create category"); - } - - let curInstance; - if(data.instanceId){ - curInstance = await prisma.instance.findUnique({ - where: { - id: data.instanceId - }, - include: { - Category: true - } - }); - - if(!curInstance){ - throw new Error("could not find instance to add category to"); - } - - await prisma.category.update({ - where: { - id: newCategory.id - }, - data: { - instanceId: curInstance.id - } - }); - - return newCategory; - } - - return newCategory; - }catch(err){ - console.log("services::channelService::createCategory - ", err); - return null; + if ( + !requestingUser || + !requestingUserCredentials || + !requestingUser.admin || + requestingUserCredentials.token == null || + data.requestingUserToken != requestingUserCredentials.token + ) { + return null; } + + const newCategory = await prisma.category.create({ + data: { + name: data.name, + position: data.position, + }, + }); + + if (!newCategory) { + throw new Error("could not create category"); + } + + let curInstance; + if (data.instanceId) { + curInstance = await prisma.instance.findUnique({ + where: { + id: data.instanceId, + }, + include: { + Category: true, + }, + }); + + if (!curInstance) { + throw new Error("could not find instance to add category to"); + } + + await prisma.category.update({ + where: { + id: newCategory.id, + }, + data: { + instanceId: curInstance.id, + }, + }); + + return newCategory; + } + + return newCategory; + } catch (err) { + console.log("services::channelService::createCategory - ", err); + return null; + } } export async function getCategory( - categoryId: string, -): Promise{ - try{ - const category = await prisma.category.findUnique({ - where: { - id: categoryId - } - }); - - if(!category){ - throw new Error("could not find category"); - } + categoryId: string, +): Promise { + try { + const category = await prisma.category.findUnique({ + where: { + id: categoryId, + }, + }); - return category; - }catch(err){ - console.log("services::channelService::getCategory - ", err); - return null; + if (!category) { + throw new Error("could not find category"); } + + return category; + } catch (err) { + console.log("services::channelService::getCategory - ", err); + return null; + } } export async function getCategoriesByInstance( - instanceId: string -): Promise{ - try{ - const categories = await prisma.category.findMany({ - where: { - instanceId: instanceId - }, - include: { - Channel: true - }, - orderBy: { - position: 'asc' - } - }); + instanceId: string, +): Promise { + try { + const categories = await prisma.category.findMany({ + where: { + instanceId: instanceId, + }, + include: { + Channel: true, + }, + orderBy: { + position: "asc", + }, + }); - if(!categories){ - throw new Error("could not find categories for instance"); - } - - return categories; - }catch(err){ - console.log("services::channelService::getCategoriesByInstance - ", err); - return null; + if (!categories) { + throw new Error("could not find categories for instance"); } + + return categories; + } catch (err) { + console.log("services::channelService::getCategoriesByInstance - ", err); + return null; + } } -export async function updateCategory(data: UpdateCategoryInput): Promise{ - try{ +export async function updateCategory( + data: UpdateCategoryInput, +): Promise { + try { + //Confirm if user exists and is admin + const requestingUser = await getUserInformation(data.requestingUserId); + const requestingUserCredentials = await getUserCredentials( + data.requestingUserId, + ); - //Confirm if user exists and is admin - const requestingUser = await getUserInformation(data.requestingUserId); - const requestingUserCredentials = await getUserCredentials( - data.requestingUserId, - ) - - if ( - !requestingUser || - !requestingUserCredentials || - !requestingUser.admin || - requestingUserCredentials.token == null || - data.requestingUserToken != requestingUserCredentials.token - ) { - return null; - } - - const updatedCategory = await prisma.category.update({ - where: { - id: data.id - }, - data: { - name: data.name, - position: data.position, - Channel: data.channels ? { set: data.channels } : undefined - } - }); - - if(!updatedCategory){ - throw new Error("could not update category"); - } - - return updatedCategory; - }catch(err){ - console.log("services::channelService::updateCategory - ", err); - return null; + if ( + !requestingUser || + !requestingUserCredentials || + !requestingUser.admin || + requestingUserCredentials.token == null || + data.requestingUserToken != requestingUserCredentials.token + ) { + return null; } + + const updatedCategory = await prisma.category.update({ + where: { + id: data.id, + }, + data: { + name: data.name, + position: data.position, + Channel: data.channels ? { set: data.channels } : undefined, + }, + }); + + if (!updatedCategory) { + throw new Error("could not update category"); + } + + return updatedCategory; + } catch (err) { + console.log("services::channelService::updateCategory - ", err); + return null; + } } -export async function deleteCategory(data: DeleteCategoryInput): Promise{ - try{ +export async function deleteCategory( + data: DeleteCategoryInput, +): Promise { + try { + //Confirm if user exists and is admin + const requestingUser = await getUserInformation(data.requestingUserId); + const requestingUserCredentials = await getUserCredentials( + data.requestingUserId, + ); - //Confirm if user exists and is admin - const requestingUser = await getUserInformation(data.requestingUserId); - const requestingUserCredentials = await getUserCredentials( - data.requestingUserId, - ) - - if ( - !requestingUser || - !requestingUserCredentials || - !requestingUser.admin || - requestingUserCredentials.token == null || - data.requestingUserToken != requestingUserCredentials.token - ) { - return null; - } - - const deleteAllChannels = await prisma.channel.deleteMany({ - where: { - categoryId: data.id - } - }); - - if(deleteAllChannels.count === 0){ - throw new Error("could not delete channels from category"); - } - - const deletedCategory = await prisma.category.delete({ - where: { - id: data.id - } - }); - - if(!deletedCategory){ - throw new Error("could not delete category"); - } - - return true; - }catch(err){ - console.log("services::channelService::deleteCategory - ", err); - return false; + if ( + !requestingUser || + !requestingUserCredentials || + !requestingUser.admin || + requestingUserCredentials.token == null || + data.requestingUserToken != requestingUserCredentials.token + ) { + return null; } + + const deleteAllChannels = await prisma.channel.deleteMany({ + where: { + categoryId: data.id, + }, + }); + + if (deleteAllChannels.count === 0) { + throw new Error("could not delete channels from category"); + } + + const deletedCategory = await prisma.category.delete({ + where: { + id: data.id, + }, + }); + + if (!deletedCategory) { + throw new Error("could not delete category"); + } + + return true; + } catch (err) { + console.log("services::channelService::deleteCategory - ", err); + return false; + } } -export async function deleteAllCategoriesFromInstance(data: DeleteCategoriesByInstanceIdInput): Promise{ - try{ +export async function deleteAllCategoriesFromInstance( + data: DeleteCategoriesByInstanceIdInput, +): Promise { + try { + //Confirm if user exists and is admin + const requestingUser = await getUserInformation(data.requestingUserId); + const requestingUserCredentials = await getUserCredentials( + data.requestingUserId, + ); - //Confirm if user exists and is admin - const requestingUser = await getUserInformation(data.requestingUserId); - const requestingUserCredentials = await getUserCredentials( - data.requestingUserId, - ) - - if ( - !requestingUser || - !requestingUserCredentials || - !requestingUser.admin || - requestingUserCredentials.token == null || - data.requestingUserToken != requestingUserCredentials.token - ) { - return null; - } - - const deletedCategories = await prisma.category.deleteMany({ - where: { - instanceId: data.instanceId - } - }); - - if(deletedCategories.count === 0){ - throw new Error("could not delete categories from instance"); - } - - return true; - }catch(err){ - console.log("services::channelService::deleteAllCategoriesFromInstance - ", err); - return false; + if ( + !requestingUser || + !requestingUserCredentials || + !requestingUser.admin || + requestingUserCredentials.token == null || + data.requestingUserToken != requestingUserCredentials.token + ) { + return null; } + + const deletedCategories = await prisma.category.deleteMany({ + where: { + instanceId: data.instanceId, + }, + }); + + if (deletedCategories.count === 0) { + throw new Error("could not delete categories from instance"); + } + + return true; + } catch (err) { + console.log( + "services::channelService::deleteAllCategoriesFromInstance - ", + err, + ); + return false; + } } -export async function createChannel(data: CreateChannelInput): Promise{ - try{ +export async function createChannel( + data: CreateChannelInput, +): Promise { + try { + //Confirm if user exists and is admin + const requestingUser = await getUserInformation(data.requestingUserId); + const requestingUserCredentials = await getUserCredentials( + data.requestingUserId, + ); - //Confirm if user exists and is admin - const requestingUser = await getUserInformation(data.requestingUserId); - const requestingUserCredentials = await getUserCredentials( - data.requestingUserId, - ) - - if ( - !requestingUser || - !requestingUserCredentials || - !requestingUser.admin || - requestingUserCredentials.token == null || - data.requestingUserToken != requestingUserCredentials.token - ) { - return null; - } - - const newChannel = await prisma.channel.create({ - data: { - type: data.type, - name: data.name, - description: data.description, - categoryId: data.categoryId ? data.categoryId : null - } - }); - - if(!newChannel){ - throw new Error("could not create channel"); - } - - return newChannel; - }catch(err){ - console.log("services::channelService::createChannel - ", err); - return null; + if ( + !requestingUser || + !requestingUserCredentials || + !requestingUser.admin || + requestingUserCredentials.token == null || + data.requestingUserToken != requestingUserCredentials.token + ) { + return null; } + + const newChannel = await prisma.channel.create({ + data: { + type: data.type, + name: data.name, + description: data.description, + categoryId: data.categoryId ? data.categoryId : null, + }, + }); + + if (!newChannel) { + throw new Error("could not create channel"); + } + + return newChannel; + } catch (err) { + console.log("services::channelService::createChannel - ", err); + return null; + } } -export async function getChannel( - channelId: string -): Promise{ - try{ - const channel = await prisma.channel.findUnique({ - where: { - id: channelId - } - }); +export async function getChannel(channelId: string): Promise { + try { + const channel = await prisma.channel.findUnique({ + where: { + id: channelId, + }, + }); - if(!channel){ - throw new Error("could not find channel"); - } - - return channel; - }catch(err){ - console.log("services::channelService::getChannel - ", err); - return null; + if (!channel) { + throw new Error("could not find channel"); } + + return channel; + } catch (err) { + console.log("services::channelService::getChannel - ", err); + return null; + } } export async function getChannelsByCategory( - categoryId: string -): Promise{ - try{ - const channels = await prisma.channel.findMany({ - where: { - categoryId: categoryId - } - }); - - if(!channels){ - throw new Error("could not find channels for category"); - } - return channels; + categoryId: string, +): Promise { + try { + const channels = await prisma.channel.findMany({ + where: { + categoryId: categoryId, + }, + }); + + if (!channels) { + throw new Error("could not find channels for category"); } - catch(err){ - console.log("services::channelService::getChannelsByCategory - ", err); - return null; - } + return channels; + } catch (err) { + console.log("services::channelService::getChannelsByCategory - ", err); + return null; + } } -export async function updateChannel(data: UpdateChannelInput): Promise{ - try{ +export async function updateChannel( + data: UpdateChannelInput, +): Promise { + try { + //Confirm if user exists and is admin + const requestingUser = await getUserInformation(data.requestingUserId); + const requestingUserCredentials = await getUserCredentials( + data.requestingUserId, + ); - //Confirm if user exists and is admin - const requestingUser = await getUserInformation(data.requestingUserId); - const requestingUserCredentials = await getUserCredentials( - data.requestingUserId, - ) - - if ( - !requestingUser || - !requestingUserCredentials || - !requestingUser.admin || - requestingUserCredentials.token == null || - data.requestingUserToken != requestingUserCredentials.token - ) { - return null; - } - - const updatedChannel = await prisma.channel.update({ - where: { - id: data.id - }, - data: { - name: data.name, - description: data.description, - categoryId: data.categoryId ? data.categoryId : undefined - } - }); - - if(!updatedChannel){ - throw new Error("could not update channel"); - } - - return updatedChannel; - }catch(err){ - console.log("services::channelService::updateChannel - ", err); - return null; + if ( + !requestingUser || + !requestingUserCredentials || + !requestingUser.admin || + requestingUserCredentials.token == null || + data.requestingUserToken != requestingUserCredentials.token + ) { + return null; } + + const updatedChannel = await prisma.channel.update({ + where: { + id: data.id, + }, + data: { + name: data.name, + description: data.description, + categoryId: data.categoryId ? data.categoryId : undefined, + }, + }); + + if (!updatedChannel) { + throw new Error("could not update channel"); + } + + return updatedChannel; + } catch (err) { + console.log("services::channelService::updateChannel - ", err); + return null; + } } -export async function deleteChannel(data: DeleteChannelInput): Promise{ - try{ +export async function deleteChannel( + data: DeleteChannelInput, +): Promise { + try { + //Confirm if user exists and is admin + const requestingUser = await getUserInformation(data.requestingUserId); + const requestingUserCredentials = await getUserCredentials( + data.requestingUserId, + ); - //Confirm if user exists and is admin - const requestingUser = await getUserInformation(data.requestingUserId); - const requestingUserCredentials = await getUserCredentials( - data.requestingUserId, - ) - - if ( - !requestingUser || - !requestingUserCredentials || - !requestingUser.admin || - requestingUserCredentials.token == null || - data.requestingUserToken != requestingUserCredentials.token - ) { - return null; - } - - const deletedChannel = await prisma.channel.delete({ - where: { - id: data.id - } - }); - - if(!deletedChannel){ - throw new Error("could not delete channel"); - } - - return true; - }catch(err){ - console.log("services::channelService::deleteChannel - ", err); - return false; + if ( + !requestingUser || + !requestingUserCredentials || + !requestingUser.admin || + requestingUserCredentials.token == null || + data.requestingUserToken != requestingUserCredentials.token + ) { + return null; } + + const deletedChannel = await prisma.channel.delete({ + where: { + id: data.id, + }, + }); + + if (!deletedChannel) { + throw new Error("could not delete channel"); + } + + return true; + } catch (err) { + console.log("services::channelService::deleteChannel - ", err); + return false; + } } -export async function deleteAllChannelsFromCategory(data: DeleteChannelsByCategoryIdInput): Promise -{ - try{ - //Confirm if user exists and is admin - const requestingUser = await getUserInformation(data.requestingUserId); - const requestingUserCredentials = await getUserCredentials( - data.requestingUserId, - ) +export async function deleteAllChannelsFromCategory( + data: DeleteChannelsByCategoryIdInput, +): Promise { + try { + //Confirm if user exists and is admin + const requestingUser = await getUserInformation(data.requestingUserId); + const requestingUserCredentials = await getUserCredentials( + data.requestingUserId, + ); - if ( - !requestingUser || - !requestingUserCredentials || - !requestingUser.admin || - requestingUserCredentials.token == null || - data.requestingUserToken != requestingUserCredentials.token - ) { - return null; - } - - const deletedChannels = await prisma.channel.deleteMany({ - where: { - categoryId: data.categoryId - } - }); - - if(deletedChannels.count === 0){ - throw new Error("could not delete channels from category"); - } - - return true; - }catch(err){ - console.log("services::channelService::deleteAllChannelsFromCategory - ", err); - return false; + if ( + !requestingUser || + !requestingUserCredentials || + !requestingUser.admin || + requestingUserCredentials.token == null || + data.requestingUserToken != requestingUserCredentials.token + ) { + return null; } + + const deletedChannels = await prisma.channel.deleteMany({ + where: { + categoryId: data.categoryId, + }, + }); + + if (deletedChannels.count === 0) { + throw new Error("could not delete channels from category"); + } + + return true; + } catch (err) { + console.log( + "services::channelService::deleteAllChannelsFromCategory - ", + err, + ); + return false; + } } diff --git a/concord-server/src/services/instanceService.ts b/concord-server/src/services/instanceService.ts index d37f3a0..bb4008a 100644 --- a/concord-server/src/services/instanceService.ts +++ b/concord-server/src/services/instanceService.ts @@ -5,53 +5,55 @@ import { getUserCredentials, getUserInformation } from "./userService"; const prisma = new PrismaClient(); export async function createInstance(data: CreateInstanceRequest) { - try { - const creds = await getUserCredentials(data.requestingUserId); - const user = await getUserInformation(data.requestingUserId); - if (!creds - || creds.token != data.requestingUserToken - || !user - || !user.admin) { - return null; - } - - const newInstance = await prisma.instance.create({ - data: { - name: data.name, - icon: data.icon - } - }); - - return { - success: true, - data: newInstance - }; - } catch (error) { - console.error("Error creating instance:", error); - return { - success: false, - error: "Failed to create instance" - }; + try { + const creds = await getUserCredentials(data.requestingUserId); + const user = await getUserInformation(data.requestingUserId); + if ( + !creds || + creds.token != data.requestingUserToken || + !user || + !user.admin + ) { + return null; } + + const newInstance = await prisma.instance.create({ + data: { + name: data.name, + icon: data.icon, + }, + }); + + return { + success: true, + data: newInstance, + }; + } catch (error) { + console.error("Error creating instance:", error); + return { + success: false, + error: "Failed to create instance", + }; + } } export async function getAllInstances() { - try { - const instances = await prisma.instance.findMany({ - orderBy: { - createdAt: 'desc' - } - }); - - return { - success: true, - data: instances - }; - } catch (error) { - console.error("Error fetching instances:", error); - return { - success: false, - error: "Failed to fetch instances" - }; - } -} \ No newline at end of file + try { + const instances = await prisma.instance.findMany({ + orderBy: { + createdAt: "desc", + }, + }); + + return { + success: true, + data: instances, + }; + } catch (error) { + console.error("Error fetching instances:", error); + return { + success: false, + error: "Failed to fetch instances", + }; + } +} diff --git a/concord-server/src/services/messageService.ts b/concord-server/src/services/messageService.ts index 33c3ca1..a01f59e 100644 --- a/concord-server/src/services/messageService.ts +++ b/concord-server/src/services/messageService.ts @@ -1,16 +1,14 @@ -import { - PrismaClient, -} from "@prisma/client"; +import { PrismaClient } from "@prisma/client"; import { getUserCredentials } from "./userService"; const prisma = new PrismaClient(); -export async function getMessageInformation(id:string): Promise<{ - id: string, - channelId: string, - userId: string, - text: string, - deleted: boolean, +export async function getMessageInformation(id: string): Promise<{ + id: string; + channelId: string; + userId: string; + text: string; + deleted: boolean; replies: null | { messageId: string; repliesToId: string; @@ -66,26 +64,28 @@ export async function getMessageInformation(id:string): Promise<{ const errMessage = err as Error; if (errMessage.message === "missing messageId") { - console.log("services::actions::getMessageInformation - missing messageId"); + console.log( + "services::actions::getMessageInformation - missing messageId", + ); return null; } if (errMessage.message === "could not find message") { console.log( - "services::actions::getMessageInformation - unable to find message" + "services::actions::getMessageInformation - unable to find message", ); return null; } console.log( "services::actions::getMessageInformation - unknown error", - errMessage + errMessage, ); return null; } } -export async function getMessagesBefore(date:string, channelId:string) { +export async function getMessagesBefore(date: string, channelId: string) { try { if (!date || !channelId) { throw new Error("missing date or channelId"); @@ -141,13 +141,15 @@ export async function getMessagesBefore(date:string, channelId:string) { const errMessage = err as Error; if (errMessage.message === "missing date or channelId") { - console.log("services::actions::getMessagesBefore - missing date or channelId"); + console.log( + "services::actions::getMessagesBefore - missing date or channelId", + ); return null; } console.log( "services::actions::getMessagesBefore - unknown error", - errMessage + errMessage, ); return null; } diff --git a/concord-server/src/services/userService.ts b/concord-server/src/services/userService.ts index 6598b19..4730981 100644 --- a/concord-server/src/services/userService.ts +++ b/concord-server/src/services/userService.ts @@ -1,4 +1,3 @@ - import { Message, MessagePing, diff --git a/concord-server/src/validators/categoryValidator.ts b/concord-server/src/validators/categoryValidator.ts index 5dde0ee..f8ef491 100644 --- a/concord-server/src/validators/categoryValidator.ts +++ b/concord-server/src/validators/categoryValidator.ts @@ -1,55 +1,61 @@ -import { z } from 'zod'; +import { z } from "zod"; //category validators export const createCategorySchema = z.object({ - name: z.string().min(1).max(50), - position: z.number().min(0), - instanceId : z.uuidv7().optional(), - admin: z.boolean(), - requestingUserId: z.uuidv7(), - requestingUserToken: z.uuidv4() -}) + name: z.string().min(1).max(50), + position: z.number().min(0), + instanceId: z.uuidv7().optional(), + admin: z.boolean(), + requestingUserId: z.uuidv7(), + requestingUserToken: z.uuidv4(), +}); export const getCategorySchema = z.object({ - id: z.uuidv7() -}) + id: z.uuidv7(), +}); export const getCategoriesByInstanceIdSchema = z.object({ - instanceId: z.uuidv7() - -}) + instanceId: z.uuidv7(), +}); export const updateCategorySchema = z.object({ - id: z.uuidv7(), - name: z.string().min(1).max(50).optional(), - position: z.number().min(0).optional(), - channels: z.array(z.object({ - id: z.string() - })).optional(), - admin: z.boolean(), - requestingUserId: z.uuidv7(), - requestingUserToken: z.uuidv4() -}) + id: z.uuidv7(), + name: z.string().min(1).max(50).optional(), + position: z.number().min(0).optional(), + channels: z + .array( + z.object({ + id: z.string(), + }), + ) + .optional(), + admin: z.boolean(), + requestingUserId: z.uuidv7(), + requestingUserToken: z.uuidv4(), +}); export const deleteCategorySchema = z.object({ - id: z.uuidv7(), - admin: z.boolean(), - requestingUserId: z.uuidv7(), - requestingUserToken: z.uuidv4() -}) + id: z.uuidv7(), + admin: z.boolean(), + requestingUserId: z.uuidv7(), + requestingUserToken: z.uuidv4(), +}); export const deleteCategoriesByInstanceIdSchema = z.object({ - instanceId: z.uuidv7(), - admin: z.boolean(), - requestingUserId: z.uuidv7(), - requestingUserToken: z.uuidv4() -}) + instanceId: z.uuidv7(), + admin: z.boolean(), + requestingUserId: z.uuidv7(), + requestingUserToken: z.uuidv4(), +}); - -export type CreateCategoryInput = z.infer -export type GetCategoryInput = z.infer -export type GetCategoriesByInstanceIdInput = z.infer -export type UpdateCategoryInput = z.infer -export type DeleteCategoryInput = z.infer -export type DeleteCategoriesByInstanceIdInput = z.infer +export type CreateCategoryInput = z.infer; +export type GetCategoryInput = z.infer; +export type GetCategoriesByInstanceIdInput = z.infer< + typeof getCategoriesByInstanceIdSchema +>; +export type UpdateCategoryInput = z.infer; +export type DeleteCategoryInput = z.infer; +export type DeleteCategoriesByInstanceIdInput = z.infer< + typeof deleteCategoriesByInstanceIdSchema +>; diff --git a/concord-server/src/validators/channelValidator.ts b/concord-server/src/validators/channelValidator.ts index 53e48bc..23cb918 100644 --- a/concord-server/src/validators/channelValidator.ts +++ b/concord-server/src/validators/channelValidator.ts @@ -3,50 +3,54 @@ import { z } from "zod"; //channel validators export const createChannelSchema = z.object({ - type: z.enum(['text', 'voice']), - name: z.string().min(1).max(50), - description: z.string().max(255), - categoryId: z.uuidv7().optional(), - admin: z.boolean(), - requestingUserId: z.uuidv7(), - requestingUserToken: z.uuidv4() -}) + type: z.enum(["text", "voice"]), + name: z.string().min(1).max(50), + description: z.string().max(255), + categoryId: z.uuidv7().optional(), + admin: z.boolean(), + requestingUserId: z.uuidv7(), + requestingUserToken: z.uuidv4(), +}); export const getChannelSchema = z.object({ - id: z.uuidv7() -}) + id: z.uuidv7(), +}); export const getChannelsByCategoryIdSchema = z.object({ - categoryId: z.uuidv7() -}) + categoryId: z.uuidv7(), +}); export const updateChannelSchema = z.object({ - id: z.uuidv7(), - name: z.string().min(1).max(50).optional(), - description: z.string().max(255).optional(), - categoryId: z.uuidv7().optional(), - admin: z.boolean(), - requestingUserId: z.uuidv7(), - requestingUserToken: z.uuidv4() -}) + id: z.uuidv7(), + name: z.string().min(1).max(50).optional(), + description: z.string().max(255).optional(), + categoryId: z.uuidv7().optional(), + admin: z.boolean(), + requestingUserId: z.uuidv7(), + requestingUserToken: z.uuidv4(), +}); export const deleteChannelSchema = z.object({ - id: z.uuidv7(), - admin: z.boolean(), - requestingUserId: z.uuidv7(), - requestingUserToken: z.uuidv4() -}) + id: z.uuidv7(), + admin: z.boolean(), + requestingUserId: z.uuidv7(), + requestingUserToken: z.uuidv4(), +}); export const deleteChannelsByCategoryIdSchema = z.object({ - categoryId: z.uuidv7(), - admin: z.boolean(), - requestingUserId: z.uuidv7(), - requestingUserToken: z.uuidv4() -}) + categoryId: z.uuidv7(), + admin: z.boolean(), + requestingUserId: z.uuidv7(), + requestingUserToken: z.uuidv4(), +}); -export type CreateChannelInput = z.infer -export type GetChannelInput = z.infer -export type GetChannelsByCategoryIdInput = z.infer -export type UpdateChannelInput = z.infer -export type DeleteChannelInput = z.infer -export type DeleteChannelsByCategoryIdInput = z.infer \ No newline at end of file +export type CreateChannelInput = z.infer; +export type GetChannelInput = z.infer; +export type GetChannelsByCategoryIdInput = z.infer< + typeof getChannelsByCategoryIdSchema +>; +export type UpdateChannelInput = z.infer; +export type DeleteChannelInput = z.infer; +export type DeleteChannelsByCategoryIdInput = z.infer< + typeof deleteChannelsByCategoryIdSchema +>; diff --git a/concord-server/src/validators/instanceValidator.ts b/concord-server/src/validators/instanceValidator.ts index a4497fe..880f957 100644 --- a/concord-server/src/validators/instanceValidator.ts +++ b/concord-server/src/validators/instanceValidator.ts @@ -1,29 +1,33 @@ -import { z } from 'zod'; +import { z } from "zod"; export const createInstanceRequestSchema = z.object({ - name: z.string().min(1, 'Instance name cannot be empty'), - icon: z.url().optional(), - requestingUserId: z.uuidv7(), - requestingUserToken: z.string() + name: z.string().min(1, "Instance name cannot be empty"), + icon: z.url().optional(), + requestingUserId: z.uuidv7(), + requestingUserToken: z.string(), }); export const getAllInstancesResponseSchema = z.object({ - success: z.boolean(), - data: z.array( - z.object({ - id: z.string(), - name: z.string(), - icon: z.string().nullable(), - createdAt: z.string().refine((val) => !isNaN(Date.parse(val)), { - message: "Invalid date string format" - }), - updatedAt: z.string().refine((val) => !isNaN(Date.parse(val)), { - message: "Invalid date string format" - }) - }) - ).optional(), - error: z.string().optional() + success: z.boolean(), + data: z + .array( + z.object({ + id: z.string(), + name: z.string(), + icon: z.string().nullable(), + createdAt: z.string().refine((val) => !isNaN(Date.parse(val)), { + message: "Invalid date string format", + }), + updatedAt: z.string().refine((val) => !isNaN(Date.parse(val)), { + message: "Invalid date string format", + }), + }), + ) + .optional(), + error: z.string().optional(), }); export type CreateInstanceRequest = z.infer; -export type GetAllInstancesResponse = z.infer; \ No newline at end of file +export type GetAllInstancesResponse = z.infer< + typeof getAllInstancesResponseSchema +>; diff --git a/concord-server/src/validators/messageValidator.ts b/concord-server/src/validators/messageValidator.ts index 83e4920..baead86 100644 --- a/concord-server/src/validators/messageValidator.ts +++ b/concord-server/src/validators/messageValidator.ts @@ -1,20 +1,20 @@ import { z } from "zod"; export const getMessageByIdSchema = z.object({ - id: z.uuidv7() -}) + id: z.uuidv7(), +}); export const getMessagesBeforeDate = z.object({ - date: z.string().refine((val) => !isNaN(Date.parse(val)), { - message: "Invalid date string format" - }), - channelId: z.uuidv7() -}) + date: z.string().refine((val) => !isNaN(Date.parse(val)), { + message: "Invalid date string format", + }), + channelId: z.uuidv7(), +}); export const sendMessageSchema = z.object({ - channelId: z.uuidv7(), - userId: z.uuidv7(), - content: z.string(), - token: z.string(), - repliedMessageId: z.uuidv7().nullable().optional() -}) \ No newline at end of file + channelId: z.uuidv7(), + userId: z.uuidv7(), + content: z.string(), + token: z.string(), + repliedMessageId: z.uuidv7().nullable().optional(), +}); diff --git a/concord-server/src/validators/userValidator.ts b/concord-server/src/validators/userValidator.ts index 0789128..e4f1632 100644 --- a/concord-server/src/validators/userValidator.ts +++ b/concord-server/src/validators/userValidator.ts @@ -7,7 +7,7 @@ export const queryUserByIdSchema = z.object({ export const queryAllUsersByInstanceId = z.object({ instanceId: z.uuidv7(), }); -import { is } from 'zod/v4/locales'; +import { is } from "zod/v4/locales"; export const createUserSchema = z.object({ username: z.string().min(3).max(30), nickname: z.string().min(1).max(30).optional(),