Compare commits
17
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5fd2d47a64 | ||
|
|
858c43a6aa | ||
|
|
2fbfcf53be | ||
|
|
288628f20e | ||
|
|
1467c8bdf6 | ||
|
|
3bc40e5908 | ||
|
|
1614c19102 | ||
|
|
4902cfe85d | ||
|
|
2f76790135 | ||
|
|
cfa0070eab | ||
|
|
be13d8350b | ||
|
|
7c8f35f0f0 | ||
|
|
f12bb51f3e | ||
|
|
92e0029732 | ||
|
|
d8ff49480d | ||
|
|
6f4a446ae4 | ||
|
|
7da9a362e1 |
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"projectId": "5fe999f9-fbff-4a09-a987-48c4e7540b38"
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
**/node_modules
|
||||
**/.dart_tool
|
||||
**/coverage
|
||||
**/*.log
|
||||
.git
|
||||
.github
|
||||
backend/dist
|
||||
# Flutter platform scaffolding / caches — the build/web bundle IS needed.
|
||||
client/app/android
|
||||
client/app/ios
|
||||
client/app/linux
|
||||
client/app/macos
|
||||
client/app/windows
|
||||
client/app/.dart_tool
|
||||
# Secrets: passed at runtime via env_file / bind mount, never baked in.
|
||||
backend/.env
|
||||
backend/serviceAccount.json
|
||||
+9
-1
@@ -7,7 +7,7 @@ AUTHENTIK_ISSUER_URL="https://sso.konfi-castle.com/application/o/konfi-castle-ap
|
||||
# Name of the Authentik group whose members are Leitungsteam. Mirrored to
|
||||
# User.isLeitungsteam on every login (the access token must carry a `groups`
|
||||
# claim; add the "groups" scope to the Authentik provider).
|
||||
AUTHENTIK_LEITUNGSTEAM_GROUP="Leitungsteam"
|
||||
AUTHENTIK_LEITUNGSTEAM_GROUP="KC-APP-LT"
|
||||
|
||||
# Secret used to sign guest/Konfi session tokens (local accounts only)
|
||||
GUEST_JWT_SECRET="change-me"
|
||||
@@ -31,6 +31,14 @@ SMTP_SECURE="false"
|
||||
SMTP_USER=""
|
||||
SMTP_PASS=""
|
||||
|
||||
# Push: defaults to "log" (no delivery). Set PUSH_PROVIDER=fcm plus
|
||||
# FCM_PROJECT_ID and GOOGLE_APPLICATION_CREDENTIALS (path to a Firebase
|
||||
# service-account JSON with the "Firebase Cloud Messaging API" enabled) to
|
||||
# send real notifications via FCM HTTP v1.
|
||||
PUSH_PROVIDER="log"
|
||||
FCM_PROJECT_ID="konfi-castle-app"
|
||||
GOOGLE_APPLICATION_CREDENTIALS="/absolute/path/to/serviceAccount.json"
|
||||
|
||||
# File storage: defaults to Nextcloud via WebDAV; set STORAGE_PROVIDER=s3 to
|
||||
# use an S3-compatible bucket instead (see S3_* vars below).
|
||||
STORAGE_PROVIDER="webdav"
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
name: CybeDefend Security Scan
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- main
|
||||
- master
|
||||
- 'feat/**'
|
||||
pull_request:
|
||||
branches:
|
||||
- main
|
||||
- master
|
||||
|
||||
jobs:
|
||||
cybedefend_scan:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Run CybeDefend Security Scan
|
||||
env:
|
||||
CYBEDEFEND_PAT: ${{ secrets.CYBEDEFEND_PAT }}
|
||||
CYBEDEFEND_PROJECT_ID: ${{ secrets.CYBEDEFEND_PROJECT_ID }}
|
||||
run: |
|
||||
docker run --rm \
|
||||
-v "${{ gitea.workspace }}":/src -w /src \
|
||||
-e CYBEDEFEND_PAT="$CYBEDEFEND_PAT" \
|
||||
-e CYBEDEFEND_PROJECT_ID="$CYBEDEFEND_PROJECT_ID" \
|
||||
ghcr.io/cybedefend/cybedefend-cli:latest \
|
||||
scan --dir . --region eu --ci --break-on-severity critical
|
||||
|
||||
- name: Fetch detailed SARIF results
|
||||
if: always()
|
||||
env:
|
||||
CYBEDEFEND_PAT: ${{ secrets.CYBEDEFEND_PAT }}
|
||||
CYBEDEFEND_PROJECT_ID: ${{ secrets.CYBEDEFEND_PROJECT_ID }}
|
||||
run: |
|
||||
docker run --rm \
|
||||
-v "${{ gitea.workspace }}":/src -w /src \
|
||||
-e CYBEDEFEND_PAT="$CYBEDEFEND_PAT" \
|
||||
-e CYBEDEFEND_PROJECT_ID="$CYBEDEFEND_PROJECT_ID" \
|
||||
ghcr.io/cybedefend/cybedefend-cli:latest \
|
||||
results --project-id "$CYBEDEFEND_PROJECT_ID" --all --output sarif --filename results.sarif --ci
|
||||
|
||||
- name: Upload scan results as artifact
|
||||
if: always()
|
||||
uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: cybedefend-results
|
||||
path: results.sarif
|
||||
@@ -3,3 +3,7 @@ dist
|
||||
coverage
|
||||
.env
|
||||
*.log
|
||||
|
||||
# Firebase service account (secret)
|
||||
serviceAccount.json
|
||||
*.serviceAccount.json
|
||||
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
# syntax=docker/dockerfile:1
|
||||
#
|
||||
# Server-only image (this repo has no Flutter client). The web client is
|
||||
# built in the KC-APP client repo and its `build/web` output is mounted
|
||||
# into the container at runtime via WEB_CLIENT_DIR (see docker-compose.yml).
|
||||
|
||||
# --- 1. Backend build ---------------------------------------------------------
|
||||
FROM node:20-bookworm-slim AS api-build
|
||||
WORKDIR /src
|
||||
# Prisma detects the OpenSSL version at `generate` time to pick the matching
|
||||
# query engine binary; without OpenSSL present here it silently defaults to
|
||||
# openssl-1.1.x, which then fails to load in the runtime stage (openssl 3.0.x).
|
||||
RUN apt-get update && apt-get install -y --no-install-recommends openssl \
|
||||
&& rm -rf /var/lib/apt/lists/*
|
||||
COPY package.json package-lock.json ./
|
||||
RUN npm ci
|
||||
COPY . .
|
||||
RUN npx prisma generate && npm run build
|
||||
|
||||
# --- 2. Runtime -------------------------------------------------------------
|
||||
FROM node:20-bookworm-slim AS runtime
|
||||
ENV NODE_ENV=production
|
||||
WORKDIR /app
|
||||
# Prisma needs OpenSSL at runtime.
|
||||
RUN apt-get update && apt-get install -y --no-install-recommends openssl \
|
||||
&& rm -rf /var/lib/apt/lists/*
|
||||
RUN groupadd -g 1001 kc-user && useradd -u 1001 -g kc-user -m -d /home/kc-user kc-user
|
||||
COPY --from=api-build --chown=kc-user:kc-user /src/node_modules ./node_modules
|
||||
COPY --from=api-build --chown=kc-user:kc-user /src/dist ./dist
|
||||
COPY --from=api-build --chown=kc-user:kc-user /src/prisma ./prisma
|
||||
RUN chown -R kc-user:kc-user /app
|
||||
USER kc-user
|
||||
# Web client bundle is bind-mounted at runtime, not baked into the image;
|
||||
# app.module reads WEB_CLIENT_DIR. See docker-compose.yml.
|
||||
EXPOSE 3000
|
||||
# Apply pending migrations, then boot.
|
||||
CMD ["sh", "-c", "npx prisma migrate deploy && node dist/main.js"]
|
||||
@@ -1,7 +1,7 @@
|
||||
# KC-App Backend
|
||||
|
||||
NestJS API for the KC-App platform (see repo root README + plan for
|
||||
architecture context).
|
||||
NestJS API for the KC-App platform. Split out of the main KC-APP monorepo
|
||||
(https://git.konfi-castle.com/linus/KC-APP); the Flutter clients live there.
|
||||
|
||||
## Setup
|
||||
|
||||
@@ -83,6 +83,13 @@ client's host - no separate web server is needed.
|
||||
`MailService.sendTeamerInvite()` composes the personal-invite email with a
|
||||
link built from `APP_BASE_URL`. Delivery is best-effort — failures are
|
||||
logged and swallowed, never blocking the invite.
|
||||
- `push/` — global `PushProvider` abstraction; default `log`, `PUSH_PROVIDER=fcm`
|
||||
uses FCM HTTP v1 (service-account JWT → OAuth token, no extra dep;
|
||||
`FCM_PROJECT_ID`, `GOOGLE_APPLICATION_CREDENTIALS`). `DeviceToken` rows
|
||||
(bound to a `User` or `GuestAccount`) via `POST /push/register` +
|
||||
`/unregister`. `PushService.notifyChannel()` resolves a channel's readable
|
||||
audience → their tokens (minus the sender) → send, pruning invalid ones;
|
||||
`ChatService.sendMessage()` fires it best-effort.
|
||||
- `wahl/` — Wahl/Workshop administration (Leitungsteam-only), guest
|
||||
Teilnehmer submission, Force-Zuteilung overrides, and `ZuteilungService`:
|
||||
a faithful port of the WP plugin's `kc_run_zuteilung` (force-assignments →
|
||||
@@ -96,13 +103,28 @@ client's host - no separate web server is needed.
|
||||
(`WEBDAV_*` env vars), switchable to S3-compatible storage with
|
||||
`STORAGE_PROVIDER=s3` (`S3_*` env vars).
|
||||
- `chat/` — Gemeinde-Gruppenchat, 1:1 Direktnachrichten, Leitungsteam-über-
|
||||
greifende Kanäle und Broadcast (Konfis lesen nur). Channel administration
|
||||
and message history are plain REST (`ChatController`); real-time send/
|
||||
receive is a raw `ws` gateway (`ChatGateway`, path `/chat`) since passport
|
||||
guards don't apply to WS upgrades — auth happens once via `?token=` at
|
||||
connect time (`TokenVerificationService` tries Authentik JWKS, then falls
|
||||
back to a guest token). Access rules live in `ChatService` and are shared
|
||||
between the REST and WS entry points.
|
||||
greifende Kanäle, Broadcast (Konfis lesen nur), and free-form `GRUPPE`
|
||||
chats. Channel administration and message history are plain REST
|
||||
(`ChatController`); real-time send/receive is a raw `ws` gateway
|
||||
(`ChatGateway`, path `/chat`) since passport guards don't apply to WS
|
||||
upgrades — auth happens once via `?token=` at connect time
|
||||
(`TokenVerificationService` tries Authentik JWKS, then falls back to a
|
||||
guest token). Access rules live in `ChatService` and are shared between
|
||||
the REST and WS entry points.
|
||||
- `POST /chat/:kcId/gruppen` lets a Leitungsteam member (any KC) or a
|
||||
Gemeinde Verantwortliche/r (their own KC — `RolesGuard`'s kcId scoping)
|
||||
create a `GRUPPE` channel with any mix of team users and Konfis (guests)
|
||||
from that KC as initial participants (`participantUserIds`,
|
||||
`participantGuestIds`); the creator is always included. Unlike
|
||||
`GEMEINDE_GRUPPE`, membership isn't derived from `Gemeinde` — every
|
||||
participant is an explicit `ChatParticipant` row, so a Konfi (who always
|
||||
belongs to exactly one Gemeinde) can be added regardless of which
|
||||
Gemeinde the chat's creator manages.
|
||||
- `POST` / `DELETE /chat/gruppen/:channelId/participants` (body
|
||||
`{ userId }` or `{ guestId }`) add/remove a participant afterwards.
|
||||
Allowed for the channel's creator, any Leitungsteam member, or a
|
||||
Verantwortliche/r of that KC — not the participants themselves, and not
|
||||
guests.
|
||||
- `sync/` — replicates mutations between the local (on-site) and cloud
|
||||
server. `SyncService.capture()` is called by feature services right after
|
||||
a write, appending an entry to the append-only `SyncLogEntry` log tagged
|
||||
@@ -119,11 +141,11 @@ client's host - no separate web server is needed.
|
||||
- `common/` — `Role` enum, `@Roles()` decorator, `RolesGuard` (KC-scoped,
|
||||
Leitungsteam roles are global across all KCs).
|
||||
|
||||
All planned backend phases are implemented. `npm test` runs Jest unit tests
|
||||
(`ZuteilungService`, `TeamAuthService`, `TeamerService`, `OnboardingService`,
|
||||
All planned backend features are implemented (`prisma/migrations/` holds the
|
||||
schema history). `npm test` runs Jest unit tests (`ZuteilungService`,
|
||||
`TeamAuthService`, `TeamerService`, `OnboardingService`,
|
||||
`resolveOrProvisionAuthentikUser` / `toAuthenticatedUser`; Prisma mocked).
|
||||
Remaining work: the Flutter clients (see repo root README), push
|
||||
notifications, and the first real Prisma migration (only `schema.prisma`
|
||||
exists so far). Ops notes: the Authentik provider must emit a `groups` claim
|
||||
for the LT check, and `MAIL_PROVIDER=smtp` + `SMTP_*` must be set for invite
|
||||
emails to actually leave the box.
|
||||
Ops notes to go live: the Authentik provider must emit a `groups` claim for
|
||||
the LT check; `MAIL_PROVIDER=smtp` + `SMTP_*` for invite emails;
|
||||
`PUSH_PROVIDER=fcm` + a Firebase service-account JSON for push; and real
|
||||
Nextcloud/S3 credentials for file storage.
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
// TEMPORARY TEST FILE — intentionally vulnerable code to trigger CybeDefend scan
|
||||
// Safe to delete after the scan demo.
|
||||
|
||||
const AWS_ACCESS_KEY = "AKIAABCDEFGHIJKLMNOP"; // hardcoded secret (should trigger secret scanner)
|
||||
const DB_PASSWORD = "SuperSecret123!"; // hardcoded credential
|
||||
|
||||
const mysql = require('mysql');
|
||||
|
||||
function getUser(db, userId) {
|
||||
// SQL injection: string concatenation of user input directly into query
|
||||
const query = "SELECT * FROM users WHERE id = '" + userId + "'";
|
||||
return db.query(query);
|
||||
}
|
||||
|
||||
function runCommand(userInput) {
|
||||
const { exec } = require('child_process');
|
||||
// command injection: unsanitized user input passed to shell
|
||||
exec("echo " + userInput);
|
||||
}
|
||||
|
||||
module.exports = { getUser, runCommand, AWS_ACCESS_KEY, DB_PASSWORD };
|
||||
@@ -0,0 +1,48 @@
|
||||
services:
|
||||
db:
|
||||
image: postgres:16-alpine
|
||||
environment:
|
||||
POSTGRES_USER: postgres
|
||||
POSTGRES_PASSWORD: postgres
|
||||
POSTGRES_DB: kcapp
|
||||
volumes:
|
||||
- pgdata:/var/lib/postgresql/data
|
||||
ports:
|
||||
- "5433:5432"
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "pg_isready -U postgres -d kcapp"]
|
||||
interval: 5s
|
||||
timeout: 5s
|
||||
retries: 10
|
||||
|
||||
api:
|
||||
build:
|
||||
context: .
|
||||
dockerfile: Dockerfile
|
||||
depends_on:
|
||||
db:
|
||||
condition: service_healthy
|
||||
# All non-DB config comes from .env (needs Docker Compose v2, which
|
||||
# strips surrounding quotes). DATABASE_URL and the FCM credential path
|
||||
# are overridden below for the container.
|
||||
env_file:
|
||||
- .env
|
||||
environment:
|
||||
DATABASE_URL: postgresql://postgres:postgres@db:5432/kcapp?schema=public
|
||||
PORT: "3000"
|
||||
GOOGLE_APPLICATION_CREDENTIALS: /app/serviceAccount.json
|
||||
APP_BASE_URL: http://localhost:3010
|
||||
WEB_CLIENT_DIR: /app/web
|
||||
ports:
|
||||
- "3010:3000"
|
||||
volumes:
|
||||
# Firebase service account — kept out of the image, mounted read-only.
|
||||
- ./serviceAccount.json:/app/serviceAccount.json:ro
|
||||
# Pre-built Flutter web bundle, built separately in the KC-APP client
|
||||
# repo (flutter build web --release) and mounted read-only here.
|
||||
# Set WEB_CLIENT_BUILD_PATH (e.g. in .env) to that build/web directory;
|
||||
# defaults to a sibling ../KC-APP checkout.
|
||||
- ${WEB_CLIENT_BUILD_PATH:-../KC-APP/client/app/build/web}:/app/web:ro
|
||||
|
||||
volumes:
|
||||
pgdata:
|
||||
@@ -0,0 +1,21 @@
|
||||
-- CreateTable
|
||||
CREATE TABLE "DeviceToken" (
|
||||
"id" TEXT NOT NULL,
|
||||
"token" TEXT NOT NULL,
|
||||
"platform" TEXT NOT NULL,
|
||||
"userId" TEXT,
|
||||
"guestAccountId" TEXT,
|
||||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"lastSeenAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
|
||||
CONSTRAINT "DeviceToken_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
-- CreateIndex
|
||||
CREATE UNIQUE INDEX "DeviceToken_token_key" ON "DeviceToken"("token");
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "DeviceToken" ADD CONSTRAINT "DeviceToken_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "DeviceToken" ADD CONSTRAINT "DeviceToken_guestAccountId_fkey" FOREIGN KEY ("guestAccountId") REFERENCES "GuestAccount"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
-- DropIndex
|
||||
DROP INDEX "Teilnehmer_wahlId_guestAccountId_key";
|
||||
|
||||
-- AlterTable
|
||||
ALTER TABLE "Wahl" ADD COLUMN "beschreibung" TEXT,
|
||||
ADD COLUMN "phasenAnzahl" INTEGER NOT NULL DEFAULT 1;
|
||||
|
||||
-- AlterTable
|
||||
ALTER TABLE "Workshop" ADD COLUMN "beschreibung" TEXT,
|
||||
ADD COLUMN "phase" INTEGER NOT NULL DEFAULT 1;
|
||||
|
||||
-- AlterTable
|
||||
ALTER TABLE "Teilnehmer" ADD COLUMN "phase" INTEGER NOT NULL DEFAULT 1;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "VerantwortlicheInvite" (
|
||||
"id" TEXT NOT NULL,
|
||||
"kcId" TEXT NOT NULL,
|
||||
"gemeindeId" TEXT NOT NULL,
|
||||
"token" TEXT NOT NULL,
|
||||
"email" TEXT,
|
||||
"maxUses" INTEGER,
|
||||
"usedCount" INTEGER NOT NULL DEFAULT 0,
|
||||
"expiresAt" TIMESTAMP(3),
|
||||
"revokedAt" TIMESTAMP(3),
|
||||
"createdByUserId" TEXT NOT NULL,
|
||||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
|
||||
CONSTRAINT "VerantwortlicheInvite_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
-- CreateIndex
|
||||
CREATE UNIQUE INDEX "VerantwortlicheInvite_token_key" ON "VerantwortlicheInvite"("token");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE UNIQUE INDEX "Teilnehmer_wahlId_guestAccountId_phase_key" ON "Teilnehmer"("wahlId", "guestAccountId", "phase");
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "VerantwortlicheInvite" ADD CONSTRAINT "VerantwortlicheInvite_kcId_fkey" FOREIGN KEY ("kcId") REFERENCES "Kc"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "VerantwortlicheInvite" ADD CONSTRAINT "VerantwortlicheInvite_gemeindeId_fkey" FOREIGN KEY ("gemeindeId") REFERENCES "Gemeinde"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
-- AlterTable
|
||||
ALTER TABLE "SyncLogEntry" ADD COLUMN "occurredAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "SyncRecordVersion" (
|
||||
"id" TEXT NOT NULL,
|
||||
"model" TEXT NOT NULL,
|
||||
"recordId" TEXT NOT NULL,
|
||||
"lastWriteAt" TIMESTAMP(3) NOT NULL,
|
||||
"lastWriteOrigin" TEXT NOT NULL,
|
||||
"updatedAt" TIMESTAMP(3) NOT NULL,
|
||||
|
||||
CONSTRAINT "SyncRecordVersion_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "SyncConflict" (
|
||||
"id" TEXT NOT NULL,
|
||||
"model" TEXT NOT NULL,
|
||||
"recordId" TEXT NOT NULL,
|
||||
"winningOrigin" TEXT NOT NULL,
|
||||
"losingOrigin" TEXT NOT NULL,
|
||||
"winningPayload" JSONB NOT NULL,
|
||||
"losingPayload" JSONB NOT NULL,
|
||||
"detectedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
|
||||
CONSTRAINT "SyncConflict_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
-- CreateIndex
|
||||
CREATE UNIQUE INDEX "SyncRecordVersion_model_recordId_key" ON "SyncRecordVersion"("model", "recordId");
|
||||
@@ -0,0 +1,17 @@
|
||||
-- AlterEnum
|
||||
ALTER TYPE "ChatChannelType" ADD VALUE 'GRUPPE';
|
||||
|
||||
-- AlterTable
|
||||
ALTER TABLE "ChatChannel" ADD COLUMN "createdByUserId" TEXT,
|
||||
ADD COLUMN "name" TEXT;
|
||||
|
||||
-- AlterTable
|
||||
ALTER TABLE "ChatParticipant" ADD COLUMN "guestAccountId" TEXT,
|
||||
ALTER COLUMN "userId" DROP NOT NULL;
|
||||
|
||||
-- CreateIndex
|
||||
CREATE UNIQUE INDEX "ChatParticipant_channelId_guestAccountId_key" ON "ChatParticipant"("channelId", "guestAccountId");
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "ChatParticipant" ADD CONSTRAINT "ChatParticipant_guestAccountId_fkey" FOREIGN KEY ("guestAccountId") REFERENCES "GuestAccount"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
+114
-6
@@ -24,6 +24,7 @@ model Kc {
|
||||
guests GuestAccount[]
|
||||
localUsers User[]
|
||||
teamerInvites TeamerInvite[]
|
||||
verantwortlicheInvites VerantwortlicheInvite[]
|
||||
}
|
||||
|
||||
/// A local congregation/community participating in one Kc.
|
||||
@@ -37,6 +38,7 @@ model Gemeinde {
|
||||
memberships Membership[]
|
||||
guests GuestAccount[]
|
||||
teamerInvites TeamerInvite[]
|
||||
verantwortlicheInvites VerantwortlicheInvite[]
|
||||
|
||||
@@unique([kcId, name])
|
||||
}
|
||||
@@ -78,6 +80,7 @@ model User {
|
||||
memberships Membership[]
|
||||
messages ChatMessage[]
|
||||
chatParticipations ChatParticipant[]
|
||||
deviceTokens DeviceToken[]
|
||||
}
|
||||
|
||||
/// Scopes a User's role to a specific Kc (and Gemeinde, if applicable).
|
||||
@@ -111,6 +114,24 @@ model GuestAccount {
|
||||
gemeinde Gemeinde? @relation(fields: [gemeindeId], references: [id], onDelete: Cascade)
|
||||
messages ChatMessage[]
|
||||
teilnehmer Teilnehmer[]
|
||||
deviceTokens DeviceToken[]
|
||||
chatParticipations ChatParticipant[]
|
||||
}
|
||||
|
||||
/// A push-notification target (FCM registration token) bound to whoever
|
||||
/// registered it — a team `User` or a `GuestAccount`. Replicated so a
|
||||
/// notification can be sent from either server.
|
||||
model DeviceToken {
|
||||
id String @id @default(cuid())
|
||||
token String @unique
|
||||
platform String
|
||||
userId String?
|
||||
guestAccountId String?
|
||||
createdAt DateTime @default(now())
|
||||
lastSeenAt DateTime @default(now())
|
||||
|
||||
user User? @relation(fields: [userId], references: [id], onDelete: Cascade)
|
||||
guestAccount GuestAccount? @relation(fields: [guestAccountId], references: [id], onDelete: Cascade)
|
||||
}
|
||||
|
||||
/// Invitation issued by a Gemeinde Verantwortliche/r so new Gemeinde Teamer
|
||||
@@ -134,13 +155,42 @@ model TeamerInvite {
|
||||
gemeinde Gemeinde @relation(fields: [gemeindeId], references: [id], onDelete: Cascade)
|
||||
}
|
||||
|
||||
/// Invitation issued by a Leitungsteam member so a person can register as
|
||||
/// Gemeinde Verantwortliche/r for a specific Gemeinde via their
|
||||
/// Konfi-Castle-ID (Authentik) — skips the self-registration approval step
|
||||
/// since a Leitungsteam member is vouching for them directly. A group link
|
||||
/// leaves `email` null and may be redeemed up to `maxUses` times (null =
|
||||
/// unlimited); a personal invite pins `email` and defaults to a single use.
|
||||
model VerantwortlicheInvite {
|
||||
id String @id @default(cuid())
|
||||
kcId String
|
||||
gemeindeId String
|
||||
token String @unique
|
||||
email String?
|
||||
maxUses Int?
|
||||
usedCount Int @default(0)
|
||||
expiresAt DateTime?
|
||||
revokedAt DateTime?
|
||||
createdByUserId String
|
||||
createdAt DateTime @default(now())
|
||||
|
||||
kc Kc @relation(fields: [kcId], references: [id], onDelete: Cascade)
|
||||
gemeinde Gemeinde @relation(fields: [gemeindeId], references: [id], onDelete: Cascade)
|
||||
}
|
||||
|
||||
/// A workshop election, scoped to a Kc; name carries a date key + "Teil".
|
||||
/// `phasenAnzahl` mirrors the WP plugin's `anzahl_einheiten`: a Wahl can run
|
||||
/// several independent phases (e.g. morning/afternoon), each with its own
|
||||
/// workshops, its own guest submission, and its own assignment run — a guest
|
||||
/// submits once per phase, not once for the whole Wahl.
|
||||
model Wahl {
|
||||
id String @id @default(cuid())
|
||||
kcId String
|
||||
name String
|
||||
datumsSchluessel String
|
||||
teil String
|
||||
beschreibung String?
|
||||
phasenAnzahl Int @default(1)
|
||||
isOpen Boolean @default(true)
|
||||
createdAt DateTime @default(now())
|
||||
|
||||
@@ -150,10 +200,14 @@ model Wahl {
|
||||
forceZuteilungen ForceZuteilung[]
|
||||
}
|
||||
|
||||
/// A workshop offered in one phase of a Wahl. `phase` is 1-based and must be
|
||||
/// <= the owning Wahl's `phasenAnzahl`.
|
||||
model Workshop {
|
||||
id String @id @default(cuid())
|
||||
wahlId String
|
||||
phase Int @default(1)
|
||||
name String
|
||||
beschreibung String?
|
||||
kapazitaet Int
|
||||
minTeilnehmer Int @default(0)
|
||||
|
||||
@@ -162,10 +216,13 @@ model Workshop {
|
||||
forceZuteilungen ForceZuteilung[]
|
||||
}
|
||||
|
||||
/// A participant's submitted choices for a Wahl.
|
||||
/// A participant's submitted choices for one phase of a Wahl. A guest submits
|
||||
/// separately per phase (matching the WP plugin), so the same guest can have
|
||||
/// one row per (wahlId, phase).
|
||||
model Teilnehmer {
|
||||
id String @id @default(cuid())
|
||||
wahlId String
|
||||
phase Int @default(1)
|
||||
guestAccountId String
|
||||
prioritaeten Json
|
||||
createdAt DateTime @default(now())
|
||||
@@ -175,7 +232,7 @@ model Teilnehmer {
|
||||
zuteilung Zuteilung?
|
||||
forceZuteilung ForceZuteilung?
|
||||
|
||||
@@unique([wahlId, guestAccountId])
|
||||
@@unique([wahlId, guestAccountId, phase])
|
||||
}
|
||||
|
||||
/// Manual override set by LT before running the assignment algorithm; takes precedence.
|
||||
@@ -226,6 +283,11 @@ enum ChatChannelType {
|
||||
DIREKT
|
||||
LT_UEBERGREIFEND
|
||||
BROADCAST
|
||||
/// Freely composed group chat: created by a Leitungsteam member or a
|
||||
/// Gemeinde Verantwortliche/r (for their own KC), with an explicit,
|
||||
/// mutable participant list (team users and/or guests) via ChatParticipant
|
||||
/// - unlike GEMEINDE_GRUPPE, membership is not derived from Gemeinde.
|
||||
GRUPPE
|
||||
}
|
||||
|
||||
model ChatChannel {
|
||||
@@ -233,6 +295,12 @@ model ChatChannel {
|
||||
kcId String
|
||||
type ChatChannelType
|
||||
gemeindeId String?
|
||||
/// Display name; used by GRUPPE channels (optional for other types).
|
||||
name String?
|
||||
/// Who created the channel; only set for GRUPPE so far. Used to let the
|
||||
/// creator manage participants alongside Leitungsteam/Verantwortliche.
|
||||
createdByUserId String?
|
||||
|
||||
createdAt DateTime @default(now())
|
||||
|
||||
kc Kc @relation(fields: [kcId], references: [id], onDelete: Cascade)
|
||||
@@ -240,18 +308,22 @@ model ChatChannel {
|
||||
participants ChatParticipant[]
|
||||
}
|
||||
|
||||
/// Explicit membership for DIREKT (1:1) channels; other channel types derive
|
||||
/// access from Membership/Gemeinde instead of this table.
|
||||
/// Explicit membership for DIREKT (1:1) and GRUPPE channels; other channel
|
||||
/// types derive access from Membership/Gemeinde instead of this table.
|
||||
/// Exactly one of userId/guestAccountId is set per row.
|
||||
model ChatParticipant {
|
||||
id String @id @default(cuid())
|
||||
channelId String
|
||||
userId String
|
||||
userId String?
|
||||
guestAccountId String?
|
||||
createdAt DateTime @default(now())
|
||||
|
||||
channel ChatChannel @relation(fields: [channelId], references: [id], onDelete: Cascade)
|
||||
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
|
||||
user User? @relation(fields: [userId], references: [id], onDelete: Cascade)
|
||||
guestAccount GuestAccount? @relation(fields: [guestAccountId], references: [id], onDelete: Cascade)
|
||||
|
||||
@@unique([channelId, userId])
|
||||
@@unique([channelId, guestAccountId])
|
||||
}
|
||||
|
||||
model ChatMessage {
|
||||
@@ -276,6 +348,9 @@ enum SyncOperation {
|
||||
/// Append-only log of local mutations, replicated to the peer server (local
|
||||
/// <-> cloud). `originId` is the SERVER_ID that made the change, so applying
|
||||
/// an incoming entry never gets re-captured/re-pushed back (no echo loops).
|
||||
/// `occurredAt` is the wall-clock moment of the mutation itself (set at
|
||||
/// capture time), distinct from `createdAt` which is just row-insert time -
|
||||
/// conflict resolution compares `occurredAt`, never sync/network timing.
|
||||
model SyncLogEntry {
|
||||
id String @id @default(cuid())
|
||||
sequence Int @default(autoincrement())
|
||||
@@ -284,6 +359,7 @@ model SyncLogEntry {
|
||||
operation SyncOperation
|
||||
payload Json
|
||||
originId String
|
||||
occurredAt DateTime @default(now())
|
||||
createdAt DateTime @default(now())
|
||||
}
|
||||
|
||||
@@ -296,3 +372,35 @@ model SyncCursor {
|
||||
lastPushedSequence Int @default(0)
|
||||
lastPulledSequence Int @default(0)
|
||||
}
|
||||
|
||||
/// Last-write-wins register, one row per replicated record. Tracks the
|
||||
/// wall-clock time and origin of whichever mutation - local or remote - is
|
||||
/// currently considered authoritative for that record, so a concurrent edit
|
||||
/// on both servers resolves deterministically by actual edit time instead
|
||||
/// of by sync/network arrival order.
|
||||
model SyncRecordVersion {
|
||||
id String @id @default(cuid())
|
||||
model String
|
||||
recordId String
|
||||
lastWriteAt DateTime
|
||||
lastWriteOrigin String
|
||||
updatedAt DateTime @updatedAt
|
||||
|
||||
@@unique([model, recordId])
|
||||
}
|
||||
|
||||
/// Audit trail of detected conflicts: two servers wrote the same record
|
||||
/// within the replication window. Resolution (last-write-wins by
|
||||
/// occurredAt) still happens automatically and immediately - nothing here
|
||||
/// blocks live sync - but Leitungsteam can review afterwards whether a
|
||||
/// discarded edit needs to be manually reapplied.
|
||||
model SyncConflict {
|
||||
id String @id @default(cuid())
|
||||
model String
|
||||
recordId String
|
||||
winningOrigin String
|
||||
losingOrigin String
|
||||
winningPayload Json
|
||||
losingPayload Json
|
||||
detectedAt DateTime @default(now())
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import { existsSync } from 'fs';
|
||||
import { join } from 'path';
|
||||
import { PrismaModule } from './prisma/prisma.module';
|
||||
import { MailModule } from './mail/mail.module';
|
||||
import { PushModule } from './push/push.module';
|
||||
import { AuthModule } from './auth/auth.module';
|
||||
import { KcModule } from './kc/kc.module';
|
||||
import { GemeindeModule } from './gemeinde/gemeinde.module';
|
||||
@@ -35,6 +36,7 @@ const webRoot =
|
||||
}),
|
||||
PrismaModule,
|
||||
MailModule,
|
||||
PushModule,
|
||||
SyncModule,
|
||||
AuthModule,
|
||||
KcModule,
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
import { Body, Controller, Get, Post, Req, UseGuards } from '@nestjs/common';
|
||||
import { BadRequestException, Body, Controller, Get, Post, Req, UseGuards } from '@nestjs/common';
|
||||
import { AuthGuard } from '@nestjs/passport';
|
||||
import { GuestAuthService } from './guest-auth.service';
|
||||
import { TeamAuthService } from './team-auth.service';
|
||||
import { CreateGuestDto } from './dto/create-guest.dto';
|
||||
import { TeamLoginDto } from './dto/team-login.dto';
|
||||
import { RegisterTeamerDto } from './dto/register-teamer.dto';
|
||||
import { ResolveCodeDto } from './dto/resolve-code.dto';
|
||||
import { CodeResolverService } from './code-resolver.service';
|
||||
import { AuthenticatedRequest } from './authenticated-request';
|
||||
import { GuestJwtPayload } from './guest-auth.service';
|
||||
|
||||
@@ -13,8 +15,19 @@ export class AuthController {
|
||||
constructor(
|
||||
private readonly guestAuth: GuestAuthService,
|
||||
private readonly teamAuth: TeamAuthService,
|
||||
private readonly codeResolver: CodeResolverService,
|
||||
) {}
|
||||
|
||||
/// Single entry point for the unified login screen: the caller types one
|
||||
/// "Code" and this classifies it (guest invite code, Teamer/Verantwortliche
|
||||
/// invite token, Gemeinde name, email, or the "login" SSO keyword) so the
|
||||
/// client can render the matching follow-up form. Never reveals *why* a
|
||||
/// code didn't match — always the same 404 "Code ungültig".
|
||||
@Post('resolve-code')
|
||||
resolveCode(@Body() dto: ResolveCodeDto) {
|
||||
return this.codeResolver.resolve(dto.code);
|
||||
}
|
||||
|
||||
/// Returns the identity + scope behind whichever token was presented, so a
|
||||
/// client can render a role-aware UI. `kind` is "guest" for a Konfi token,
|
||||
/// "user" for an Authentik or local Teamer token.
|
||||
@@ -49,10 +62,14 @@ export class AuthController {
|
||||
return this.guestAuth.createGuest(dto.inviteCode, dto.firstName, dto.lastName);
|
||||
}
|
||||
|
||||
/// Password login for local Gemeinde Teamer accounts.
|
||||
/// Password login for local Gemeinde Teamer accounts — by Gemeinde name
|
||||
/// (the normal path) or by email (legacy/personal accounts).
|
||||
@Post('team-login')
|
||||
teamLogin(@Body() dto: TeamLoginDto) {
|
||||
return this.teamAuth.login(dto.email, dto.password);
|
||||
if (!dto.email && !dto.gemeindeName) {
|
||||
throw new BadRequestException('email or gemeindeName is required');
|
||||
}
|
||||
return this.teamAuth.login({ email: dto.email, gemeindeName: dto.gemeindeName }, dto.password);
|
||||
}
|
||||
|
||||
/// Self-registration for a Gemeinde Teamer via an invite token/link.
|
||||
|
||||
@@ -9,6 +9,7 @@ import { AuthentikStrategy } from './authentik.strategy';
|
||||
import { GuestJwtStrategy } from './guest-jwt.strategy';
|
||||
import { TeamJwtStrategy } from './team-jwt.strategy';
|
||||
import { TokenVerificationService } from './token-verification.service';
|
||||
import { CodeResolverService } from './code-resolver.service';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
@@ -29,7 +30,8 @@ import { TokenVerificationService } from './token-verification.service';
|
||||
GuestJwtStrategy,
|
||||
TeamJwtStrategy,
|
||||
TokenVerificationService,
|
||||
CodeResolverService,
|
||||
],
|
||||
exports: [TokenVerificationService, TeamAuthService],
|
||||
exports: [TokenVerificationService, TeamAuthService, CodeResolverService],
|
||||
})
|
||||
export class AuthModule {}
|
||||
|
||||
@@ -7,13 +7,19 @@ import { Request } from 'express';
|
||||
import { PrismaClient } from '../prisma/prisma.module';
|
||||
import { SyncService } from '../sync/sync.service';
|
||||
import { AuthenticatedUser } from './authenticated-request';
|
||||
import { resolveOrProvisionAuthentikUser, toAuthenticatedUser } from './provision-user';
|
||||
import {
|
||||
authentikEmail,
|
||||
resolveOrProvisionAuthentikUser,
|
||||
toAuthenticatedUser,
|
||||
} from './provision-user';
|
||||
|
||||
interface AuthentikJwtPayload {
|
||||
sub: string;
|
||||
email?: string;
|
||||
given_name?: string;
|
||||
family_name?: string;
|
||||
preferred_username?: string;
|
||||
name?: string;
|
||||
groups?: string[];
|
||||
}
|
||||
|
||||
@@ -52,8 +58,8 @@ export class AuthentikStrategy extends PassportStrategy(Strategy, 'authentik') {
|
||||
}
|
||||
|
||||
async validate(payload: AuthentikJwtPayload): Promise<AuthenticatedUser> {
|
||||
if (!payload.email) {
|
||||
throw new UnauthorizedException('Authentik token missing email claim');
|
||||
if (!payload.sub) {
|
||||
throw new UnauthorizedException('Authentik token missing subject');
|
||||
}
|
||||
const isLeitungsteam = (payload.groups ?? []).includes(this.leitungsteamGroup);
|
||||
const user = await resolveOrProvisionAuthentikUser(
|
||||
@@ -61,8 +67,8 @@ export class AuthentikStrategy extends PassportStrategy(Strategy, 'authentik') {
|
||||
this.sync,
|
||||
{
|
||||
sub: payload.sub,
|
||||
email: payload.email,
|
||||
firstName: payload.given_name ?? '',
|
||||
email: authentikEmail(payload),
|
||||
firstName: payload.given_name ?? payload.preferred_username ?? payload.name ?? '',
|
||||
lastName: payload.family_name ?? '',
|
||||
},
|
||||
isLeitungsteam,
|
||||
|
||||
@@ -0,0 +1,186 @@
|
||||
import { NotFoundException } from '@nestjs/common';
|
||||
import { CodeResolverService } from './code-resolver.service';
|
||||
|
||||
/// Focus: the classification order (SSO keyword > email > guest code >
|
||||
/// Teamer invite > Verantwortliche invite > Gemeinde name), invite
|
||||
/// usability checks (revoked/expired/exhausted), and the generic 404
|
||||
/// for anything that matches nothing.
|
||||
|
||||
function makeService(opts: {
|
||||
kc?: any;
|
||||
teamerInvite?: any;
|
||||
verantwortlicheInvite?: any;
|
||||
gemeinde?: any;
|
||||
} = {}) {
|
||||
const prisma = {
|
||||
kc: {
|
||||
findUnique: jest.fn().mockResolvedValue(opts.kc ?? null),
|
||||
findFirst: jest.fn().mockResolvedValue(opts.kc ?? null),
|
||||
},
|
||||
teamerInvite: {
|
||||
findUnique: jest.fn().mockResolvedValue(opts.teamerInvite ?? null),
|
||||
findFirst: jest.fn().mockResolvedValue(opts.teamerInvite ?? null),
|
||||
},
|
||||
verantwortlicheInvite: {
|
||||
findUnique: jest.fn().mockResolvedValue(opts.verantwortlicheInvite ?? null),
|
||||
findFirst: jest.fn().mockResolvedValue(opts.verantwortlicheInvite ?? null),
|
||||
},
|
||||
gemeinde: { findFirst: jest.fn().mockResolvedValue(opts.gemeinde ?? null) },
|
||||
};
|
||||
return { service: new CodeResolverService(prisma as never), prisma };
|
||||
}
|
||||
|
||||
describe('CodeResolverService.resolve', () => {
|
||||
it('resolves the "lt", "login", and "sso" keywords to SSO regardless of case', async () => {
|
||||
const { service } = makeService();
|
||||
await expect(service.resolve('lt')).resolves.toEqual({ kind: 'sso' });
|
||||
await expect(service.resolve('LT')).resolves.toEqual({ kind: 'sso' });
|
||||
await expect(service.resolve(' Lt ')).resolves.toEqual({ kind: 'sso' });
|
||||
await expect(service.resolve('login')).resolves.toEqual({ kind: 'sso' });
|
||||
await expect(service.resolve('LOGIN')).resolves.toEqual({ kind: 'sso' });
|
||||
await expect(service.resolve(' Login ')).resolves.toEqual({ kind: 'sso' });
|
||||
await expect(service.resolve('sso')).resolves.toEqual({ kind: 'sso' });
|
||||
await expect(service.resolve('SSO')).resolves.toEqual({ kind: 'sso' });
|
||||
});
|
||||
|
||||
it('resolves codes with LT suffix to SSO', async () => {
|
||||
const { service } = makeService();
|
||||
await expect(service.resolve('ABC123LT')).resolves.toEqual({ kind: 'sso' });
|
||||
await expect(service.resolve('dev123lt')).resolves.toEqual({ kind: 'sso' });
|
||||
await expect(service.resolve('32d814ed9011LT')).resolves.toEqual({ kind: 'sso' });
|
||||
});
|
||||
|
||||
it('treats an @-containing value as an email team-login, without hitting the DB', async () => {
|
||||
const { service, prisma } = makeService();
|
||||
const result = await service.resolve('someone@example.org');
|
||||
expect(result).toEqual({ kind: 'team_login', identifierType: 'email' });
|
||||
expect(prisma.kc.findUnique).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('resolves an active KC invite code to a guest login', async () => {
|
||||
const { service } = makeService({ kc: { id: 'kc-1', name: 'KC Dev', isActive: true } });
|
||||
await expect(service.resolve('DEV123')).resolves.toEqual({ kind: 'guest', kcName: 'KC Dev' });
|
||||
});
|
||||
|
||||
it('does not treat an inactive KC as a valid guest code', async () => {
|
||||
const { service } = makeService({ kc: { id: 'kc-1', name: 'KC Dev', isActive: false } });
|
||||
await expect(service.resolve('DEV123')).rejects.toBeInstanceOf(NotFoundException);
|
||||
});
|
||||
|
||||
it('resolves a usable Teamer invite token', async () => {
|
||||
const { service } = makeService({
|
||||
teamerInvite: {
|
||||
email: null,
|
||||
revokedAt: null,
|
||||
expiresAt: null,
|
||||
maxUses: null,
|
||||
usedCount: 0,
|
||||
gemeinde: { name: 'Nord' },
|
||||
kc: { name: 'KC Dev' },
|
||||
},
|
||||
});
|
||||
await expect(service.resolve('sometoken')).resolves.toEqual({
|
||||
kind: 'teamer_invite',
|
||||
kcName: 'KC Dev',
|
||||
gemeindeName: 'Nord',
|
||||
pinnedEmail: null,
|
||||
});
|
||||
});
|
||||
|
||||
it('rejects a revoked Teamer invite token', async () => {
|
||||
const { service } = makeService({
|
||||
teamerInvite: {
|
||||
email: null,
|
||||
revokedAt: new Date(),
|
||||
expiresAt: null,
|
||||
maxUses: null,
|
||||
usedCount: 0,
|
||||
gemeinde: { name: 'Nord' },
|
||||
kc: { name: 'KC Dev' },
|
||||
},
|
||||
});
|
||||
await expect(service.resolve('sometoken')).rejects.toBeInstanceOf(NotFoundException);
|
||||
});
|
||||
|
||||
it('rejects an expired Teamer invite token', async () => {
|
||||
const { service } = makeService({
|
||||
teamerInvite: {
|
||||
email: null,
|
||||
revokedAt: null,
|
||||
expiresAt: new Date(Date.now() - 1000),
|
||||
maxUses: null,
|
||||
usedCount: 0,
|
||||
gemeinde: { name: 'Nord' },
|
||||
kc: { name: 'KC Dev' },
|
||||
},
|
||||
});
|
||||
await expect(service.resolve('sometoken')).rejects.toBeInstanceOf(NotFoundException);
|
||||
});
|
||||
|
||||
it('rejects a used-up Teamer invite token', async () => {
|
||||
const { service } = makeService({
|
||||
teamerInvite: {
|
||||
email: null,
|
||||
revokedAt: null,
|
||||
expiresAt: null,
|
||||
maxUses: 1,
|
||||
usedCount: 1,
|
||||
gemeinde: { name: 'Nord' },
|
||||
kc: { name: 'KC Dev' },
|
||||
},
|
||||
});
|
||||
await expect(service.resolve('sometoken')).rejects.toBeInstanceOf(NotFoundException);
|
||||
});
|
||||
|
||||
it('resolves a usable Verantwortliche invite token', async () => {
|
||||
const { service } = makeService({
|
||||
verantwortlicheInvite: {
|
||||
email: 'pinned@example.org',
|
||||
revokedAt: null,
|
||||
expiresAt: null,
|
||||
maxUses: 1,
|
||||
usedCount: 0,
|
||||
gemeinde: { name: 'Süd' },
|
||||
kc: { name: 'KC Dev' },
|
||||
},
|
||||
});
|
||||
await expect(service.resolve('vertoken')).resolves.toEqual({
|
||||
kind: 'verantwortliche_invite',
|
||||
kcName: 'KC Dev',
|
||||
gemeindeName: 'Süd',
|
||||
pinnedEmail: 'pinned@example.org',
|
||||
});
|
||||
});
|
||||
|
||||
it('resolves a Gemeinde name to a Gemeinde-name team-login', async () => {
|
||||
const { service } = makeService({ gemeinde: { id: 'gem-1', name: 'Mustergemeinde' } });
|
||||
await expect(service.resolve('Mustergemeinde')).resolves.toEqual({
|
||||
kind: 'team_login',
|
||||
identifierType: 'gemeindeName',
|
||||
gemeindeName: 'Mustergemeinde',
|
||||
});
|
||||
});
|
||||
|
||||
it('resolves codes from invite/token URLs', async () => {
|
||||
const { service } = makeService({ kc: { id: 'kc-1', name: 'KC Dev', isActive: true } });
|
||||
await expect(service.resolve('https://example.org/join?code=DEV123')).resolves.toEqual({
|
||||
kind: 'guest',
|
||||
kcName: 'KC Dev',
|
||||
});
|
||||
// noinspection HttpUrlsUsage
|
||||
await expect(service.resolve('http://example.org/join?token=DEV123')).resolves.toEqual({
|
||||
kind: 'guest',
|
||||
kcName: 'KC Dev',
|
||||
});
|
||||
});
|
||||
|
||||
it('rejects an empty code', async () => {
|
||||
const { service } = makeService();
|
||||
await expect(service.resolve(' ')).rejects.toBeInstanceOf(NotFoundException);
|
||||
});
|
||||
|
||||
it('rejects a code that matches nothing', async () => {
|
||||
const { service } = makeService();
|
||||
await expect(service.resolve('totally-unknown-code')).rejects.toBeInstanceOf(NotFoundException);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,135 @@
|
||||
import { Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { PrismaClient } from '../prisma/prisma.module';
|
||||
|
||||
export type ResolvedCodeKind = 'guest' | 'teamer_invite' | 'verantwortliche_invite' | 'team_login' | 'sso';
|
||||
|
||||
export interface ResolvedCode {
|
||||
kind: ResolvedCodeKind;
|
||||
kcName?: string;
|
||||
gemeindeName?: string;
|
||||
/// *_invite only: whether the invite is pinned to a specific email
|
||||
/// (personal invite) — if so the email field should be locked to it.
|
||||
pinnedEmail?: string | null;
|
||||
/// team_login only: which field the entered value should be sent back as.
|
||||
identifierType?: 'email' | 'gemeindeName';
|
||||
}
|
||||
|
||||
const SSO_KEYWORDS = new Set(['lt', 'login', 'sso']);
|
||||
|
||||
/// Classifies whatever the user typed into the single "Code" field on the
|
||||
/// unified login screen, so the frontend can show the right follow-up form
|
||||
/// without the user picking a login type up front.
|
||||
///
|
||||
/// Resolution order:
|
||||
/// 1. the fixed keywords "lt", "login", "sso" or codes ending with "LT" (e.g. "ABC123LT")
|
||||
/// -> Konfi-Castle-ID (Authentik SSO), for Leitungsteam/Verantwortliche
|
||||
/// 2. an '@'-shaped value -> email + password (Teamer legacy login)
|
||||
/// 3. a KC guest invite code (case-insensitive)
|
||||
/// 4. a Teamer-invite token (self-registration link)
|
||||
/// 5. a Verantwortliche-invite token (LT-issued SSO shortcut link)
|
||||
/// 6. a Gemeinde name -> Gemeinde-name + password (normal Teamer login)
|
||||
/// Anything else: "Code ungültig" (never leaks *why* it didn't match).
|
||||
@Injectable()
|
||||
export class CodeResolverService {
|
||||
constructor(private readonly prisma: PrismaClient) {}
|
||||
|
||||
async resolve(rawCode: string): Promise<ResolvedCode> {
|
||||
const code = rawCode.trim();
|
||||
if (!code) {
|
||||
throw new NotFoundException('Code ungültig');
|
||||
}
|
||||
|
||||
const lower = code.toLowerCase();
|
||||
const upper = code.toUpperCase();
|
||||
|
||||
if (SSO_KEYWORDS.has(lower) || (code.length > 2 && upper.endsWith('LT'))) {
|
||||
return { kind: 'sso' };
|
||||
}
|
||||
|
||||
if (code.includes('@')) {
|
||||
return { kind: 'team_login', identifierType: 'email' };
|
||||
}
|
||||
|
||||
let lookupCode = code;
|
||||
try {
|
||||
if (/^https?:\/\//i.test(code)) {
|
||||
const url = new URL(code);
|
||||
lookupCode =
|
||||
url.searchParams.get('token') ||
|
||||
url.searchParams.get('code') ||
|
||||
url.searchParams.get('invite') ||
|
||||
url.pathname.split('/').filter(Boolean).pop() ||
|
||||
code;
|
||||
}
|
||||
} catch {
|
||||
// Ignore URL parsing errors and keep code as is
|
||||
}
|
||||
|
||||
const kc =
|
||||
(await this.prisma.kc.findFirst({
|
||||
where: { inviteCode: { equals: lookupCode, mode: 'insensitive' } },
|
||||
})) ||
|
||||
(await this.prisma.kc.findUnique({
|
||||
where: { inviteCode: lookupCode },
|
||||
}));
|
||||
if (kc && kc.isActive) {
|
||||
return { kind: 'guest', kcName: kc.name };
|
||||
}
|
||||
|
||||
const teamerInvite =
|
||||
(await this.prisma.teamerInvite.findFirst({
|
||||
where: { token: { equals: lookupCode, mode: 'insensitive' } },
|
||||
include: { gemeinde: true, kc: true },
|
||||
})) ||
|
||||
(await this.prisma.teamerInvite.findUnique({
|
||||
where: { token: lookupCode },
|
||||
include: { gemeinde: true, kc: true },
|
||||
}));
|
||||
if (teamerInvite && this.isInviteUsable(teamerInvite)) {
|
||||
return {
|
||||
kind: 'teamer_invite',
|
||||
kcName: teamerInvite.kc.name,
|
||||
gemeindeName: teamerInvite.gemeinde.name,
|
||||
pinnedEmail: teamerInvite.email,
|
||||
};
|
||||
}
|
||||
|
||||
const verantwortlicheInvite =
|
||||
(await this.prisma.verantwortlicheInvite.findFirst({
|
||||
where: { token: { equals: lookupCode, mode: 'insensitive' } },
|
||||
include: { gemeinde: true, kc: true },
|
||||
})) ||
|
||||
(await this.prisma.verantwortlicheInvite.findUnique({
|
||||
where: { token: lookupCode },
|
||||
include: { gemeinde: true, kc: true },
|
||||
}));
|
||||
if (verantwortlicheInvite && this.isInviteUsable(verantwortlicheInvite)) {
|
||||
return {
|
||||
kind: 'verantwortliche_invite',
|
||||
kcName: verantwortlicheInvite.kc.name,
|
||||
gemeindeName: verantwortlicheInvite.gemeinde.name,
|
||||
pinnedEmail: verantwortlicheInvite.email,
|
||||
};
|
||||
}
|
||||
|
||||
const gemeinde = await this.prisma.gemeinde.findFirst({
|
||||
where: { name: { equals: lookupCode, mode: 'insensitive' } },
|
||||
});
|
||||
if (gemeinde) {
|
||||
return { kind: 'team_login', identifierType: 'gemeindeName', gemeindeName: gemeinde.name };
|
||||
}
|
||||
|
||||
throw new NotFoundException('Code ungültig');
|
||||
}
|
||||
|
||||
private isInviteUsable(invite: {
|
||||
revokedAt: Date | null;
|
||||
expiresAt: Date | null;
|
||||
maxUses: number | null;
|
||||
usedCount: number;
|
||||
}): boolean {
|
||||
if (invite.revokedAt) return false;
|
||||
if (invite.expiresAt && invite.expiresAt.getTime() < Date.now()) return false;
|
||||
return invite.maxUses === null || invite.usedCount < invite.maxUses;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
import { IsNotEmpty, IsString } from 'class-validator';
|
||||
|
||||
export class ResolveCodeDto {
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
code!: string;
|
||||
}
|
||||
@@ -1,8 +1,19 @@
|
||||
import { IsEmail, IsNotEmpty, IsString } from 'class-validator';
|
||||
import { IsEmail, IsNotEmpty, IsOptional, IsString } from 'class-validator';
|
||||
|
||||
/// Team login accepts EITHER an email (legacy/personal Verantwortliche
|
||||
/// accounts) OR a Gemeinde name (the normal Teamer login path, since a
|
||||
/// Teamer thinks of their login as "meine Gemeinde", not their email).
|
||||
/// At least one of email/gemeindeName is required; enforced in the
|
||||
/// controller rather than a custom validator to keep this DTO simple.
|
||||
export class TeamLoginDto {
|
||||
@IsOptional()
|
||||
@IsEmail()
|
||||
email!: string;
|
||||
email?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
gemeindeName?: string;
|
||||
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
|
||||
@@ -20,20 +20,45 @@ export class GuestAuthService {
|
||||
private readonly sync: SyncService,
|
||||
) {}
|
||||
|
||||
/// Redeems a KC invite code for a guest/Konfi session. If a guest account
|
||||
/// with the same (trimmed, case-insensitive) name already exists for this
|
||||
/// KC, reuses it instead of creating a new one — this is what lets a Konfi
|
||||
/// "log back in" with the same code + name and keep their chat history /
|
||||
/// Workshop-Wahl submission instead of losing it to a fresh blank account.
|
||||
async createGuest(
|
||||
inviteCode: string,
|
||||
firstName: string,
|
||||
lastName: string,
|
||||
): Promise<{ accessToken: string }> {
|
||||
const kc = await this.prisma.kc.findUnique({ where: { inviteCode } });
|
||||
const trimmedCode = inviteCode.trim();
|
||||
const kc =
|
||||
(await this.prisma.kc.findFirst({
|
||||
where: { inviteCode: { equals: trimmedCode, mode: 'insensitive' } },
|
||||
})) ||
|
||||
(await this.prisma.kc.findUnique({
|
||||
where: { inviteCode: trimmedCode },
|
||||
}));
|
||||
if (!kc || !kc.isActive) {
|
||||
throw new NotFoundException('Unknown or inactive KC invite code');
|
||||
}
|
||||
|
||||
const guest = await this.prisma.guestAccount.create({
|
||||
data: { kcId: kc.id, firstName, lastName },
|
||||
const trimmedFirst = firstName.trim();
|
||||
const trimmedLast = lastName.trim();
|
||||
|
||||
let guest = await this.prisma.guestAccount.findFirst({
|
||||
where: {
|
||||
kcId: kc.id,
|
||||
firstName: { equals: trimmedFirst, mode: 'insensitive' },
|
||||
lastName: { equals: trimmedLast, mode: 'insensitive' },
|
||||
},
|
||||
});
|
||||
|
||||
if (!guest) {
|
||||
guest = await this.prisma.guestAccount.create({
|
||||
data: { kcId: kc.id, firstName: trimmedFirst, lastName: trimmedLast },
|
||||
});
|
||||
await this.sync.capture('GuestAccount', SyncOperation.CREATE, guest.id, guest);
|
||||
}
|
||||
|
||||
const payload: GuestJwtPayload = {
|
||||
guestId: guest.id,
|
||||
|
||||
@@ -10,6 +10,18 @@ export interface AuthentikClaims {
|
||||
lastName: string;
|
||||
}
|
||||
|
||||
/// Authentik users don't necessarily have an email set. Fall back to a stable,
|
||||
/// per-user placeholder so provisioning still has a unique handle for the row.
|
||||
export function authentikEmail(p: {
|
||||
email?: string;
|
||||
preferred_username?: string;
|
||||
sub: string;
|
||||
}): string {
|
||||
const e = p.email?.trim();
|
||||
if (e) return e.toLowerCase();
|
||||
return `${p.preferred_username?.trim() || p.sub}@no-email.authentik`.toLowerCase();
|
||||
}
|
||||
|
||||
/// Placeholder kcId for the synthetic, global LEITUNGSTEAM membership. RolesGuard
|
||||
/// never compares it (LT short-circuits the KC check), it only needs to exist.
|
||||
export const GLOBAL_LT_KC_ID = '*';
|
||||
|
||||
@@ -28,6 +28,10 @@ interface InviteRow {
|
||||
function makeService(seed: {
|
||||
invites?: InviteRow[];
|
||||
users?: { id: string; email: string; passwordHash: string | null }[];
|
||||
memberships?: {
|
||||
gemeindeName: string;
|
||||
user: { id: string; passwordHash: string | null };
|
||||
}[];
|
||||
}) {
|
||||
const invites = [...(seed.invites ?? [])];
|
||||
const users = [...(seed.users ?? [])].map((u) => ({
|
||||
@@ -39,6 +43,7 @@ function makeService(seed: {
|
||||
memberships: [] as unknown[],
|
||||
...u,
|
||||
}));
|
||||
const memberships = seed.memberships ?? [];
|
||||
|
||||
const prisma = {
|
||||
user: {
|
||||
@@ -64,6 +69,21 @@ function makeService(seed: {
|
||||
create: jest.fn(({ data }: { data: Record<string, unknown> }) =>
|
||||
Promise.resolve({ id: `m-1`, ...data }),
|
||||
),
|
||||
findMany: jest.fn(
|
||||
({
|
||||
where,
|
||||
}: {
|
||||
where: { gemeinde: { name: { equals: string; mode: string } } };
|
||||
}) =>
|
||||
Promise.resolve(
|
||||
memberships
|
||||
.filter(
|
||||
(m) =>
|
||||
m.gemeindeName.toLowerCase() === where.gemeinde.name.equals.toLowerCase(),
|
||||
)
|
||||
.map((m) => ({ user: m.user })),
|
||||
),
|
||||
),
|
||||
},
|
||||
teamerInvite: {
|
||||
findUnique: jest.fn(({ where }: { where: { token: string } }) =>
|
||||
@@ -195,18 +215,18 @@ describe('TeamAuthService.registerFromInvite', () => {
|
||||
describe('TeamAuthService.login', () => {
|
||||
it('rejects an unknown email', async () => {
|
||||
const { service } = makeService({ users: [] });
|
||||
await expect(service.login('nobody@example.org', 'x')).rejects.toBeInstanceOf(
|
||||
UnauthorizedException,
|
||||
);
|
||||
await expect(
|
||||
service.login({ email: 'nobody@example.org' }, 'x'),
|
||||
).rejects.toBeInstanceOf(UnauthorizedException);
|
||||
});
|
||||
|
||||
it('rejects a user without a password hash (Authentik-only account)', async () => {
|
||||
const { service } = makeService({
|
||||
users: [{ id: 'u-1', email: 'lt@example.org', passwordHash: null }],
|
||||
});
|
||||
await expect(service.login('lt@example.org', 'x')).rejects.toBeInstanceOf(
|
||||
UnauthorizedException,
|
||||
);
|
||||
await expect(
|
||||
service.login({ email: 'lt@example.org' }, 'x'),
|
||||
).rejects.toBeInstanceOf(UnauthorizedException);
|
||||
});
|
||||
|
||||
it('rejects a wrong password', async () => {
|
||||
@@ -215,18 +235,74 @@ describe('TeamAuthService.login', () => {
|
||||
{ id: 'u-1', email: 't@example.org', passwordHash: bcrypt.hashSync('right', 10) },
|
||||
],
|
||||
});
|
||||
await expect(service.login('t@example.org', 'wrong')).rejects.toBeInstanceOf(
|
||||
UnauthorizedException,
|
||||
);
|
||||
await expect(
|
||||
service.login({ email: 't@example.org' }, 'wrong'),
|
||||
).rejects.toBeInstanceOf(UnauthorizedException);
|
||||
});
|
||||
|
||||
it('issues a token for correct credentials', async () => {
|
||||
it('issues a token for correct credentials by email', async () => {
|
||||
const { service } = makeService({
|
||||
users: [
|
||||
{ id: 'u-1', email: 't@example.org', passwordHash: bcrypt.hashSync('right', 10) },
|
||||
],
|
||||
});
|
||||
const res = await service.login('T@example.org', 'right');
|
||||
const res = await service.login({ email: 'T@example.org' }, 'right');
|
||||
expect(res.accessToken).toEqual(expect.any(String));
|
||||
});
|
||||
|
||||
it('rejects when neither email nor gemeindeName is given', async () => {
|
||||
const { service } = makeService({});
|
||||
await expect(service.login({}, 'x')).rejects.toBeInstanceOf(UnauthorizedException);
|
||||
});
|
||||
|
||||
it('rejects an unknown Gemeinde name', async () => {
|
||||
const { service } = makeService({});
|
||||
await expect(
|
||||
service.login({ gemeindeName: 'Nirgendwo' }, 'x'),
|
||||
).rejects.toBeInstanceOf(UnauthorizedException);
|
||||
});
|
||||
|
||||
it('logs in by Gemeinde name, matching case-insensitively and trimmed', async () => {
|
||||
const { service } = makeService({
|
||||
memberships: [
|
||||
{
|
||||
gemeindeName: 'Musterstadt',
|
||||
user: { id: 'u-1', passwordHash: bcrypt.hashSync('right', 10) },
|
||||
},
|
||||
],
|
||||
});
|
||||
const res = await service.login({ gemeindeName: ' musterstadt ' }, 'right');
|
||||
expect(res.accessToken).toEqual(expect.any(String));
|
||||
});
|
||||
|
||||
it('tries every Teamer account for a Gemeinde until one password matches', async () => {
|
||||
const { service } = makeService({
|
||||
memberships: [
|
||||
{
|
||||
gemeindeName: 'Musterstadt',
|
||||
user: { id: 'u-1', passwordHash: bcrypt.hashSync('wrong-one', 10) },
|
||||
},
|
||||
{
|
||||
gemeindeName: 'Musterstadt',
|
||||
user: { id: 'u-2', passwordHash: bcrypt.hashSync('right', 10) },
|
||||
},
|
||||
],
|
||||
});
|
||||
const res = await service.login({ gemeindeName: 'Musterstadt' }, 'right');
|
||||
expect(res.accessToken).toEqual(expect.any(String));
|
||||
});
|
||||
|
||||
it('rejects a Gemeinde login when no account password matches', async () => {
|
||||
const { service } = makeService({
|
||||
memberships: [
|
||||
{
|
||||
gemeindeName: 'Musterstadt',
|
||||
user: { id: 'u-1', passwordHash: bcrypt.hashSync('right', 10) },
|
||||
},
|
||||
],
|
||||
});
|
||||
await expect(
|
||||
service.login({ gemeindeName: 'Musterstadt' }, 'wrong'),
|
||||
).rejects.toBeInstanceOf(UnauthorizedException);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -38,21 +38,46 @@ export class TeamAuthService {
|
||||
this.secret = config.getOrThrow<string>('TEAM_JWT_SECRET');
|
||||
}
|
||||
|
||||
async login(email: string, password: string): Promise<{ accessToken: string }> {
|
||||
/// Logs a Teamer in by email (legacy) OR by Gemeinde name — the normal
|
||||
/// path, since a Teamer thinks of their login as "meine Gemeinde" rather
|
||||
/// than an email address. A Gemeinde can have several Teamer accounts, so
|
||||
/// a name lookup tries the password against every active GEMEINDE_TEAMER
|
||||
/// membership for that Gemeinde (case-insensitive, trimmed name) until one
|
||||
/// matches, rather than assuming a 1:1 Gemeinde-to-account mapping.
|
||||
async login(
|
||||
credentials: { email?: string; gemeindeName?: string },
|
||||
password: string,
|
||||
): Promise<{ accessToken: string }> {
|
||||
if (credentials.email) {
|
||||
const user = await this.prisma.user.findUnique({
|
||||
where: { email: email.toLowerCase() },
|
||||
include: { memberships: true },
|
||||
where: { email: credentials.email.toLowerCase() },
|
||||
});
|
||||
if (!user || !user.passwordHash) {
|
||||
throw new UnauthorizedException('Invalid credentials');
|
||||
}
|
||||
const ok = await bcrypt.compare(password, user.passwordHash);
|
||||
if (!ok) {
|
||||
if (!user || !user.passwordHash || !(await bcrypt.compare(password, user.passwordHash))) {
|
||||
throw new UnauthorizedException('Invalid credentials');
|
||||
}
|
||||
return { accessToken: this.sign(user.id) };
|
||||
}
|
||||
|
||||
const gemeindeName = credentials.gemeindeName?.trim();
|
||||
if (!gemeindeName) {
|
||||
throw new UnauthorizedException('Invalid credentials');
|
||||
}
|
||||
const memberships = await this.prisma.membership.findMany({
|
||||
where: {
|
||||
role: Role.GEMEINDE_TEAMER,
|
||||
status: 'ACTIVE',
|
||||
gemeinde: { name: { equals: gemeindeName, mode: 'insensitive' } },
|
||||
},
|
||||
include: { user: true },
|
||||
});
|
||||
for (const m of memberships) {
|
||||
if (m.user.passwordHash && (await bcrypt.compare(password, m.user.passwordHash))) {
|
||||
return { accessToken: this.sign(m.user.id) };
|
||||
}
|
||||
}
|
||||
throw new UnauthorizedException('Invalid credentials');
|
||||
}
|
||||
|
||||
/// Redeems an invite token and creates the local Teamer account + its
|
||||
/// GEMEINDE_TEAMER membership for the invite's Gemeinde.
|
||||
async registerFromInvite(input: {
|
||||
|
||||
@@ -10,6 +10,7 @@ import { GuestJwtPayload } from './guest-auth.service';
|
||||
import { TeamAuthService } from './team-auth.service';
|
||||
import {
|
||||
AuthentikClaims,
|
||||
authentikEmail,
|
||||
resolveOrProvisionAuthentikUser,
|
||||
toAuthenticatedUser,
|
||||
} from './provision-user';
|
||||
@@ -55,15 +56,22 @@ export class TokenVerificationService {
|
||||
email?: string;
|
||||
given_name?: string;
|
||||
family_name?: string;
|
||||
preferred_username?: string;
|
||||
name?: string;
|
||||
groups?: string[];
|
||||
};
|
||||
if (!payload.sub || !payload.email) {
|
||||
throw new UnauthorizedException('Authentik token missing subject or email');
|
||||
const sub = payload.sub;
|
||||
if (!sub) {
|
||||
throw new UnauthorizedException('Authentik token missing subject');
|
||||
}
|
||||
return {
|
||||
sub: payload.sub,
|
||||
sub,
|
||||
email: authentikEmail({
|
||||
email: payload.email,
|
||||
firstName: payload.given_name ?? '',
|
||||
preferred_username: payload.preferred_username,
|
||||
sub,
|
||||
}),
|
||||
firstName: payload.given_name ?? payload.preferred_username ?? payload.name ?? '',
|
||||
lastName: payload.family_name ?? '',
|
||||
isLeitungsteam: (payload.groups ?? []).includes(this.leitungsteamGroup),
|
||||
};
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
import { Body, Controller, Get, Param, Post, Req, UseGuards } from '@nestjs/common';
|
||||
import { Body, Controller, Delete, Get, Param, Post, Req, UseGuards } from '@nestjs/common';
|
||||
import { AuthGuard } from '@nestjs/passport';
|
||||
import { ChatService } from './chat.service';
|
||||
import { ChatGateway } from './chat.gateway';
|
||||
import { CreateChannelDto } from './dto/create-channel.dto';
|
||||
import { CreateDirectChannelDto } from './dto/create-direct-channel.dto';
|
||||
import { AddParticipantDto } from './dto/add-participant.dto';
|
||||
import { Roles } from '../common/roles.decorator';
|
||||
import { RolesGuard } from '../common/roles.guard';
|
||||
import { Role } from '../common/role.enum';
|
||||
@@ -14,7 +16,10 @@ type ChatRequest = AuthenticatedRequest & { user?: AuthenticatedRequest['user']
|
||||
|
||||
@Controller('chat')
|
||||
export class ChatController {
|
||||
constructor(private readonly chat: ChatService) {}
|
||||
constructor(
|
||||
private readonly chat: ChatService,
|
||||
private readonly gateway: ChatGateway,
|
||||
) {}
|
||||
|
||||
/// Channel administration (Gemeinde-Gruppen, LT-Kanäle, Broadcasts) is Leitungsteam-only.
|
||||
@Post(':kcId/channels')
|
||||
@@ -24,6 +29,73 @@ export class ChatController {
|
||||
return this.chat.createChannel(kcId, dto.type, dto.gemeindeId);
|
||||
}
|
||||
|
||||
/// Free-form group chat ("Gruppenchat"): a Leitungsteam member (any KC) or
|
||||
/// a Gemeinde Verantwortliche/r (their own KC, enforced by RolesGuard's
|
||||
/// kcId scoping) can create one and pick any mix of team users and Konfis
|
||||
/// (guests) from this KC as initial participants.
|
||||
@Post(':kcId/gruppen')
|
||||
@UseGuards(AuthGuard(['authentik', 'team']), RolesGuard)
|
||||
@Roles(Role.LEITUNGSTEAM, Role.GEMEINDE_VERANTWORTLICHER)
|
||||
createGruppe(
|
||||
@Param('kcId') kcId: string,
|
||||
@Body() dto: CreateChannelDto,
|
||||
@Req() req: AuthenticatedRequest,
|
||||
) {
|
||||
return this.chat.createGruppe(kcId, dto.name, req.user!.userId, {
|
||||
userIds: dto.participantUserIds,
|
||||
guestIds: dto.participantGuestIds,
|
||||
});
|
||||
}
|
||||
|
||||
/// Candidates (team users + Konfis) a caller may add to a Gruppenchat in
|
||||
/// this KC. Allowed for LT or a Verantwortliche/r of this KC.
|
||||
@Get(':kcId/gruppen/participant-candidates')
|
||||
@UseGuards(AuthGuard(['authentik', 'team']))
|
||||
listPossibleParticipants(@Param('kcId') kcId: string, @Req() req: AuthenticatedRequest) {
|
||||
return this.chat.listPossibleParticipants(kcId, req.user!);
|
||||
}
|
||||
|
||||
/// Add a team user or Konfi to a Gruppenchat. Allowed for the channel's
|
||||
/// creator, any Leitungsteam member, or a Verantwortliche/r of that KC.
|
||||
@Post('gruppen/:channelId/participants')
|
||||
@UseGuards(AuthGuard(['authentik', 'team']))
|
||||
addParticipant(
|
||||
@Param('channelId') channelId: string,
|
||||
@Body() dto: AddParticipantDto,
|
||||
@Req() req: AuthenticatedRequest,
|
||||
) {
|
||||
return this.chat
|
||||
.addParticipant(
|
||||
channelId,
|
||||
{ kind: 'user', user: req.user! },
|
||||
{ userId: dto.userId, guestId: dto.guestId },
|
||||
)
|
||||
.then((result) => {
|
||||
this.gateway.notifyParticipantsChanged(channelId);
|
||||
return result;
|
||||
});
|
||||
}
|
||||
|
||||
/// Remove a team user or Konfi from a Gruppenchat. Same authorization as add.
|
||||
@Delete('gruppen/:channelId/participants')
|
||||
@UseGuards(AuthGuard(['authentik', 'team']))
|
||||
removeParticipant(
|
||||
@Param('channelId') channelId: string,
|
||||
@Body() dto: AddParticipantDto,
|
||||
@Req() req: AuthenticatedRequest,
|
||||
) {
|
||||
return this.chat
|
||||
.removeParticipant(
|
||||
channelId,
|
||||
{ kind: 'user', user: req.user! },
|
||||
{ userId: dto.userId, guestId: dto.guestId },
|
||||
)
|
||||
.then((result) => {
|
||||
this.gateway.notifyParticipantsChanged(channelId);
|
||||
return result;
|
||||
});
|
||||
}
|
||||
|
||||
/// Any two team members of the same KC can start a direct conversation
|
||||
/// (Authentik-backed members and local Gemeinde Teamer alike).
|
||||
@Post('direct')
|
||||
|
||||
@@ -106,4 +106,12 @@ export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Called by ChatController after add/removeParticipant so anyone with the
|
||||
/// channel already open (e.g. the creator's participant-management UI)
|
||||
/// gets a live update. Newly added participants join the room themselves
|
||||
/// via `chat:join` once they open the chat.
|
||||
notifyParticipantsChanged(channelId: string) {
|
||||
this.broadcast(channelId, { event: 'chat:participants-changed', data: { channelId } });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,280 @@
|
||||
import { BadRequestException, ForbiddenException, NotFoundException } from '@nestjs/common';
|
||||
import { ChatChannelType, Role } from '@prisma/client';
|
||||
import { ChatService } from './chat.service';
|
||||
import { AuthenticatedUser } from '../auth/authenticated-request';
|
||||
import { GuestJwtPayload } from '../auth/guest-auth.service';
|
||||
|
||||
/// Focus: GRUPPE channel creation + participant management authorization
|
||||
/// (creator / Leitungsteam / Verantwortliche/r of that KC), and read/write
|
||||
/// access for team users and guests. Prisma + Sync + Push faked in memory.
|
||||
|
||||
function userCaller(userId: string, memberships: AuthenticatedUser['memberships']) {
|
||||
return {
|
||||
kind: 'user' as const,
|
||||
user: { userId, authentikSub: `sub-${userId}`, email: `${userId}@example.org`, memberships },
|
||||
};
|
||||
}
|
||||
function guestCaller(guestId: string, kcId: string, gemeindeId: string | null = null) {
|
||||
const guest: GuestJwtPayload = { guestId, kcId, gemeindeId };
|
||||
return { kind: 'guest' as const, guest };
|
||||
}
|
||||
|
||||
const LT = userCaller('lt-1', [{ kcId: 'kc-1', gemeindeId: null, role: Role.LEITUNGSTEAM }]);
|
||||
const VERANTW = userCaller('ver-1', [
|
||||
{ kcId: 'kc-1', gemeindeId: 'gem-1', role: Role.GEMEINDE_VERANTWORTLICHER },
|
||||
]);
|
||||
const TEAMER = userCaller('teamer-1', [
|
||||
{ kcId: 'kc-1', gemeindeId: 'gem-1', role: Role.GEMEINDE_TEAMER },
|
||||
]);
|
||||
|
||||
function makeService(
|
||||
opts: {
|
||||
channels?: Record<string, any>;
|
||||
memberships?: { kcId: string; userId: string }[];
|
||||
guests?: { id: string; kcId: string }[];
|
||||
} = {},
|
||||
) {
|
||||
const channels: Record<string, any> = opts.channels ?? {};
|
||||
const memberships = opts.memberships ?? [];
|
||||
const guests = opts.guests ?? [];
|
||||
const participants: any[] = [];
|
||||
let participantSeq = 0;
|
||||
|
||||
const prisma = {
|
||||
chatChannel: {
|
||||
create: jest.fn(({ data, include }: any) => {
|
||||
const id = `chan-${Object.keys(channels).length + 1}`;
|
||||
const created = { id, ...data, participants: [] };
|
||||
if (data.participants?.create) {
|
||||
for (const p of data.participants.create) {
|
||||
const row = { id: `part-${++participantSeq}`, channelId: id, userId: null, guestAccountId: null, ...p };
|
||||
participants.push(row);
|
||||
created.participants.push(row);
|
||||
}
|
||||
}
|
||||
channels[id] = created;
|
||||
return Promise.resolve(include ? created : { id, ...data });
|
||||
}),
|
||||
findUnique: jest.fn(({ where, include }: any) => {
|
||||
const channel = channels[where.id];
|
||||
if (!channel) return Promise.resolve(null);
|
||||
if (include?.participants) {
|
||||
const seeded = participants.filter((p) => p.channelId === channel.id);
|
||||
const fallback = Array.isArray(channel.participants) ? channel.participants : [];
|
||||
return Promise.resolve({
|
||||
...channel,
|
||||
participants: seeded.length ? seeded : fallback,
|
||||
});
|
||||
}
|
||||
return Promise.resolve(channel);
|
||||
}),
|
||||
findMany: jest.fn().mockResolvedValue([]),
|
||||
},
|
||||
membership: {
|
||||
findMany: jest.fn(({ where }: any) => {
|
||||
const ids: string[] = where.userId.in;
|
||||
const rows = memberships.filter((m) => m.kcId === where.kcId && ids.includes(m.userId));
|
||||
const seen = new Set<string>();
|
||||
const distinct = rows.filter((r) => (seen.has(r.userId) ? false : (seen.add(r.userId), true)));
|
||||
return Promise.resolve(distinct);
|
||||
}),
|
||||
findFirst: jest.fn(({ where }: any) =>
|
||||
Promise.resolve(memberships.find((m) => m.kcId === where.kcId && m.userId === where.userId) ?? null),
|
||||
),
|
||||
count: jest.fn().mockResolvedValue(0),
|
||||
},
|
||||
guestAccount: {
|
||||
count: jest.fn(({ where }: any) =>
|
||||
Promise.resolve(guests.filter((g) => where.id.in.includes(g.id) && g.kcId === where.kcId).length),
|
||||
),
|
||||
findFirst: jest.fn(({ where }: any) =>
|
||||
Promise.resolve(guests.find((g) => g.id === where.id && g.kcId === where.kcId) ?? null),
|
||||
),
|
||||
},
|
||||
chatParticipant: {
|
||||
upsert: jest.fn(({ create }: any) => {
|
||||
const existing = participants.find(
|
||||
(p) =>
|
||||
p.channelId === create.channelId &&
|
||||
p.userId === (create.userId ?? null) &&
|
||||
p.guestAccountId === (create.guestAccountId ?? null),
|
||||
);
|
||||
if (existing) return Promise.resolve(existing);
|
||||
const row = { id: `part-${++participantSeq}`, userId: null, guestAccountId: null, ...create };
|
||||
participants.push(row);
|
||||
return Promise.resolve(row);
|
||||
}),
|
||||
findFirst: jest.fn(({ where }: any) =>
|
||||
Promise.resolve(
|
||||
participants.find(
|
||||
(p) =>
|
||||
p.channelId === where.channelId &&
|
||||
(where.userId === undefined || p.userId === where.userId) &&
|
||||
(where.guestAccountId === undefined || p.guestAccountId === where.guestAccountId),
|
||||
) ?? null,
|
||||
),
|
||||
),
|
||||
delete: jest.fn(({ where }: any) => {
|
||||
const idx = participants.findIndex((p) => p.id === where.id);
|
||||
const [removed] = participants.splice(idx, 1);
|
||||
return Promise.resolve(removed);
|
||||
}),
|
||||
},
|
||||
chatMessage: { create: jest.fn(), findMany: jest.fn() },
|
||||
};
|
||||
const sync = { capture: jest.fn().mockResolvedValue(undefined) };
|
||||
const push = { notifyChannel: jest.fn().mockResolvedValue(undefined) };
|
||||
const service = new ChatService(prisma as never, sync as never, push as never);
|
||||
return { service, prisma, sync, push, channels, participants };
|
||||
}
|
||||
|
||||
describe('ChatService.createGruppe', () => {
|
||||
it('creates a GRUPPE channel with the creator plus given team/guest participants', async () => {
|
||||
const { service, sync } = makeService({
|
||||
memberships: [{ kcId: 'kc-1', userId: 'ver-1' }, { kcId: 'kc-1', userId: 'teamer-1' }],
|
||||
guests: [{ id: 'guest-1', kcId: 'kc-1' }],
|
||||
});
|
||||
const channel = await service.createGruppe('kc-1', 'Ausflugsplanung', 'ver-1', {
|
||||
userIds: ['teamer-1'],
|
||||
guestIds: ['guest-1'],
|
||||
});
|
||||
expect(channel.type).toBe(ChatChannelType.GRUPPE);
|
||||
expect(channel.createdByUserId).toBe('ver-1');
|
||||
const userIds = channel.participants.map((p: any) => p.userId).filter(Boolean);
|
||||
const guestIds = channel.participants.map((p: any) => p.guestAccountId).filter(Boolean);
|
||||
expect(userIds.sort()).toEqual(['teamer-1', 'ver-1']);
|
||||
expect(guestIds).toEqual(['guest-1']);
|
||||
expect(sync.capture).toHaveBeenCalledWith('ChatChannel', 'CREATE', channel.id, expect.anything());
|
||||
});
|
||||
|
||||
it('does not duplicate the creator if already listed as a participant', async () => {
|
||||
const { service } = makeService({ memberships: [{ kcId: 'kc-1', userId: 'ver-1' }] });
|
||||
const channel = await service.createGruppe('kc-1', 'X', 'ver-1', { userIds: ['ver-1'] });
|
||||
const userIds = channel.participants.map((p: any) => p.userId).filter(Boolean);
|
||||
expect(userIds).toEqual(['ver-1']);
|
||||
});
|
||||
|
||||
it('rejects a participant who is not a member of the KC', async () => {
|
||||
const { service } = makeService({ memberships: [] });
|
||||
await expect(
|
||||
service.createGruppe('kc-1', 'X', 'ver-1', { userIds: ['ghost'] }),
|
||||
).rejects.toBeInstanceOf(BadRequestException);
|
||||
});
|
||||
|
||||
it('rejects a guest who is not part of the KC', async () => {
|
||||
const { service } = makeService({ guests: [{ id: 'guest-1', kcId: 'kc-2' }] });
|
||||
await expect(
|
||||
service.createGruppe('kc-1', 'X', 'ver-1', { guestIds: ['guest-1'] }),
|
||||
).rejects.toBeInstanceOf(BadRequestException);
|
||||
});
|
||||
});
|
||||
|
||||
describe('ChatService participant management', () => {
|
||||
function seedGruppe() {
|
||||
const channels = {
|
||||
'chan-1': { id: 'chan-1', kcId: 'kc-1', type: ChatChannelType.GRUPPE, createdByUserId: 'ver-1', gemeindeId: null },
|
||||
};
|
||||
return channels;
|
||||
}
|
||||
|
||||
it('lets the creator add a team user', async () => {
|
||||
const { service } = makeService({
|
||||
channels: seedGruppe(),
|
||||
memberships: [{ kcId: 'kc-1', userId: 'teamer-1' }],
|
||||
});
|
||||
const p = await service.addParticipant('chan-1', VERANTW, { userId: 'teamer-1' });
|
||||
expect(p.userId).toBe('teamer-1');
|
||||
});
|
||||
|
||||
it('lets a Leitungsteam member add a guest even if not the creator', async () => {
|
||||
const { service } = makeService({
|
||||
channels: seedGruppe(),
|
||||
guests: [{ id: 'guest-1', kcId: 'kc-1' }],
|
||||
});
|
||||
const p = await service.addParticipant('chan-1', LT, { guestId: 'guest-1' });
|
||||
expect(p.guestAccountId).toBe('guest-1');
|
||||
});
|
||||
|
||||
it('forbids a plain Teamer (not creator, not LT, not Verantwortliche/r) from managing participants', async () => {
|
||||
const { service } = makeService({ channels: seedGruppe() });
|
||||
await expect(
|
||||
service.addParticipant('chan-1', TEAMER, { userId: 'teamer-1' }),
|
||||
).rejects.toBeInstanceOf(ForbiddenException);
|
||||
});
|
||||
|
||||
it('forbids guests from managing participants', async () => {
|
||||
const { service } = makeService({ channels: seedGruppe() });
|
||||
await expect(
|
||||
service.addParticipant('chan-1', guestCaller('g-1', 'kc-1') as never, { userId: 'x' }),
|
||||
).rejects.toBeInstanceOf(ForbiddenException);
|
||||
});
|
||||
|
||||
it('404s for a non-GRUPPE channel', async () => {
|
||||
const channels = {
|
||||
'chan-2': { id: 'chan-2', kcId: 'kc-1', type: ChatChannelType.GEMEINDE_GRUPPE, createdByUserId: null },
|
||||
};
|
||||
const { service } = makeService({ channels });
|
||||
await expect(
|
||||
service.addParticipant('chan-2', LT, { userId: 'teamer-1' }),
|
||||
).rejects.toBeInstanceOf(NotFoundException);
|
||||
});
|
||||
|
||||
it('rejects adding a user not in the KC', async () => {
|
||||
const { service } = makeService({ channels: seedGruppe(), memberships: [] });
|
||||
await expect(
|
||||
service.addParticipant('chan-1', LT, { userId: 'ghost' }),
|
||||
).rejects.toBeInstanceOf(BadRequestException);
|
||||
});
|
||||
|
||||
it('removes a participant and is a no-op if already absent', async () => {
|
||||
const { service } = makeService({
|
||||
channels: seedGruppe(),
|
||||
memberships: [{ kcId: 'kc-1', userId: 'teamer-1' }],
|
||||
});
|
||||
await service.addParticipant('chan-1', VERANTW, { userId: 'teamer-1' });
|
||||
const res = await service.removeParticipant('chan-1', VERANTW, { userId: 'teamer-1' });
|
||||
expect(res).toEqual({ ok: true });
|
||||
const res2 = await service.removeParticipant('chan-1', VERANTW, { userId: 'teamer-1' });
|
||||
expect(res2).toEqual({ ok: true });
|
||||
});
|
||||
});
|
||||
|
||||
describe('ChatService GRUPPE read/write access', () => {
|
||||
function seedGruppeWithParticipants(participants: any[]) {
|
||||
return {
|
||||
'chan-1': {
|
||||
id: 'chan-1',
|
||||
kcId: 'kc-1',
|
||||
type: ChatChannelType.GRUPPE,
|
||||
createdByUserId: 'ver-1',
|
||||
gemeindeId: null,
|
||||
participants,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
it('lets a listed guest read messages', async () => {
|
||||
const { service } = makeService({
|
||||
channels: seedGruppeWithParticipants([{ userId: null, guestAccountId: 'guest-1' }]),
|
||||
});
|
||||
await expect(
|
||||
service.assertCanRead('chan-1', guestCaller('guest-1', 'kc-1') as never),
|
||||
).resolves.toBeDefined();
|
||||
});
|
||||
|
||||
it('forbids a guest not in the participant list', async () => {
|
||||
const { service } = makeService({
|
||||
channels: seedGruppeWithParticipants([{ userId: null, guestAccountId: 'guest-1' }]),
|
||||
});
|
||||
await expect(
|
||||
service.assertCanRead('chan-1', guestCaller('guest-2', 'kc-1') as never),
|
||||
).rejects.toBeInstanceOf(ForbiddenException);
|
||||
});
|
||||
|
||||
it('forbids a team user not in the participant list', async () => {
|
||||
const { service } = makeService({
|
||||
channels: seedGruppeWithParticipants([{ userId: 'someone-else', guestAccountId: null }]),
|
||||
});
|
||||
await expect(service.assertCanRead('chan-1', TEAMER)).rejects.toBeInstanceOf(ForbiddenException);
|
||||
});
|
||||
});
|
||||
+258
-7
@@ -1,19 +1,34 @@
|
||||
import { ForbiddenException, Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { BadRequestException, ForbiddenException, Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { ChatChannelType, Role, SyncOperation } from '@prisma/client';
|
||||
import { PrismaClient } from '../prisma/prisma.module';
|
||||
import { AuthenticatedUser } from '../auth/authenticated-request';
|
||||
import { GuestJwtPayload } from '../auth/guest-auth.service';
|
||||
import { SyncService } from '../sync/sync.service';
|
||||
import { PushService } from '../push/push.service';
|
||||
|
||||
export type ChatCaller =
|
||||
| { kind: 'user'; user: AuthenticatedUser }
|
||||
| { kind: 'guest'; guest: GuestJwtPayload };
|
||||
|
||||
const CHANNEL_TITLES: Record<ChatChannelType, string> = {
|
||||
[ChatChannelType.GEMEINDE_GRUPPE]: 'Gemeinde-Gruppe',
|
||||
[ChatChannelType.DIREKT]: 'Direktnachricht',
|
||||
[ChatChannelType.LT_UEBERGREIFEND]: 'Leitungsteam',
|
||||
[ChatChannelType.BROADCAST]: 'Ankündigung',
|
||||
[ChatChannelType.GRUPPE]: 'Gruppenchat',
|
||||
};
|
||||
|
||||
export interface CreateGruppeParticipants {
|
||||
userIds?: string[];
|
||||
guestIds?: string[];
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class ChatService {
|
||||
constructor(
|
||||
private readonly prisma: PrismaClient,
|
||||
private readonly sync: SyncService,
|
||||
private readonly push: PushService,
|
||||
) {}
|
||||
|
||||
async createChannel(kcId: string, type: ChatChannelType, gemeindeId?: string) {
|
||||
@@ -22,6 +37,206 @@ export class ChatService {
|
||||
return channel;
|
||||
}
|
||||
|
||||
/// Free-form group chat: created by a Leitungsteam member (any KC) or a
|
||||
/// Gemeinde Verantwortliche/r (their own KC — enforced by the RolesGuard's
|
||||
/// kcId scoping at the controller level). Konfis (guests) may be included
|
||||
/// directly, unlike DIREKT/GEMEINDE_GRUPPE channels which are team-only.
|
||||
async createGruppe(
|
||||
kcId: string,
|
||||
name: string | undefined,
|
||||
createdByUserId: string,
|
||||
participants: CreateGruppeParticipants,
|
||||
) {
|
||||
const userIds = [...new Set(participants.userIds ?? [])];
|
||||
const guestIds = [...new Set(participants.guestIds ?? [])];
|
||||
|
||||
if (userIds.length) {
|
||||
// A user may show up under more than one Gemeinde membership; just
|
||||
// make sure every requested id resolves to at least one row for this KC.
|
||||
const distinctUsers = await this.prisma.membership.findMany({
|
||||
where: { kcId, userId: { in: userIds } },
|
||||
select: { userId: true },
|
||||
distinct: ['userId'],
|
||||
});
|
||||
if (distinctUsers.length !== userIds.length) {
|
||||
throw new BadRequestException('One or more users are not part of this KC');
|
||||
}
|
||||
}
|
||||
if (guestIds.length) {
|
||||
const guestCount = await this.prisma.guestAccount.count({
|
||||
where: { id: { in: guestIds }, kcId },
|
||||
});
|
||||
if (guestCount !== guestIds.length) {
|
||||
throw new BadRequestException('One or more guests are not part of this KC');
|
||||
}
|
||||
}
|
||||
|
||||
const channel = await this.prisma.chatChannel.create({
|
||||
data: {
|
||||
kcId,
|
||||
type: ChatChannelType.GRUPPE,
|
||||
name,
|
||||
createdByUserId,
|
||||
participants: {
|
||||
create: [
|
||||
...(userIds.includes(createdByUserId) ? [] : [{ userId: createdByUserId }]),
|
||||
...userIds.map((userId) => ({ userId })),
|
||||
...guestIds.map((guestAccountId) => ({ guestAccountId })),
|
||||
],
|
||||
},
|
||||
},
|
||||
include: { participants: true },
|
||||
});
|
||||
await this.sync.capture('ChatChannel', SyncOperation.CREATE, channel.id, channel);
|
||||
return channel;
|
||||
}
|
||||
|
||||
/// Candidates a caller may add to a GRUPPE channel in this KC: every team
|
||||
/// member (any Gemeinde) plus every Konfi/guest, so a Verantwortliche/r can
|
||||
/// pick across Gemeinde boundaries as intended. Same authorization as
|
||||
/// creating a Gruppenchat (LT or Verantwortliche/r of this KC).
|
||||
async listPossibleParticipants(kcId: string, caller: AuthenticatedUser) {
|
||||
const isLt = caller.memberships.some((m) => m.role === Role.LEITUNGSTEAM);
|
||||
const isVerantwortlicherHere = caller.memberships.some(
|
||||
(m) => m.kcId === kcId && m.role === Role.GEMEINDE_VERANTWORTLICHER,
|
||||
);
|
||||
if (!isLt && !isVerantwortlicherHere) {
|
||||
throw new ForbiddenException('Not allowed to list participants for this KC');
|
||||
}
|
||||
|
||||
const [memberships, guests] = await Promise.all([
|
||||
this.prisma.membership.findMany({
|
||||
where: { kcId, status: 'ACTIVE' },
|
||||
include: { user: { select: { id: true, firstName: true, lastName: true, email: true } } },
|
||||
orderBy: { user: { lastName: 'asc' } },
|
||||
}),
|
||||
this.prisma.guestAccount.findMany({
|
||||
where: { kcId },
|
||||
select: { id: true, firstName: true, lastName: true, gemeindeId: true },
|
||||
orderBy: { lastName: 'asc' },
|
||||
}),
|
||||
]);
|
||||
|
||||
const seenUsers = new Set<string>();
|
||||
const users = [];
|
||||
for (const m of memberships) {
|
||||
if (seenUsers.has(m.userId)) continue;
|
||||
seenUsers.add(m.userId);
|
||||
users.push({
|
||||
userId: m.user.id,
|
||||
firstName: m.user.firstName,
|
||||
lastName: m.user.lastName,
|
||||
email: m.user.email,
|
||||
role: m.role,
|
||||
gemeindeId: m.gemeindeId,
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
users,
|
||||
guests: guests.map((g) => ({
|
||||
guestId: g.id,
|
||||
firstName: g.firstName,
|
||||
lastName: g.lastName,
|
||||
gemeindeId: g.gemeindeId,
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
/// Adds a team user or a guest/Konfi to an existing GRUPPE channel. Only
|
||||
/// the channel's creator or a Leitungsteam member may manage participants.
|
||||
async addParticipant(
|
||||
channelId: string,
|
||||
caller: ChatCaller,
|
||||
target: { userId?: string; guestId?: string },
|
||||
) {
|
||||
const channel = await this.getGruppeForManagementOrThrow(channelId, caller);
|
||||
|
||||
if (!target.userId && !target.guestId) {
|
||||
throw new BadRequestException('userId or guestId is required');
|
||||
}
|
||||
if (target.userId && target.guestId) {
|
||||
throw new BadRequestException('Provide either userId or guestId, not both');
|
||||
}
|
||||
|
||||
if (target.userId) {
|
||||
const isMember = await this.prisma.membership.findFirst({
|
||||
where: { kcId: channel.kcId, userId: target.userId },
|
||||
});
|
||||
if (!isMember) {
|
||||
throw new BadRequestException('User is not part of this KC');
|
||||
}
|
||||
const participant = await this.prisma.chatParticipant.upsert({
|
||||
where: { channelId_userId: { channelId, userId: target.userId } },
|
||||
create: { channelId, userId: target.userId },
|
||||
update: {},
|
||||
});
|
||||
await this.sync.capture('ChatParticipant', SyncOperation.CREATE, participant.id, participant);
|
||||
return participant;
|
||||
}
|
||||
|
||||
const guest = await this.prisma.guestAccount.findFirst({
|
||||
where: { id: target.guestId, kcId: channel.kcId },
|
||||
});
|
||||
if (!guest) {
|
||||
throw new BadRequestException('Guest is not part of this KC');
|
||||
}
|
||||
const participant = await this.prisma.chatParticipant.upsert({
|
||||
where: { channelId_guestAccountId: { channelId, guestAccountId: target.guestId! } },
|
||||
create: { channelId, guestAccountId: target.guestId },
|
||||
update: {},
|
||||
});
|
||||
await this.sync.capture('ChatParticipant', SyncOperation.CREATE, participant.id, participant);
|
||||
return participant;
|
||||
}
|
||||
|
||||
/// Removes a team user or a guest/Konfi from a GRUPPE channel. Same
|
||||
/// authorization as addParticipant.
|
||||
async removeParticipant(
|
||||
channelId: string,
|
||||
caller: ChatCaller,
|
||||
target: { userId?: string; guestId?: string },
|
||||
) {
|
||||
await this.getGruppeForManagementOrThrow(channelId, caller);
|
||||
|
||||
if (!target.userId && !target.guestId) {
|
||||
throw new BadRequestException('userId or guestId is required');
|
||||
}
|
||||
|
||||
const existing = await this.prisma.chatParticipant.findFirst({
|
||||
where: {
|
||||
channelId,
|
||||
userId: target.userId ?? undefined,
|
||||
guestAccountId: target.guestId ?? undefined,
|
||||
},
|
||||
});
|
||||
if (!existing) return { ok: true };
|
||||
|
||||
await this.prisma.chatParticipant.delete({ where: { id: existing.id } });
|
||||
await this.sync.capture('ChatParticipant', SyncOperation.DELETE, existing.id, { id: existing.id });
|
||||
return { ok: true };
|
||||
}
|
||||
|
||||
private async getGruppeForManagementOrThrow(channelId: string, caller: ChatCaller) {
|
||||
if (caller.kind !== 'user') {
|
||||
throw new ForbiddenException('Guests may not manage channel participants');
|
||||
}
|
||||
const channel = await this.prisma.chatChannel.findUnique({ where: { id: channelId } });
|
||||
if (!channel || channel.type !== ChatChannelType.GRUPPE) {
|
||||
throw new NotFoundException('Gruppenchat not found');
|
||||
}
|
||||
const { user } = caller;
|
||||
const isLt = user.memberships.some((m) => m.role === Role.LEITUNGSTEAM);
|
||||
const isCreator = channel.createdByUserId === user.userId;
|
||||
const isVerantwortlicherHere = user.memberships.some(
|
||||
(m) => m.kcId === channel.kcId && m.role === Role.GEMEINDE_VERANTWORTLICHER,
|
||||
);
|
||||
if (!isLt && !isCreator && !isVerantwortlicherHere) {
|
||||
throw new ForbiddenException('Not allowed to manage this Gruppenchat');
|
||||
}
|
||||
return channel;
|
||||
}
|
||||
|
||||
async getOrCreateDirectChannel(kcId: string, userAId: string, userBId: string) {
|
||||
const existing = await this.prisma.chatChannel.findFirst({
|
||||
where: {
|
||||
@@ -48,7 +263,13 @@ export class ChatService {
|
||||
async listChannelsForCaller(kcId: string, caller: ChatCaller) {
|
||||
if (caller.kind === 'guest') {
|
||||
return this.prisma.chatChannel.findMany({
|
||||
where: { kcId, type: ChatChannelType.BROADCAST },
|
||||
where: {
|
||||
kcId,
|
||||
OR: [
|
||||
{ type: ChatChannelType.BROADCAST },
|
||||
{ type: ChatChannelType.GRUPPE, participants: { some: { guestAccountId: caller.guest.guestId } } },
|
||||
],
|
||||
},
|
||||
});
|
||||
}
|
||||
const { user } = caller;
|
||||
@@ -66,6 +287,7 @@ export class ChatService {
|
||||
{ type: ChatChannelType.BROADCAST },
|
||||
{ type: ChatChannelType.GEMEINDE_GRUPPE, gemeindeId: { in: gemeindeIds } },
|
||||
{ type: ChatChannelType.DIREKT, participants: { some: { userId: user.userId } } },
|
||||
{ type: ChatChannelType.GRUPPE, participants: { some: { userId: user.userId } } },
|
||||
],
|
||||
},
|
||||
});
|
||||
@@ -93,15 +315,23 @@ export class ChatService {
|
||||
}
|
||||
|
||||
if (caller.kind === 'guest') {
|
||||
const allowed = channel.type === ChatChannelType.BROADCAST && mode === 'read';
|
||||
if (!allowed) {
|
||||
throw new ForbiddenException('Guests may only read broadcast channels');
|
||||
}
|
||||
if (channel.type === ChatChannelType.BROADCAST && mode === 'read') {
|
||||
if (caller.guest.kcId !== channel.kcId) {
|
||||
throw new ForbiddenException('Guest does not belong to this KC');
|
||||
}
|
||||
return channel;
|
||||
}
|
||||
if (channel.type === ChatChannelType.GRUPPE) {
|
||||
const isParticipant = channel.participants.some(
|
||||
(p) => p.guestAccountId === caller.guest.guestId,
|
||||
);
|
||||
if (!isParticipant) {
|
||||
throw new ForbiddenException('Not a participant of this Gruppenchat');
|
||||
}
|
||||
return channel;
|
||||
}
|
||||
throw new ForbiddenException('Guests may only read broadcast channels or their Gruppenchats');
|
||||
}
|
||||
|
||||
const { user } = caller;
|
||||
const isLt = user.memberships.some((m) => m.role === Role.LEITUNGSTEAM);
|
||||
@@ -136,13 +366,20 @@ export class ChatService {
|
||||
}
|
||||
return channel;
|
||||
}
|
||||
case ChatChannelType.GRUPPE: {
|
||||
const isParticipant = channel.participants.some((p) => p.userId === user.userId);
|
||||
if (!isParticipant) {
|
||||
throw new ForbiddenException('Not a participant of this Gruppenchat');
|
||||
}
|
||||
return channel;
|
||||
}
|
||||
default:
|
||||
throw new ForbiddenException('Unknown channel type');
|
||||
}
|
||||
}
|
||||
|
||||
async sendMessage(channelId: string, caller: ChatCaller, body: string) {
|
||||
await this.assertCanWrite(channelId, caller);
|
||||
const channel = await this.assertCanWrite(channelId, caller);
|
||||
const message = await this.prisma.chatMessage.create({
|
||||
data: {
|
||||
channelId,
|
||||
@@ -152,6 +389,20 @@ export class ChatService {
|
||||
},
|
||||
});
|
||||
await this.sync.capture('ChatMessage', SyncOperation.CREATE, message.id, message);
|
||||
|
||||
void this.push.notifyChannel(
|
||||
channelId,
|
||||
{
|
||||
title: CHANNEL_TITLES[channel?.type ?? ChatChannelType.GEMEINDE_GRUPPE],
|
||||
body: body.length > 140 ? `${body.slice(0, 137)}…` : body,
|
||||
data: { channelId },
|
||||
},
|
||||
{
|
||||
userId: caller.kind === 'user' ? caller.user.userId : null,
|
||||
guestId: caller.kind === 'guest' ? caller.guest.guestId : null,
|
||||
},
|
||||
);
|
||||
|
||||
return message;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
import { IsOptional, IsString } from 'class-validator';
|
||||
|
||||
/// Exactly one of userId/guestId must be set; validated in the service since
|
||||
/// class-validator doesn't express "exactly one of" declaratively.
|
||||
export class AddParticipantDto {
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
userId?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
guestId?: string;
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
import { IsEnum, IsOptional, IsString } from 'class-validator';
|
||||
import { ArrayUnique, IsArray, IsEnum, IsOptional, IsString } from 'class-validator';
|
||||
import { ChatChannelType } from '@prisma/client';
|
||||
|
||||
export class CreateChannelDto {
|
||||
@@ -8,4 +8,24 @@ export class CreateChannelDto {
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
gemeindeId?: string;
|
||||
|
||||
/// Display name; used for GRUPPE channels.
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
name?: string;
|
||||
|
||||
/// Initial participants for a GRUPPE channel (team users). More can be
|
||||
/// added/removed later via the participants endpoints.
|
||||
@IsOptional()
|
||||
@IsArray()
|
||||
@ArrayUnique()
|
||||
@IsString({ each: true })
|
||||
participantUserIds?: string[];
|
||||
|
||||
/// Initial guest/Konfi participants for a GRUPPE channel.
|
||||
@IsOptional()
|
||||
@IsArray()
|
||||
@ArrayUnique()
|
||||
@IsString({ each: true })
|
||||
participantGuestIds?: string[];
|
||||
}
|
||||
|
||||
@@ -34,7 +34,7 @@ export class FilesController {
|
||||
|
||||
/// Only the Leitungsteam uploads files (per KC), tagged with a visibility tier.
|
||||
@Post(':kcId')
|
||||
@UseGuards(AuthGuard('authentik'), RolesGuard)
|
||||
@UseGuards(AuthGuard(['authentik', 'team']), RolesGuard)
|
||||
@Roles(Role.LEITUNGSTEAM)
|
||||
@UseInterceptors(FileInterceptor('file'))
|
||||
upload(
|
||||
|
||||
+49
-1
@@ -3,11 +3,59 @@ import { ValidationPipe } from '@nestjs/common';
|
||||
import { WsAdapter } from '@nestjs/platform-ws';
|
||||
import { AppModule } from './app.module';
|
||||
|
||||
function parseAllowedOrigins(): string[] {
|
||||
const envOrigins = process.env.ALLOWED_ORIGINS || process.env.CORS_ORIGIN;
|
||||
if (envOrigins) {
|
||||
return envOrigins
|
||||
.split(',')
|
||||
.map((o) => o.trim())
|
||||
.filter(Boolean);
|
||||
}
|
||||
|
||||
const defaultOrigins: string[] = [
|
||||
'http://localhost:3000',
|
||||
'http://localhost:3010',
|
||||
'http://localhost:8080',
|
||||
'http://127.0.0.1:3000',
|
||||
'http://127.0.0.1:3010',
|
||||
'http://127.0.0.1:8080',
|
||||
];
|
||||
|
||||
if (process.env.APP_BASE_URL) {
|
||||
try {
|
||||
const parsed = new URL(process.env.APP_BASE_URL);
|
||||
if (!defaultOrigins.includes(parsed.origin)) {
|
||||
defaultOrigins.push(parsed.origin);
|
||||
}
|
||||
} catch {
|
||||
const trimmed = process.env.APP_BASE_URL.trim();
|
||||
if (!defaultOrigins.includes(trimmed)) {
|
||||
defaultOrigins.push(trimmed);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return defaultOrigins;
|
||||
}
|
||||
|
||||
async function bootstrap() {
|
||||
const app = await NestFactory.create(AppModule);
|
||||
app.setGlobalPrefix('api');
|
||||
app.useGlobalPipes(new ValidationPipe({ whitelist: true, transform: true }));
|
||||
app.enableCors();
|
||||
|
||||
const allowedOrigins = parseAllowedOrigins();
|
||||
app.enableCors({
|
||||
origin: (origin, callback) => {
|
||||
// Allow requests with no origin (e.g. mobile apps, curl, same-origin)
|
||||
if (!origin || allowedOrigins.includes(origin)) {
|
||||
callback(null, true);
|
||||
} else {
|
||||
callback(new Error('Not allowed by CORS'));
|
||||
}
|
||||
},
|
||||
credentials: true,
|
||||
});
|
||||
|
||||
app.useWebSocketAdapter(new WsAdapter(app));
|
||||
await app.listen(process.env.PORT ?? 3000);
|
||||
}
|
||||
|
||||
@@ -43,6 +43,17 @@ export class OnboardingController {
|
||||
);
|
||||
}
|
||||
|
||||
/// Redeems a Leitungsteam-issued Verantwortliche invite: immediately
|
||||
/// ACTIVE membership, no approval step (unlike self-registration above).
|
||||
/// Authenticated by the caller's raw Authentik bearer token.
|
||||
@Post('verantwortliche-invites/:token/redeem')
|
||||
redeemVerantwortlicheInvite(
|
||||
@Param('token') token: string,
|
||||
@Headers('authorization') authorization?: string,
|
||||
) {
|
||||
return this.onboarding.redeemInvite(bearer(authorization), token);
|
||||
}
|
||||
|
||||
/// Leitungsteam: review and act on pending self-registrations.
|
||||
@Get('requests')
|
||||
@UseGuards(AuthGuard(['authentik', 'team']), RolesGuard)
|
||||
|
||||
@@ -39,6 +39,11 @@ function makeService(seed: {
|
||||
? { id: 'kc-1', name: 'KC 2026', inviteCode: 'code-1', isActive: true, gemeinden: [] }
|
||||
: seed.kc,
|
||||
),
|
||||
findFirst: jest.fn().mockResolvedValue(
|
||||
seed.kc === undefined
|
||||
? { id: 'kc-1', name: 'KC 2026', inviteCode: 'code-1', isActive: true, gemeinden: [] }
|
||||
: seed.kc,
|
||||
),
|
||||
},
|
||||
gemeinde: {
|
||||
findUnique: jest.fn().mockResolvedValue(
|
||||
@@ -90,12 +95,14 @@ describe('OnboardingService.resolveInvite', () => {
|
||||
});
|
||||
|
||||
it('returns the KC name and its Gemeinden', async () => {
|
||||
const { service, prisma } = makeService({});
|
||||
prisma.kc.findUnique = jest.fn().mockResolvedValue({
|
||||
const { service } = makeService({
|
||||
kc: {
|
||||
id: 'kc-1',
|
||||
name: 'KC 2026',
|
||||
inviteCode: 'code-1',
|
||||
isActive: true,
|
||||
gemeinden: [{ id: 'gem-1', name: 'Nord' }],
|
||||
} as never,
|
||||
});
|
||||
await expect(service.resolveInvite('code-1')).resolves.toEqual({
|
||||
kcId: 'kc-1',
|
||||
|
||||
@@ -5,16 +5,15 @@ import {
|
||||
UnauthorizedException,
|
||||
} from '@nestjs/common';
|
||||
import { MembershipStatus, Role, SyncOperation } from '@prisma/client';
|
||||
import { randomBytes } from 'crypto';
|
||||
import { PrismaClient } from '../prisma/prisma.module';
|
||||
import { SyncService } from '../sync/sync.service';
|
||||
import { TokenVerificationService } from '../auth/token-verification.service';
|
||||
import { resolveOrProvisionAuthentikUser } from '../auth/provision-user';
|
||||
import { AuthenticatedUser } from '../auth/authenticated-request';
|
||||
|
||||
/// Self-service onboarding for Gemeinde Verantwortliche. The person signs in
|
||||
/// with their Konfi-Castle-ID (Authentik) and submits a KC invite code plus
|
||||
/// the Gemeinde they belong to; this provisions their local User (JIT) and a
|
||||
/// PENDING membership that a Leitungsteam member must approve before it grants
|
||||
/// any rights.
|
||||
/// Self-service onboarding for Gemeinde Verantwortliche, plus the
|
||||
/// Leitungsteam-initiated shortcut that skips the approval step entirely.
|
||||
@Injectable()
|
||||
export class OnboardingService {
|
||||
constructor(
|
||||
@@ -26,12 +25,20 @@ export class OnboardingService {
|
||||
/// Public: resolves an invite code to the KC name and its Gemeinden so the
|
||||
/// registrant can pick theirs. The code itself is the shared secret.
|
||||
async resolveInvite(inviteCode: string) {
|
||||
const kc = await this.prisma.kc.findUnique({
|
||||
where: { inviteCode },
|
||||
const trimmed = inviteCode.trim();
|
||||
const kc =
|
||||
(await this.prisma.kc.findFirst({
|
||||
where: { inviteCode: { equals: trimmed, mode: 'insensitive' } },
|
||||
include: {
|
||||
gemeinden: { select: { id: true, name: true }, orderBy: { name: 'asc' } },
|
||||
},
|
||||
});
|
||||
})) ||
|
||||
(await this.prisma.kc.findUnique({
|
||||
where: { inviteCode: trimmed },
|
||||
include: {
|
||||
gemeinden: { select: { id: true, name: true }, orderBy: { name: 'asc' } },
|
||||
},
|
||||
}));
|
||||
if (!kc || !kc.isActive) {
|
||||
throw new NotFoundException('Unknown or inactive KC invite code');
|
||||
}
|
||||
@@ -44,7 +51,12 @@ export class OnboardingService {
|
||||
}
|
||||
const { isLeitungsteam, ...claims } = await this.tokens.verifyAuthentikClaims(token);
|
||||
|
||||
const kc = await this.prisma.kc.findUnique({ where: { inviteCode } });
|
||||
const trimmed = inviteCode.trim();
|
||||
const kc =
|
||||
(await this.prisma.kc.findFirst({
|
||||
where: { inviteCode: { equals: trimmed, mode: 'insensitive' } },
|
||||
})) ||
|
||||
(await this.prisma.kc.findUnique({ where: { inviteCode: trimmed } }));
|
||||
if (!kc || !kc.isActive) {
|
||||
throw new NotFoundException('Unknown or inactive KC invite code');
|
||||
}
|
||||
@@ -133,4 +145,145 @@ export class OnboardingService {
|
||||
) {
|
||||
return { membershipId, status, kcName, gemeindeName };
|
||||
}
|
||||
|
||||
// --- Leitungsteam-issued Verantwortliche invites ---
|
||||
// Skips the PENDING approval step: an LT member vouching for someone
|
||||
// directly is enough, unlike self-registration which needs review.
|
||||
|
||||
async createInvite(
|
||||
caller: AuthenticatedUser,
|
||||
gemeindeId: string,
|
||||
dto: { email?: string; maxUses?: number; expiresInHours?: number },
|
||||
) {
|
||||
if (!caller.memberships.some((m) => m.role === Role.LEITUNGSTEAM)) {
|
||||
throw new UnauthorizedException('Only Leitungsteam can issue this invite');
|
||||
}
|
||||
const gemeinde = await this.prisma.gemeinde.findUnique({ where: { id: gemeindeId } });
|
||||
if (!gemeinde) {
|
||||
throw new NotFoundException('Gemeinde not found');
|
||||
}
|
||||
const email = dto.email?.toLowerCase() ?? null;
|
||||
const maxUses = dto.maxUses ?? (email ? 1 : null);
|
||||
const expiresAt = dto.expiresInHours
|
||||
? new Date(Date.now() + dto.expiresInHours * 3600_000)
|
||||
: null;
|
||||
|
||||
const invite = await this.prisma.verantwortlicheInvite.create({
|
||||
data: {
|
||||
kcId: gemeinde.kcId,
|
||||
gemeindeId,
|
||||
token: randomBytes(24).toString('base64url'),
|
||||
email,
|
||||
maxUses,
|
||||
expiresAt,
|
||||
createdByUserId: caller.userId,
|
||||
},
|
||||
});
|
||||
await this.sync.capture('VerantwortlicheInvite', SyncOperation.CREATE, invite.id, invite);
|
||||
return invite;
|
||||
}
|
||||
|
||||
async listInvites(caller: AuthenticatedUser, gemeindeId: string) {
|
||||
if (!caller.memberships.some((m) => m.role === Role.LEITUNGSTEAM)) {
|
||||
throw new UnauthorizedException('Only Leitungsteam can view this');
|
||||
}
|
||||
return this.prisma.verantwortlicheInvite.findMany({
|
||||
where: { gemeindeId },
|
||||
orderBy: { createdAt: 'desc' },
|
||||
});
|
||||
}
|
||||
|
||||
async revokeInvite(caller: AuthenticatedUser, gemeindeId: string, inviteId: string) {
|
||||
if (!caller.memberships.some((m) => m.role === Role.LEITUNGSTEAM)) {
|
||||
throw new UnauthorizedException('Only Leitungsteam can revoke this');
|
||||
}
|
||||
const invite = await this.prisma.verantwortlicheInvite.findFirst({
|
||||
where: { id: inviteId, gemeindeId },
|
||||
});
|
||||
if (!invite) {
|
||||
throw new NotFoundException('Invite not found');
|
||||
}
|
||||
const updated = await this.prisma.verantwortlicheInvite.update({
|
||||
where: { id: inviteId },
|
||||
data: { revokedAt: new Date() },
|
||||
});
|
||||
await this.sync.capture('VerantwortlicheInvite', SyncOperation.UPDATE, updated.id, updated);
|
||||
return updated;
|
||||
}
|
||||
|
||||
/// Redeems an LT-issued invite: provisions/updates the caller's Authentik
|
||||
/// User and grants an immediately-ACTIVE GEMEINDE_VERANTWORTLICHER
|
||||
/// membership (no approval step, unlike self-registration).
|
||||
async redeemInvite(token: string | undefined, inviteToken: string) {
|
||||
if (!token) {
|
||||
throw new UnauthorizedException('Missing Authentik bearer token');
|
||||
}
|
||||
const invite = await this.prisma.verantwortlicheInvite.findUnique({
|
||||
where: { token: inviteToken },
|
||||
});
|
||||
if (!invite || invite.revokedAt) {
|
||||
throw new NotFoundException('Unknown or revoked invite');
|
||||
}
|
||||
if (invite.expiresAt && invite.expiresAt.getTime() < Date.now()) {
|
||||
throw new BadRequestException('Invite has expired');
|
||||
}
|
||||
if (invite.maxUses !== null && invite.usedCount >= invite.maxUses) {
|
||||
throw new BadRequestException('Invite has already been used up');
|
||||
}
|
||||
|
||||
const { isLeitungsteam, ...claims } = await this.tokens.verifyAuthentikClaims(token);
|
||||
if (invite.email && invite.email !== claims.email.toLowerCase()) {
|
||||
throw new BadRequestException('This invite is pinned to a different account');
|
||||
}
|
||||
|
||||
const user = await resolveOrProvisionAuthentikUser(
|
||||
this.prisma,
|
||||
this.sync,
|
||||
claims,
|
||||
isLeitungsteam,
|
||||
);
|
||||
|
||||
const existing = await this.prisma.membership.findUnique({
|
||||
where: {
|
||||
userId_kcId_gemeindeId: {
|
||||
userId: user.id,
|
||||
kcId: invite.kcId,
|
||||
gemeindeId: invite.gemeindeId,
|
||||
},
|
||||
},
|
||||
});
|
||||
const membership = existing
|
||||
? await this.prisma.membership.update({
|
||||
where: { id: existing.id },
|
||||
data: { status: MembershipStatus.ACTIVE, role: Role.GEMEINDE_VERANTWORTLICHER },
|
||||
})
|
||||
: await this.prisma.membership.create({
|
||||
data: {
|
||||
userId: user.id,
|
||||
kcId: invite.kcId,
|
||||
gemeindeId: invite.gemeindeId,
|
||||
role: Role.GEMEINDE_VERANTWORTLICHER,
|
||||
status: MembershipStatus.ACTIVE,
|
||||
},
|
||||
});
|
||||
await this.sync.capture(
|
||||
'Membership',
|
||||
existing ? SyncOperation.UPDATE : SyncOperation.CREATE,
|
||||
membership.id,
|
||||
membership,
|
||||
);
|
||||
|
||||
const updatedInvite = await this.prisma.verantwortlicheInvite.update({
|
||||
where: { id: invite.id },
|
||||
data: { usedCount: { increment: 1 } },
|
||||
});
|
||||
await this.sync.capture(
|
||||
'VerantwortlicheInvite',
|
||||
SyncOperation.UPDATE,
|
||||
updatedInvite.id,
|
||||
updatedInvite,
|
||||
);
|
||||
|
||||
return { membershipId: membership.id, status: membership.status };
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
import { IsIn, IsNotEmpty, IsString } from 'class-validator';
|
||||
|
||||
export class RegisterDeviceDto {
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
token!: string;
|
||||
|
||||
@IsIn(['web', 'android', 'ios'])
|
||||
platform!: 'web' | 'android' | 'ios';
|
||||
}
|
||||
|
||||
export class UnregisterDeviceDto {
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
token!: string;
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
import { readFileSync } from 'fs';
|
||||
import { Logger } from '@nestjs/common';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import * as jwt from 'jsonwebtoken';
|
||||
import { PushNotification, PushProvider, PushResult } from './push-provider';
|
||||
|
||||
interface ServiceAccount {
|
||||
client_email: string;
|
||||
private_key: string;
|
||||
token_uri: string;
|
||||
}
|
||||
|
||||
/// Firebase Cloud Messaging HTTP v1. Auth is a service-account JWT exchanged
|
||||
/// for an OAuth access token (no google-auth-library dependency — jsonwebtoken
|
||||
/// is already here). Delivery failures are logged and swallowed.
|
||||
export class FcmPushProvider implements PushProvider {
|
||||
private readonly logger = new Logger('PushProvider');
|
||||
private readonly projectId: string;
|
||||
private readonly sa: ServiceAccount;
|
||||
private accessToken: { value: string; expiresAt: number } | null = null;
|
||||
|
||||
constructor(config: ConfigService) {
|
||||
this.projectId = config.getOrThrow<string>('FCM_PROJECT_ID');
|
||||
const path = config.getOrThrow<string>('GOOGLE_APPLICATION_CREDENTIALS');
|
||||
this.sa = JSON.parse(readFileSync(path, 'utf8')) as ServiceAccount;
|
||||
}
|
||||
|
||||
async sendToTokens(tokens: string[], n: PushNotification): Promise<PushResult> {
|
||||
if (tokens.length === 0) return { sent: 0, invalidTokens: [] };
|
||||
let accessToken: string;
|
||||
try {
|
||||
accessToken = await this.getAccessToken();
|
||||
} catch (err) {
|
||||
this.logger.error(`FCM auth failed: ${(err as Error).message}`);
|
||||
return { sent: 0, invalidTokens: [] };
|
||||
}
|
||||
|
||||
const url = `https://fcm.googleapis.com/v1/projects/${this.projectId}/messages:send`;
|
||||
const invalidTokens: string[] = [];
|
||||
let sent = 0;
|
||||
|
||||
await Promise.all(
|
||||
tokens.map(async (token) => {
|
||||
try {
|
||||
const res = await fetch(url, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
Authorization: `Bearer ${accessToken}`,
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
message: {
|
||||
token,
|
||||
notification: { title: n.title, body: n.body },
|
||||
data: n.data,
|
||||
webpush: { fcmOptions: {} },
|
||||
},
|
||||
}),
|
||||
});
|
||||
if (res.ok) {
|
||||
sent += 1;
|
||||
} else if (res.status === 404 || res.status === 400) {
|
||||
invalidTokens.push(token);
|
||||
} else {
|
||||
this.logger.warn(`FCM send ${res.status}: ${await res.text()}`);
|
||||
}
|
||||
} catch (err) {
|
||||
this.logger.warn(`FCM send error: ${(err as Error).message}`);
|
||||
}
|
||||
}),
|
||||
);
|
||||
|
||||
return { sent, invalidTokens };
|
||||
}
|
||||
|
||||
private async getAccessToken(): Promise<string> {
|
||||
if (this.accessToken && this.accessToken.expiresAt > Date.now() + 60_000) {
|
||||
return this.accessToken.value;
|
||||
}
|
||||
const now = Math.floor(Date.now() / 1000);
|
||||
const assertion = jwt.sign(
|
||||
{
|
||||
iss: this.sa.client_email,
|
||||
scope: 'https://www.googleapis.com/auth/firebase.messaging',
|
||||
aud: this.sa.token_uri,
|
||||
iat: now,
|
||||
exp: now + 3600,
|
||||
},
|
||||
this.sa.private_key,
|
||||
{ algorithm: 'RS256' },
|
||||
);
|
||||
const res = await fetch(this.sa.token_uri, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
||||
body: new URLSearchParams({
|
||||
grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer',
|
||||
assertion,
|
||||
}),
|
||||
});
|
||||
if (!res.ok) {
|
||||
throw new Error(`token exchange ${res.status}: ${await res.text()}`);
|
||||
}
|
||||
const json = (await res.json()) as { access_token: string; expires_in: number };
|
||||
this.accessToken = {
|
||||
value: json.access_token,
|
||||
expiresAt: Date.now() + json.expires_in * 1000,
|
||||
};
|
||||
return json.access_token;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
import { Logger } from '@nestjs/common';
|
||||
import { PushNotification, PushProvider, PushResult } from './push-provider';
|
||||
|
||||
/// Default provider: doesn't send, just logs. Keeps the app working before
|
||||
/// FCM credentials are configured.
|
||||
export class LogPushProvider implements PushProvider {
|
||||
private readonly logger = new Logger('PushProvider');
|
||||
|
||||
async sendToTokens(tokens: string[], n: PushNotification): Promise<PushResult> {
|
||||
this.logger.log(
|
||||
`[log-only] would push "${n.title}" to ${tokens.length} device(s): ${n.body}`,
|
||||
);
|
||||
return { sent: 0, invalidTokens: [] };
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
/// Abstraction over the push backend. Default is a no-send provider that only
|
||||
/// logs; PUSH_PROVIDER=fcm switches to Firebase Cloud Messaging (HTTP v1).
|
||||
export interface PushNotification {
|
||||
title: string;
|
||||
body: string;
|
||||
data?: Record<string, string>;
|
||||
}
|
||||
|
||||
export interface PushResult {
|
||||
sent: number;
|
||||
/// Tokens FCM reported as permanently invalid — the caller prunes them.
|
||||
invalidTokens: string[];
|
||||
}
|
||||
|
||||
export interface PushProvider {
|
||||
/// Best-effort: never throws for delivery problems.
|
||||
sendToTokens(tokens: string[], notification: PushNotification): Promise<PushResult>;
|
||||
}
|
||||
|
||||
export const PUSH_PROVIDER = Symbol('PUSH_PROVIDER');
|
||||
@@ -0,0 +1,29 @@
|
||||
import { Body, Controller, Post, Req, UseGuards } from '@nestjs/common';
|
||||
import { AuthGuard } from '@nestjs/passport';
|
||||
import { PushService } from './push.service';
|
||||
import { RegisterDeviceDto, UnregisterDeviceDto } from './dto/register-device.dto';
|
||||
// Import the util directly (not via chat.service) to keep the module graph acyclic.
|
||||
import { resolveChatCaller } from '../chat/caller.util';
|
||||
import { AuthenticatedRequest } from '../auth/authenticated-request';
|
||||
import { GuestJwtPayload } from '../auth/guest-auth.service';
|
||||
|
||||
type PushRequest = AuthenticatedRequest & {
|
||||
user?: AuthenticatedRequest['user'] | GuestJwtPayload;
|
||||
};
|
||||
|
||||
@Controller('push')
|
||||
export class PushController {
|
||||
constructor(private readonly push: PushService) {}
|
||||
|
||||
@Post('register')
|
||||
@UseGuards(AuthGuard(['authentik', 'team', 'guest']))
|
||||
register(@Body() dto: RegisterDeviceDto, @Req() req: PushRequest) {
|
||||
return this.push.register(dto.token, dto.platform, resolveChatCaller(req.user!));
|
||||
}
|
||||
|
||||
@Post('unregister')
|
||||
@UseGuards(AuthGuard(['authentik', 'team', 'guest']))
|
||||
unregister(@Body() dto: UnregisterDeviceDto) {
|
||||
return this.push.unregister(dto.token);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
import { Global, Module } from '@nestjs/common';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { PUSH_PROVIDER } from './push-provider';
|
||||
import { LogPushProvider } from './log-push.provider';
|
||||
import { FcmPushProvider } from './fcm-push.provider';
|
||||
import { PushService } from './push.service';
|
||||
import { PushController } from './push.controller';
|
||||
|
||||
/// Global so ChatService can inject PushService. Provider defaults to
|
||||
/// log-only; PUSH_PROVIDER=fcm switches to Firebase Cloud Messaging.
|
||||
@Global()
|
||||
@Module({
|
||||
controllers: [PushController],
|
||||
providers: [
|
||||
PushService,
|
||||
{
|
||||
provide: PUSH_PROVIDER,
|
||||
inject: [ConfigService],
|
||||
useFactory: (config: ConfigService) =>
|
||||
config.get<string>('PUSH_PROVIDER') === 'fcm'
|
||||
? new FcmPushProvider(config)
|
||||
: new LogPushProvider(),
|
||||
},
|
||||
],
|
||||
exports: [PushService],
|
||||
})
|
||||
export class PushModule {}
|
||||
@@ -0,0 +1,163 @@
|
||||
import { Inject, Injectable, Logger } from '@nestjs/common';
|
||||
import { ChatChannelType, SyncOperation } from '@prisma/client';
|
||||
import { PrismaClient } from '../prisma/prisma.module';
|
||||
import { SyncService } from '../sync/sync.service';
|
||||
import type { ChatCaller } from '../chat/chat.service';
|
||||
import { PUSH_PROVIDER, PushNotification, PushProvider } from './push-provider';
|
||||
|
||||
@Injectable()
|
||||
export class PushService {
|
||||
private readonly logger = new Logger(PushService.name);
|
||||
|
||||
constructor(
|
||||
private readonly prisma: PrismaClient,
|
||||
private readonly sync: SyncService,
|
||||
@Inject(PUSH_PROVIDER) private readonly provider: PushProvider,
|
||||
) {}
|
||||
|
||||
/// Upsert a device token for the current caller (team user or guest).
|
||||
async register(token: string, platform: string, caller: ChatCaller) {
|
||||
const owner =
|
||||
caller.kind === 'user'
|
||||
? { userId: caller.user.userId, guestAccountId: null }
|
||||
: { userId: null, guestAccountId: caller.guest.guestId };
|
||||
const row = await this.prisma.deviceToken.upsert({
|
||||
where: { token },
|
||||
create: { token, platform, ...owner },
|
||||
update: { platform, lastSeenAt: new Date(), ...owner },
|
||||
});
|
||||
await this.sync.capture('DeviceToken', SyncOperation.UPDATE, row.id, row);
|
||||
return { ok: true };
|
||||
}
|
||||
|
||||
async unregister(token: string) {
|
||||
const existing = await this.prisma.deviceToken.findUnique({ where: { token } });
|
||||
if (!existing) return { ok: true };
|
||||
await this.prisma.deviceToken.delete({ where: { token } });
|
||||
await this.sync.capture('DeviceToken', SyncOperation.DELETE, existing.id, {
|
||||
id: existing.id,
|
||||
});
|
||||
return { ok: true };
|
||||
}
|
||||
|
||||
/// Fan a chat message out as a push to everyone who can read the channel,
|
||||
/// minus the sender. Best-effort — never throws into the caller.
|
||||
async notifyChannel(
|
||||
channelId: string,
|
||||
notification: PushNotification,
|
||||
exclude: { userId?: string | null; guestId?: string | null } = {},
|
||||
): Promise<void> {
|
||||
try {
|
||||
const channel = await this.prisma.chatChannel.findUnique({
|
||||
where: { id: channelId },
|
||||
include: { participants: { select: { userId: true, guestAccountId: true } } },
|
||||
});
|
||||
if (!channel) return;
|
||||
|
||||
const { userIds, guestIds } = await this.audience(channel);
|
||||
const tokens = await this.prisma.deviceToken.findMany({
|
||||
where: {
|
||||
OR: [
|
||||
userIds.length ? { userId: { in: userIds } } : undefined,
|
||||
guestIds.length ? { guestAccountId: { in: guestIds } } : undefined,
|
||||
].filter(Boolean) as object[],
|
||||
NOT: {
|
||||
OR: [
|
||||
exclude.userId ? { userId: exclude.userId } : undefined,
|
||||
exclude.guestId ? { guestAccountId: exclude.guestId } : undefined,
|
||||
].filter(Boolean) as object[],
|
||||
},
|
||||
},
|
||||
select: { token: true },
|
||||
});
|
||||
if (tokens.length === 0) return;
|
||||
|
||||
const { invalidTokens } = await this.provider.sendToTokens(
|
||||
tokens.map((t) => t.token),
|
||||
notification,
|
||||
);
|
||||
if (invalidTokens.length) {
|
||||
await this.prisma.deviceToken.deleteMany({
|
||||
where: { token: { in: invalidTokens } },
|
||||
});
|
||||
}
|
||||
} catch (err) {
|
||||
this.logger.warn(`notifyChannel failed: ${(err as Error).message}`);
|
||||
}
|
||||
}
|
||||
|
||||
private async audience(channel: {
|
||||
kcId: string;
|
||||
type: ChatChannelType;
|
||||
gemeindeId: string | null;
|
||||
participants: { userId: string | null; guestAccountId: string | null }[];
|
||||
}): Promise<{ userIds: string[]; guestIds: string[] }> {
|
||||
if (channel.type === ChatChannelType.DIREKT) {
|
||||
return {
|
||||
userIds: channel.participants.map((p) => p.userId).filter((id): id is string => !!id),
|
||||
guestIds: [],
|
||||
};
|
||||
}
|
||||
|
||||
if (channel.type === ChatChannelType.GRUPPE) {
|
||||
return {
|
||||
userIds: channel.participants.map((p) => p.userId).filter((id): id is string => !!id),
|
||||
guestIds: channel.participants
|
||||
.map((p) => p.guestAccountId)
|
||||
.filter((id): id is string => !!id),
|
||||
};
|
||||
}
|
||||
|
||||
const ltUsers = await this.prisma.user.findMany({
|
||||
where: {
|
||||
OR: [
|
||||
{ isLeitungsteam: true },
|
||||
{ memberships: { some: { kcId: channel.kcId, role: 'LEITUNGSTEAM' } } },
|
||||
],
|
||||
},
|
||||
select: { id: true },
|
||||
});
|
||||
const ltIds = ltUsers.map((u) => u.id);
|
||||
|
||||
if (channel.type === ChatChannelType.LT_UEBERGREIFEND) {
|
||||
return { userIds: ltIds, guestIds: [] };
|
||||
}
|
||||
|
||||
if (channel.type === ChatChannelType.GEMEINDE_GRUPPE) {
|
||||
const [members, guests] = await Promise.all([
|
||||
this.prisma.membership.findMany({
|
||||
where: {
|
||||
kcId: channel.kcId,
|
||||
gemeindeId: channel.gemeindeId,
|
||||
status: 'ACTIVE',
|
||||
},
|
||||
select: { userId: true },
|
||||
}),
|
||||
this.prisma.guestAccount.findMany({
|
||||
where: { kcId: channel.kcId, gemeindeId: channel.gemeindeId },
|
||||
select: { id: true },
|
||||
}),
|
||||
]);
|
||||
return {
|
||||
userIds: [...new Set([...ltIds, ...members.map((m) => m.userId)])],
|
||||
guestIds: guests.map((g) => g.id),
|
||||
};
|
||||
}
|
||||
|
||||
// BROADCAST: everyone in the KC.
|
||||
const [members, guests] = await Promise.all([
|
||||
this.prisma.membership.findMany({
|
||||
where: { kcId: channel.kcId, status: 'ACTIVE' },
|
||||
select: { userId: true },
|
||||
}),
|
||||
this.prisma.guestAccount.findMany({
|
||||
where: { kcId: channel.kcId },
|
||||
select: { id: true },
|
||||
}),
|
||||
]);
|
||||
return {
|
||||
userIds: [...new Set([...ltIds, ...members.map((m) => m.userId)])],
|
||||
guestIds: guests.map((g) => g.id),
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,55 @@
|
||||
import { IsArray, IsNotEmpty } from 'class-validator';
|
||||
import { Type } from 'class-transformer';
|
||||
import {
|
||||
IsArray,
|
||||
IsDateString,
|
||||
IsIn,
|
||||
IsInt,
|
||||
IsNotEmpty,
|
||||
IsObject,
|
||||
IsString,
|
||||
Min,
|
||||
ValidateNested,
|
||||
} from 'class-validator';
|
||||
import { SyncOperation } from '@prisma/client';
|
||||
import { SYNCED_MODELS, SyncedModel } from '../synced-models';
|
||||
|
||||
/// Strict per-entry validation: only whitelisted models/operations are
|
||||
/// accepted, and the payload must be a plain object. This is the boundary
|
||||
/// where an untrusted peer's JSON becomes typed data - reject anything that
|
||||
/// doesn't match rather than letting it reach Prisma's generic delegate.
|
||||
export class SyncEntryDto {
|
||||
@IsInt()
|
||||
@Min(1)
|
||||
sequence!: number;
|
||||
|
||||
@IsIn(SYNCED_MODELS)
|
||||
model!: SyncedModel;
|
||||
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
recordId!: string;
|
||||
|
||||
@IsIn(Object.values(SyncOperation))
|
||||
operation!: SyncOperation;
|
||||
|
||||
@IsObject()
|
||||
payload!: Record<string, unknown>;
|
||||
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
originId!: string;
|
||||
|
||||
/// Wall-clock time the mutation actually happened, used for last-write-wins
|
||||
/// conflict resolution - required so a peer can't omit it and silently
|
||||
/// win every conflict via a default "now".
|
||||
@IsDateString()
|
||||
occurredAt!: string;
|
||||
}
|
||||
|
||||
export class IngestEntriesDto {
|
||||
@IsArray()
|
||||
@IsNotEmpty()
|
||||
entries!: unknown[];
|
||||
@ValidateNested({ each: true })
|
||||
@Type(() => SyncEntryDto)
|
||||
entries!: SyncEntryDto[];
|
||||
}
|
||||
|
||||
|
||||
@@ -9,6 +9,11 @@ import { SyncService } from './sync.service';
|
||||
@Injectable()
|
||||
export class SyncSchedulerService {
|
||||
private readonly logger = new Logger(SyncSchedulerService.name);
|
||||
/// Guards against a tick starting while the previous one is still running
|
||||
/// (e.g. a large backlog push/pull that exceeds the 30s interval).
|
||||
/// SyncService.withPeerLock is the authoritative per-peer guard; this flag
|
||||
/// just avoids logging noisy "already in progress" warnings every tick.
|
||||
private running = false;
|
||||
|
||||
constructor(
|
||||
private readonly sync: SyncService,
|
||||
@@ -21,12 +26,22 @@ export class SyncSchedulerService {
|
||||
const peerUrl = this.config.get<string>('SYNC_PEER_URL');
|
||||
const peerSecret = this.config.get<string>('SYNC_SHARED_SECRET');
|
||||
if (!peerUrl || !peerSecret) return;
|
||||
if (this.running) {
|
||||
this.logger.debug('Previous sync tick still running, skipping this tick');
|
||||
return;
|
||||
}
|
||||
|
||||
this.running = true;
|
||||
try {
|
||||
await this.sync.pushToPeer(peerUrl, peerSecret);
|
||||
await this.sync.pullFromPeer(peerUrl, peerSecret);
|
||||
const pushResult = await this.sync.pushToPeer(peerUrl, peerSecret);
|
||||
const pullResult = await this.sync.pullFromPeer(peerUrl, peerSecret);
|
||||
this.logger.debug(
|
||||
`Sync tick ok: pushed=${pushResult.pushed} pulled=${pullResult.pulled}`,
|
||||
);
|
||||
} catch (err) {
|
||||
this.logger.debug(`Sync with peer skipped: ${(err as Error).message}`);
|
||||
} finally {
|
||||
this.running = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { CanActivate, ExecutionContext, ForbiddenException, Injectable } from '@nestjs/common';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { Request } from 'express';
|
||||
import { timingSafeEqual } from 'crypto';
|
||||
|
||||
/// Server-to-server auth for /sync/*: a shared secret header, not a user token.
|
||||
@Injectable()
|
||||
@@ -10,9 +11,26 @@ export class SyncSecretGuard implements CanActivate {
|
||||
canActivate(context: ExecutionContext): boolean {
|
||||
const request = context.switchToHttp().getRequest<Request>();
|
||||
const expected = this.config.getOrThrow<string>('SYNC_SHARED_SECRET');
|
||||
if (request.headers['x-sync-secret'] !== expected) {
|
||||
const provided = request.headers['x-sync-secret'];
|
||||
|
||||
if (typeof provided !== 'string' || !this.secretsMatch(provided, expected)) {
|
||||
throw new ForbiddenException('Invalid sync secret');
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// Plain `!==` leaks timing info proportional to the matching prefix
|
||||
/// length, letting an attacker brute-force the secret byte by byte over
|
||||
/// enough requests. Compare as fixed-length buffers instead.
|
||||
private secretsMatch(provided: string, expected: string): boolean {
|
||||
const providedBuf = Buffer.from(provided);
|
||||
const expectedBuf = Buffer.from(expected);
|
||||
if (providedBuf.length !== expectedBuf.length) {
|
||||
// Still run a constant-time compare against a same-length dummy so
|
||||
// the length check itself doesn't introduce a distinct fast path.
|
||||
timingSafeEqual(expectedBuf, expectedBuf);
|
||||
return false;
|
||||
}
|
||||
return timingSafeEqual(providedBuf, expectedBuf);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,8 @@ import { Roles } from '../common/roles.decorator';
|
||||
import { RolesGuard } from '../common/roles.guard';
|
||||
import { Role } from '../common/role.enum';
|
||||
|
||||
const EXPORT_QUERY_MAX = 1_000_000_000;
|
||||
|
||||
@Controller('sync')
|
||||
export class SyncController {
|
||||
constructor(
|
||||
@@ -19,15 +21,20 @@ export class SyncController {
|
||||
@Post('ingest')
|
||||
@UseGuards(SyncSecretGuard)
|
||||
async ingest(@Body() dto: IngestEntriesDto) {
|
||||
await this.sync.applyIncoming(dto.entries as never);
|
||||
return { applied: dto.entries.length };
|
||||
return this.sync.applyIncoming(dto.entries);
|
||||
}
|
||||
|
||||
/// Peer pulls our new entries since their last known sequence.
|
||||
@Get('export')
|
||||
@UseGuards(SyncSecretGuard)
|
||||
async export(@Query('since') since: string) {
|
||||
const entries = await this.sync.getEntriesSince(Number(since) || 0);
|
||||
// Reject garbage/negative/absurd cursors outright rather than silently
|
||||
// coercing them to 0 (which would re-export the whole log to a peer
|
||||
// that sent a malformed value).
|
||||
const parsed = Number(since);
|
||||
const sinceSequence =
|
||||
Number.isInteger(parsed) && parsed >= 0 && parsed <= EXPORT_QUERY_MAX ? parsed : 0;
|
||||
const entries = await this.sync.getEntriesSince(sinceSequence);
|
||||
return { entries };
|
||||
}
|
||||
|
||||
@@ -42,4 +49,16 @@ export class SyncController {
|
||||
const pulled = await this.sync.pullFromPeer(peerUrl, peerSecret);
|
||||
return { ...pushed, ...pulled };
|
||||
}
|
||||
|
||||
/// Leitungsteam-only visibility into replication health: cursors, pending
|
||||
/// backlog size, and the last push/pull timestamps or error, so a stalled
|
||||
/// sync (e.g. bad secret, network down) shows up before anyone notices
|
||||
/// stale data.
|
||||
@Get('status')
|
||||
@UseGuards(AuthGuard(['authentik', 'team']), RolesGuard)
|
||||
@Roles(Role.LEITUNGSTEAM)
|
||||
async status() {
|
||||
const peerUrl = this.config.getOrThrow<string>('SYNC_PEER_URL');
|
||||
return this.sync.getStatus(peerUrl);
|
||||
}
|
||||
}
|
||||
|
||||
+255
-53
@@ -2,43 +2,58 @@ import { Injectable, Logger } from '@nestjs/common';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { SyncOperation } from '@prisma/client';
|
||||
import { PrismaClient } from '../prisma/prisma.module';
|
||||
import { SYNCED_MODELS, SyncedModel } from './synced-models';
|
||||
|
||||
const SYNCED_MODELS = [
|
||||
'Kc',
|
||||
'Gemeinde',
|
||||
'User',
|
||||
'Membership',
|
||||
'TeamerInvite',
|
||||
'GuestAccount',
|
||||
'Wahl',
|
||||
'Workshop',
|
||||
'Teilnehmer',
|
||||
'ForceZuteilung',
|
||||
'Zuteilung',
|
||||
'File',
|
||||
'ChatChannel',
|
||||
'ChatMessage',
|
||||
] as const;
|
||||
export type SyncedModel = (typeof SYNCED_MODELS)[number];
|
||||
const PAGE_SIZE = 500;
|
||||
/// Hard ceiling per push/pull call so a huge backlog (e.g. days offline)
|
||||
/// can't turn one tick into an unbounded, memory-hungry transfer. The
|
||||
/// scheduler just picks it back up on the next tick.
|
||||
const MAX_ENTRIES_PER_CALL = 10 * PAGE_SIZE;
|
||||
|
||||
interface IncomingEntry {
|
||||
export interface IncomingEntry {
|
||||
sequence: number;
|
||||
model: string;
|
||||
model: SyncedModel;
|
||||
recordId: string;
|
||||
operation: SyncOperation;
|
||||
payload: Record<string, unknown>;
|
||||
originId: string;
|
||||
occurredAt: string | Date;
|
||||
}
|
||||
|
||||
/// Replicates mutations between the local (on-site) and cloud server. The
|
||||
/// local server is the sole source of truth while an event is live, so
|
||||
/// incoming entries are applied with simple upserts - no conflict resolution
|
||||
/// is needed by design (see plan doc).
|
||||
export interface SyncPeerStatus {
|
||||
peerId: string;
|
||||
lastPushedSequence: number;
|
||||
lastPulledSequence: number;
|
||||
localMaxSequence: number;
|
||||
pendingPush: number;
|
||||
lastPushAt: Date | null;
|
||||
lastPullAt: Date | null;
|
||||
lastError: string | null;
|
||||
recentConflicts: number;
|
||||
}
|
||||
|
||||
/// Replicates mutations between the local (on-site) and cloud server.
|
||||
///
|
||||
/// Concurrent-edit handling: both servers can legitimately write while an
|
||||
/// event is live (e.g. Leitungsteam edits in the cloud admin UI while the
|
||||
/// on-site server is also active), so we resolve conflicts automatically by
|
||||
/// last-write-wins on the mutation's real wall-clock time (`occurredAt`),
|
||||
/// never by sync/network arrival order. `SyncRecordVersion` tracks, per
|
||||
/// record, the most recent writer and timestamp seen by *this* server
|
||||
/// (whether written locally or applied from a peer). A losing write is
|
||||
/// still recorded to `SyncConflict` for after-the-fact review - resolution
|
||||
/// itself never blocks or pauses live sync.
|
||||
@Injectable()
|
||||
export class SyncService {
|
||||
private readonly logger = new Logger(SyncService.name);
|
||||
readonly serverId: string;
|
||||
|
||||
/// In-memory, per-peer status for observability (health endpoint) plus a
|
||||
/// crude mutex so overlapping ticks (a push+pull pair that takes longer
|
||||
/// than 30s) can't race each other's cursor updates.
|
||||
private readonly peerStatus = new Map<string, SyncPeerStatus>();
|
||||
private readonly peerLocks = new Set<string>();
|
||||
|
||||
constructor(
|
||||
private readonly prisma: PrismaClient,
|
||||
private readonly config: ConfigService,
|
||||
@@ -46,62 +61,142 @@ export class SyncService {
|
||||
this.serverId = config.getOrThrow<string>('SERVER_ID');
|
||||
}
|
||||
|
||||
/// Called by feature services right after a mutation to append it to the replication log.
|
||||
async capture(model: SyncedModel, operation: SyncOperation, recordId: string, payload: object) {
|
||||
await this.prisma.syncLogEntry.create({
|
||||
/// Called by feature services right after a mutation to append it to the
|
||||
/// replication log and mark this server as the latest writer of record.
|
||||
async capture(
|
||||
model: SyncedModel,
|
||||
operation: SyncOperation,
|
||||
recordId: string,
|
||||
payload: object,
|
||||
occurredAt: Date = new Date(),
|
||||
) {
|
||||
await this.prisma.$transaction([
|
||||
this.prisma.syncLogEntry.create({
|
||||
data: {
|
||||
model,
|
||||
recordId,
|
||||
operation,
|
||||
payload: payload as never,
|
||||
originId: this.serverId,
|
||||
occurredAt,
|
||||
},
|
||||
});
|
||||
}),
|
||||
this.prisma.syncRecordVersion.upsert({
|
||||
where: { model_recordId: { model, recordId } },
|
||||
create: { model, recordId, lastWriteAt: occurredAt, lastWriteOrigin: this.serverId },
|
||||
update: { lastWriteAt: occurredAt, lastWriteOrigin: this.serverId },
|
||||
}),
|
||||
]);
|
||||
}
|
||||
|
||||
async getEntriesSince(sequence: number, limit = 500) {
|
||||
async getEntriesSince(sequence: number, limit = PAGE_SIZE) {
|
||||
return this.prisma.syncLogEntry.findMany({
|
||||
where: { sequence: { gt: sequence } },
|
||||
orderBy: { sequence: 'asc' },
|
||||
take: limit,
|
||||
take: Math.min(limit, MAX_ENTRIES_PER_CALL),
|
||||
});
|
||||
}
|
||||
|
||||
/// Applies entries received from a peer; never re-captures them, which is
|
||||
/// what prevents echo loops between the two servers.
|
||||
/// Applies entries received from a peer inside one transaction, so a
|
||||
/// mid-batch failure can't leave the local DB half-updated relative to the
|
||||
/// cursor we're about to advance. Never re-captures them, which is what
|
||||
/// prevents echo loops between the two servers.
|
||||
///
|
||||
/// Per entry: if this server has a newer local write for the same record
|
||||
/// (by occurredAt) from a *different* origin, the incoming entry loses -
|
||||
/// it's recorded as a SyncConflict and skipped, keeping the newer local
|
||||
/// data intact. Otherwise the incoming entry wins and is applied.
|
||||
async applyIncoming(entries: IncomingEntry[]) {
|
||||
let appliedCount = 0;
|
||||
let conflictCount = 0;
|
||||
await this.prisma.$transaction(async (tx) => {
|
||||
for (const entry of entries) {
|
||||
if (entry.originId === this.serverId) continue;
|
||||
const delegate = this.delegateFor(entry.model);
|
||||
if (!delegate) {
|
||||
this.logger.warn(`Skipping sync entry for unknown model "${entry.model}"`);
|
||||
if (!SYNCED_MODELS.includes(entry.model)) {
|
||||
this.logger.warn(`Rejecting sync entry for unknown model "${entry.model}"`);
|
||||
continue;
|
||||
}
|
||||
|
||||
const occurredAt = new Date(entry.occurredAt);
|
||||
const existing = await tx.syncRecordVersion.findUnique({
|
||||
where: { model_recordId: { model: entry.model, recordId: entry.recordId } },
|
||||
});
|
||||
|
||||
if (existing && existing.lastWriteOrigin !== entry.originId && existing.lastWriteAt > occurredAt) {
|
||||
// A newer write (by a different origin) already won for this
|
||||
// record - keep it, log the loser for manual review.
|
||||
conflictCount += 1;
|
||||
this.logger.warn(
|
||||
`Sync conflict on ${entry.model}/${entry.recordId}: keeping ${existing.lastWriteOrigin}'s newer write over ${entry.originId}'s`,
|
||||
);
|
||||
const delegate = this.delegateFor(tx, entry.model);
|
||||
const currentRecord = await delegate
|
||||
.findUnique({ where: { id: entry.recordId } })
|
||||
.catch(() => null);
|
||||
await tx.syncConflict.create({
|
||||
data: {
|
||||
model: entry.model,
|
||||
recordId: entry.recordId,
|
||||
winningOrigin: existing.lastWriteOrigin,
|
||||
losingOrigin: entry.originId,
|
||||
winningPayload: (currentRecord ?? {}) as never,
|
||||
losingPayload: entry.payload as never,
|
||||
},
|
||||
});
|
||||
continue;
|
||||
}
|
||||
|
||||
const delegate = this.delegateFor(tx, entry.model);
|
||||
try {
|
||||
if (entry.operation === SyncOperation.DELETE) {
|
||||
await delegate.delete({ where: { id: entry.recordId } });
|
||||
} else {
|
||||
// Force the record's id from recordId, not from payload, so a
|
||||
// mismatched/forged id in the payload can never redirect the
|
||||
// write onto a different row.
|
||||
const { id: _ignoredId, ...rest } = entry.payload;
|
||||
await delegate.upsert({
|
||||
where: { id: entry.recordId },
|
||||
create: entry.payload,
|
||||
update: entry.payload,
|
||||
create: { id: entry.recordId, ...rest },
|
||||
update: rest,
|
||||
});
|
||||
}
|
||||
await tx.syncRecordVersion.upsert({
|
||||
where: { model_recordId: { model: entry.model, recordId: entry.recordId } },
|
||||
create: {
|
||||
model: entry.model,
|
||||
recordId: entry.recordId,
|
||||
lastWriteAt: occurredAt,
|
||||
lastWriteOrigin: entry.originId,
|
||||
},
|
||||
update: { lastWriteAt: occurredAt, lastWriteOrigin: entry.originId },
|
||||
});
|
||||
appliedCount += 1;
|
||||
} catch (err) {
|
||||
this.logger.warn(
|
||||
`Failed to apply sync entry ${entry.model}/${entry.recordId}: ${(err as Error).message}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
});
|
||||
return { applied: appliedCount, conflicts: conflictCount };
|
||||
}
|
||||
|
||||
async pushToPeer(peerUrl: string, peerSecret: string) {
|
||||
const peerId = new URL(peerUrl).host;
|
||||
return this.withPeerLock(peerId, async () => {
|
||||
const cursor = await this.getOrCreateCursor(peerId);
|
||||
const entries = await this.getEntriesSince(cursor.lastPushedSequence);
|
||||
if (entries.length === 0) return { pushed: 0 };
|
||||
let pushed = 0;
|
||||
let lastSequence = cursor.lastPushedSequence;
|
||||
|
||||
const res = await fetch(`${peerUrl}/sync/ingest`, {
|
||||
// Loop pages so a large backlog (offline event site catching back up)
|
||||
// is fully drained in one tick instead of trickling 500 at a time
|
||||
// across many 30s intervals.
|
||||
for (;;) {
|
||||
const entries = await this.getEntriesSince(lastSequence);
|
||||
if (entries.length === 0) break;
|
||||
|
||||
const res = await this.fetchWithTimeout(`${peerUrl}/sync/ingest`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json', 'x-sync-secret': peerSecret },
|
||||
body: JSON.stringify({ entries }),
|
||||
@@ -109,31 +204,131 @@ export class SyncService {
|
||||
if (!res.ok) {
|
||||
throw new Error(`Peer rejected sync push: ${res.status}`);
|
||||
}
|
||||
|
||||
lastSequence = entries[entries.length - 1].sequence;
|
||||
pushed += entries.length;
|
||||
await this.prisma.syncCursor.update({
|
||||
where: { peerId },
|
||||
data: { lastPushedSequence: entries[entries.length - 1].sequence },
|
||||
data: { lastPushedSequence: lastSequence },
|
||||
});
|
||||
|
||||
if (entries.length < PAGE_SIZE || pushed >= MAX_ENTRIES_PER_CALL) break;
|
||||
}
|
||||
|
||||
this.recordSuccess(peerId, { lastPushAt: new Date() });
|
||||
return { pushed };
|
||||
});
|
||||
return { pushed: entries.length };
|
||||
}
|
||||
|
||||
async pullFromPeer(peerUrl: string, peerSecret: string) {
|
||||
const peerId = new URL(peerUrl).host;
|
||||
return this.withPeerLock(peerId, async () => {
|
||||
const cursor = await this.getOrCreateCursor(peerId);
|
||||
const res = await fetch(`${peerUrl}/sync/export?since=${cursor.lastPulledSequence}`, {
|
||||
headers: { 'x-sync-secret': peerSecret },
|
||||
});
|
||||
let pulled = 0;
|
||||
let conflicts = 0;
|
||||
let lastSequence = cursor.lastPulledSequence;
|
||||
|
||||
for (;;) {
|
||||
const res = await this.fetchWithTimeout(
|
||||
`${peerUrl}/sync/export?since=${lastSequence}`,
|
||||
{ headers: { 'x-sync-secret': peerSecret } },
|
||||
);
|
||||
if (!res.ok) {
|
||||
throw new Error(`Peer rejected sync pull: ${res.status}`);
|
||||
}
|
||||
const { entries } = (await res.json()) as { entries: IncomingEntry[] };
|
||||
if (entries.length === 0) return { pulled: 0 };
|
||||
if (entries.length === 0) break;
|
||||
|
||||
await this.applyIncoming(entries);
|
||||
const result = await this.applyIncoming(entries);
|
||||
conflicts += result.conflicts;
|
||||
lastSequence = entries[entries.length - 1].sequence;
|
||||
pulled += entries.length;
|
||||
await this.prisma.syncCursor.update({
|
||||
where: { peerId },
|
||||
data: { lastPulledSequence: entries[entries.length - 1].sequence },
|
||||
data: { lastPulledSequence: lastSequence },
|
||||
});
|
||||
return { pulled: entries.length };
|
||||
|
||||
if (entries.length < PAGE_SIZE || pulled >= MAX_ENTRIES_PER_CALL) break;
|
||||
}
|
||||
|
||||
this.recordSuccess(peerId, { lastPullAt: new Date(), recentConflicts: conflicts });
|
||||
return { pulled, conflicts };
|
||||
});
|
||||
}
|
||||
|
||||
/// Snapshot of replication health for a peer, for the /sync/status endpoint.
|
||||
async getStatus(peerUrl: string): Promise<SyncPeerStatus> {
|
||||
const peerId = new URL(peerUrl).host;
|
||||
const cursor = await this.getOrCreateCursor(peerId);
|
||||
const latest = await this.prisma.syncLogEntry.findFirst({ orderBy: { sequence: 'desc' } });
|
||||
const localMaxSequence = latest?.sequence ?? 0;
|
||||
const recentConflicts = await this.prisma.syncConflict.count({
|
||||
where: { detectedAt: { gt: new Date(Date.now() - 24 * 60 * 60 * 1000) } },
|
||||
});
|
||||
const cached = this.peerStatus.get(peerId);
|
||||
return {
|
||||
peerId,
|
||||
lastPushedSequence: cursor.lastPushedSequence,
|
||||
lastPulledSequence: cursor.lastPulledSequence,
|
||||
localMaxSequence,
|
||||
pendingPush: Math.max(0, localMaxSequence - cursor.lastPushedSequence),
|
||||
lastPushAt: cached?.lastPushAt ?? null,
|
||||
lastPullAt: cached?.lastPullAt ?? null,
|
||||
lastError: cached?.lastError ?? null,
|
||||
recentConflicts,
|
||||
};
|
||||
}
|
||||
|
||||
/// Serializes push/pull per peer so an overrunning tick (slow network,
|
||||
/// big backlog) can never overlap with the next scheduled tick and race
|
||||
/// the same cursor row.
|
||||
private async withPeerLock<T>(peerId: string, fn: () => Promise<T>): Promise<T> {
|
||||
if (this.peerLocks.has(peerId)) {
|
||||
throw new Error(`Sync with ${peerId} already in progress, skipping`);
|
||||
}
|
||||
this.peerLocks.add(peerId);
|
||||
try {
|
||||
return await fn();
|
||||
} catch (err) {
|
||||
this.recordFailure(peerId, err as Error);
|
||||
throw err;
|
||||
} finally {
|
||||
this.peerLocks.delete(peerId);
|
||||
}
|
||||
}
|
||||
|
||||
private blankStatus(peerId: string): SyncPeerStatus {
|
||||
return {
|
||||
peerId,
|
||||
lastPushedSequence: 0,
|
||||
lastPulledSequence: 0,
|
||||
localMaxSequence: 0,
|
||||
pendingPush: 0,
|
||||
lastPushAt: null,
|
||||
lastPullAt: null,
|
||||
lastError: null,
|
||||
recentConflicts: 0,
|
||||
};
|
||||
}
|
||||
|
||||
private recordSuccess(peerId: string, patch: Partial<SyncPeerStatus>) {
|
||||
const current = this.peerStatus.get(peerId) ?? this.blankStatus(peerId);
|
||||
this.peerStatus.set(peerId, { ...current, ...patch, lastError: null });
|
||||
}
|
||||
|
||||
private recordFailure(peerId: string, err: Error) {
|
||||
const current = this.peerStatus.get(peerId) ?? this.blankStatus(peerId);
|
||||
this.peerStatus.set(peerId, { ...current, lastError: err.message });
|
||||
}
|
||||
|
||||
private async fetchWithTimeout(url: string, init: RequestInit, timeoutMs = 15_000) {
|
||||
const controller = new AbortController();
|
||||
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
||||
try {
|
||||
return await fetch(url, { ...init, signal: controller.signal });
|
||||
} finally {
|
||||
clearTimeout(timer);
|
||||
}
|
||||
}
|
||||
|
||||
private async getOrCreateCursor(peerId: string) {
|
||||
@@ -144,14 +339,21 @@ export class SyncService {
|
||||
});
|
||||
}
|
||||
|
||||
private delegateFor(model: string) {
|
||||
if (!SYNCED_MODELS.includes(model as SyncedModel)) return null;
|
||||
const key = (model.charAt(0).toLowerCase() + model.slice(1)) as keyof PrismaClient;
|
||||
private delegateFor(
|
||||
tx: Parameters<Parameters<PrismaClient['$transaction']>[0]>[0],
|
||||
model: SyncedModel,
|
||||
) {
|
||||
const key = (model.charAt(0).toLowerCase() + model.slice(1)) as keyof typeof tx;
|
||||
// Generic dispatch across models is inherent to a replication log; each
|
||||
// delegate exposes the same upsert/delete shape we need here.
|
||||
return this.prisma[key] as unknown as {
|
||||
upsert: (args: { where: { id: string }; create: object; update: object }) => Promise<unknown>;
|
||||
return tx[key] as unknown as {
|
||||
upsert: (args: {
|
||||
where: { id: string };
|
||||
create: object;
|
||||
update: object;
|
||||
}) => Promise<unknown>;
|
||||
delete: (args: { where: { id: string } }) => Promise<unknown>;
|
||||
findUnique: (args: { where: { id: string } }) => Promise<unknown>;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
export const SYNCED_MODELS = [
|
||||
'Kc',
|
||||
'Gemeinde',
|
||||
'User',
|
||||
'Membership',
|
||||
'TeamerInvite',
|
||||
'VerantwortlicheInvite',
|
||||
'GuestAccount',
|
||||
'Wahl',
|
||||
'Workshop',
|
||||
'Teilnehmer',
|
||||
'ForceZuteilung',
|
||||
'Zuteilung',
|
||||
'File',
|
||||
'ChatChannel',
|
||||
'ChatParticipant',
|
||||
'ChatMessage',
|
||||
'DeviceToken',
|
||||
] as const;
|
||||
|
||||
export type SyncedModel = (typeof SYNCED_MODELS)[number];
|
||||
@@ -0,0 +1,7 @@
|
||||
import { IsBoolean, IsOptional } from 'class-validator';
|
||||
|
||||
export class UpdateWahlDto {
|
||||
@IsOptional()
|
||||
@IsBoolean()
|
||||
isOpen?: boolean;
|
||||
}
|
||||
@@ -3,6 +3,7 @@ import {
|
||||
Controller,
|
||||
Get,
|
||||
Param,
|
||||
Patch,
|
||||
Post,
|
||||
Query,
|
||||
Req,
|
||||
@@ -14,6 +15,7 @@ import { Response } from 'express';
|
||||
import { WahlService } from './wahl.service';
|
||||
import { ZuteilungService } from './zuteilung.service';
|
||||
import { CreateWahlDto } from './dto/create-wahl.dto';
|
||||
import { UpdateWahlDto } from './dto/update-wahl.dto';
|
||||
import { CreateWorkshopDto } from './dto/create-workshop.dto';
|
||||
import { SubmitTeilnehmerDto } from './dto/submit-teilnehmer.dto';
|
||||
import { CreateForceZuteilungDto } from './dto/create-force-zuteilung.dto';
|
||||
@@ -32,35 +34,49 @@ export class WahlController {
|
||||
/// Wahl-/Workshop-/Force-Zuteilung-Verwaltung ist ausschließlich Sache des
|
||||
/// Leitungsteams (global über alle KCs, siehe RolesGuard).
|
||||
@Post()
|
||||
@UseGuards(AuthGuard('authentik'), RolesGuard)
|
||||
@UseGuards(AuthGuard(['authentik', 'team']), RolesGuard)
|
||||
@Roles(Role.LEITUNGSTEAM)
|
||||
createWahl(@Body() dto: CreateWahlDto) {
|
||||
return this.wahl.createWahl(dto.kcId, dto.name, dto.datumsSchluessel, dto.teil);
|
||||
}
|
||||
|
||||
@Get()
|
||||
@UseGuards(AuthGuard('authentik'), RolesGuard)
|
||||
@UseGuards(AuthGuard(['authentik', 'team']), RolesGuard)
|
||||
@Roles(Role.LEITUNGSTEAM)
|
||||
listWahlen(@Query('kcId') kcId: string) {
|
||||
return this.wahl.listWahlen(kcId);
|
||||
}
|
||||
|
||||
@Patch(':wahlId')
|
||||
@UseGuards(AuthGuard(['authentik', 'team']), RolesGuard)
|
||||
@Roles(Role.LEITUNGSTEAM)
|
||||
updateWahl(@Param('wahlId') wahlId: string, @Body() dto: UpdateWahlDto) {
|
||||
return this.wahl.updateWahl(wahlId, { isOpen: dto.isOpen });
|
||||
}
|
||||
|
||||
@Get(':wahlId/teilnehmer')
|
||||
@UseGuards(AuthGuard(['authentik', 'team']), RolesGuard)
|
||||
@Roles(Role.LEITUNGSTEAM)
|
||||
listTeilnehmer(@Param('wahlId') wahlId: string) {
|
||||
return this.wahl.listTeilnehmer(wahlId);
|
||||
}
|
||||
|
||||
@Post(':wahlId/workshops')
|
||||
@UseGuards(AuthGuard('authentik'), RolesGuard)
|
||||
@UseGuards(AuthGuard(['authentik', 'team']), RolesGuard)
|
||||
@Roles(Role.LEITUNGSTEAM)
|
||||
createWorkshop(@Param('wahlId') wahlId: string, @Body() dto: CreateWorkshopDto) {
|
||||
return this.wahl.createWorkshop(wahlId, dto.name, dto.kapazitaet, dto.minTeilnehmer);
|
||||
}
|
||||
|
||||
@Get(':wahlId/workshops')
|
||||
@UseGuards(AuthGuard('authentik'), RolesGuard)
|
||||
@UseGuards(AuthGuard(['authentik', 'team']), RolesGuard)
|
||||
@Roles(Role.LEITUNGSTEAM)
|
||||
listWorkshops(@Param('wahlId') wahlId: string) {
|
||||
return this.wahl.listWorkshops(wahlId);
|
||||
}
|
||||
|
||||
@Post(':wahlId/force-zuteilung')
|
||||
@UseGuards(AuthGuard('authentik'), RolesGuard)
|
||||
@UseGuards(AuthGuard(['authentik', 'team']), RolesGuard)
|
||||
@Roles(Role.LEITUNGSTEAM)
|
||||
createForceZuteilung(
|
||||
@Param('wahlId') wahlId: string,
|
||||
@@ -99,21 +115,21 @@ export class WahlController {
|
||||
}
|
||||
|
||||
@Post(':wahlId/zuteilung/run')
|
||||
@UseGuards(AuthGuard('authentik'), RolesGuard)
|
||||
@UseGuards(AuthGuard(['authentik', 'team']), RolesGuard)
|
||||
@Roles(Role.LEITUNGSTEAM)
|
||||
runZuteilung(@Param('wahlId') wahlId: string) {
|
||||
return this.zuteilung.run(wahlId);
|
||||
}
|
||||
|
||||
@Get(':wahlId/zuteilung')
|
||||
@UseGuards(AuthGuard('authentik'), RolesGuard)
|
||||
@UseGuards(AuthGuard(['authentik', 'team']), RolesGuard)
|
||||
@Roles(Role.LEITUNGSTEAM)
|
||||
getZuteilung(@Param('wahlId') wahlId: string) {
|
||||
return this.zuteilung.getResults(wahlId);
|
||||
}
|
||||
|
||||
@Get(':wahlId/zuteilung/csv')
|
||||
@UseGuards(AuthGuard('authentik'), RolesGuard)
|
||||
@UseGuards(AuthGuard(['authentik', 'team']), RolesGuard)
|
||||
@Roles(Role.LEITUNGSTEAM)
|
||||
async exportCsv(@Param('wahlId') wahlId: string, @Res() res: Response) {
|
||||
const csv = await this.zuteilung.exportCsv(wahlId);
|
||||
|
||||
@@ -22,6 +22,33 @@ export class WahlService {
|
||||
return this.prisma.wahl.findMany({ where: { kcId } });
|
||||
}
|
||||
|
||||
async updateWahl(wahlId: string, data: { isOpen?: boolean }) {
|
||||
await this.getWahlOrThrow(wahlId);
|
||||
const wahl = await this.prisma.wahl.update({ where: { id: wahlId }, data });
|
||||
await this.sync.capture('Wahl', SyncOperation.UPDATE, wahl.id, wahl);
|
||||
return wahl;
|
||||
}
|
||||
|
||||
/// LT view of who took part in a Wahl, with their priorities and any
|
||||
/// existing Force-Zuteilung.
|
||||
async listTeilnehmer(wahlId: string) {
|
||||
await this.getWahlOrThrow(wahlId);
|
||||
const rows = await this.prisma.teilnehmer.findMany({
|
||||
where: { wahlId },
|
||||
orderBy: { guestAccount: { lastName: 'asc' } },
|
||||
include: {
|
||||
guestAccount: { select: { firstName: true, lastName: true } },
|
||||
forceZuteilung: { select: { workshopId: true } },
|
||||
},
|
||||
});
|
||||
return rows.map((t) => ({
|
||||
id: t.id,
|
||||
name: `${t.guestAccount.firstName} ${t.guestAccount.lastName}`.trim(),
|
||||
prioritaeten: (t.prioritaeten as string[] | null) ?? [],
|
||||
forcedWorkshopId: t.forceZuteilung?.workshopId ?? null,
|
||||
}));
|
||||
}
|
||||
|
||||
/// Guest-facing view: open Wahlen for the guest's KC, each with its
|
||||
/// workshops and the guest's own current priorities (null if not submitted).
|
||||
async guestOverview(kcId: string, guestAccountId: string) {
|
||||
@@ -145,7 +172,7 @@ export class WahlService {
|
||||
throw new ForbiddenException('Wahl is closed');
|
||||
}
|
||||
const teilnehmer = await this.prisma.teilnehmer.upsert({
|
||||
where: { wahlId_guestAccountId: { wahlId, guestAccountId } },
|
||||
where: { wahlId_guestAccountId_phase: { wahlId, guestAccountId, phase: 1 } },
|
||||
create: { wahlId, guestAccountId, prioritaeten },
|
||||
update: { prioritaeten },
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user