Kundenportal arbeiten
This commit is contained in:
@@ -10,7 +10,9 @@ import { secrets } from "../utils/secrets"
|
||||
import { saveFile } from "../utils/files"
|
||||
|
||||
import { eq, inArray } from "drizzle-orm"
|
||||
import { and } from "drizzle-orm"
|
||||
import {
|
||||
authProfiles,
|
||||
files,
|
||||
createddocuments,
|
||||
customers
|
||||
@@ -18,6 +20,55 @@ import {
|
||||
|
||||
|
||||
export default async function fileRoutes(server: FastifyInstance) {
|
||||
const getPortalCustomerId = async (req: any) => {
|
||||
const tenantId = req.user?.tenant_id
|
||||
const userId = req.user?.user_id
|
||||
|
||||
if (!tenantId || !userId) return null
|
||||
|
||||
const [profile] = await server.db
|
||||
.select({ customer_for_portal: authProfiles.customer_for_portal })
|
||||
.from(authProfiles)
|
||||
.where(and(
|
||||
eq(authProfiles.tenant_id, tenantId),
|
||||
eq(authProfiles.user_id, userId)
|
||||
))
|
||||
.limit(1)
|
||||
|
||||
return profile?.customer_for_portal || null
|
||||
}
|
||||
|
||||
const loadSingleFileForRequest = async (req: any, id: string) => {
|
||||
const tenantId = req.user?.tenant_id
|
||||
if (!tenantId) return null
|
||||
|
||||
const portalCustomerId = await getPortalCustomerId(req)
|
||||
|
||||
if (!portalCustomerId) {
|
||||
const rows = await server.db
|
||||
.select()
|
||||
.from(files)
|
||||
.where(and(eq(files.id, id), eq(files.tenant, tenantId)))
|
||||
|
||||
return rows[0] || null
|
||||
}
|
||||
|
||||
const rows = await server.db
|
||||
.select({
|
||||
file: files,
|
||||
})
|
||||
.from(files)
|
||||
.leftJoin(createddocuments, eq(files.createddocument, createddocuments.id))
|
||||
.where(and(
|
||||
eq(files.id, id),
|
||||
eq(files.tenant, tenantId),
|
||||
eq(createddocuments.customer, portalCustomerId),
|
||||
eq(createddocuments.availableInPortal, true)
|
||||
))
|
||||
.limit(1)
|
||||
|
||||
return rows[0]?.file || null
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------
|
||||
// MULTIPART INIT
|
||||
@@ -80,12 +131,7 @@ export default async function fileRoutes(server: FastifyInstance) {
|
||||
|
||||
// 🔹 EINZELNE DATEI
|
||||
if (id) {
|
||||
const rows = await server.db
|
||||
.select()
|
||||
.from(files)
|
||||
.where(eq(files.id, id))
|
||||
|
||||
const file = rows[0]
|
||||
const file = await loadSingleFileForRequest(req, id)
|
||||
if (!file) return reply.code(404).send({ error: "Not found" })
|
||||
|
||||
return file
|
||||
@@ -135,12 +181,7 @@ export default async function fileRoutes(server: FastifyInstance) {
|
||||
// 1️⃣ SINGLE DOWNLOAD
|
||||
// -------------------------------------------------
|
||||
if (id) {
|
||||
const rows = await server.db
|
||||
.select()
|
||||
.from(files)
|
||||
.where(eq(files.id, id))
|
||||
|
||||
const file = rows[0]
|
||||
const file = await loadSingleFileForRequest(req, id)
|
||||
if (!file) return reply.code(404).send({ error: "File not found" })
|
||||
|
||||
const command = new GetObjectCommand({
|
||||
@@ -217,12 +258,7 @@ export default async function fileRoutes(server: FastifyInstance) {
|
||||
// SINGLE FILE PRESIGNED URL
|
||||
// -------------------------------------------------
|
||||
if (id) {
|
||||
const rows = await server.db
|
||||
.select()
|
||||
.from(files)
|
||||
.where(eq(files.id, id))
|
||||
|
||||
const file = rows[0]
|
||||
const file = await loadSingleFileForRequest(req, id)
|
||||
if (!file) return reply.code(404).send({ error: "Not found" })
|
||||
|
||||
const url = await getSignedUrl(
|
||||
|
||||
Reference in New Issue
Block a user