feat: create bulk insertion for characters

This commit is contained in:
Rafi Arrafif
2026-01-25 10:57:35 +07:00
parent fe10412f1a
commit 11834924e9
12 changed files with 198 additions and 6 deletions

View File

@ -1,6 +1,10 @@
import { SystemAccountId } from "../../../../config/account/system";
import { getContentReferenceAPI } from "../../../../config/apis/media.reference";
import { ErrorForwarder } from "../../../../helpers/error/instances/forwarder";
import { bulkInsertCharactersRepository } from "../../repositories/bulkInsertCharacters.repository";
import { bulkInsertLangVARepository } from "../../repositories/bulkInsertLangVA.repository";
import { MediaCharWithVAInfo } from "../../types/mediaCharWithVAInfo";
import { bulkInsertStaffOrPeopleService } from "./bulkInsertStaffOrPeople.service";
export const bulkInsertCharWithVAService = async (malId: number) => {
try {
@ -9,7 +13,45 @@ export const bulkInsertCharWithVAService = async (malId: number) => {
`${baseURL}${getMediaCharactersWithVA}`,
).then((res) => res.json())) as MediaCharWithVAInfo;
return;
const insertedCharacters = [];
for (const charEntry of charactersWithVAData.data) {
// Insert character if not exists
const characterInsertedId = await bulkInsertCharactersRepository({
malId: charEntry.character.mal_id,
name: charEntry.character.name,
role: charEntry.role,
favorites: charEntry.favorites,
imageUrl: charEntry.character.images.webp.image_url,
smallImageUrl: charEntry.character.images.webp.small_image_url,
creatorId: SystemAccountId,
});
// Insert character voice actors if not exists
const insertedVAs: { staffId: string; lang: string }[] = [];
for (const VAEntries of charEntry.voice_actors) {
const insertedVAId = await bulkInsertStaffOrPeopleService(
VAEntries.person.mal_id,
);
insertedVAs.push({
staffId: insertedVAId.id,
lang: VAEntries.language,
});
}
// Link character with inserted VAs
for (const langVA of insertedVAs) {
await bulkInsertLangVARepository({
language: langVA.lang,
vaId: langVA.staffId,
charId: characterInsertedId.id,
creatorId: SystemAccountId,
});
}
insertedCharacters.push(characterInsertedId);
}
return insertedCharacters;
} catch (error) {
ErrorForwarder(error);
}

View File

@ -0,0 +1,27 @@
import { SystemAccountId } from "../../../../config/account/system";
import { getPeopleAPI } from "../../../../config/apis/people.reference";
import { ErrorForwarder } from "../../../../helpers/error/instances/forwarder";
import { bulkInsertVoiceActorRepository } from "../../repositories/bulkInsertVoiceActor.repository";
import { PeopleInfoResponse } from "../../types/peopleInfo";
export const bulkInsertStaffOrPeopleService = async (malId: number) => {
try {
const { baseURL, getPeopleInfo } = getPeopleAPI(malId);
const peopleData = (await fetch(baseURL + getPeopleInfo).then((res) =>
res.json(),
)) as PeopleInfoResponse;
return await bulkInsertVoiceActorRepository({
malId: peopleData.data.mal_id,
name: peopleData.data.name,
birthday: peopleData.data.birthday,
description: peopleData.data.about,
aboutUrl: peopleData.data.url,
imageUrl: peopleData.data.images.jpg.image_url,
websiteUrl: peopleData.data.website_url,
creatorId: SystemAccountId,
});
} catch (error) {
ErrorForwarder(error);
}
};