feat/collection #29
@ -2,6 +2,7 @@ import slugify from "slugify";
|
||||
import { AppError } from "../../../helpers/error/instances/app";
|
||||
import { prisma } from "../../../utils/databases/prisma/connection";
|
||||
import { generateUUIDv7 } from "../../../helpers/databases/uuidv7";
|
||||
import { Prisma } from "@prisma/client";
|
||||
|
||||
export interface UpsertUserCollectionRepositoryPayload {
|
||||
userId: string;
|
||||
@ -9,7 +10,7 @@ export interface UpsertUserCollectionRepositoryPayload {
|
||||
mediaConnectId: string;
|
||||
}
|
||||
|
||||
export const upsertUserCollectionRepository = async (payload: UpsertUserCollectionRepositoryPayload) => {
|
||||
export const upsertUserCollectionBySystemRepository = async (payload: UpsertUserCollectionRepositoryPayload) => {
|
||||
try {
|
||||
return await prisma.collection.upsert({
|
||||
where: {
|
||||
@ -19,9 +20,14 @@ export const upsertUserCollectionRepository = async (payload: UpsertUserCollecti
|
||||
},
|
||||
},
|
||||
update: {
|
||||
medias: {
|
||||
connect: {
|
||||
id: payload.mediaConnectId,
|
||||
media_saved: {
|
||||
create: {
|
||||
id: generateUUIDv7(),
|
||||
media: {
|
||||
connect: {
|
||||
id: payload.mediaConnectId,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
@ -29,15 +35,26 @@ export const upsertUserCollectionRepository = async (payload: UpsertUserCollecti
|
||||
id: generateUUIDv7(),
|
||||
name: payload.collectionName,
|
||||
slug: slugify(payload.collectionName, { lower: true }),
|
||||
ownerId: payload.userId,
|
||||
medias: {
|
||||
owner: {
|
||||
connect: {
|
||||
id: payload.mediaConnectId,
|
||||
id: payload.userId,
|
||||
},
|
||||
},
|
||||
media_saved: {
|
||||
create: {
|
||||
id: generateUUIDv7(),
|
||||
media: {
|
||||
connect: {
|
||||
id: payload.mediaConnectId,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
if (error instanceof Prisma.PrismaClientKnownRequestError && error.code === "P2002")
|
||||
throw new AppError(400, "Media item is already in the collection");
|
||||
throw new AppError(500, "Failed to upsert user collection");
|
||||
}
|
||||
};
|
||||
@ -1,7 +1,7 @@
|
||||
import { parse } from "cookie";
|
||||
import { jwtDecode } from "../../../helpers/http/jwt/decode";
|
||||
import { tokenValidationService } from "../../auth/services/http/tokenValidation.service";
|
||||
import slugify from "slugify";
|
||||
import { ErrorForwarder } from "../../../helpers/error/instances/forwarder";
|
||||
import { upsertUserCollectionBySystemRepository } from "../repositories/upsertUserCollectionBySystem.repository";
|
||||
|
||||
export type AddItemToCollectionPayload = {
|
||||
cookie: string;
|
||||
@ -10,6 +10,17 @@ export type AddItemToCollectionPayload = {
|
||||
};
|
||||
|
||||
export const addItemToCollectionService = async (payload: AddItemToCollectionPayload) => {
|
||||
const { auth_token } = parse(payload.cookie);
|
||||
return (await tokenValidationService(auth_token as string)).user.id;
|
||||
try {
|
||||
const { auth_token } = parse(payload.cookie);
|
||||
const userData = await tokenValidationService(auth_token as string);
|
||||
const saveMediaToCollection = await upsertUserCollectionBySystemRepository({
|
||||
userId: userData.user.id,
|
||||
collectionName: payload.collectionName,
|
||||
mediaConnectId: payload.mediaId,
|
||||
});
|
||||
|
||||
return saveMediaToCollection;
|
||||
} catch (error) {
|
||||
ErrorForwarder(error);
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user