-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(web): download file feature on admin
- Loading branch information
Showing
5 changed files
with
70 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { env } from "@/env"; | ||
import { auth } from "@/server/auth"; | ||
|
||
export async function GET({ params }: { params: { fileId: string } }) { | ||
const session = await auth(); | ||
if (session?.user.role !== "admin") { | ||
return new Response("Unauthorized", { status: 401 }); | ||
} | ||
|
||
const res = await fetch(env.BACKEND_URL + "/files/download/" + params.fileId); | ||
return res; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 13 additions & 17 deletions
30
apps/web/src/server/api/routers/documents/documents.procedure.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,20 @@ | ||
import type { Document } from "@/server/api/routers/documents/documents.schema"; | ||
import { env } from "@/env"; | ||
import { adminProcedure, createTRPCRouter } from "@/server/api/trpc"; | ||
import { z } from "zod"; | ||
|
||
import { documentSchema } from "./documents.schema"; | ||
|
||
export const documentsRouter = createTRPCRouter({ | ||
get: adminProcedure.query(() => { | ||
const documents: Document[] = [ | ||
{ | ||
id: "1", | ||
name: "Document 1.pdf", | ||
createdAt: new Date(), | ||
uploadedBy: "3152ffe5-6496-4214-8fcd-b69fb4f70fd5", | ||
}, | ||
get: adminProcedure.query(async () => { | ||
const res = await fetch(env.BACKEND_URL + "/files"); | ||
|
||
{ | ||
id: "2", | ||
name: "Document 2.pdf", | ||
createdAt: new Date(), | ||
uploadedBy: "3152ffe5-6496-4214-8fcd-b69fb4f70fd5", | ||
}, | ||
]; | ||
const validate = z | ||
.object({ | ||
status: z.string(), | ||
files: documentSchema.array(), | ||
}) | ||
.parse(await res.json()); | ||
|
||
return documents; | ||
return validate.files; | ||
}), | ||
}); |
13 changes: 9 additions & 4 deletions
13
apps/web/src/server/api/routers/documents/documents.schema.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,11 @@ | ||
import type { z } from "zod"; | ||
import { documents } from "@/server/db/schema"; | ||
import { createSelectSchema } from "drizzle-zod"; | ||
import { z } from "zod"; | ||
|
||
export const documentSchema = z.object({ | ||
id: z.number(), | ||
name: z.string(), | ||
filename: z.string(), | ||
created: z.coerce.date(), | ||
lastModified: z.coerce.date(), | ||
}); | ||
|
||
export const documentSchema = createSelectSchema(documents); | ||
export type Document = z.infer<typeof documentSchema>; |