code made pretty

This commit is contained in:
Kevin Puig
2025-09-27 21:42:43 -04:00
parent 11f25ca915
commit c305298927
17 changed files with 1394 additions and 1292 deletions

View File

@@ -1,4 +1,4 @@
import{
import {
createCategory,
getCategory,
getCategoriesByInstance,
@@ -6,11 +6,11 @@ import{
deleteCategory,
deleteAllCategoriesFromInstance,
} from "../services/channelService";
import{
import {
CreateCategoryInput,
UpdateCategoryInput,
DeleteCategoryInput,
DeleteCategoriesByInstanceIdInput
DeleteCategoriesByInstanceIdInput,
} from "../validators/categoryValidator";
export async function createNewCategory(data: CreateCategoryInput) {
@@ -33,7 +33,8 @@ export async function deleteExistingCategory(data: DeleteCategoryInput) {
return await deleteCategory(data);
}
export async function deleteAllCategoriesByInstance(data: DeleteCategoriesByInstanceIdInput) {
export async function deleteAllCategoriesByInstance(
data: DeleteCategoriesByInstanceIdInput,
) {
return await deleteAllCategoriesFromInstance(data);
}

View File

@@ -1,16 +1,16 @@
import{
import {
createChannel,
getChannel,
getChannelsByCategory,
updateChannel,
deleteChannel,
deleteAllChannelsFromCategory
deleteAllChannelsFromCategory,
} from "../services/channelService";
import {
CreateChannelInput,
UpdateChannelInput,
DeleteChannelInput,
DeleteChannelsByCategoryIdInput
DeleteChannelsByCategoryIdInput,
} from "../validators/channelValidator";
export async function createNewChannel(data: CreateChannelInput) {
@@ -33,6 +33,8 @@ export async function deleteExistingChannel(data: DeleteChannelInput) {
return await deleteChannel(data);
}
export async function deleteAllChannelsByCategory(data: DeleteChannelsByCategoryIdInput) {
export async function deleteAllChannelsByCategory(
data: DeleteChannelsByCategoryIdInput,
) {
return await deleteAllChannelsFromCategory(data);
}

View File

@@ -1,7 +1,7 @@
import { createInstance, getAllInstances } from "../services/instanceService";
import { CreateInstanceRequest } from "../validators/instanceValidator";
export async function createInstanceReq(data:CreateInstanceRequest) {
export async function createInstanceReq(data: CreateInstanceRequest) {
return await createInstance(data);
}

View File

@@ -1,10 +1,14 @@
import { getMessageInformation, getMessagesBefore, sendMessageToChannel } from "../services/messageService";
import {
getMessageInformation,
getMessagesBefore,
sendMessageToChannel,
} from "../services/messageService";
export async function fetchMessageData(id:string) {
export async function fetchMessageData(id: string) {
return await getMessageInformation(id);
}
export async function fetchMessagesBefore(date:string, channelId:string) {
export async function fetchMessagesBefore(date: string, channelId: string) {
return getMessagesBefore(date, channelId);
}
@@ -13,13 +17,13 @@ export async function sendMessage(
userId: string,
content: string,
token: string,
repliedMessageId: string | null
repliedMessageId: string | null,
) {
return await sendMessageToChannel(
channelId,
userId,
content,
token,
repliedMessageId
repliedMessageId,
);
}

View File

@@ -24,7 +24,7 @@ import {
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(
@@ -41,19 +41,19 @@ categoryRoutes.post(
400: {
description: "Bad Request - Invalid input data",
content: {
"application/json": { schema: resolver(createCategorySchema)},
"application/json": { schema: resolver(createCategorySchema) },
},
},
401: {
description: "Unauthorized - Admin access required",
content: {
"application/json": { schema: resolver(createCategorySchema)},
"application/json": { schema: resolver(createCategorySchema) },
},
},
404: {
description: "User Id not found",
content: {
"application/json": { schema: resolver(createCategorySchema)},
"application/json": { schema: resolver(createCategorySchema) },
},
},
},
@@ -67,8 +67,8 @@ categoryRoutes.post(
} else {
return c.json({ error: "Failed to create category" }, 400);
}
}
)
},
);
// Get a category by ID
categoryRoutes.get(
@@ -98,7 +98,7 @@ categoryRoutes.get(
} else {
return c.json({ error: "Category not found" }, 404);
}
}
},
);
// Get all categories by instance ID
@@ -110,13 +110,17 @@ categoryRoutes.get(
200: {
description: "Success getting all categories in instance",
content: {
"application/json": { schema: resolver(getCategoriesByInstanceIdSchema) },
"application/json": {
schema: resolver(getCategoriesByInstanceIdSchema),
},
},
},
400: {
description: "Bad Request - Missing instance ID",
content: {
"application/json": { schema: resolver(getCategoriesByInstanceIdSchema) },
"application/json": {
schema: resolver(getCategoriesByInstanceIdSchema),
},
},
},
},
@@ -131,9 +135,12 @@ categoryRoutes.get(
if (categoryData) {
return c.json(categoryData);
} else {
return c.json({ error: "Error getting all categories from instance" }, 500);
}
return c.json(
{ error: "Error getting all categories from instance" },
500,
);
}
},
);
// Update a category
@@ -151,19 +158,19 @@ categoryRoutes.put(
400: {
description: "Bad Request - Invalid input data",
content: {
"application/json": { schema: resolver(updateCategorySchema)},
"application/json": { schema: resolver(updateCategorySchema) },
},
},
401: {
description: "Unauthorized - Admin access required",
content: {
"application/json": { schema: resolver(updateCategorySchema)},
"application/json": { schema: resolver(updateCategorySchema) },
},
},
404: {
description: "Category id or User Id not found",
content: {
"application/json": { schema: resolver(updateCategorySchema)},
"application/json": { schema: resolver(updateCategorySchema) },
},
},
},
@@ -189,7 +196,7 @@ categoryRoutes.put(
} else {
return c.json({ error: "Failed to update category" }, 400);
}
}
},
);
// Delete a specific category
@@ -207,19 +214,19 @@ categoryRoutes.delete(
400: {
description: "Bad Request - Invalid input data",
content: {
"application/json": { schema: resolver(deleteCategorySchema)},
"application/json": { schema: resolver(deleteCategorySchema) },
},
},
401: {
description: "Unauthorized - Admin access required",
content: {
"application/json": { schema: resolver(deleteCategorySchema)},
"application/json": { schema: resolver(deleteCategorySchema) },
},
},
404: {
description: "Category id or User Id not found",
content: {
"application/json": { schema: resolver(deleteCategorySchema)},
"application/json": { schema: resolver(deleteCategorySchema) },
},
},
},
@@ -240,8 +247,8 @@ categoryRoutes.delete(
} else {
return c.json({ error: "Failed to delete category" }, 400);
}
}
)
},
);
// Delete all categories by instance ID
categoryRoutes.delete(
@@ -252,25 +259,33 @@ categoryRoutes.delete(
200: {
description: "Success deleting all categories in instance",
content: {
"application/json": { schema: resolver(deleteCategoriesByInstanceIdSchema) },
"application/json": {
schema: resolver(deleteCategoriesByInstanceIdSchema),
},
},
},
400: {
description: "Bad Request - Invalid input data",
content: {
"application/json": { schema: resolver(deleteCategoriesByInstanceIdSchema)},
"application/json": {
schema: resolver(deleteCategoriesByInstanceIdSchema),
},
},
},
401: {
description: "Unauthorized - Admin access required",
content: {
"application/json": { schema: resolver(deleteCategoriesByInstanceIdSchema)},
"application/json": {
schema: resolver(deleteCategoriesByInstanceIdSchema),
},
},
},
404: {
description: "Instance id or User Id not found",
content: {
"application/json": { schema: resolver(deleteCategoriesByInstanceIdSchema)},
"application/json": {
schema: resolver(deleteCategoriesByInstanceIdSchema),
},
},
},
},
@@ -282,7 +297,10 @@ categoryRoutes.delete(
// 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);
return c.json(
{ error: "Instance ID in path does not match Instance ID in body" },
400,
);
}
const categoryData = await deleteAllCategoriesByInstance(data);
@@ -291,7 +309,7 @@ categoryRoutes.delete(
} else {
return c.json({ error: "Failed to delete categories" }, 400);
}
}
)
},
);
export { categoryRoutes };

View File

@@ -14,7 +14,6 @@ import {
updateChannelSchema,
deleteChannelSchema,
deleteChannelsByCategoryIdSchema,
CreateChannelInput,
GetChannelInput,
GetChannelsByCategoryIdInput,
@@ -26,7 +25,7 @@ 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(
@@ -43,19 +42,19 @@ channelRoutes.post(
400: {
description: "Bad Request - Invalid input data",
content: {
"application/json": { schema: resolver(createChannelSchema)},
"application/json": { schema: resolver(createChannelSchema) },
},
},
401: {
description: "Unauthorized - Admin access required",
content: {
"application/json": { schema: resolver(createChannelSchema)},
"application/json": { schema: resolver(createChannelSchema) },
},
},
404: {
description: "User Id not found",
content: {
"application/json": { schema: resolver(createChannelSchema)},
"application/json": { schema: resolver(createChannelSchema) },
},
},
},
@@ -69,8 +68,8 @@ channelRoutes.post(
} else {
return c.json({ error: "Failed to create channel" }, 400);
}
}
)
},
);
// Get a channel by ID
channelRoutes.get(
@@ -100,7 +99,7 @@ channelRoutes.get(
} else {
return c.json({ error: "Channel not found" }, 404);
}
}
},
);
// Get all channels by category ID
@@ -112,13 +111,17 @@ channelRoutes.get(
200: {
description: "Success getting all channels in category",
content: {
"application/json": { schema: resolver(getChannelsByCategoryIdSchema) },
"application/json": {
schema: resolver(getChannelsByCategoryIdSchema),
},
},
},
400: {
description: "Bad Request - Missing category ID",
content: {
"application/json": { schema: resolver(getChannelsByCategoryIdSchema) },
"application/json": {
schema: resolver(getChannelsByCategoryIdSchema),
},
},
},
},
@@ -135,7 +138,7 @@ channelRoutes.get(
} else {
return c.json({ error: "Error getting channels from category" }, 500);
}
}
},
);
// Update a channel
@@ -153,19 +156,19 @@ channelRoutes.put(
400: {
description: "Bad Request - Invalid input data",
content: {
"application/json": { schema: resolver(updateChannelSchema)},
"application/json": { schema: resolver(updateChannelSchema) },
},
},
401: {
description: "Unauthorized - Admin access required",
content: {
"application/json": { schema: resolver(updateChannelSchema)},
"application/json": { schema: resolver(updateChannelSchema) },
},
},
404: {
description: "Channel id or User Id not found",
content: {
"application/json": { schema: resolver(updateChannelSchema)},
"application/json": { schema: resolver(updateChannelSchema) },
},
},
},
@@ -191,7 +194,7 @@ channelRoutes.put(
} else {
return c.json({ error: "Failed to update channel" }, 400);
}
}
},
);
// Delete a specific channel
@@ -209,19 +212,19 @@ channelRoutes.delete(
400: {
description: "Bad Request - Invalid input data",
content: {
"application/json": { schema: resolver(deleteChannelSchema)},
"application/json": { schema: resolver(deleteChannelSchema) },
},
},
401: {
description: "Unauthorized - Admin access required",
content: {
"application/json": { schema: resolver(deleteChannelSchema)},
"application/json": { schema: resolver(deleteChannelSchema) },
},
},
404: {
description: "Channel id or User Id not found",
content: {
"application/json": { schema: resolver(deleteChannelSchema)},
"application/json": { schema: resolver(deleteChannelSchema) },
},
},
},
@@ -242,7 +245,7 @@ channelRoutes.delete(
} else {
return c.json({ error: "Failed to delete channel" }, 400);
}
}
},
);
// Delete all channels by category ID
@@ -254,25 +257,33 @@ channelRoutes.delete(
200: {
description: "Success deleting all channels in category",
content: {
"application/json": { schema: resolver(deleteChannelsByCategoryIdSchema) },
"application/json": {
schema: resolver(deleteChannelsByCategoryIdSchema),
},
},
},
400: {
description: "Bad Request - Invalid input data",
content: {
"application/json": { schema: resolver(deleteChannelsByCategoryIdSchema)},
"application/json": {
schema: resolver(deleteChannelsByCategoryIdSchema),
},
},
},
401: {
description: "Unauthorized - Admin access required",
content: {
"application/json": { schema: resolver(deleteChannelsByCategoryIdSchema)},
"application/json": {
schema: resolver(deleteChannelsByCategoryIdSchema),
},
},
},
404: {
description: "Category id or User Id not found",
content: {
"application/json": { schema: resolver(deleteChannelsByCategoryIdSchema)},
"application/json": {
schema: resolver(deleteChannelsByCategoryIdSchema),
},
},
},
},
@@ -284,7 +295,10 @@ channelRoutes.delete(
// 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);
return c.json(
{ error: "Category ID in path does not match Category ID in body" },
400,
);
}
const result = await deleteAllChannelsByCategory(data);
@@ -293,7 +307,7 @@ channelRoutes.delete(
} else {
return c.json({ error: "Failed to delete channels" }, 400);
}
}
)
},
);
export { channelRoutes };

View File

@@ -1,8 +1,14 @@
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();
@@ -14,15 +20,15 @@ instanceRoutes.post(
200: {
description: "Instance created",
content: {
"application/json": { schema: resolver(createInstanceRequestSchema) }
"application/json": { schema: resolver(createInstanceRequestSchema) },
},
},
400: {
description: "Invalid request",
},
}
},
}),
zValidator('json', createInstanceRequestSchema),
zValidator("json", createInstanceRequestSchema),
async (c) => {
const data = await c.req.json();
if (!data) {
@@ -31,7 +37,7 @@ instanceRoutes.post(
const instance = await createInstanceReq(data);
return c.json(instance, 201);
}
},
);
instanceRoutes.get(
@@ -42,22 +48,30 @@ instanceRoutes.get(
200: {
description: "List of all instances",
content: {
"application/json": { schema: resolver(getAllInstancesResponseSchema) }
"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);
}
return c.json(
{
success: false,
error: instances.error || "Failed to fetch instances",
},
500,
);
}
},
);
export default instanceRoutes;

View File

@@ -1,8 +1,16 @@
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();
@@ -14,16 +22,16 @@ messageRoutes.get(
200: {
description: "Success getting message",
content: {
"application/json": { schema: resolver(getMessageByIdSchema) }
}
"application/json": { schema: resolver(getMessageByIdSchema) },
},
},
404: {
description: "Message id not found",
content: {
"application/json": { schema: resolver(getMessageByIdSchema) }
}
}
}
"application/json": { schema: resolver(getMessageByIdSchema) },
},
},
},
}),
zValidator("param", getMessageByIdSchema),
async (c) => {
@@ -35,8 +43,8 @@ messageRoutes.get(
} else {
return c.json({ error: "Message not found" }, 404);
}
}
)
},
);
messageRoutes.get(
"",
@@ -46,10 +54,10 @@ messageRoutes.get(
200: {
description: "Success getting up to 50 messages",
content: {
"application/json": { schema: resolver(getMessagesBeforeDate) }
}
}
}
"application/json": { schema: resolver(getMessagesBeforeDate) },
},
},
},
}),
zValidator("query", getMessagesBeforeDate),
async (c) => {
@@ -70,8 +78,8 @@ messageRoutes.get(
} else {
return c.json({ error: "Failed to fetch messages" }, 500);
}
}
)
},
);
messageRoutes.post(
"",
@@ -81,41 +89,58 @@ messageRoutes.post(
201: {
description: "Message sent successfully",
content: {
"application/json": { schema: resolver(sendMessageSchema) }
}
"application/json": { schema: resolver(sendMessageSchema) },
},
},
401: {
description: "Unauthorized - invalid token or user credentials",
content: {
"application/json": { schema: { type: "object", properties: { error: { type: "string" } } } }
}
"application/json": {
schema: {
type: "object",
properties: { error: { type: "string" } },
},
},
},
},
500: {
description: "Server error",
content: {
"application/json": { schema: { type: "object", properties: { error: { type: "string" } } } }
}
}
}
"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 { channelId, userId, content, token, repliedMessageId } =
await c.req.json();
const result = await sendMessage(
channelId,
userId,
content,
token,
repliedMessageId || null
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);
return c.json(
{
error:
"Failed to send message. Check your credentials and try again.",
},
401,
);
}
}
)
},
);
export default messageRoutes;

View File

@@ -1,33 +1,30 @@
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{
DeleteChannelsByCategoryIdInput,
} from "../validators/channelValidator";
import {
UpdateCategoryInput,
DeleteCategoryInput,
DeleteCategoriesByInstanceIdInput,
CreateCategoryInput
} from '../validators/categoryValidator';
CreateCategoryInput,
} from "../validators/categoryValidator";
const prisma = new PrismaClient();
export async function createCategory(data: CreateCategoryInput): Promise<Category | null>{
try{
export async function createCategory(
data: CreateCategoryInput,
): Promise<Category | null> {
try {
//Confirm if user exists and is admin
const requestingUser = await getUserInformation(data.requestingUserId);
const requestingUserCredentials = await getUserCredentials(
data.requestingUserId,
)
);
if (
!requestingUser ||
@@ -42,43 +39,43 @@ export async function createCategory(data: CreateCategoryInput): Promise<Categor
const newCategory = await prisma.category.create({
data: {
name: data.name,
position: data.position
}
position: data.position,
},
});
if(!newCategory){
if (!newCategory) {
throw new Error("could not create category");
}
let curInstance;
if(data.instanceId){
if (data.instanceId) {
curInstance = await prisma.instance.findUnique({
where: {
id: data.instanceId
id: data.instanceId,
},
include: {
Category: true
}
Category: true,
},
});
if(!curInstance){
if (!curInstance) {
throw new Error("could not find instance to add category to");
}
await prisma.category.update({
where: {
id: newCategory.id
id: newCategory.id,
},
data: {
instanceId: curInstance.id
}
instanceId: curInstance.id,
},
});
return newCategory;
}
return newCategory;
}catch(err){
} catch (err) {
console.log("services::channelService::createCategory - ", err);
return null;
}
@@ -86,60 +83,61 @@ export async function createCategory(data: CreateCategoryInput): Promise<Categor
export async function getCategory(
categoryId: string,
): Promise<Category | null>{
try{
): Promise<Category | null> {
try {
const category = await prisma.category.findUnique({
where: {
id: categoryId
}
id: categoryId,
},
});
if(!category){
if (!category) {
throw new Error("could not find category");
}
return category;
}catch(err){
} catch (err) {
console.log("services::channelService::getCategory - ", err);
return null;
}
}
export async function getCategoriesByInstance(
instanceId: string
): Promise<Category[] | null>{
try{
instanceId: string,
): Promise<Category[] | null> {
try {
const categories = await prisma.category.findMany({
where: {
instanceId: instanceId
instanceId: instanceId,
},
include: {
Channel: true
Channel: true,
},
orderBy: {
position: 'asc'
}
position: "asc",
},
});
if(!categories){
if (!categories) {
throw new Error("could not find categories for instance");
}
return categories;
}catch(err){
} catch (err) {
console.log("services::channelService::getCategoriesByInstance - ", err);
return null;
}
}
export async function updateCategory(data: UpdateCategoryInput): Promise<Category | null>{
try{
export async function updateCategory(
data: UpdateCategoryInput,
): Promise<Category | null> {
try {
//Confirm if user exists and is admin
const requestingUser = await getUserInformation(data.requestingUserId);
const requestingUserCredentials = await getUserCredentials(
data.requestingUserId,
)
);
if (
!requestingUser ||
@@ -153,34 +151,35 @@ export async function updateCategory(data: UpdateCategoryInput): Promise<Categor
const updatedCategory = await prisma.category.update({
where: {
id: data.id
id: data.id,
},
data: {
name: data.name,
position: data.position,
Channel: data.channels ? { set: data.channels } : undefined
}
Channel: data.channels ? { set: data.channels } : undefined,
},
});
if(!updatedCategory){
if (!updatedCategory) {
throw new Error("could not update category");
}
return updatedCategory;
}catch(err){
} catch (err) {
console.log("services::channelService::updateCategory - ", err);
return null;
}
}
export async function deleteCategory(data: DeleteCategoryInput): Promise<boolean | null>{
try{
export async function deleteCategory(
data: DeleteCategoryInput,
): Promise<boolean | null> {
try {
//Confirm if user exists and is admin
const requestingUser = await getUserInformation(data.requestingUserId);
const requestingUserCredentials = await getUserCredentials(
data.requestingUserId,
)
);
if (
!requestingUser ||
@@ -194,39 +193,40 @@ export async function deleteCategory(data: DeleteCategoryInput): Promise<boolean
const deleteAllChannels = await prisma.channel.deleteMany({
where: {
categoryId: data.id
}
categoryId: data.id,
},
});
if(deleteAllChannels.count === 0){
if (deleteAllChannels.count === 0) {
throw new Error("could not delete channels from category");
}
const deletedCategory = await prisma.category.delete({
where: {
id: data.id
}
id: data.id,
},
});
if(!deletedCategory){
if (!deletedCategory) {
throw new Error("could not delete category");
}
return true;
}catch(err){
} catch (err) {
console.log("services::channelService::deleteCategory - ", err);
return false;
}
}
export async function deleteAllCategoriesFromInstance(data: DeleteCategoriesByInstanceIdInput): Promise<boolean | null>{
try{
export async function deleteAllCategoriesFromInstance(
data: DeleteCategoriesByInstanceIdInput,
): Promise<boolean | null> {
try {
//Confirm if user exists and is admin
const requestingUser = await getUserInformation(data.requestingUserId);
const requestingUserCredentials = await getUserCredentials(
data.requestingUserId,
)
);
if (
!requestingUser ||
@@ -240,29 +240,33 @@ export async function deleteAllCategoriesFromInstance(data: DeleteCategoriesByIn
const deletedCategories = await prisma.category.deleteMany({
where: {
instanceId: data.instanceId
}
instanceId: data.instanceId,
},
});
if(deletedCategories.count === 0){
if (deletedCategories.count === 0) {
throw new Error("could not delete categories from instance");
}
return true;
}catch(err){
console.log("services::channelService::deleteAllCategoriesFromInstance - ", err);
} catch (err) {
console.log(
"services::channelService::deleteAllCategoriesFromInstance - ",
err,
);
return false;
}
}
export async function createChannel(data: CreateChannelInput): Promise<Channel | null>{
try{
export async function createChannel(
data: CreateChannelInput,
): Promise<Channel | null> {
try {
//Confirm if user exists and is admin
const requestingUser = await getUserInformation(data.requestingUserId);
const requestingUserCredentials = await getUserCredentials(
data.requestingUserId,
)
);
if (
!requestingUser ||
@@ -279,71 +283,69 @@ export async function createChannel(data: CreateChannelInput): Promise<Channel |
type: data.type,
name: data.name,
description: data.description,
categoryId: data.categoryId ? data.categoryId : null
}
categoryId: data.categoryId ? data.categoryId : null,
},
});
if(!newChannel){
if (!newChannel) {
throw new Error("could not create channel");
}
return newChannel;
}catch(err){
} catch (err) {
console.log("services::channelService::createChannel - ", err);
return null;
}
}
export async function getChannel(
channelId: string
): Promise<Channel | null>{
try{
export async function getChannel(channelId: string): Promise<Channel | null> {
try {
const channel = await prisma.channel.findUnique({
where: {
id: channelId
}
id: channelId,
},
});
if(!channel){
if (!channel) {
throw new Error("could not find channel");
}
return channel;
}catch(err){
} catch (err) {
console.log("services::channelService::getChannel - ", err);
return null;
}
}
export async function getChannelsByCategory(
categoryId: string
): Promise<Channel[] | null>{
try{
categoryId: string,
): Promise<Channel[] | null> {
try {
const channels = await prisma.channel.findMany({
where: {
categoryId: categoryId
}
categoryId: categoryId,
},
});
if(!channels){
if (!channels) {
throw new Error("could not find channels for category");
}
return channels;
}
catch(err){
} catch (err) {
console.log("services::channelService::getChannelsByCategory - ", err);
return null;
}
}
export async function updateChannel(data: UpdateChannelInput): Promise<Channel | null>{
try{
export async function updateChannel(
data: UpdateChannelInput,
): Promise<Channel | null> {
try {
//Confirm if user exists and is admin
const requestingUser = await getUserInformation(data.requestingUserId);
const requestingUserCredentials = await getUserCredentials(
data.requestingUserId,
)
);
if (
!requestingUser ||
@@ -357,34 +359,35 @@ export async function updateChannel(data: UpdateChannelInput): Promise<Channel |
const updatedChannel = await prisma.channel.update({
where: {
id: data.id
id: data.id,
},
data: {
name: data.name,
description: data.description,
categoryId: data.categoryId ? data.categoryId : undefined
}
categoryId: data.categoryId ? data.categoryId : undefined,
},
});
if(!updatedChannel){
if (!updatedChannel) {
throw new Error("could not update channel");
}
return updatedChannel;
}catch(err){
} catch (err) {
console.log("services::channelService::updateChannel - ", err);
return null;
}
}
export async function deleteChannel(data: DeleteChannelInput): Promise<boolean | null>{
try{
export async function deleteChannel(
data: DeleteChannelInput,
): Promise<boolean | null> {
try {
//Confirm if user exists and is admin
const requestingUser = await getUserInformation(data.requestingUserId);
const requestingUserCredentials = await getUserCredentials(
data.requestingUserId,
)
);
if (
!requestingUser ||
@@ -398,29 +401,30 @@ export async function deleteChannel(data: DeleteChannelInput): Promise<boolean |
const deletedChannel = await prisma.channel.delete({
where: {
id: data.id
}
id: data.id,
},
});
if(!deletedChannel){
if (!deletedChannel) {
throw new Error("could not delete channel");
}
return true;
}catch(err){
} catch (err) {
console.log("services::channelService::deleteChannel - ", err);
return false;
}
}
export async function deleteAllChannelsFromCategory(data: DeleteChannelsByCategoryIdInput): Promise<boolean | null>
{
try{
export async function deleteAllChannelsFromCategory(
data: DeleteChannelsByCategoryIdInput,
): Promise<boolean | null> {
try {
//Confirm if user exists and is admin
const requestingUser = await getUserInformation(data.requestingUserId);
const requestingUserCredentials = await getUserCredentials(
data.requestingUserId,
)
);
if (
!requestingUser ||
@@ -434,17 +438,20 @@ export async function deleteAllChannelsFromCategory(data: DeleteChannelsByCatego
const deletedChannels = await prisma.channel.deleteMany({
where: {
categoryId: data.categoryId
}
categoryId: data.categoryId,
},
});
if(deletedChannels.count === 0){
if (deletedChannels.count === 0) {
throw new Error("could not delete channels from category");
}
return true;
}catch(err){
console.log("services::channelService::deleteAllChannelsFromCategory - ", err);
} catch (err) {
console.log(
"services::channelService::deleteAllChannelsFromCategory - ",
err,
);
return false;
}
}

View File

@@ -8,29 +8,31 @@ 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) {
if (
!creds ||
creds.token != data.requestingUserToken ||
!user ||
!user.admin
) {
return null;
}
const newInstance = await prisma.instance.create({
data: {
name: data.name,
icon: data.icon
}
icon: data.icon,
},
});
return {
success: true,
data: newInstance
data: newInstance,
};
} catch (error) {
console.error("Error creating instance:", error);
return {
success: false,
error: "Failed to create instance"
error: "Failed to create instance",
};
}
}
@@ -39,19 +41,19 @@ export async function getAllInstances() {
try {
const instances = await prisma.instance.findMany({
orderBy: {
createdAt: 'desc'
}
createdAt: "desc",
},
});
return {
success: true,
data: instances
data: instances,
};
} catch (error) {
console.error("Error fetching instances:", error);
return {
success: false,
error: "Failed to fetch instances"
error: "Failed to fetch instances",
};
}
}

View File

@@ -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;
}

View File

@@ -1,4 +1,3 @@
import {
Message,
MessagePing,

View File

@@ -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(),
instanceId: z.uuidv7().optional(),
admin: z.boolean(),
requestingUserId: z.uuidv7(),
requestingUserToken: z.uuidv4()
})
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(),
channels: z
.array(
z.object({
id: z.string(),
}),
)
.optional(),
admin: z.boolean(),
requestingUserId: z.uuidv7(),
requestingUserToken: z.uuidv4()
})
requestingUserToken: z.uuidv4(),
});
export const deleteCategorySchema = z.object({
id: z.uuidv7(),
admin: z.boolean(),
requestingUserId: z.uuidv7(),
requestingUserToken: z.uuidv4()
})
requestingUserToken: z.uuidv4(),
});
export const deleteCategoriesByInstanceIdSchema = z.object({
instanceId: z.uuidv7(),
admin: z.boolean(),
requestingUserId: z.uuidv7(),
requestingUserToken: z.uuidv4()
})
requestingUserToken: z.uuidv4(),
});
export type CreateCategoryInput = z.infer<typeof createCategorySchema>
export type GetCategoryInput = z.infer<typeof getCategorySchema>
export type GetCategoriesByInstanceIdInput = z.infer<typeof getCategoriesByInstanceIdSchema>
export type UpdateCategoryInput = z.infer<typeof updateCategorySchema>
export type DeleteCategoryInput = z.infer<typeof deleteCategorySchema>
export type DeleteCategoriesByInstanceIdInput = z.infer<typeof deleteCategoriesByInstanceIdSchema>
export type CreateCategoryInput = z.infer<typeof createCategorySchema>;
export type GetCategoryInput = z.infer<typeof getCategorySchema>;
export type GetCategoriesByInstanceIdInput = z.infer<
typeof getCategoriesByInstanceIdSchema
>;
export type UpdateCategoryInput = z.infer<typeof updateCategorySchema>;
export type DeleteCategoryInput = z.infer<typeof deleteCategorySchema>;
export type DeleteCategoriesByInstanceIdInput = z.infer<
typeof deleteCategoriesByInstanceIdSchema
>;

View File

@@ -3,22 +3,22 @@ import { z } from "zod";
//channel validators
export const createChannelSchema = z.object({
type: z.enum(['text', 'voice']),
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()
})
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(),
@@ -27,26 +27,30 @@ export const updateChannelSchema = z.object({
categoryId: z.uuidv7().optional(),
admin: z.boolean(),
requestingUserId: z.uuidv7(),
requestingUserToken: z.uuidv4()
})
requestingUserToken: z.uuidv4(),
});
export const deleteChannelSchema = z.object({
id: z.uuidv7(),
admin: z.boolean(),
requestingUserId: z.uuidv7(),
requestingUserToken: z.uuidv4()
})
requestingUserToken: z.uuidv4(),
});
export const deleteChannelsByCategoryIdSchema = z.object({
categoryId: z.uuidv7(),
admin: z.boolean(),
requestingUserId: z.uuidv7(),
requestingUserToken: z.uuidv4()
})
requestingUserToken: z.uuidv4(),
});
export type CreateChannelInput = z.infer<typeof createChannelSchema>
export type GetChannelInput = z.infer<typeof getChannelSchema>
export type GetChannelsByCategoryIdInput = z.infer<typeof getChannelsByCategoryIdSchema>
export type UpdateChannelInput = z.infer<typeof updateChannelSchema>
export type DeleteChannelInput = z.infer<typeof deleteChannelSchema>
export type DeleteChannelsByCategoryIdInput = z.infer<typeof deleteChannelsByCategoryIdSchema>
export type CreateChannelInput = z.infer<typeof createChannelSchema>;
export type GetChannelInput = z.infer<typeof getChannelSchema>;
export type GetChannelsByCategoryIdInput = z.infer<
typeof getChannelsByCategoryIdSchema
>;
export type UpdateChannelInput = z.infer<typeof updateChannelSchema>;
export type DeleteChannelInput = z.infer<typeof deleteChannelSchema>;
export type DeleteChannelsByCategoryIdInput = z.infer<
typeof deleteChannelsByCategoryIdSchema
>;

View File

@@ -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'),
name: z.string().min(1, "Instance name cannot be empty"),
icon: z.url().optional(),
requestingUserId: z.uuidv7(),
requestingUserToken: z.string()
requestingUserToken: z.string(),
});
export const getAllInstancesResponseSchema = z.object({
success: z.boolean(),
data: z.array(
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"
message: "Invalid date string format",
}),
updatedAt: z.string().refine((val) => !isNaN(Date.parse(val)), {
message: "Invalid date string format"
})
})
).optional(),
error: z.string().optional()
message: "Invalid date string format",
}),
}),
)
.optional(),
error: z.string().optional(),
});
export type CreateInstanceRequest = z.infer<typeof createInstanceRequestSchema>;
export type GetAllInstancesResponse = z.infer<typeof getAllInstancesResponseSchema>;
export type GetAllInstancesResponse = z.infer<
typeof getAllInstancesResponseSchema
>;

View File

@@ -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"
message: "Invalid date string format",
}),
channelId: z.uuidv7()
})
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()
})
repliedMessageId: z.uuidv7().nullable().optional(),
});

View File

@@ -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(),