2025-02-05 20:40:59 +00:00
|
|
|
import { UniqueID } from "@common/domain";
|
2025-02-04 14:58:33 +00:00
|
|
|
import { ApiError, ExpressController } from "@common/presentation";
|
2025-02-05 20:40:59 +00:00
|
|
|
import { TabContext } from "@contexts/auth/domain";
|
2025-02-04 14:58:33 +00:00
|
|
|
import { NextFunction, Request, Response } from "express";
|
|
|
|
|
import httpStatus from "http-status";
|
|
|
|
|
|
2025-02-05 20:40:59 +00:00
|
|
|
// Extender el Request de Express para incluir el usuario autenticado optionalmente
|
|
|
|
|
interface TabContextRequest extends Request {
|
|
|
|
|
tabContext?: TabContext;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const validateTabContextHeader = async (
|
|
|
|
|
req: TabContextRequest,
|
|
|
|
|
res: Response,
|
|
|
|
|
next: NextFunction
|
|
|
|
|
) => {
|
|
|
|
|
const tabId = String(req.headers["x-tab-id"]);
|
2025-02-04 14:58:33 +00:00
|
|
|
if (!tabId) {
|
|
|
|
|
return ExpressController.errorResponse(
|
|
|
|
|
new ApiError({
|
|
|
|
|
status: 401,
|
|
|
|
|
title: httpStatus["401"],
|
|
|
|
|
name: httpStatus["401_NAME"],
|
|
|
|
|
detail: "Tab ID is required",
|
|
|
|
|
}),
|
|
|
|
|
res
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-05 20:40:59 +00:00
|
|
|
const tabIdOrError = UniqueID.create(tabId, false);
|
|
|
|
|
if (tabIdOrError.isFailure) {
|
2025-02-04 14:58:33 +00:00
|
|
|
return ExpressController.errorResponse(
|
|
|
|
|
new ApiError({
|
2025-02-05 20:40:59 +00:00
|
|
|
status: 422,
|
|
|
|
|
title: httpStatus["422"],
|
|
|
|
|
name: httpStatus["422_NAME"],
|
|
|
|
|
detail: "Invalid Tab ID",
|
2025-02-04 14:58:33 +00:00
|
|
|
}),
|
|
|
|
|
res
|
|
|
|
|
);
|
|
|
|
|
}
|
2025-02-07 11:14:36 +00:00
|
|
|
/*const contextOrError = await createTabContextService().getContextByTabId(tabIdOrError.data);
|
2025-02-04 14:58:33 +00:00
|
|
|
if (contextOrError.isFailure) {
|
|
|
|
|
return ExpressController.errorResponse(
|
|
|
|
|
new ApiError({
|
|
|
|
|
status: 401,
|
|
|
|
|
title: httpStatus["401"],
|
|
|
|
|
name: httpStatus["401_NAME"],
|
|
|
|
|
detail: "Invalid or expired Tab ID",
|
|
|
|
|
}),
|
|
|
|
|
res
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const context = contextOrError.data;
|
|
|
|
|
|
2025-02-07 11:14:36 +00:00
|
|
|
req.tabContext = context;*/
|
2025-02-04 14:58:33 +00:00
|
|
|
next();
|
|
|
|
|
};
|