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>
269 lines
7.1 KiB
JavaScript
269 lines
7.1 KiB
JavaScript
import { randomUUID } from 'node:crypto'
|
|
import { config, WOMS_DATABASE_ID } from '../config.js'
|
|
|
|
function buildQueries(queries = []) {
|
|
return queries.map((q) => {
|
|
if (typeof q === 'string') return q
|
|
return JSON.stringify(q)
|
|
})
|
|
}
|
|
|
|
function adminHeaders() {
|
|
return {
|
|
'Content-Type': 'application/json',
|
|
'X-Appwrite-Project': config.appwrite.projectId,
|
|
'X-Appwrite-Key': config.appwrite.apiKey,
|
|
}
|
|
}
|
|
|
|
function formatRequestBody(body, method, { raw = false } = {}) {
|
|
if (!body || method === 'GET' || method === 'DELETE') return body
|
|
if (raw || body.data !== undefined) return body
|
|
const { documentId, ...fields } = body
|
|
const payload = { data: fields }
|
|
if (documentId) payload.documentId = documentId
|
|
return payload
|
|
}
|
|
|
|
async function adminFetch(path, { method = 'GET', body, queries = [] } = {}) {
|
|
if (!config.appwrite.apiKey) {
|
|
const error = new Error('APPWRITE_API_KEY fehlt in .env')
|
|
error.status = 500
|
|
throw error
|
|
}
|
|
|
|
const url = new URL(`${config.appwrite.endpoint}${path}`)
|
|
for (const q of buildQueries(queries)) {
|
|
url.searchParams.append('queries[]', q)
|
|
}
|
|
|
|
const useRawBody = path.startsWith('/users')
|
|
const requestBody = formatRequestBody(body, method, { raw: useRawBody })
|
|
|
|
const response = await fetch(url.toString(), {
|
|
method,
|
|
headers: adminHeaders(),
|
|
body: requestBody ? JSON.stringify(requestBody) : undefined,
|
|
})
|
|
|
|
const text = await response.text()
|
|
let data = null
|
|
if (text) {
|
|
try {
|
|
data = JSON.parse(text)
|
|
} catch {
|
|
data = { message: text }
|
|
}
|
|
}
|
|
|
|
if (!response.ok) {
|
|
const error = new Error(data?.message || `Appwrite ${response.status}`)
|
|
error.status = response.status >= 500 ? 500 : response.status
|
|
error.code = data?.code
|
|
error.type = data?.type
|
|
throw error
|
|
}
|
|
|
|
return data
|
|
}
|
|
|
|
/** Appwrite Query-Helper (kompatibel zu bisherigen Aufrufen) */
|
|
export const Query = {
|
|
equal: (attribute, value) => ({
|
|
method: 'equal',
|
|
attribute,
|
|
values: Array.isArray(value) ? value : [value],
|
|
}),
|
|
limit: (n) => ({ method: 'limit', values: [n] }),
|
|
greaterThan: (attribute, value) => ({ method: 'greaterThan', attribute, values: [value] }),
|
|
orderDesc: (attribute) => ({ method: 'orderDesc', attribute }),
|
|
orderAsc: (attribute) => ({ method: 'orderAsc', attribute }),
|
|
}
|
|
|
|
export const ID = {
|
|
unique: () => randomUUID(),
|
|
}
|
|
|
|
function collectionPath(collectionId) {
|
|
return `/databases/${config.appwrite.databaseId}/collections/${collectionId}/documents`
|
|
}
|
|
|
|
export async function getUserById(userId) {
|
|
return adminFetch(`/users/${userId}`)
|
|
}
|
|
|
|
export async function createAppwriteUser({ email, password, name }) {
|
|
return adminFetch('/users', {
|
|
method: 'POST',
|
|
body: {
|
|
userId: ID.unique(),
|
|
email: email.trim(),
|
|
password,
|
|
name: name?.trim() || email.trim(),
|
|
},
|
|
})
|
|
}
|
|
|
|
export async function updateAppwriteUserEmail(userId, email) {
|
|
return adminFetch(`/users/${userId}/email`, {
|
|
method: 'PATCH',
|
|
body: { email: email.trim() },
|
|
})
|
|
}
|
|
|
|
export async function updateAppwriteUserPassword(userId, password) {
|
|
return adminFetch(`/users/${userId}/password`, {
|
|
method: 'PATCH',
|
|
body: { password },
|
|
})
|
|
}
|
|
|
|
export async function updateAppwriteUserName(userId, name) {
|
|
return adminFetch(`/users/${userId}/name`, {
|
|
method: 'PATCH',
|
|
body: { name: name.trim() },
|
|
})
|
|
}
|
|
|
|
export async function updateAppwriteUserLabels(userId, labels) {
|
|
return adminFetch(`/users/${userId}/labels`, {
|
|
method: 'PUT',
|
|
body: { labels },
|
|
})
|
|
}
|
|
|
|
export async function deleteAppwriteUser(userId) {
|
|
return adminFetch(`/users/${userId}`, { method: 'DELETE' })
|
|
}
|
|
|
|
export async function deleteUserSession(userId, sessionId) {
|
|
return adminFetch(`/users/${userId}/sessions/${sessionId}`, { method: 'DELETE' })
|
|
}
|
|
|
|
export async function listDocuments(collectionId, queries = []) {
|
|
const result = await adminFetch(collectionPath(collectionId), { queries })
|
|
return result.documents
|
|
}
|
|
|
|
export async function getCustomerByAppwriteUserId(appwriteUserId) {
|
|
const docs = await listDocuments(config.collections.customers, [
|
|
Query.equal('appwriteUserId', appwriteUserId),
|
|
Query.limit(1),
|
|
])
|
|
return docs[0] || null
|
|
}
|
|
|
|
export async function getCustomerByEmail(email) {
|
|
if (!email) return null
|
|
const docs = await listDocuments(config.collections.customers, [
|
|
Query.equal('email', email.trim()),
|
|
Query.limit(1),
|
|
])
|
|
return docs[0] || null
|
|
}
|
|
|
|
export async function getPortalAccessByCustomerId(customerId) {
|
|
const docs = await listDocuments(config.collections.customerPortalAccess, [
|
|
Query.equal('customerId', customerId),
|
|
Query.limit(1),
|
|
])
|
|
return docs[0] || null
|
|
}
|
|
|
|
export async function updateDocument(collectionId, documentId, data) {
|
|
return adminFetch(`${collectionPath(collectionId)}/${documentId}`, {
|
|
method: 'PATCH',
|
|
body: data,
|
|
})
|
|
}
|
|
|
|
export async function getDocument(collectionId, documentId) {
|
|
return adminFetch(`${collectionPath(collectionId)}/${documentId}`)
|
|
}
|
|
|
|
export async function createDocument(collectionId, data, documentId = ID.unique()) {
|
|
return adminFetch(collectionPath(collectionId), {
|
|
method: 'POST',
|
|
body: { ...data, documentId },
|
|
})
|
|
}
|
|
|
|
export async function deleteDocument(collectionId, documentId) {
|
|
return adminFetch(`${collectionPath(collectionId)}/${documentId}`, {
|
|
method: 'DELETE',
|
|
})
|
|
}
|
|
|
|
const PRESERVE_IF_EMPTY = new Set(['customerId', 'ticketId'])
|
|
|
|
function buildProjectPayload(existing, data) {
|
|
const now = new Date().toISOString()
|
|
const payload = { updatedAt: now }
|
|
|
|
for (const [key, value] of Object.entries(data)) {
|
|
if (value === undefined) continue
|
|
if (PRESERVE_IF_EMPTY.has(key) && (value === '' || value === null)) {
|
|
if (existing?.[key]) continue
|
|
}
|
|
payload[key] = value
|
|
}
|
|
|
|
return payload
|
|
}
|
|
|
|
export async function upsertWebsiteProjectByRepo(repoFullName, data) {
|
|
const existing = await listDocuments(config.collections.websiteProjects, [
|
|
Query.equal('repoFullName', repoFullName),
|
|
Query.limit(1),
|
|
])
|
|
|
|
const payload = buildProjectPayload(existing[0], data)
|
|
|
|
if (existing[0]) {
|
|
return updateDocument(
|
|
config.collections.websiteProjects,
|
|
existing[0].$id,
|
|
payload
|
|
)
|
|
}
|
|
|
|
const now = new Date().toISOString()
|
|
return adminFetch(collectionPath(config.collections.websiteProjects), {
|
|
method: 'POST',
|
|
body: {
|
|
...payload,
|
|
customerId: payload.customerId ?? '',
|
|
ticketId: payload.ticketId ?? '',
|
|
projectName: payload.projectName || repoFullName.split('/').pop() || repoFullName,
|
|
createdAt: now,
|
|
documentId: ID.unique(),
|
|
},
|
|
})
|
|
}
|
|
|
|
export async function verifyDatabaseAccess() {
|
|
if (!config.appwrite.apiKey) {
|
|
return { ok: false, reason: 'APPWRITE_API_KEY fehlt' }
|
|
}
|
|
try {
|
|
await listDocuments(config.collections.customers, [Query.limit(1)])
|
|
return { ok: true }
|
|
} catch (err) {
|
|
return {
|
|
ok: false,
|
|
reason: err.message,
|
|
code: err.code,
|
|
type: err.type,
|
|
status: err.status,
|
|
}
|
|
}
|
|
}
|
|
|
|
export function createAdminClient() {
|
|
return { usesNativeFetch: true, databaseId: WOMS_DATABASE_ID }
|
|
}
|
|
|
|
export function createUserClient() {
|
|
return { usesNativeFetch: true }
|
|
}
|