diff --git a/src/modules/internal/controllers/purgeUnusedSession.controller.ts b/src/modules/internal/controllers/purgeUnusedSession.controller.ts new file mode 100644 index 0000000..b109204 --- /dev/null +++ b/src/modules/internal/controllers/purgeUnusedSession.controller.ts @@ -0,0 +1,18 @@ +import { Context } from "elysia"; +import { mainErrorHandler } from "../../../helpers/error/handler"; +import { returnWriteResponse } from "../../../helpers/callback/httpResponse"; +import { purgeUnusedSessionService } from "../services/http/purgeUnusedSession.service"; + +export const purgeUnusedSessionController = async (ctx: Context) => { + try { + const result = await purgeUnusedSessionService(); + return returnWriteResponse( + ctx.set, + 200, + "Successfully purged all unused user sessions", + result, + ); + } catch (error) { + return mainErrorHandler(ctx.set, error); + } +}; diff --git a/src/modules/internal/index.ts b/src/modules/internal/index.ts index 683667b..4e9b58c 100644 --- a/src/modules/internal/index.ts +++ b/src/modules/internal/index.ts @@ -4,10 +4,12 @@ import { bulkInsertMediaController } from "./controllers/bulkInsertMedia.control import { createVideoServiceInternalController } from "./controllers/createVideoService.controller"; import { bulkInsertVideoController } from "./controllers/bulkInsertVideo.controller"; import { updateAllEpisodeThumbnailController } from "./controllers/updateAllEpisodeThumbnail.controller"; +import { purgeUnusedSessionController } from "./controllers/purgeUnusedSession.controller"; export const internalModule = new Elysia({ prefix: "/internal" }) .post("/media/bulk-insert", bulkInsertMediaController) .post("/episode/bulk-insert", bulkInsertEpisodeController) .put("/episode/update-thumbnails", updateAllEpisodeThumbnailController) .post("/video/bulk-insert", bulkInsertVideoController) - .post("/video-service", createVideoServiceInternalController); + .post("/video-service", createVideoServiceInternalController) + .post("/user-session/purge-unused", purgeUnusedSessionController); diff --git a/src/modules/internal/repositories/deleteAllUnusedUserSession.repository.ts b/src/modules/internal/repositories/deleteAllUnusedUserSession.repository.ts new file mode 100644 index 0000000..e213196 --- /dev/null +++ b/src/modules/internal/repositories/deleteAllUnusedUserSession.repository.ts @@ -0,0 +1,17 @@ +import { AppError } from "../../../helpers/error/instances/app"; +import { prisma } from "../../../utils/databases/prisma/connection"; + +export const deleteAllUnusedUserSessionRepository = async () => { + try { + return await prisma.userSession.deleteMany({ + where: { + isAuthenticated: false, + deletedAt: { + not: null, + }, + }, + }); + } catch (error) { + throw new AppError(500, "Failed to delete all unused user sessions", error); + } +}; diff --git a/src/modules/internal/services/http/purgeUnusedSession.service.ts b/src/modules/internal/services/http/purgeUnusedSession.service.ts new file mode 100644 index 0000000..5be408b --- /dev/null +++ b/src/modules/internal/services/http/purgeUnusedSession.service.ts @@ -0,0 +1,10 @@ +import { ErrorForwarder } from "../../../../helpers/error/instances/forwarder"; +import { deleteAllUnusedUserSessionRepository } from "../../repositories/deleteAllUnusedUserSession.repository"; + +export const purgeUnusedSessionService = async () => { + try { + return await deleteAllUnusedUserSessionRepository(); + } catch (error) { + ErrorForwarder(error); + } +};