Spaces:
Sleeping

File size: 2,282 Bytes
7f81110
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { Request, Response } from "express";
import { AuthOnboardingService } from "../../../domain/services/AuthOnboardingService";
import { UnitOfWorkFactory } from "../../../../../libs/shared/infrastructure/postgres/unitOfWork/UnitOfWorkFactory";
import { IUnitOfWork } from "../../../../../libs/shared/infrastructure/postgres/unitOfWork/IUnitOfWork";
import { logger } from "../../../../../libs/common/logger";
import { env } from "../../../../../config/env";
import jwt from "jsonwebtoken";

export class TenantRegisterController {
    private readonly onboardingService = new AuthOnboardingService();

    constructor(private readonly uowFactory: UnitOfWorkFactory) { }

    async register(req: Request, res: Response) {
        // Set headers for streaming
        res.setHeader('Content-Type', 'application/json');
        res.setHeader('Transfer-Encoding', 'chunked');
        res.setHeader('Connection', 'keep-alive');

        const sendProgress = (message: string) => {
            const chunk = JSON.stringify({ type: 'progress', message }) + "\n";
            res.write(chunk);
        };

        try {
            logger.info("tenant.registration_request_received", { email: req.body.adminEmail });

            const result = await this.uowFactory.execute(async (uow: IUnitOfWork) => {
                const onboardingResult = await this.onboardingService.onboard(uow, req.body, sendProgress);

                // Issue JWT (Step 14)
                const token = jwt.sign(
                    {
                        tenant_id: onboardingResult.tenantId,
                        admin_email: req.body.adminEmail,
                        plan: req.body.plan ?? "free",
                        role: "admin"
                    },
                    env.SESSION_SECRET!,
                    { expiresIn: "15m" }
                );

                return { ...onboardingResult, token };
            });

            res.write(JSON.stringify({ type: 'success', data: result }) + "\n");
            res.end();
        } catch (err: any) {
            logger.error("tenant.registration_failed", { error: err.message, stack: err.stack });
            res.write(JSON.stringify({ type: 'error', message: err.message }) + "\n");
            res.end();
        }
    }
}