diff --git a/server/src/contexts/common/infrastructure/ContextFactory.ts b/server/src/contexts/common/infrastructure/ContextFactory.ts index 5d5111c..1f1287c 100644 --- a/server/src/contexts/common/infrastructure/ContextFactory.ts +++ b/server/src/contexts/common/infrastructure/ContextFactory.ts @@ -7,7 +7,7 @@ export interface IContext { } export class ContextFactory { - private static instance: ContextFactory | null = null; + protected static instance: ContextFactory | null = null; public static getInstance(): IContext { if (!ContextFactory.instance) { @@ -20,9 +20,9 @@ export class ContextFactory { return ContextFactory.instance.context; } - private context: IContext; + protected context: IContext; - private constructor(context: IContext) { + protected constructor(context: IContext) { this.context = context; } } diff --git a/server/src/contexts/common/infrastructure/express/ControllerFactory.ts b/server/src/contexts/common/infrastructure/express/ControllerFactory.ts new file mode 100644 index 0000000..ce83606 --- /dev/null +++ b/server/src/contexts/common/infrastructure/express/ControllerFactory.ts @@ -0,0 +1,12 @@ +import { NextFunction, Request, Response } from "express"; +import { IContext } from "../ContextFactory"; +import { ExpressController } from "./ExpressController"; + +export type ControllerFactory = (context: T) => ExpressController; + +export const handleRequest = + (controllerFactory: ControllerFactory) => + (req: Request, res: Response, next: NextFunction) => { + const context: T = res.locals["context"]; + return controllerFactory(context).execute(req, res, next); + }; diff --git a/server/src/contexts/common/infrastructure/express/index.ts b/server/src/contexts/common/infrastructure/express/index.ts index ad84f70..7cf956b 100644 --- a/server/src/contexts/common/infrastructure/express/index.ts +++ b/server/src/contexts/common/infrastructure/express/index.ts @@ -1,4 +1,5 @@ +export * from "./ControllerFactory"; export * from "./ExpressController"; export * from "./ExpressErrorResponse"; -export * from "./PassportStrategyController"; export * from "./middlewares"; +export * from "./PassportStrategyController"; diff --git a/server/src/contexts/profile/infrastructure/Profile.context.ts b/server/src/contexts/profile/infrastructure/Profile.context.ts index f0d1e91..6ff6001 100644 --- a/server/src/contexts/profile/infrastructure/Profile.context.ts +++ b/server/src/contexts/profile/infrastructure/Profile.context.ts @@ -1,34 +1,29 @@ -import { IRepositoryManager, RepositoryManager } from "@/contexts/common/domain"; -import { - ISequelizeAdapter, - createSequelizeAdapter, -} from "@/contexts/common/infrastructure/sequelize"; +import { ContextFactory, IContext } from "@/contexts/common/infrastructure"; import { config } from "../../../config"; -export interface IProfileContext { - adapter: ISequelizeAdapter; - repositoryManager: IRepositoryManager; +export interface IProfileContext extends IContext { defaults: Record; } -export class ProfileContext { - private static instance: ProfileContext | null = null; +export class ProfileContext extends ContextFactory { + protected static instance: ProfileContext | null = null; public static getInstance(): IProfileContext { if (!ProfileContext.instance) { - ProfileContext.instance = new ProfileContext({ - adapter: createSequelizeAdapter(), - repositoryManager: RepositoryManager.getInstance(), - defaults: config.defaults, - }); + try { + ProfileContext.instance = new ProfileContext({ + ...ContextFactory.getInstance(), // Reutilizamos el contexto de la clase base + defaults: config.defaults, // Agregamos los valores específicos de ProfileContext + }); + } catch (error: unknown) { + throw new Error(`Error initializing ProfileContext: ${(error as Error).message}`); + } } - return ProfileContext.instance.context; + return ProfileContext.instance.context as IProfileContext; } - private context: IProfileContext; - private constructor(context: IProfileContext) { - this.context = context; + super(context); // Llamamos al constructor de la clase base } } diff --git a/server/src/contexts/profile/infrastructure/express/controllers/uploadProfileLogo/index.ts b/server/src/contexts/profile/infrastructure/express/controllers/uploadProfileLogo/index.ts index f7e0cb3..12d5d01 100644 --- a/server/src/contexts/profile/infrastructure/express/controllers/uploadProfileLogo/index.ts +++ b/server/src/contexts/profile/infrastructure/express/controllers/uploadProfileLogo/index.ts @@ -3,7 +3,7 @@ import { IProfileContext } from "../../../Profile.context"; import { registerProfileRepository } from "../../../Profile.repository"; import { UploadProfileLogoController } from "./UploadProfileLogo.controller"; -export const createUpdloadProfileLogoController = (context: IProfileContext) => { +export const createUploadProfileLogoController = (context: IProfileContext) => { registerProfileRepository(context); return new UploadProfileLogoController( diff --git a/server/src/infrastructure/express/api/routes/profile.routes.ts b/server/src/infrastructure/express/api/routes/profile.routes.ts index 5f3cb9e..ddaab94 100644 --- a/server/src/infrastructure/express/api/routes/profile.routes.ts +++ b/server/src/infrastructure/express/api/routes/profile.routes.ts @@ -1,12 +1,14 @@ import { checkUser } from "@/contexts/auth"; +import { handleRequest } from "@/contexts/common/infrastructure/express"; import { createGetProfileController, createUpdateProfileController, ProfileContext, } from "@/contexts/profile/infrastructure"; import { createGetProfileLogoController } from "@/contexts/profile/infrastructure/express/controllers/getProfileLogo"; -import { createUpdloadProfileLogoController } from "@/contexts/profile/infrastructure/express/controllers/uploadProfileLogo"; +import { createUploadProfileLogoController } from "@/contexts/profile/infrastructure/express/controllers/uploadProfileLogo"; import { NextFunction, Request, Response, Router } from "express"; +import multer from "multer"; import { createMulterMiddleware } from "../upload.middleware"; const uploadProfileLogo = createMulterMiddleware({ @@ -18,29 +20,33 @@ const uploadProfileLogo = createMulterMiddleware({ export const profileRouter = (appRouter: Router) => { const profileRoutes: Router = Router({ mergeParams: true }); - profileRoutes.use((req: Request, res: Response, next: NextFunction) => { + const profileContextMiddleware = (req: Request, res: Response, next: NextFunction) => { res.locals["context"] = ProfileContext.getInstance(); - return next(); - }); + next(); + }; - profileRoutes.get("/", checkUser, (req: Request, res: Response, next: NextFunction) => - createGetProfileController(res.locals["context"]).execute(req, res, next) - ); + profileRoutes.use(profileContextMiddleware); - profileRoutes.put("/", checkUser, (req: Request, res: Response, next: NextFunction) => - createUpdateProfileController(res.locals["context"]).execute(req, res, next) - ); - - profileRoutes.get("/logo", checkUser, (req: Request, res: Response, next: NextFunction) => - createGetProfileLogoController(res.locals["context"]).execute(req, res, next) - ); + profileRoutes.get("/", checkUser, handleRequest(createGetProfileController)); + profileRoutes.put("/", checkUser, handleRequest(createUpdateProfileController)); + profileRoutes.get("/logo", checkUser, handleRequest(createGetProfileLogoController)); profileRoutes.post( "/logo", checkUser, - uploadProfileLogo.single("logo"), - (req: Request, res: Response, next: NextFunction) => - createUpdloadProfileLogoController(res.locals["context"]).execute(req, res, next) + (req, res, next) => { + uploadProfileLogo.single("logo")(req, res, (err) => { + if (err instanceof multer.MulterError) { + // Error relacionado con Multer (e.g. tamaño del archivo excedido) + return res.status(400).json({ error: err.message }); + } else if (err) { + // Otro tipo de error + return res.status(500).json({ error: "Error uploading the file" }); + } + next(); + }); + }, + handleRequest(createUploadProfileLogoController) ); appRouter.use("/profile", profileRoutes);