Compare commits

..

3 Commits

6 changed files with 66 additions and 72 deletions

View File

@ -1,34 +0,0 @@
import { AppError } from "../../../../helpers/error/instances/app";
import { episodeModel } from "../../episode.model";
export const getAllEpisodeWithThumbnailLinkRepository = async (
serviceReferenceId: string,
) => {
try {
return await episodeModel.findMany({
where: {
deletedAt: null,
},
select: {
id: true,
episode: true,
videos: {
where: {
deletedAt: null,
serviceId: serviceReferenceId,
},
select: {
code: true,
service: {
select: {
endpointThumbnail: true,
},
},
},
},
},
});
} catch (error) {
throw new AppError(500, "Failed to get all episode thumbnails", error);
}
};

View File

@ -1,18 +0,0 @@
import { Prisma } from "@prisma/client";
import { AppError } from "../../../../helpers/error/instances/app";
import { episodeModel } from "../../episode.model";
export const updateEpisodeRepository = async (
payload: Prisma.EpisodeUncheckedUpdateInput,
) => {
try {
return await episodeModel.update({
where: {
id: payload.id as string,
},
data: payload,
});
} catch (error) {
throw new AppError(500, "Failed to edit episode", error);
}
};

View File

@ -39,7 +39,7 @@ import { updateAllEpisodeThumbnailService } from "../services/http/updateAllEpis
* } * }
*/ */
export const updateAllEpisodeThumbnailController = async ( export const updateAllEpisodeThumbnailController = async (
ctx: Context & { body: { service_reference_id: string } }, ctx: Context & { body: { service_reference_id?: string } },
) => { ) => {
try { try {
const newEpisodeThumbnailsCount = await updateAllEpisodeThumbnailService( const newEpisodeThumbnailsCount = await updateAllEpisodeThumbnailService(
@ -48,7 +48,8 @@ export const updateAllEpisodeThumbnailController = async (
return returnWriteResponse( return returnWriteResponse(
ctx.set, ctx.set,
204, 204,
`Updating ${newEpisodeThumbnailsCount} episode thumbnails successfully.`, `Updating episode thumbnails successfully.`,
newEpisodeThumbnailsCount,
); );
} catch (error) { } catch (error) {
return mainErrorHandler(ctx.set, error); return mainErrorHandler(ctx.set, error);

View File

@ -1,35 +1,38 @@
import { AppError } from "../../../../helpers/error/instances/app"; import { AppError } from "../../../../helpers/error/instances/app";
import { ErrorForwarder } from "../../../../helpers/error/instances/forwarder"; import { ErrorForwarder } from "../../../../helpers/error/instances/forwarder";
import { updateEpisodeRepository } from "../../../episode/repositories/PUT/updateEpisode.repository"; import { getAllVideoServiceWithEpisodeRepository } from "../../../videoService/repositories/GET/getAllVideoServiceWithEpisode.repository";
import { getAllEpisodeWithThumbnailLinkRepository } from "../../../episode/repositories/GET/getAllEpisodeWithThumbnailLink.repository";
export const updateAllEpisodeThumbnailService = async ( export const updateAllEpisodeThumbnailService = async (
serviceReferenceId: string, serviceReferenceId?: string,
) => { ) => {
try { try {
if (!serviceReferenceId) if (!serviceReferenceId)
throw new AppError(400, "Service Reference ID is required."); throw new AppError(400, "Service Reference ID is required.");
const episodesData = await getAllEpisodeWithThumbnailLinkRepository( const videosData = await getAllVideoServiceWithEpisodeRepository(
serviceReferenceId, serviceReferenceId,
); );
let updatedThumbnailsCount = 0; if (!videosData || videosData.length === 0)
for (const episode of episodesData) { throw new AppError(
if (episode.videos.length === 0) continue; 404,
await updateEpisodeRepository({ "No episode with no thumbnail found in the specified video service.",
id: episode.id, );
pictureThumbnail:
episode.videos[0].service.endpointThumbnail?.replace( const updatePayload = videosData.map((videoService) => {
const { endpointThumbnail, videos } = videoService;
return videos
.filter((video) => video.thumbnailCode !== null)
.map((video) => ({
episodeId: video.episode.id,
thumbnailCode: endpointThumbnail?.replace(
":code:", ":code:",
episode.videos[0].code, video.thumbnailCode!,
) || null, ),
}); }));
});
updatedThumbnailsCount++; return updatePayload;
}
return updatedThumbnailsCount;
} catch (error) { } catch (error) {
ErrorForwarder(error); ErrorForwarder(error);
} }

View File

@ -0,0 +1,3 @@
import { prisma } from "../../utils/databases/prisma/connection";
export const videoServiceModel = prisma.videoService;

View File

@ -0,0 +1,39 @@
import { AppError } from "../../../../helpers/error/instances/app";
import { videoServiceModel } from "../../model";
export const getAllVideoServiceWithEpisodeRepository = async (
videoServiceId: string,
) => {
try {
return await videoServiceModel.findMany({
where: {
id: videoServiceId,
videos: {
some: {
episode: {
pictureThumbnail: null,
},
},
},
},
select: {
endpointThumbnail: true,
videos: {
select: {
thumbnailCode: true,
episode: {
select: {
id: true,
},
},
},
},
},
});
} catch (error) {
throw new AppError(
500,
"An error occurred while fetching video services with episodes.",
);
}
};