71 lines
2.2 KiB
JavaScript
71 lines
2.2 KiB
JavaScript
import { config } from '../config.js'
|
|
import { listDocuments, Query } from './appwriteAdmin.js'
|
|
import { isPreviewProjectReady } from './previewReadiness.js'
|
|
import { createStaffPreviewToken } from './staffPreviewTokens.js'
|
|
|
|
function safePreviewUrl(url) {
|
|
try {
|
|
const parsed = new URL(String(url || ''))
|
|
if (
|
|
parsed.hostname.endsWith('.project.webklar.com') ||
|
|
parsed.hostname === 'project.webklar.com'
|
|
) {
|
|
return parsed.toString()
|
|
}
|
|
} catch {
|
|
/* ignore */
|
|
}
|
|
return null
|
|
}
|
|
|
|
function previewUrlForSubdomain(subdomain) {
|
|
const safe = String(subdomain || '')
|
|
.trim()
|
|
.toLowerCase()
|
|
.replace(/[^a-z0-9-]/g, '')
|
|
if (!safe) return null
|
|
return `https://${safe}.${config.preview.baseHost}`
|
|
}
|
|
|
|
/**
|
|
* Einmal-Link fuer die Preview eines Projekts (eingeloest von GET /api/auth/staff-preview), genutzt vom
|
|
* Team-Eingang (/api/team/preview-link, alle Mitarbeiter). Liefert { status, body }.
|
|
*/
|
|
export async function buildStaffPreviewLink({ userId, email, name, subdomain: rawSubdomain, previewUrl: rawPreviewUrl }) {
|
|
const subdomain = String(rawSubdomain || '')
|
|
.trim()
|
|
.toLowerCase()
|
|
const previewUrl = safePreviewUrl(rawPreviewUrl) || previewUrlForSubdomain(subdomain)
|
|
|
|
if (!subdomain || !previewUrl) {
|
|
return { status: 400, body: { error: 'subdomain oder gueltige previewUrl erforderlich' } }
|
|
}
|
|
|
|
try {
|
|
const projects = await listDocuments(config.collections.websiteProjects, [
|
|
Query.equal('subdomain', subdomain),
|
|
Query.limit(1),
|
|
])
|
|
const project = projects[0]
|
|
if (!project) {
|
|
return { status: 404, body: { error: 'Projekt nicht gefunden' } }
|
|
}
|
|
if (!isPreviewProjectReady(project)) {
|
|
return { status: 409, body: { error: 'Projekt-Vorschau ist noch nicht bereit' } }
|
|
}
|
|
|
|
const token = createStaffPreviewToken({
|
|
userId,
|
|
email: email || '',
|
|
name: name || email || 'Admin',
|
|
previewUrl,
|
|
customerId: project.customerId || '',
|
|
})
|
|
|
|
const portalBase = `https://${config.preview.baseHost}`
|
|
return { status: 200, body: { openUrl: `${portalBase}/api/auth/staff-preview?token=${encodeURIComponent(token)}` } }
|
|
} catch (err) {
|
|
return { status: err.status || 500, body: { error: err.message || 'Preview-Login fehlgeschlagen' } }
|
|
}
|
|
}
|