Kunden-Dashboard: - Tab-Navigation: Projekte | Was wurde gemacht | Rechnungen | Chat - Projekt-Details pro Projekt: letzte Git-Commits (Titel+Beschreibung), Projektgroesse, Datei-Uebersicht (Top-Level aggregiert), Ticket-Arbeiten - Rechnungen: gestylte Liste mit Status-Pillen, Ansehen/Zahlen-Link, PDF-Download ueber Server-Proxy (IDOR-geschuetzt) - Chat mit dem Webklar-Team (Polling, Ungelesen-Badge, viewAs blockiert) Admin-Dashboard: - Chat-Tab: Konversationsliste + Thread + Antwortfeld, Ungelesen-Badge Backend: - giteaAdmin: getRepoInfo, listRepoCommits, getRepoTreeSummary - projectInsights: 5-min-Cache, Invalidierung per Gitea-Webhook - /api/projects/:id/git|files|work mit Ownership-Check (404) - /api/chat/* (Kunde) + /api/portal-admin/chats/* (Admin), portalMessages-Collection - mailer: E-Mail-Benachrichtigung an Admins bei Kundennachricht (15-min-Throttle) - gitWorksheet: dd.mm.yyyy, voller Commit-Body, startTime leer (Zeit-Nachtrag), Auto-Webpage-Ticket bei Push auf Projekt ohne Ticket (ensureProjectTicket) - customerActivity: Git-Push-Eintraege fuer Kunden sichtbar Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
198 lines
6.0 KiB
JavaScript
198 lines
6.0 KiB
JavaScript
import { config } from '../config.js'
|
|
|
|
function getPrimaryContact(client) {
|
|
const contacts = client?.contacts || []
|
|
return contacts.find((c) => c.is_primary) || contacts[0] || {}
|
|
}
|
|
|
|
export function sanitizeInvoiceClient(client) {
|
|
const contact = getPrimaryContact(client)
|
|
const baseUrl = config.invoiceNinja.url
|
|
|
|
return {
|
|
id: client.id,
|
|
number: client.number || '',
|
|
displayName: client.display_name || client.name || '',
|
|
companyName: client.name || '',
|
|
website: client.website || '',
|
|
phone: contact.phone || client.phone || '',
|
|
email: contact.email || '',
|
|
contactFirstName: contact.first_name || '',
|
|
contactLastName: contact.last_name || '',
|
|
address1: client.address1 || '',
|
|
address2: client.address2 || '',
|
|
city: client.city || '',
|
|
state: client.state || '',
|
|
postalCode: client.postal_code || '',
|
|
countryId: client.country_id || '',
|
|
vatNumber: client.vat_number || '',
|
|
idNumber: client.id_number || '',
|
|
balance: Number(client.balance ?? 0),
|
|
paidToDate: Number(client.paid_to_date ?? 0),
|
|
publicNotes: client.public_notes || '',
|
|
privateNotes: client.private_notes || '',
|
|
editUrl: `${baseUrl}/clients/${client.id}/edit`,
|
|
}
|
|
}
|
|
|
|
const INVOICE_STATUS_LABELS = {
|
|
1: 'Entwurf',
|
|
2: 'Offen',
|
|
3: 'Teilzahlung',
|
|
4: 'Bezahlt',
|
|
5: 'Storniert',
|
|
6: 'Storniert',
|
|
}
|
|
|
|
export function sanitizeInvoice(inv) {
|
|
const today = new Date().toISOString().slice(0, 10)
|
|
const balance = Number(inv.balance ?? 0)
|
|
const statusId = Number(inv.status_id ?? 0)
|
|
const overdue = balance > 0 && Boolean(inv.due_date) && inv.due_date < today && statusId !== 5
|
|
const invitationKey = inv.invitations?.[0]?.key || ''
|
|
|
|
return {
|
|
id: inv.id,
|
|
number: inv.number || '',
|
|
date: inv.date || '',
|
|
dueDate: inv.due_date || '',
|
|
amount: Number(inv.amount ?? 0),
|
|
balance,
|
|
statusId,
|
|
status: overdue ? 'Überfällig' : INVOICE_STATUS_LABELS[statusId] || 'Offen',
|
|
overdue,
|
|
poNumber: inv.po_number || '',
|
|
publicNotes: inv.public_notes || '',
|
|
// Link ins InvoiceNinja-Kundenportal (Rechnung ansehen/zahlen)
|
|
portalUrl: invitationKey ? `${config.invoiceNinja.url}/client/invoice/${invitationKey}` : '',
|
|
}
|
|
}
|
|
|
|
/** Rohe Rechnung inkl. Invitations (fuer PDF-Download + Ownership-Pruefung). */
|
|
export async function getInvoiceRaw(invoiceId) {
|
|
const token = config.invoiceNinja.apiToken
|
|
if (!token) {
|
|
const error = new Error('INVOICE_NINJA_API_TOKEN ist nicht konfiguriert')
|
|
error.status = 503
|
|
throw error
|
|
}
|
|
const response = await fetch(
|
|
`${config.invoiceNinja.url}/api/v1/invoices/${encodeURIComponent(invoiceId)}?include=invitations`,
|
|
{
|
|
headers: {
|
|
'X-API-TOKEN': token,
|
|
'X-Requested-With': 'XMLHttpRequest',
|
|
Accept: 'application/json',
|
|
},
|
|
}
|
|
)
|
|
const data = await response.json().catch(() => ({}))
|
|
if (!response.ok) {
|
|
const error = new Error(data.message || 'Rechnung nicht gefunden')
|
|
error.status = response.status === 401 ? 502 : response.status
|
|
throw error
|
|
}
|
|
return data.data
|
|
}
|
|
|
|
/** PDF einer Rechnung ueber den Client-Portal-Invitation-Link streamen. */
|
|
export async function fetchInvoicePdf(invitationKey) {
|
|
const token = config.invoiceNinja.apiToken
|
|
const response = await fetch(
|
|
`${config.invoiceNinja.url}/client/invoice/${encodeURIComponent(invitationKey)}/download_pdf`,
|
|
{
|
|
headers: {
|
|
'X-Api-Token': token,
|
|
'X-Requested-With': 'XMLHttpRequest',
|
|
},
|
|
}
|
|
)
|
|
if (!response.ok) {
|
|
const error = new Error('PDF-Download fehlgeschlagen')
|
|
error.status = response.status
|
|
throw error
|
|
}
|
|
return Buffer.from(await response.arrayBuffer())
|
|
}
|
|
|
|
/** Rechnungen eines InvoiceNinja-Clients (fuer Kundenportal + Admin-Ansicht). */
|
|
export async function listInvoicesForClient(clientId, { includeDrafts = false } = {}) {
|
|
const token = config.invoiceNinja.apiToken
|
|
if (!token) {
|
|
const error = new Error('INVOICE_NINJA_API_TOKEN ist nicht konfiguriert')
|
|
error.status = 503
|
|
throw error
|
|
}
|
|
if (!clientId) return []
|
|
|
|
const url = new URL(`${config.invoiceNinja.url}/api/v1/invoices`)
|
|
url.searchParams.set('client_id', String(clientId))
|
|
url.searchParams.set('per_page', '100')
|
|
url.searchParams.set('sort', 'date|desc')
|
|
|
|
const response = await fetch(url, {
|
|
headers: {
|
|
'X-API-TOKEN': token,
|
|
'X-Requested-With': 'XMLHttpRequest',
|
|
Accept: 'application/json',
|
|
},
|
|
})
|
|
|
|
const data = await response.json().catch(() => ({}))
|
|
if (!response.ok) {
|
|
const error = new Error(data.message || 'Invoice Ninja API Fehler')
|
|
error.status = response.status === 401 ? 502 : response.status
|
|
throw error
|
|
}
|
|
|
|
return (data.data || [])
|
|
.filter((inv) => !inv.is_deleted)
|
|
.map(sanitizeInvoice)
|
|
.filter((inv) => includeDrafts || inv.statusId !== 1)
|
|
}
|
|
|
|
export async function listInvoiceNinjaClients() {
|
|
const token = config.invoiceNinja.apiToken
|
|
if (!token) {
|
|
const error = new Error('INVOICE_NINJA_API_TOKEN ist nicht konfiguriert')
|
|
error.status = 503
|
|
throw error
|
|
}
|
|
|
|
const clients = []
|
|
let page = 1
|
|
let totalPages = 1
|
|
|
|
while (page <= totalPages) {
|
|
const url = new URL(`${config.invoiceNinja.url}/api/v1/clients`)
|
|
url.searchParams.set('per_page', '100')
|
|
url.searchParams.set('page', String(page))
|
|
url.searchParams.set('include', 'contacts')
|
|
|
|
const response = await fetch(url, {
|
|
headers: {
|
|
'X-API-TOKEN': token,
|
|
'X-Requested-With': 'XMLHttpRequest',
|
|
Accept: 'application/json',
|
|
},
|
|
})
|
|
|
|
const data = await response.json().catch(() => ({}))
|
|
if (!response.ok) {
|
|
const error = new Error(data.message || 'Invoice Ninja API Fehler')
|
|
error.status = response.status === 401 ? 502 : response.status
|
|
throw error
|
|
}
|
|
|
|
const batch = (data.data || []).filter((c) => !c.is_deleted)
|
|
clients.push(...batch.map(sanitizeInvoiceClient))
|
|
|
|
totalPages = Number(data.meta?.pagination?.total_pages || 1)
|
|
page += 1
|
|
}
|
|
|
|
return clients.sort((a, b) =>
|
|
(a.displayName || a.companyName).localeCompare(b.displayName || b.companyName, 'de')
|
|
)
|
|
}
|