👔 feat: add bulk thumbnail update logic
Some checks failed
Integration Tests / integration-tests (pull_request) Failing after 42s
Some checks failed
Integration Tests / integration-tests (pull_request) Failing after 42s
This commit is contained in:
@ -0,0 +1,30 @@
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { AppError } from "../../../../helpers/error/instances/app";
|
||||
import { prisma } from "../../../../utils/databases/prisma/connection";
|
||||
|
||||
export const bulkUpdateThumbnailRepository = async (
|
||||
data: { episodeId: string; thumbnailCode: string }[],
|
||||
) => {
|
||||
try {
|
||||
const values = Prisma.join(
|
||||
data.map(
|
||||
(item) => Prisma.sql`(${item.episodeId}::uuid, ${item.thumbnailCode})`,
|
||||
),
|
||||
);
|
||||
|
||||
await prisma.$executeRaw`
|
||||
UPDATE episodes e
|
||||
SET "pictureThumbnail" = v."thumbnailCode"
|
||||
FROM (
|
||||
VALUES ${values}
|
||||
) AS v("episodeId", "thumbnailCode")
|
||||
WHERE e.id = v."episodeId"
|
||||
`;
|
||||
} catch (error) {
|
||||
throw new AppError(
|
||||
500,
|
||||
"An error occurred while bulk updating episode thumbnails.",
|
||||
error,
|
||||
);
|
||||
}
|
||||
};
|
||||
@ -48,8 +48,7 @@ export const updateAllEpisodeThumbnailController = async (
|
||||
return returnWriteResponse(
|
||||
ctx.set,
|
||||
204,
|
||||
`Updating episode thumbnails successfully.`,
|
||||
newEpisodeThumbnailsCount,
|
||||
`Updating ${newEpisodeThumbnailsCount} episode thumbnails successfully.`,
|
||||
);
|
||||
} catch (error) {
|
||||
return mainErrorHandler(ctx.set, error);
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
import { AppError } from "../../../../helpers/error/instances/app";
|
||||
import { ErrorForwarder } from "../../../../helpers/error/instances/forwarder";
|
||||
import { bulkUpdateThumbnailRepository } from "../../../episode/repositories/PUT/bulkUpdateThumbnail.repository";
|
||||
import { getAllVideoServiceWithEpisodeRepository } from "../../../videoService/repositories/GET/getAllVideoServiceWithEpisode.repository";
|
||||
|
||||
export const updateAllEpisodeThumbnailService = async (
|
||||
@ -19,20 +20,20 @@ export const updateAllEpisodeThumbnailService = async (
|
||||
"No episode with no thumbnail found in the specified video service.",
|
||||
);
|
||||
|
||||
const updatePayload = videosData.map((videoService) => {
|
||||
const updatePayload = videosData.flatMap((videoService) => {
|
||||
const { endpointThumbnail, videos } = videoService;
|
||||
return videos
|
||||
.filter((video) => video.thumbnailCode !== null)
|
||||
.map((video) => ({
|
||||
episodeId: video.episode.id,
|
||||
thumbnailCode: endpointThumbnail?.replace(
|
||||
":code:",
|
||||
video.thumbnailCode!,
|
||||
),
|
||||
}));
|
||||
return videos.map((video) => ({
|
||||
episodeId: video.episode.id,
|
||||
thumbnailCode: endpointThumbnail!.replace(
|
||||
":code:",
|
||||
video.thumbnailCode || video.videoCode,
|
||||
),
|
||||
}));
|
||||
});
|
||||
|
||||
return updatePayload;
|
||||
await bulkUpdateThumbnailRepository(updatePayload);
|
||||
|
||||
return updatePayload.length;
|
||||
} catch (error) {
|
||||
ErrorForwarder(error);
|
||||
}
|
||||
|
||||
@ -8,6 +8,9 @@ export const getAllVideoServiceWithEpisodeRepository = async (
|
||||
return await videoServiceModel.findMany({
|
||||
where: {
|
||||
id: videoServiceId,
|
||||
endpointThumbnail: {
|
||||
not: null,
|
||||
},
|
||||
videos: {
|
||||
some: {
|
||||
episode: {
|
||||
@ -21,6 +24,7 @@ export const getAllVideoServiceWithEpisodeRepository = async (
|
||||
videos: {
|
||||
select: {
|
||||
thumbnailCode: true,
|
||||
videoCode: true,
|
||||
episode: {
|
||||
select: {
|
||||
id: true,
|
||||
|
||||
Reference in New Issue
Block a user