👔 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(
|
return returnWriteResponse(
|
||||||
ctx.set,
|
ctx.set,
|
||||||
204,
|
204,
|
||||||
`Updating episode thumbnails successfully.`,
|
`Updating ${newEpisodeThumbnailsCount} episode thumbnails successfully.`,
|
||||||
newEpisodeThumbnailsCount,
|
|
||||||
);
|
);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return mainErrorHandler(ctx.set, error);
|
return mainErrorHandler(ctx.set, error);
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
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 { bulkUpdateThumbnailRepository } from "../../../episode/repositories/PUT/bulkUpdateThumbnail.repository";
|
||||||
import { getAllVideoServiceWithEpisodeRepository } from "../../../videoService/repositories/GET/getAllVideoServiceWithEpisode.repository";
|
import { getAllVideoServiceWithEpisodeRepository } from "../../../videoService/repositories/GET/getAllVideoServiceWithEpisode.repository";
|
||||||
|
|
||||||
export const updateAllEpisodeThumbnailService = async (
|
export const updateAllEpisodeThumbnailService = async (
|
||||||
@ -19,20 +20,20 @@ export const updateAllEpisodeThumbnailService = async (
|
|||||||
"No episode with no thumbnail found in the specified video service.",
|
"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;
|
const { endpointThumbnail, videos } = videoService;
|
||||||
return videos
|
return videos.map((video) => ({
|
||||||
.filter((video) => video.thumbnailCode !== null)
|
|
||||||
.map((video) => ({
|
|
||||||
episodeId: video.episode.id,
|
episodeId: video.episode.id,
|
||||||
thumbnailCode: endpointThumbnail?.replace(
|
thumbnailCode: endpointThumbnail!.replace(
|
||||||
":code:",
|
":code:",
|
||||||
video.thumbnailCode!,
|
video.thumbnailCode || video.videoCode,
|
||||||
),
|
),
|
||||||
}));
|
}));
|
||||||
});
|
});
|
||||||
|
|
||||||
return updatePayload;
|
await bulkUpdateThumbnailRepository(updatePayload);
|
||||||
|
|
||||||
|
return updatePayload.length;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
ErrorForwarder(error);
|
ErrorForwarder(error);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -8,6 +8,9 @@ export const getAllVideoServiceWithEpisodeRepository = async (
|
|||||||
return await videoServiceModel.findMany({
|
return await videoServiceModel.findMany({
|
||||||
where: {
|
where: {
|
||||||
id: videoServiceId,
|
id: videoServiceId,
|
||||||
|
endpointThumbnail: {
|
||||||
|
not: null,
|
||||||
|
},
|
||||||
videos: {
|
videos: {
|
||||||
some: {
|
some: {
|
||||||
episode: {
|
episode: {
|
||||||
@ -21,6 +24,7 @@ export const getAllVideoServiceWithEpisodeRepository = async (
|
|||||||
videos: {
|
videos: {
|
||||||
select: {
|
select: {
|
||||||
thumbnailCode: true,
|
thumbnailCode: true,
|
||||||
|
videoCode: true,
|
||||||
episode: {
|
episode: {
|
||||||
select: {
|
select: {
|
||||||
id: true,
|
id: true,
|
||||||
|
|||||||
Reference in New Issue
Block a user