From 9f47f8f298793c334212bf2fdaee4e3119b7fe29 Mon Sep 17 00:00:00 2001 From: Rafi Arrafif Date: Sun, 8 Mar 2026 07:31:38 +0700 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20refactor:=20align=20update?= =?UTF-8?q?-thumbnail=20and=20bulk-insert-video=20with=20latest=20Elysia?= =?UTF-8?q?=20standards?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controllers/bulkInsertVideo.controller.ts | 24 +++---- .../updateAllEpisodeThumbnail.controller.ts | 55 ++++------------ src/modules/internal/index.ts | 6 +- .../schemas/bulkInsertVideo.schema.ts | 63 +++++++++++++++++++ .../updateAllEpisodeThumbnail.schema.ts | 35 +++++++++++ .../services/http/bulkInsertVideo.service.ts | 7 +-- 6 files changed, 124 insertions(+), 66 deletions(-) create mode 100644 src/modules/internal/schemas/bulkInsertVideo.schema.ts create mode 100644 src/modules/internal/schemas/updateAllEpisodeThumbnail.schema.ts diff --git a/src/modules/internal/controllers/bulkInsertVideo.controller.ts b/src/modules/internal/controllers/bulkInsertVideo.controller.ts index 2342caa..f66137e 100644 --- a/src/modules/internal/controllers/bulkInsertVideo.controller.ts +++ b/src/modules/internal/controllers/bulkInsertVideo.controller.ts @@ -1,19 +1,8 @@ -import { Context } from "elysia"; +import { Context, Static } from "elysia"; import { mainErrorHandler } from "../../../helpers/error/handler"; import { bulkInsertVideoService } from "../services/http/bulkInsertVideo.service"; import { returnWriteResponse } from "../../../helpers/callback/httpResponse"; - -export interface BulkInsertVideoBodyRequest { - media_id: string; - data: Array<{ - episode: number; - videos: Array<{ - service_id: string; - video_code: string; - thumbnail_code?: string; - }>; - }>; -} +import { bulkInsertVideoSchema } from "../schemas/bulkInsertVideo.schema"; /** * @function bulkInsertVideoController @@ -76,12 +65,13 @@ export interface BulkInsertVideoBodyRequest { * error: { ...errorDetails } // Additional error details if available and the env run on development mode * } */ -export const bulkInsertVideoController = async ( - ctx: Context & { body: BulkInsertVideoBodyRequest }, -) => { +export const bulkInsertVideoController = async (ctx: { + set: Context["set"]; + body: Static; +}) => { try { const insertedVideos = await bulkInsertVideoService(ctx.body); - return returnWriteResponse(ctx.set, 201, "Videos inserted", insertedVideos); + return returnWriteResponse(ctx.set, 201, "Videos inserted successfully", insertedVideos); } catch (error) { throw mainErrorHandler(ctx.set, error); } diff --git a/src/modules/internal/controllers/updateAllEpisodeThumbnail.controller.ts b/src/modules/internal/controllers/updateAllEpisodeThumbnail.controller.ts index c83279e..3d5d387 100644 --- a/src/modules/internal/controllers/updateAllEpisodeThumbnail.controller.ts +++ b/src/modules/internal/controllers/updateAllEpisodeThumbnail.controller.ts @@ -1,55 +1,24 @@ -import { Context } from "elysia"; +import { Context, Static } from "elysia"; import { mainErrorHandler } from "../../../helpers/error/handler"; import { returnWriteResponse } from "../../../helpers/callback/httpResponse"; import { updateAllEpisodeThumbnailService } from "../services/http/updateAllEpisodeThumbnail.service"; +import { updateAllEpisodeThumbnailSchema } from "../schemas/updateAllEpisodeThumbnail.schema"; /** - * @function updateAllEpisodeThumbnailController - * @description Controller to handle the bulk updating of episode thumbnails for all episodes associated with a specific service reference ID. + * Updating all episode thumbnails for a specific target service reference ID. * - * @param {Context & { body: { service_reference_id: string } }} ctx - * The context object containing the request body. - * The body must include: - * - service_reference_id: string - The ID of the service to which the episodes belong. + * This controller handles the bulk update of episode thumbnails for all episodes associated with a specific service reference ID. + * It fetches the latest thumbnail data from external sources and updates the existing episode records in the database accordingly. * - * @example - * Request route: PUT /internal/episode/update-thumbnails - * Request body: - * { - * "service_reference_id": "019c0df6-f8fe-7565-82cd-9c29b20232ab" - * }, - * - * @returns {Promise} - * A response object indicating success or failure. - * Return example: - * { - * success: true, - * status: 204, - * message: "Updating {newEpisodeThumbnailsCount} episode thumbnails successfully.", - * } - * - * @throws {Object} - * An error response object if validation fails or an error occurs during bulk insert operation. - * Return example: - * { - * success: false, - * status: , - * message: "", - * error: { ...errorDetails } // Additional error details if available and the env run on development mode - * } + * See OpenAPI documentation for request/response schema. */ -export const updateAllEpisodeThumbnailController = async ( - ctx: Context & { body: { service_reference_id?: string } }, -) => { +export const updateAllEpisodeThumbnailController = async (ctx: { + set: Context["set"]; + body: Static; +}) => { try { - const newEpisodeThumbnailsCount = await updateAllEpisodeThumbnailService( - ctx.body.service_reference_id, - ); - return returnWriteResponse( - ctx.set, - 204, - `Updating ${newEpisodeThumbnailsCount} episode thumbnails successfully.`, - ); + const newEpisodeThumbnailsCount = await updateAllEpisodeThumbnailService(ctx.body.service_reference_id); + return returnWriteResponse(ctx.set, 204, `Updating ${newEpisodeThumbnailsCount} episode thumbnails successfully.`); } catch (error) { return mainErrorHandler(ctx.set, error); } diff --git a/src/modules/internal/index.ts b/src/modules/internal/index.ts index 59ce26a..08076df 100644 --- a/src/modules/internal/index.ts +++ b/src/modules/internal/index.ts @@ -8,6 +8,8 @@ import { purgeUnusedSessionController } from "./controllers/purgeUnusedSession.c import { createHeroBannerController } from "./controllers/createHeroBanner.controller"; import { bulkInsertMediaSchema } from "./schemas/bulkInsertMedia.schema"; import { bulkInsertEpisodeSchema } from "./schemas/bulkInsertEpisode.schema"; +import { updateAllEpisodeThumbnailSchema } from "./schemas/updateAllEpisodeThumbnail.schema"; +import { bulkInsertVideoSchema } from "./schemas/bulkInsertVideo.schema"; export const internalModule = new Elysia({ prefix: "/internal", @@ -15,8 +17,8 @@ export const internalModule = new Elysia({ }) .post("/media/bulk-insert", bulkInsertMediaController, bulkInsertMediaSchema) .post("/episode/bulk-insert", bulkInsertEpisodeController, bulkInsertEpisodeSchema) - .put("/episode/update-thumbnails", updateAllEpisodeThumbnailController) - .post("/video/bulk-insert", bulkInsertVideoController) + .put("/episode/update-thumbnails", updateAllEpisodeThumbnailController, updateAllEpisodeThumbnailSchema) + .post("/video/bulk-insert", bulkInsertVideoController, bulkInsertVideoSchema) .post("/video-service", createVideoServiceInternalController) .post("/user-session/purge-unused", purgeUnusedSessionController) .post("/hero-banner", createHeroBannerController); diff --git a/src/modules/internal/schemas/bulkInsertVideo.schema.ts b/src/modules/internal/schemas/bulkInsertVideo.schema.ts new file mode 100644 index 0000000..ed117ad --- /dev/null +++ b/src/modules/internal/schemas/bulkInsertVideo.schema.ts @@ -0,0 +1,63 @@ +import { t } from "elysia"; +import { AppRouteSchema } from "../../../helpers/types/AppRouteSchema"; + +export const bulkInsertVideoSchema = { + body: t.Object({ + media_id: t.String({ + description: "The ID of the media for which episodes will be inserted", + }), + data: t.Array( + t.Object({ + episode: t.Number({ + description: "The episode number", + }), + videos: t.Array( + t.Object({ + service_id: t.String({ + description: "The ID of the video service", + }), + video_code: t.String({ + description: "The code of the video on the service", + }), + thumbnail_code: t.Optional( + t.String({ + description: "The code of the thumbnail for the video on the service", + }), + ), + }), + ), + }), + ), + }), + detail: { + summary: "Bulk insert videos for a media episode", + description: + "Perform bulk insert of videos for specific episodes of a media. This operation inserts multiple videos associated with different episodes into the database based on the provided data.", + responses: { + 201: { + description: "Videos inserted successfully", + content: { + "application/json": { + schema: { + type: "object", + properties: { + success: { type: "boolean", default: true }, + status: { type: "integer", default: 201 }, + message: { type: "string", default: "Videos inserted successfully" }, + data: { + type: "array", + default: ["xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"], + description: "An array of IDs of the inserted videos", + items: { + type: "string", + description: "The ID of the inserted video", + }, + }, + }, + }, + }, + }, + }, + }, + }, +} satisfies AppRouteSchema; diff --git a/src/modules/internal/schemas/updateAllEpisodeThumbnail.schema.ts b/src/modules/internal/schemas/updateAllEpisodeThumbnail.schema.ts new file mode 100644 index 0000000..3949bc8 --- /dev/null +++ b/src/modules/internal/schemas/updateAllEpisodeThumbnail.schema.ts @@ -0,0 +1,35 @@ +import { t } from "elysia"; +import { AppRouteSchema } from "../../../helpers/types/AppRouteSchema"; + +export const updateAllEpisodeThumbnailSchema = { + body: t.Object({ + service_reference_id: t.String({ + description: "The ID of the service to which the target of episode thumbnails belong", + }), + }), + detail: { + summary: "Bulk update episode thumbnails", + description: + "Perform bulk update of episode thumbnails for all episodes associated with a specific service reference ID. This operation fetches the latest thumbnail data from external sources and updates the existing episode records in the database accordingly.", + responses: { + 204: { + description: "Updating episode thumbnails operation completed successfully", + content: { + "application/json": { + schema: { + type: "object", + properties: { + success: { type: "boolean", default: true }, + status: { type: "integer", default: 204 }, + message: { + type: "string", + default: "Updating {newEpisodeThumbnailsCount} episode thumbnails operation completed successfully", + }, + }, + }, + }, + }, + }, + }, + }, +} satisfies AppRouteSchema; diff --git a/src/modules/internal/services/http/bulkInsertVideo.service.ts b/src/modules/internal/services/http/bulkInsertVideo.service.ts index 705acf6..2c206f4 100644 --- a/src/modules/internal/services/http/bulkInsertVideo.service.ts +++ b/src/modules/internal/services/http/bulkInsertVideo.service.ts @@ -1,12 +1,11 @@ import { SystemAccountId } from "../../../../config/account/system"; import { ErrorForwarder } from "../../../../helpers/error/instances/forwarder"; -import { BulkInsertVideoBodyRequest } from "../../controllers/bulkInsertVideo.controller"; import { findEpisodeWithMediaIdRepository } from "../../repositories/findEpisodeWithMediaId.repository"; import { bulkInsertVideoRepository } from "../../repositories/bulkInsertVideo.repository"; +import { Static } from "elysia"; +import { bulkInsertVideoSchema } from "../../schemas/bulkInsertVideo.schema"; -export const bulkInsertVideoService = async ( - body: BulkInsertVideoBodyRequest, -) => { +export const bulkInsertVideoService = async (body: Static) => { try { const insertedVideos: string[] = []; for (const episodeData of body.data) {