🚧 wip: update media bulk insert for new schema
This commit is contained in:
@ -1,61 +1,15 @@
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { ErrorForwarder } from "../../../../helpers/error/instances/forwarder";
|
||||
import { bulkInsertGenresRepository } from "../../repositories/bulkInsertGenres.repository";
|
||||
import { InsertMediaRepository } from "../../repositories/bulkinsertMedia.repository";
|
||||
import { bulkInsertStudiosRepository } from "../../repositories/bulkInsertStudios.repository";
|
||||
import { MediaFullInfoResponse } from "../../types/mediaFullInfo.type";
|
||||
import { generateSlug } from "../../../../helpers/characters/generateSlug";
|
||||
import { bulkInsertCharWithVAService } from "../internal/bulkInsertCharWithVA.service";
|
||||
import { generateUUIDv7 } from "../../../../helpers/databases/uuidv7";
|
||||
import { SystemAccountId } from "../../../../config/account/system";
|
||||
import { getContentReferenceAPI } from "../../../../config/apis/jikan/media.reference";
|
||||
|
||||
export const bulkInsertAnimeService = async (malId: number) => {
|
||||
try {
|
||||
const { baseURL, getMediaFullInfo } = getContentReferenceAPI(malId);
|
||||
const mediaFullInfo = (await fetch(baseURL + getMediaFullInfo).then((res) =>
|
||||
res.json(),
|
||||
)) as MediaFullInfoResponse;
|
||||
const mediaFullInfo = (await fetch(baseURL + getMediaFullInfo).then((res) => res.json())) as MediaFullInfoResponse;
|
||||
|
||||
const insertedGenres = await bulkInsertGenresRepository(mediaFullInfo);
|
||||
const insertedStudios = await bulkInsertStudiosRepository(mediaFullInfo);
|
||||
const insertedCharacters = await bulkInsertCharWithVAService(malId);
|
||||
|
||||
const constructMediaPayload: Prisma.MediaUpsertArgs["create"] = {
|
||||
id: generateUUIDv7(),
|
||||
title: mediaFullInfo.data.title,
|
||||
titleAlternative: mediaFullInfo.data
|
||||
.titles as unknown as Prisma.InputJsonValue,
|
||||
slug: await generateSlug(mediaFullInfo.data.title, {
|
||||
model: "media",
|
||||
target: "slug",
|
||||
}),
|
||||
malId: mediaFullInfo.data.mal_id,
|
||||
genres: {
|
||||
connect: insertedGenres.map((id) => ({ id })),
|
||||
},
|
||||
studios: {
|
||||
connect: insertedStudios.map((id) => ({ id })),
|
||||
},
|
||||
characters: {
|
||||
connect: insertedCharacters.map(({ id }) => ({ id })),
|
||||
},
|
||||
score: mediaFullInfo.data.score,
|
||||
pictureMedium: mediaFullInfo.data.images.webp.image_url,
|
||||
pictureLarge: mediaFullInfo.data.images.webp.large_image_url,
|
||||
status: mediaFullInfo.data.status,
|
||||
startAiring: mediaFullInfo.data.aired.from,
|
||||
endAiring: mediaFullInfo.data.aired.to,
|
||||
synopsis: mediaFullInfo.data.synopsis,
|
||||
ageRating: mediaFullInfo.data.rating,
|
||||
mediaType: mediaFullInfo.data.type,
|
||||
source: mediaFullInfo.data.source,
|
||||
onDraft: false,
|
||||
uploadedBy: SystemAccountId,
|
||||
};
|
||||
const insertedMedia = await InsertMediaRepository({
|
||||
malId: mediaFullInfo.data.mal_id,
|
||||
payload: constructMediaPayload,
|
||||
payload: mediaFullInfo.data,
|
||||
});
|
||||
|
||||
return insertedMedia;
|
||||
|
||||
@ -0,0 +1,28 @@
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { MediaFullInfoResponse } from "../../types/mediaFullInfo.type";
|
||||
import { prisma } from "../../../../utils/databases/prisma/connection";
|
||||
|
||||
interface InsertedProducer {
|
||||
producer: string[];
|
||||
licensor: string[];
|
||||
studio: string[];
|
||||
}
|
||||
|
||||
export const bulkInsertProducerService = async (payload: MediaFullInfoResponse, systemAccountId: string) => {
|
||||
const insertedPayload: InsertedProducer = {
|
||||
producer: [],
|
||||
licensor: [],
|
||||
studio: [],
|
||||
};
|
||||
|
||||
const insertingMainProducer = await prisma.producer.createMany({
|
||||
data: payload.data.producers.map((producer) => ({
|
||||
mal_id: producer.mal_id,
|
||||
type: producer.type,
|
||||
name: producer.name,
|
||||
url: producer.url,
|
||||
created_by_id: systemAccountId,
|
||||
})),
|
||||
skipDuplicates: true,
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user