refactor: migrate to tsup for builds and update tsconfigs fix: update error handling and validation in payment service chore: update package scripts and dependencies docs: update README and env examples
71 lines
2.1 KiB
TypeScript
71 lines
2.1 KiB
TypeScript
import { ResultAsync, ok } from "neverthrow";
|
|
import { PrismaClientArg, Prisma, FileLink, File } from "@repo/db";
|
|
import { FileStorageError, storageOperationFailed } from "../errors";
|
|
import { CreateFileLinkInput } from "../types";
|
|
|
|
const create = (
|
|
client: PrismaClientArg,
|
|
data: CreateFileLinkInput,
|
|
): ResultAsync<FileLink, FileStorageError> => {
|
|
const linkData: Prisma.FileLinkCreateInput = {
|
|
relationId: data.relationId,
|
|
file: {
|
|
connect: { id: data.fileId },
|
|
},
|
|
};
|
|
|
|
return ResultAsync.fromPromise(
|
|
client.fileLink.create({ data: linkData }),
|
|
(error) => storageOperationFailed("createFileLink", error as Error),
|
|
);
|
|
};
|
|
|
|
const findByRelationId = (
|
|
client: PrismaClientArg,
|
|
relationId: string,
|
|
): ResultAsync<(FileLink & { file: File })[], FileStorageError> =>
|
|
ResultAsync.fromPromise(
|
|
client.fileLink.findMany({
|
|
where: { relationId },
|
|
include: { file: true },
|
|
orderBy: { createdAt: "desc" },
|
|
}),
|
|
(error) => storageOperationFailed("findFilesByRelationId", error as Error),
|
|
);
|
|
|
|
const deleteByFileId = (
|
|
client: PrismaClientArg,
|
|
fileId: string,
|
|
): ResultAsync<void, FileStorageError> =>
|
|
ResultAsync.fromPromise(
|
|
client.fileLink.deleteMany({ where: { fileId } }),
|
|
(error) => storageOperationFailed("deleteFileLinks", error as Error),
|
|
).andThen(() => ok(undefined));
|
|
|
|
const deleteByRelationId = (
|
|
client: PrismaClientArg,
|
|
relationId: string,
|
|
): ResultAsync<void, FileStorageError> =>
|
|
ResultAsync.fromPromise(
|
|
client.fileLink.deleteMany({ where: { relationId } }),
|
|
(error) => storageOperationFailed("deleteFileLinks", error as Error),
|
|
).andThen(() => ok(undefined));
|
|
|
|
const deleteSpecificLink = (
|
|
client: PrismaClientArg,
|
|
fileId: string,
|
|
relationId: string,
|
|
): ResultAsync<void, FileStorageError> =>
|
|
ResultAsync.fromPromise(
|
|
client.fileLink.deleteMany({ where: { fileId, relationId } }),
|
|
(error) => storageOperationFailed("deleteFileLink", error as Error),
|
|
).andThen(() => ok(undefined));
|
|
|
|
export const FileLinkRepository = {
|
|
create,
|
|
findByRelationId,
|
|
deleteByFileId,
|
|
deleteByRelationId,
|
|
deleteSpecificLink,
|
|
};
|