♻️ refactor: move file to related folder

This commit is contained in:
2026-01-23 22:29:30 +07:00
parent 4c1f891f12
commit 7d26ca7f7b
8 changed files with 223 additions and 157 deletions

View File

@ -0,0 +1,61 @@
import { generateSlug } from "../../../helpers/characters/generateSlug";
import { AppError } from "../../../helpers/error/instances/app";
import { prisma } from "../../../utils/databases/prisma/connection";
import { MediaFullInfoResponse } from "../types/mediaFullInfo.type";
/**
* Studios Insertion
*
* This section manages the insertion of studios associated with the media.
* It processes each studio listed in the media data, generating a slug for
* each and performing an upsert operation to either create or update the
* studio record in the database. The IDs of the inserted or updated studios
* are collected for later association with the media.
*
* @param data - The full media data containing studios information.
* @returns An array of IDs of the inserted or updated studios.
*/
export const bulkInsertStudiosRepository = async (
data: MediaFullInfoResponse,
) => {
try {
const studioIds: string[] = [];
for (const studio of data.data.studios) {
const slug = (await generateSlug(studio.name)) as string;
const studioPayload = {
name: studio.name,
malId: studio.mal_id,
linkAbout: studio.url,
createdBy: "b734b9bc-b4ea-408f-a80e-0a837ce884da",
slug,
};
const insertedStudio = await prisma.studio.upsert({
where: { slug },
create: studioPayload,
update: studioPayload,
select: { id: true },
});
studioIds.push(insertedStudio.id);
}
for (const studio of data.data.producers) {
const slug = (await generateSlug(studio.name)) as string;
const studioPayload = {
name: studio.name,
malId: studio.mal_id,
linkAbout: studio.url,
createdBy: "b734b9bc-b4ea-408f-a80e-0a837ce884da",
slug,
};
const insertedStudio = await prisma.studio.upsert({
where: { slug },
create: studioPayload,
update: studioPayload,
select: { id: true },
});
studioIds.push(insertedStudio.id);
}
return studioIds;
} catch (error) {
throw new AppError(500, "Failed to insert studios", error);
}
};