infoalloggi-monorepo/apps/infoalloggi/src/server/api/routers/storage.ts

91 lines
2.4 KiB
TypeScript
Raw Normal View History

2025-08-04 17:45:44 +02:00
import { TRPCError } from "@trpc/server";
2025-10-24 16:10:59 +02:00
import z from "zod";
import type { NewUsersStorage } from "~/schemas/public/UsersStorage";
2025-08-04 17:45:44 +02:00
import {
2025-08-28 18:27:07 +02:00
adminProcedure,
createTRPCRouter,
protectedProcedure,
2025-08-04 17:45:44 +02:00
} from "~/server/api/trpc";
import {
2025-10-24 16:10:59 +02:00
addUserStorage,
deleteUserStorageEntry,
getAllWithFromAdmin,
2025-08-28 18:27:07 +02:00
getMultipleWithRelations,
2025-10-24 16:10:59 +02:00
purgeFileFromUserStorages,
retrieveUserFiles,
2025-08-04 17:45:44 +02:00
} from "~/server/controllers/storage.controller";
2025-08-28 18:27:07 +02:00
import { db } from "~/server/db";
2025-08-04 17:45:44 +02:00
import {
2025-10-24 16:10:59 +02:00
deleteFile,
editFile,
getFileInfo,
2025-08-04 17:45:44 +02:00
} from "~/server/services/storage.service";
2025-10-24 16:10:59 +02:00
import { zUserId } from "~/server/utils/zod_types";
2025-08-04 17:45:44 +02:00
export const storageRouter = createTRPCRouter({
2025-10-24 16:10:59 +02:00
getFileInfos: protectedProcedure
.input(z.object({ storageId: z.string() }))
.query(async ({ input }) => {
return (await getFileInfo(input.storageId)) || undefined;
2025-08-28 18:27:07 +02:00
}),
2025-10-24 16:10:59 +02:00
deleteFile: protectedProcedure
.input(z.object({ storageId: z.string() }))
2025-08-28 18:27:07 +02:00
.mutation(async ({ input }) => {
2025-10-24 16:10:59 +02:00
const deletion = await deleteFile(input.storageId);
if (!deletion.success) {
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: deletion.error,
});
}
await purgeFileFromUserStorages({
...input,
2025-08-29 16:18:32 +02:00
});
}),
2025-10-24 16:10:59 +02:00
deleteUserStorageEntry: protectedProcedure
.input(z.object({ storageId: z.string(), userId: zUserId }))
2025-08-29 16:18:32 +02:00
.mutation(async ({ input }) => {
2025-10-24 16:10:59 +02:00
return await deleteUserStorageEntry(input);
2025-08-28 18:27:07 +02:00
}),
2025-10-24 16:10:59 +02:00
deleteFilesFromUserStorage: adminProcedure
.input(z.object({ storageIds: z.string().array() }))
2025-08-28 18:27:07 +02:00
.mutation(async ({ input }) => {
2025-10-24 16:10:59 +02:00
for (const storageId of input.storageIds) {
await purgeFileFromUserStorages({ storageId });
}
return "ok";
2025-08-28 18:27:07 +02:00
}),
2025-10-24 16:10:59 +02:00
retrieveUserFileData: protectedProcedure
.input(z.object({ userId: zUserId }))
2025-08-28 18:27:07 +02:00
.query(async ({ input }) => {
2025-10-24 16:10:59 +02:00
return await retrieveUserFiles(input);
2025-08-28 18:27:07 +02:00
}),
2025-10-24 16:10:59 +02:00
//OLD
addUserStorage: protectedProcedure
.input(z.custom<NewUsersStorage>())
2025-08-29 16:18:32 +02:00
.mutation(async ({ input }) => {
2025-10-24 16:10:59 +02:00
return await addUserStorage(input);
2025-08-28 18:27:07 +02:00
}),
2025-10-24 16:10:59 +02:00
getAll: protectedProcedure.query(async () => {
return await getAllWithFromAdmin();
}),
2025-08-28 18:27:07 +02:00
getStorageVisibility: adminProcedure
2025-10-24 16:10:59 +02:00
.input(z.object({ storageIds: z.string().array() }))
2025-08-28 18:27:07 +02:00
.query(async ({ input }) => {
return await getMultipleWithRelations({
db,
ids: input.storageIds,
});
}),
2025-10-24 16:10:59 +02:00
2025-08-29 16:18:32 +02:00
renameFile: adminProcedure
2025-10-24 16:10:59 +02:00
.input(z.object({ fileId: z.string(), name: z.string() }))
2025-08-28 18:27:07 +02:00
.mutation(async ({ input }) => {
2025-10-24 16:10:59 +02:00
return await editFile(input.fileId, input.name);
2025-08-28 18:27:07 +02:00
}),
2025-08-04 17:45:44 +02:00
});