✨ feat: add collection module
This commit is contained in:
@ -2,6 +2,7 @@ import slugify from "slugify";
|
|||||||
import { AppError } from "../../../helpers/error/instances/app";
|
import { AppError } from "../../../helpers/error/instances/app";
|
||||||
import { prisma } from "../../../utils/databases/prisma/connection";
|
import { prisma } from "../../../utils/databases/prisma/connection";
|
||||||
import { generateUUIDv7 } from "../../../helpers/databases/uuidv7";
|
import { generateUUIDv7 } from "../../../helpers/databases/uuidv7";
|
||||||
|
import { Prisma } from "@prisma/client";
|
||||||
|
|
||||||
export interface UpsertUserCollectionRepositoryPayload {
|
export interface UpsertUserCollectionRepositoryPayload {
|
||||||
userId: string;
|
userId: string;
|
||||||
@ -9,7 +10,7 @@ export interface UpsertUserCollectionRepositoryPayload {
|
|||||||
mediaConnectId: string;
|
mediaConnectId: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const upsertUserCollectionRepository = async (payload: UpsertUserCollectionRepositoryPayload) => {
|
export const upsertUserCollectionBySystemRepository = async (payload: UpsertUserCollectionRepositoryPayload) => {
|
||||||
try {
|
try {
|
||||||
return await prisma.collection.upsert({
|
return await prisma.collection.upsert({
|
||||||
where: {
|
where: {
|
||||||
@ -19,25 +20,41 @@ export const upsertUserCollectionRepository = async (payload: UpsertUserCollecti
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
update: {
|
update: {
|
||||||
medias: {
|
media_saved: {
|
||||||
|
create: {
|
||||||
|
id: generateUUIDv7(),
|
||||||
|
media: {
|
||||||
connect: {
|
connect: {
|
||||||
id: payload.mediaConnectId,
|
id: payload.mediaConnectId,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
create: {
|
create: {
|
||||||
id: generateUUIDv7(),
|
id: generateUUIDv7(),
|
||||||
name: payload.collectionName,
|
name: payload.collectionName,
|
||||||
slug: slugify(payload.collectionName, { lower: true }),
|
slug: slugify(payload.collectionName, { lower: true }),
|
||||||
ownerId: payload.userId,
|
owner: {
|
||||||
medias: {
|
connect: {
|
||||||
|
id: payload.userId,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
media_saved: {
|
||||||
|
create: {
|
||||||
|
id: generateUUIDv7(),
|
||||||
|
media: {
|
||||||
connect: {
|
connect: {
|
||||||
id: payload.mediaConnectId,
|
id: payload.mediaConnectId,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} 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");
|
throw new AppError(500, "Failed to upsert user collection");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -1,7 +1,7 @@
|
|||||||
import { parse } from "cookie";
|
import { parse } from "cookie";
|
||||||
import { jwtDecode } from "../../../helpers/http/jwt/decode";
|
|
||||||
import { tokenValidationService } from "../../auth/services/http/tokenValidation.service";
|
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 = {
|
export type AddItemToCollectionPayload = {
|
||||||
cookie: string;
|
cookie: string;
|
||||||
@ -10,6 +10,17 @@ export type AddItemToCollectionPayload = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const addItemToCollectionService = async (payload: AddItemToCollectionPayload) => {
|
export const addItemToCollectionService = async (payload: AddItemToCollectionPayload) => {
|
||||||
|
try {
|
||||||
const { auth_token } = parse(payload.cookie);
|
const { auth_token } = parse(payload.cookie);
|
||||||
return (await tokenValidationService(auth_token as string)).user.id;
|
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