diff --git a/backend/src/routes/history.ts b/backend/src/routes/history.ts index eec50fc..0bcb816 100644 --- a/backend/src/routes/history.ts +++ b/backend/src/routes/history.ts @@ -59,6 +59,44 @@ const parseId = (value: string) => { } export default async function resourceHistoryRoutes(server: FastifyInstance) { + server.get("/history", { + schema: { + tags: ["History"], + summary: "Get all history entries for the active tenant", + }, + }, async (req: any) => { + const data = await server.db + .select() + .from(historyitems) + .where(eq(historyitems.tenant, req.user?.tenant_id)) + .orderBy(asc(historyitems.createdAt)); + + const userIds = Array.from( + new Set(data.map((item) => item.createdBy).filter(Boolean)) + ) as string[]; + + const profiles = userIds.length > 0 + ? await server.db + .select() + .from(authProfiles) + .where(and( + eq(authProfiles.tenant_id, req.user?.tenant_id), + inArray(authProfiles.user_id, userIds) + )) + : []; + + const profileByUserId = new Map( + profiles.map((profile) => [profile.user_id, profile]) + ); + + return data.map((historyitem) => ({ + ...historyitem, + created_at: historyitem.createdAt, + created_by: historyitem.createdBy, + created_by_profile: historyitem.createdBy ? profileByUserId.get(historyitem.createdBy) || null : null, + })); + }); + server.get<{ Params: { resource: string; id: string } }>("/resource/:resource/:id/history", { diff --git a/frontend/components/HistoryDisplay.vue b/frontend/components/HistoryDisplay.vue index a8d5a72..6385174 100644 --- a/frontend/components/HistoryDisplay.vue +++ b/frontend/components/HistoryDisplay.vue @@ -3,11 +3,13 @@ import dayjs from "dayjs" const props = defineProps({ type: { type: String, - required: true + required: false, + default: null }, elementId: { type: String, - required: true + required: false, + default: null }, renderHeadline: { type: Boolean, @@ -25,13 +27,11 @@ const items = ref([]) const platform = ref("default") const setup = async () => { - - if(props.type && props.elementId){ items.value = await useNuxtApp().$api(`/api/resource/${props.type}/${props.elementId}/history`) - } /*else { - - }*/ + } else { + items.value = await useNuxtApp().$api(`/api/history`) + } } setup() @@ -43,6 +43,10 @@ const addHistoryItemData = ref({ }) const addHistoryItem = async () => { + if (!props.type || !props.elementId) { + toast.add({ title: "Im zentralen Logbuch können keine direkten Einträge erstellt werden." }) + return + } const res = await useNuxtApp().$api(`/api/resource/${props.type}/${props.elementId}/history`, { method: "POST", @@ -161,4 +165,4 @@ const renderText = (text) => { \ No newline at end of file + diff --git a/frontend/components/MainNav.vue b/frontend/components/MainNav.vue index 770e8ed..c93d910 100644 --- a/frontend/components/MainNav.vue +++ b/frontend/components/MainNav.vue @@ -47,8 +47,7 @@ const links = computed(() => { id: 'historyitems', label: "Logbuch", to: "/historyitems", - icon: "i-heroicons-book-open", - disabled: true + icon: "i-heroicons-book-open" }, { label: "Organisation",