Compare commits
3 Commits
b69742f806
...
2a3467a737
| Author | SHA1 | Date | |
|---|---|---|---|
| 2a3467a737 | |||
| 46d7dc8da8 | |||
| 1038ad068f |
@ -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);
|
||||
}
|
||||
};
|
||||
@ -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);
|
||||
}
|
||||
};
|
||||
@ -39,7 +39,7 @@ import { updateAllEpisodeThumbnailService } from "../services/http/updateAllEpis
|
||||
* }
|
||||
*/
|
||||
export const updateAllEpisodeThumbnailController = async (
|
||||
ctx: Context & { body: { service_reference_id: string } },
|
||||
ctx: Context & { body: { service_reference_id?: string } },
|
||||
) => {
|
||||
try {
|
||||
const newEpisodeThumbnailsCount = await updateAllEpisodeThumbnailService(
|
||||
@ -48,7 +48,8 @@ export const updateAllEpisodeThumbnailController = async (
|
||||
return returnWriteResponse(
|
||||
ctx.set,
|
||||
204,
|
||||
`Updating ${newEpisodeThumbnailsCount} episode thumbnails successfully.`,
|
||||
`Updating episode thumbnails successfully.`,
|
||||
newEpisodeThumbnailsCount,
|
||||
);
|
||||
} catch (error) {
|
||||
return mainErrorHandler(ctx.set, error);
|
||||
|
||||
@ -1,35 +1,38 @@
|
||||
import { AppError } from "../../../../helpers/error/instances/app";
|
||||
import { ErrorForwarder } from "../../../../helpers/error/instances/forwarder";
|
||||
import { updateEpisodeRepository } from "../../../episode/repositories/PUT/updateEpisode.repository";
|
||||
import { getAllEpisodeWithThumbnailLinkRepository } from "../../../episode/repositories/GET/getAllEpisodeWithThumbnailLink.repository";
|
||||
import { getAllVideoServiceWithEpisodeRepository } from "../../../videoService/repositories/GET/getAllVideoServiceWithEpisode.repository";
|
||||
|
||||
export const updateAllEpisodeThumbnailService = async (
|
||||
serviceReferenceId: string,
|
||||
serviceReferenceId?: string,
|
||||
) => {
|
||||
try {
|
||||
if (!serviceReferenceId)
|
||||
throw new AppError(400, "Service Reference ID is required.");
|
||||
|
||||
const episodesData = await getAllEpisodeWithThumbnailLinkRepository(
|
||||
const videosData = await getAllVideoServiceWithEpisodeRepository(
|
||||
serviceReferenceId,
|
||||
);
|
||||
|
||||
let updatedThumbnailsCount = 0;
|
||||
for (const episode of episodesData) {
|
||||
if (episode.videos.length === 0) continue;
|
||||
await updateEpisodeRepository({
|
||||
id: episode.id,
|
||||
pictureThumbnail:
|
||||
episode.videos[0].service.endpointThumbnail?.replace(
|
||||
if (!videosData || videosData.length === 0)
|
||||
throw new AppError(
|
||||
404,
|
||||
"No episode with no thumbnail found in the specified video service.",
|
||||
);
|
||||
|
||||
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:",
|
||||
episode.videos[0].code,
|
||||
) || null,
|
||||
});
|
||||
video.thumbnailCode!,
|
||||
),
|
||||
}));
|
||||
});
|
||||
|
||||
updatedThumbnailsCount++;
|
||||
}
|
||||
|
||||
return updatedThumbnailsCount;
|
||||
return updatePayload;
|
||||
} catch (error) {
|
||||
ErrorForwarder(error);
|
||||
}
|
||||
|
||||
3
src/modules/videoService/model.ts
Normal file
3
src/modules/videoService/model.ts
Normal file
@ -0,0 +1,3 @@
|
||||
import { prisma } from "../../utils/databases/prisma/connection";
|
||||
|
||||
export const videoServiceModel = prisma.videoService;
|
||||
@ -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.",
|
||||
);
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user