Added Teams
Minor Rework of Plantafel
This commit is contained in:
31
backend/db/migrations/0028_teams.sql
Normal file
31
backend/db/migrations/0028_teams.sql
Normal file
@@ -0,0 +1,31 @@
|
||||
CREATE TABLE "teams" (
|
||||
"id" bigint PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY (sequence name "teams_id_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 9223372036854775807 START WITH 1 CACHE 1),
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
"tenant" bigint NOT NULL,
|
||||
"name" text NOT NULL,
|
||||
"description" text,
|
||||
"branch" bigint,
|
||||
"archived" boolean DEFAULT false NOT NULL,
|
||||
"updated_at" timestamp with time zone,
|
||||
"updated_by" uuid
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "auth_profile_teams" (
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
"profile_id" uuid NOT NULL,
|
||||
"team_id" bigint NOT NULL,
|
||||
"created_by" uuid,
|
||||
CONSTRAINT "auth_profile_teams_profile_id_team_id_pk" PRIMARY KEY("profile_id","team_id")
|
||||
);
|
||||
--> statement-breakpoint
|
||||
ALTER TABLE "teams" ADD CONSTRAINT "teams_tenant_tenants_id_fk" FOREIGN KEY ("tenant") REFERENCES "public"."tenants"("id") ON DELETE no action ON UPDATE no action;
|
||||
--> statement-breakpoint
|
||||
ALTER TABLE "teams" ADD CONSTRAINT "teams_branch_branches_id_fk" FOREIGN KEY ("branch") REFERENCES "public"."branches"("id") ON DELETE no action ON UPDATE no action;
|
||||
--> statement-breakpoint
|
||||
ALTER TABLE "teams" ADD CONSTRAINT "teams_updated_by_auth_users_id_fk" FOREIGN KEY ("updated_by") REFERENCES "public"."auth_users"("id") ON DELETE no action ON UPDATE no action;
|
||||
--> statement-breakpoint
|
||||
ALTER TABLE "auth_profile_teams" ADD CONSTRAINT "auth_profile_teams_profile_id_auth_profiles_id_fk" FOREIGN KEY ("profile_id") REFERENCES "public"."auth_profiles"("id") ON DELETE cascade ON UPDATE no action;
|
||||
--> statement-breakpoint
|
||||
ALTER TABLE "auth_profile_teams" ADD CONSTRAINT "auth_profile_teams_team_id_teams_id_fk" FOREIGN KEY ("team_id") REFERENCES "public"."teams"("id") ON DELETE cascade ON UPDATE no action;
|
||||
--> statement-breakpoint
|
||||
ALTER TABLE "auth_profile_teams" ADD CONSTRAINT "auth_profile_teams_created_by_auth_users_id_fk" FOREIGN KEY ("created_by") REFERENCES "public"."auth_users"("id") ON DELETE no action ON UPDATE no action;
|
||||
1
backend/db/migrations/0029_events_quick.sql
Normal file
1
backend/db/migrations/0029_events_quick.sql
Normal file
@@ -0,0 +1 @@
|
||||
ALTER TABLE "events" ADD COLUMN "quick" boolean DEFAULT false NOT NULL;
|
||||
@@ -197,6 +197,20 @@
|
||||
"when": 1774602000000,
|
||||
"tag": "0027_product_supplier_link",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 28,
|
||||
"version": "7",
|
||||
"when": 1776124800000,
|
||||
"tag": "0028_teams",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 29,
|
||||
"version": "7",
|
||||
"when": 1776211200000,
|
||||
"tag": "0029_events_quick",
|
||||
"breakpoints": true
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
30
backend/db/schema/auth_profile_teams.ts
Normal file
30
backend/db/schema/auth_profile_teams.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { pgTable, uuid, bigint, timestamp } from "drizzle-orm/pg-core"
|
||||
|
||||
import { authProfiles } from "./auth_profiles"
|
||||
import { teams } from "./teams"
|
||||
import { authUsers } from "./auth_users"
|
||||
|
||||
export const authProfileTeams = pgTable(
|
||||
"auth_profile_teams",
|
||||
{
|
||||
created_at: timestamp("created_at", { withTimezone: true })
|
||||
.notNull()
|
||||
.defaultNow(),
|
||||
|
||||
profile_id: uuid("profile_id")
|
||||
.notNull()
|
||||
.references(() => authProfiles.id, { onDelete: "cascade" }),
|
||||
|
||||
team_id: bigint("team_id", { mode: "number" })
|
||||
.notNull()
|
||||
.references(() => teams.id, { onDelete: "cascade" }),
|
||||
|
||||
created_by: uuid("created_by").references(() => authUsers.id),
|
||||
},
|
||||
(table) => ({
|
||||
primaryKey: [table.profile_id, table.team_id],
|
||||
})
|
||||
)
|
||||
|
||||
export type AuthProfileTeam = typeof authProfileTeams.$inferSelect
|
||||
export type NewAuthProfileTeam = typeof authProfileTeams.$inferInsert
|
||||
@@ -31,6 +31,7 @@ export const events = pgTable(
|
||||
endDate: timestamp("endDate", { withTimezone: true }),
|
||||
|
||||
eventtype: text("eventtype").default("Umsetzung"),
|
||||
quick: boolean("quick").notNull().default(false),
|
||||
|
||||
project: bigint("project", { mode: "number" }), // FK follows when projects.ts exists
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
export * from "./accounts"
|
||||
export * from "./auth_profiles"
|
||||
export * from "./auth_profile_branches"
|
||||
export * from "./auth_profile_teams"
|
||||
export * from "./auth_role_permisssions"
|
||||
export * from "./auth_roles"
|
||||
export * from "./auth_tenant_users"
|
||||
@@ -69,6 +70,7 @@ export * from "./staff_time_entry_connects"
|
||||
export * from "./staff_zeitstromtimestamps"
|
||||
export * from "./statementallocations"
|
||||
export * from "./tasks"
|
||||
export * from "./teams"
|
||||
export * from "./taxtypes"
|
||||
export * from "./tenants"
|
||||
export * from "./texttemplates"
|
||||
|
||||
40
backend/db/schema/teams.ts
Normal file
40
backend/db/schema/teams.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import {
|
||||
pgTable,
|
||||
bigint,
|
||||
timestamp,
|
||||
text,
|
||||
boolean,
|
||||
uuid,
|
||||
} from "drizzle-orm/pg-core"
|
||||
|
||||
import { tenants } from "./tenants"
|
||||
import { authUsers } from "./auth_users"
|
||||
import { branches } from "./branches"
|
||||
|
||||
export const teams = pgTable("teams", {
|
||||
id: bigint("id", { mode: "number" })
|
||||
.primaryKey()
|
||||
.generatedByDefaultAsIdentity(),
|
||||
|
||||
createdAt: timestamp("created_at", { withTimezone: true })
|
||||
.notNull()
|
||||
.defaultNow(),
|
||||
|
||||
tenant: bigint("tenant", { mode: "number" })
|
||||
.notNull()
|
||||
.references(() => tenants.id),
|
||||
|
||||
name: text("name").notNull(),
|
||||
description: text("description"),
|
||||
|
||||
branch: bigint("branch", { mode: "number" })
|
||||
.references(() => branches.id),
|
||||
|
||||
archived: boolean("archived").notNull().default(false),
|
||||
|
||||
updatedAt: timestamp("updated_at", { withTimezone: true }),
|
||||
updatedBy: uuid("updated_by").references(() => authUsers.id),
|
||||
})
|
||||
|
||||
export type Team = typeof teams.$inferSelect
|
||||
export type NewTeam = typeof teams.$inferInsert
|
||||
@@ -93,6 +93,7 @@ export const tenants = pgTable(
|
||||
incomingInvoices: true,
|
||||
costcentres: true,
|
||||
branches: true,
|
||||
teams: true,
|
||||
accounts: true,
|
||||
ownaccounts: true,
|
||||
banking: true,
|
||||
|
||||
@@ -9,6 +9,7 @@ import {
|
||||
|
||||
import {and, eq, inArray} from "drizzle-orm"
|
||||
import { enrichProfilesWithBranches } from "../../utils/profileBranches"
|
||||
import { enrichProfilesWithTeams } from "../../utils/profileTeams"
|
||||
|
||||
|
||||
export default async function tenantRoutesInternal(server: FastifyInstance) {
|
||||
@@ -62,7 +63,8 @@ export default async function tenantRoutesInternal(server: FastifyInstance) {
|
||||
eq(authProfiles.tenant_id, tenantId),
|
||||
inArray(authProfiles.user_id, userIds)
|
||||
))
|
||||
const profiles = await enrichProfilesWithBranches(server, profileRows)
|
||||
const profilesWithBranches = await enrichProfilesWithBranches(server, profileRows)
|
||||
const profiles = await enrichProfilesWithTeams(server, profilesWithBranches)
|
||||
|
||||
const combined = users.map(u => {
|
||||
const profile = profiles.find(p => p.user_id === u.id)
|
||||
@@ -98,7 +100,8 @@ export default async function tenantRoutesInternal(server: FastifyInstance) {
|
||||
.from(authProfiles)
|
||||
.where(eq(authProfiles.tenant_id, tenantId))
|
||||
|
||||
return await enrichProfilesWithBranches(server, profileRows)
|
||||
const profilesWithBranches = await enrichProfilesWithBranches(server, profileRows)
|
||||
return await enrichProfilesWithTeams(server, profilesWithBranches)
|
||||
|
||||
} catch (err) {
|
||||
console.error("/tenant/profiles ERROR:", err)
|
||||
|
||||
@@ -9,6 +9,11 @@ import {
|
||||
resolveTenantBranchIds,
|
||||
syncProfileBranches,
|
||||
} from "../utils/profileBranches";
|
||||
import {
|
||||
enrichProfilesWithTeams,
|
||||
resolveTenantTeamIds,
|
||||
syncProfileTeams,
|
||||
} from "../utils/profileTeams";
|
||||
|
||||
export default async function authProfilesRoutes(server: FastifyInstance) {
|
||||
|
||||
@@ -24,7 +29,10 @@ export default async function authProfilesRoutes(server: FastifyInstance) {
|
||||
return reply.code(400).send({ error: "No tenant selected" });
|
||||
}
|
||||
|
||||
const profile = await loadProfileWithBranches(server, id, tenantId)
|
||||
const profileWithBranches = await loadProfileWithBranches(server, id, tenantId)
|
||||
const [profile] = profileWithBranches
|
||||
? await enrichProfilesWithTeams(server, [profileWithBranches])
|
||||
: [null]
|
||||
|
||||
if (!profile) {
|
||||
return reply.code(404).send({ error: "User not found or not in tenant" });
|
||||
@@ -45,7 +53,7 @@ export default async function authProfilesRoutes(server: FastifyInstance) {
|
||||
const forbidden = [
|
||||
"id", "user_id", "tenant_id", "created_at", "updated_at",
|
||||
"updatedAt", "updatedBy", "old_profile_id", "full_name",
|
||||
"branch", "branches", "branch_ids"
|
||||
"branch"
|
||||
]
|
||||
forbidden.forEach(f => delete cleaned[f])
|
||||
|
||||
@@ -95,6 +103,19 @@ export default async function authProfilesRoutes(server: FastifyInstance) {
|
||||
],
|
||||
body.branch_id ?? body.branch?.id ?? null
|
||||
)
|
||||
const teamIds = await resolveTenantTeamIds(
|
||||
server,
|
||||
tenantId,
|
||||
[
|
||||
...(Array.isArray(body.team_ids) ? body.team_ids : []),
|
||||
...(Array.isArray(body.teams) ? body.teams : []),
|
||||
],
|
||||
)
|
||||
|
||||
delete body.branch_ids
|
||||
delete body.branches
|
||||
delete body.team_ids
|
||||
delete body.teams
|
||||
|
||||
const updateData = {
|
||||
...body,
|
||||
@@ -119,8 +140,12 @@ export default async function authProfilesRoutes(server: FastifyInstance) {
|
||||
}
|
||||
|
||||
await syncProfileBranches(server, id, branchIds, userId)
|
||||
await syncProfileTeams(server, id, teamIds, userId)
|
||||
|
||||
const profile = await loadProfileWithBranches(server, id, tenantId)
|
||||
const profileWithBranches = await loadProfileWithBranches(server, id, tenantId)
|
||||
const [profile] = profileWithBranches
|
||||
? await enrichProfilesWithTeams(server, [profileWithBranches])
|
||||
: [null]
|
||||
return profile || updated[0]
|
||||
|
||||
} catch (err) {
|
||||
@@ -128,6 +153,9 @@ export default async function authProfilesRoutes(server: FastifyInstance) {
|
||||
if (err instanceof Error && ["INVALID_BRANCH_SELECTION", "INVALID_PRIMARY_BRANCH"].includes(err.message)) {
|
||||
return reply.code(400).send({ error: "Ungültige Niederlassungsauswahl" })
|
||||
}
|
||||
if (err instanceof Error && err.message === "INVALID_TEAM_SELECTION") {
|
||||
return reply.code(400).send({ error: "Ungültige Teamauswahl" })
|
||||
}
|
||||
return reply.code(500).send({ error: "Internal Server Error" })
|
||||
}
|
||||
})
|
||||
|
||||
@@ -13,6 +13,7 @@ import {
|
||||
|
||||
import {and, desc, eq, inArray} from "drizzle-orm"
|
||||
import { enrichProfilesWithBranches } from "../utils/profileBranches"
|
||||
import { enrichProfilesWithTeams } from "../utils/profileTeams"
|
||||
|
||||
|
||||
export default async function tenantRoutes(server: FastifyInstance) {
|
||||
@@ -132,7 +133,8 @@ export default async function tenantRoutes(server: FastifyInstance) {
|
||||
eq(authProfiles.tenant_id, tenantId),
|
||||
inArray(authProfiles.user_id, userIds)
|
||||
))
|
||||
const profiles = await enrichProfilesWithBranches(server, profileRows)
|
||||
const profilesWithBranches = await enrichProfilesWithBranches(server, profileRows)
|
||||
const profiles = await enrichProfilesWithTeams(server, profilesWithBranches)
|
||||
|
||||
const combined = users.map(u => {
|
||||
const profile = profiles.find(p => p.user_id === u.id)
|
||||
@@ -167,7 +169,8 @@ export default async function tenantRoutes(server: FastifyInstance) {
|
||||
.from(authProfiles)
|
||||
.where(eq(authProfiles.tenant_id, tenantId))
|
||||
|
||||
const data = await enrichProfilesWithBranches(server, profileRows)
|
||||
const profilesWithBranches = await enrichProfilesWithBranches(server, profileRows)
|
||||
const data = await enrichProfilesWithTeams(server, profilesWithBranches)
|
||||
return { data }
|
||||
|
||||
} catch (err) {
|
||||
|
||||
@@ -32,6 +32,7 @@ const HISTORY_ENTITY_LABELS: Record<string, string> = {
|
||||
incominginvoices: "Eingangsrechnungen",
|
||||
files: "Dateien",
|
||||
memberrelations: "Mitgliedsverhältnisse",
|
||||
teams: "Teams",
|
||||
}
|
||||
|
||||
export function getHistoryEntityLabel(entity: string) {
|
||||
|
||||
117
backend/src/utils/profileTeams.ts
Normal file
117
backend/src/utils/profileTeams.ts
Normal file
@@ -0,0 +1,117 @@
|
||||
import { and, eq, inArray } from "drizzle-orm"
|
||||
import { FastifyInstance } from "fastify"
|
||||
|
||||
import { authProfileTeams, branches, teams } from "../../db/schema"
|
||||
|
||||
function normalizeTeamIds(values: any[]): number[] {
|
||||
return [...new Set(
|
||||
values
|
||||
.map((value) => {
|
||||
if (typeof value === "number") return value
|
||||
if (typeof value === "string" && value.trim()) return Number(value)
|
||||
if (value && typeof value === "object" && "id" in value) return Number(value.id)
|
||||
return NaN
|
||||
})
|
||||
.filter((value) => Number.isFinite(value))
|
||||
)]
|
||||
}
|
||||
|
||||
export async function enrichProfilesWithTeams(server: FastifyInstance, profiles: any[]) {
|
||||
if (!profiles.length) return profiles
|
||||
|
||||
const profileIds = profiles.map((profile) => profile.id).filter(Boolean)
|
||||
if (!profileIds.length) return profiles
|
||||
|
||||
const profileTeamRows = await server.db
|
||||
.select()
|
||||
.from(authProfileTeams)
|
||||
.where(inArray(authProfileTeams.profile_id, profileIds))
|
||||
|
||||
const teamIds = [...new Set(profileTeamRows.map((row) => row.team_id).filter(Boolean))]
|
||||
const teamRows = teamIds.length
|
||||
? await server.db.select().from(teams).where(inArray(teams.id, teamIds))
|
||||
: []
|
||||
|
||||
const branchIds = [...new Set(teamRows.map((team) => team.branch).filter(Boolean))]
|
||||
const branchRows = branchIds.length
|
||||
? await server.db.select().from(branches).where(inArray(branches.id, branchIds))
|
||||
: []
|
||||
|
||||
const branchMap = new Map(branchRows.map((branch) => [branch.id, branch]))
|
||||
const teamMap = new Map(teamRows.map((team) => [
|
||||
team.id,
|
||||
{
|
||||
...team,
|
||||
branch: team.branch ? branchMap.get(team.branch) || null : null,
|
||||
},
|
||||
]))
|
||||
const teamIdsByProfile = new Map<string, number[]>()
|
||||
|
||||
for (const row of profileTeamRows) {
|
||||
const current = teamIdsByProfile.get(row.profile_id) || []
|
||||
current.push(row.team_id)
|
||||
teamIdsByProfile.set(row.profile_id, current)
|
||||
}
|
||||
|
||||
return profiles.map((profile) => {
|
||||
const assignedTeamIds = [...new Set(teamIdsByProfile.get(profile.id) || [])]
|
||||
return {
|
||||
...profile,
|
||||
teams: assignedTeamIds
|
||||
.map((teamId) => teamMap.get(teamId))
|
||||
.filter(Boolean),
|
||||
team_ids: assignedTeamIds,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export async function resolveTenantTeamIds(
|
||||
server: FastifyInstance,
|
||||
tenantId: number,
|
||||
values: any[],
|
||||
) {
|
||||
const requestedTeamIds = normalizeTeamIds(values)
|
||||
|
||||
if (!requestedTeamIds.length) {
|
||||
return []
|
||||
}
|
||||
|
||||
const validTeams = await server.db
|
||||
.select({ id: teams.id })
|
||||
.from(teams)
|
||||
.where(
|
||||
and(
|
||||
eq(teams.tenant, tenantId),
|
||||
inArray(teams.id, requestedTeamIds)
|
||||
)
|
||||
)
|
||||
|
||||
const validTeamIds = validTeams.map((team) => team.id)
|
||||
|
||||
if (validTeamIds.length !== requestedTeamIds.length) {
|
||||
throw new Error("INVALID_TEAM_SELECTION")
|
||||
}
|
||||
|
||||
return validTeamIds
|
||||
}
|
||||
|
||||
export async function syncProfileTeams(
|
||||
server: FastifyInstance,
|
||||
profileId: string,
|
||||
teamIds: number[],
|
||||
userId?: string | null
|
||||
) {
|
||||
await server.db
|
||||
.delete(authProfileTeams)
|
||||
.where(eq(authProfileTeams.profile_id, profileId))
|
||||
|
||||
if (!teamIds.length) return
|
||||
|
||||
await server.db
|
||||
.insert(authProfileTeams)
|
||||
.values(teamIds.map((teamId) => ({
|
||||
profile_id: profileId,
|
||||
team_id: teamId,
|
||||
created_by: userId || null,
|
||||
})))
|
||||
}
|
||||
@@ -36,6 +36,7 @@ import {
|
||||
spaces,
|
||||
statementallocations,
|
||||
tasks,
|
||||
teams,
|
||||
texttemplates,
|
||||
units,
|
||||
vehicles,
|
||||
@@ -171,6 +172,11 @@ export const resourceConfig = {
|
||||
searchColumns: ["name","number","description"],
|
||||
numberRangeHolder: "number",
|
||||
},
|
||||
teams: {
|
||||
table: teams,
|
||||
searchColumns: ["name", "description"],
|
||||
mtoLoad: ["branch"],
|
||||
},
|
||||
tasks: {
|
||||
table: tasks,
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user