redone admin
added branches
This commit is contained in:
@@ -9,6 +9,7 @@ import {
|
||||
authRolePermissions,
|
||||
} from "../../../db/schema"
|
||||
import { eq, and, or, isNull } from "drizzle-orm"
|
||||
import { enrichProfilesWithBranches } from "../../utils/profileBranches"
|
||||
|
||||
export default async function meRoutes(server: FastifyInstance) {
|
||||
server.get("/me", async (req, reply) => {
|
||||
@@ -89,7 +90,8 @@ export default async function meRoutes(server: FastifyInstance) {
|
||||
)
|
||||
.limit(1)
|
||||
|
||||
profile = profileResult?.[0] ?? null
|
||||
const enrichedProfiles = await enrichProfilesWithBranches(server, profileResult)
|
||||
profile = enrichedProfiles?.[0] ?? null
|
||||
}
|
||||
|
||||
// ----------------------------------------------------
|
||||
|
||||
@@ -8,6 +8,7 @@ import {
|
||||
} from "../../../db/schema"
|
||||
|
||||
import {and, eq, inArray} from "drizzle-orm"
|
||||
import { enrichProfilesWithBranches } from "../../utils/profileBranches"
|
||||
|
||||
|
||||
export default async function tenantRoutesInternal(server: FastifyInstance) {
|
||||
@@ -53,7 +54,7 @@ export default async function tenantRoutesInternal(server: FastifyInstance) {
|
||||
.where(inArray(authUsers.id, userIds))
|
||||
|
||||
// 3) auth_profiles pro Tenant laden
|
||||
const profiles = await server.db
|
||||
const profileRows = await server.db
|
||||
.select()
|
||||
.from(authProfiles)
|
||||
.where(
|
||||
@@ -61,6 +62,7 @@ export default async function tenantRoutesInternal(server: FastifyInstance) {
|
||||
eq(authProfiles.tenant_id, tenantId),
|
||||
inArray(authProfiles.user_id, userIds)
|
||||
))
|
||||
const profiles = await enrichProfilesWithBranches(server, profileRows)
|
||||
|
||||
const combined = users.map(u => {
|
||||
const profile = profiles.find(p => p.user_id === u.id)
|
||||
@@ -91,12 +93,12 @@ export default async function tenantRoutesInternal(server: FastifyInstance) {
|
||||
const tenantId = req.params.id
|
||||
if (!tenantId) return reply.code(401).send({ error: "Unauthorized" })
|
||||
|
||||
const data = await server.db
|
||||
const profileRows = await server.db
|
||||
.select()
|
||||
.from(authProfiles)
|
||||
.where(eq(authProfiles.tenant_id, tenantId))
|
||||
|
||||
return data
|
||||
return await enrichProfilesWithBranches(server, profileRows)
|
||||
|
||||
} catch (err) {
|
||||
console.error("/tenant/profiles ERROR:", err)
|
||||
|
||||
@@ -4,6 +4,11 @@ import { eq, and } from "drizzle-orm";
|
||||
import {
|
||||
authProfiles,
|
||||
} from "../../db/schema";
|
||||
import {
|
||||
loadProfileWithBranches,
|
||||
resolveTenantBranchIds,
|
||||
syncProfileBranches,
|
||||
} from "../utils/profileBranches";
|
||||
|
||||
export default async function authProfilesRoutes(server: FastifyInstance) {
|
||||
|
||||
@@ -19,22 +24,13 @@ export default async function authProfilesRoutes(server: FastifyInstance) {
|
||||
return reply.code(400).send({ error: "No tenant selected" });
|
||||
}
|
||||
|
||||
const rows = await server.db
|
||||
.select()
|
||||
.from(authProfiles)
|
||||
.where(
|
||||
and(
|
||||
eq(authProfiles.id, id),
|
||||
eq(authProfiles.tenant_id, tenantId)
|
||||
)
|
||||
)
|
||||
.limit(1);
|
||||
const profile = await loadProfileWithBranches(server, id, tenantId)
|
||||
|
||||
if (!rows.length) {
|
||||
if (!profile) {
|
||||
return reply.code(404).send({ error: "User not found or not in tenant" });
|
||||
}
|
||||
|
||||
return rows[0];
|
||||
return profile;
|
||||
|
||||
} catch (error) {
|
||||
console.error("GET /profiles/:id ERROR:", error);
|
||||
@@ -48,7 +44,8 @@ export default async function authProfilesRoutes(server: FastifyInstance) {
|
||||
// ❌ Systemfelder entfernen
|
||||
const forbidden = [
|
||||
"id", "user_id", "tenant_id", "created_at", "updated_at",
|
||||
"updatedAt", "updatedBy", "old_profile_id", "full_name"
|
||||
"updatedAt", "updatedBy", "old_profile_id", "full_name",
|
||||
"branch", "branches", "branch_ids"
|
||||
]
|
||||
forbidden.forEach(f => delete cleaned[f])
|
||||
|
||||
@@ -89,8 +86,19 @@ export default async function authProfilesRoutes(server: FastifyInstance) {
|
||||
// Clean + Normalize
|
||||
body = sanitizeProfileUpdate(body)
|
||||
|
||||
const { primaryBranchId, branchIds } = await resolveTenantBranchIds(
|
||||
server,
|
||||
tenantId,
|
||||
[
|
||||
...(Array.isArray(body.branch_ids) ? body.branch_ids : []),
|
||||
...(Array.isArray(body.branches) ? body.branches : []),
|
||||
],
|
||||
body.branch_id ?? body.branch?.id ?? null
|
||||
)
|
||||
|
||||
const updateData = {
|
||||
...body,
|
||||
branch_id: primaryBranchId,
|
||||
updatedAt: new Date(),
|
||||
updatedBy: userId
|
||||
}
|
||||
@@ -110,10 +118,16 @@ export default async function authProfilesRoutes(server: FastifyInstance) {
|
||||
return reply.code(404).send({ error: "User not found or not in tenant" })
|
||||
}
|
||||
|
||||
return updated[0]
|
||||
await syncProfileBranches(server, id, branchIds, userId)
|
||||
|
||||
const profile = await loadProfileWithBranches(server, id, tenantId)
|
||||
return profile || updated[0]
|
||||
|
||||
} catch (err) {
|
||||
console.error("PUT /profiles/:id ERROR:", err)
|
||||
if (err instanceof Error && ["INVALID_BRANCH_SELECTION", "INVALID_PRIMARY_BRANCH"].includes(err.message)) {
|
||||
return reply.code(400).send({ error: "Ungültige Niederlassungsauswahl" })
|
||||
}
|
||||
return reply.code(500).send({ error: "Internal Server Error" })
|
||||
}
|
||||
})
|
||||
|
||||
@@ -12,6 +12,7 @@ import {
|
||||
} from "../../db/schema"
|
||||
|
||||
import {and, desc, eq, inArray} from "drizzle-orm"
|
||||
import { enrichProfilesWithBranches } from "../utils/profileBranches"
|
||||
|
||||
|
||||
export default async function tenantRoutes(server: FastifyInstance) {
|
||||
@@ -123,7 +124,7 @@ export default async function tenantRoutes(server: FastifyInstance) {
|
||||
.where(inArray(authUsers.id, userIds))
|
||||
|
||||
// 3) auth_profiles pro Tenant laden
|
||||
const profiles = await server.db
|
||||
const profileRows = await server.db
|
||||
.select()
|
||||
.from(authProfiles)
|
||||
.where(
|
||||
@@ -131,6 +132,7 @@ export default async function tenantRoutes(server: FastifyInstance) {
|
||||
eq(authProfiles.tenant_id, tenantId),
|
||||
inArray(authProfiles.user_id, userIds)
|
||||
))
|
||||
const profiles = await enrichProfilesWithBranches(server, profileRows)
|
||||
|
||||
const combined = users.map(u => {
|
||||
const profile = profiles.find(p => p.user_id === u.id)
|
||||
@@ -160,11 +162,12 @@ export default async function tenantRoutes(server: FastifyInstance) {
|
||||
const tenantId = req.user?.tenant_id
|
||||
if (!tenantId) return reply.code(401).send({ error: "Unauthorized" })
|
||||
|
||||
const data = await server.db
|
||||
const profileRows = await server.db
|
||||
.select()
|
||||
.from(authProfiles)
|
||||
.where(eq(authProfiles.tenant_id, tenantId))
|
||||
|
||||
const data = await enrichProfilesWithBranches(server, profileRows)
|
||||
return { data }
|
||||
|
||||
} catch (err) {
|
||||
|
||||
Reference in New Issue
Block a user