import { Injectable, NotFoundException } from '@nestjs/common'; import { JwtService } from '@nestjs/jwt'; import { PrismaClient } from '../prisma/prisma.module'; export interface GuestJwtPayload { guestId: string; kcId: string; gemeindeId: string | null; } /// Guest/Konfi accounts are local to this server (never Authentik-backed), /// created via a KC invite code, and scoped to that single KC. @Injectable() export class GuestAuthService { constructor( private readonly prisma: PrismaClient, private readonly jwt: JwtService, ) {} async createGuest( inviteCode: string, firstName: string, lastName: string, ): Promise<{ accessToken: string }> { const kc = await this.prisma.kc.findUnique({ where: { inviteCode } }); 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 payload: GuestJwtPayload = { guestId: guest.id, kcId: kc.id, gemeindeId: guest.gemeindeId, }; return { accessToken: await this.jwt.signAsync(payload) }; } }