File size: 4,443 Bytes
9f29339
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
var OutboundWebhookController_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.OutboundWebhookController = void 0;
const common_1 = require("@nestjs/common");
const better_auth_identity_service_1 = require("../auth/better-auth-identity.service");
const outbound_webhook_service_1 = require("./outbound-webhook.service");
let OutboundWebhookController = OutboundWebhookController_1 = class OutboundWebhookController {
    service;
    betterAuthService;
    logger = new common_1.Logger(OutboundWebhookController_1.name);
    constructor(service, betterAuthService) {
        this.service = service;
        this.betterAuthService = betterAuthService;
    }
    async registerEndpoint(req, body) {
        const userId = await this.getUserId(req);
        if (!userId) {
            throw new common_1.UnauthorizedException("Authentication required");
        }
        const endpoint = await this.service.registerEndpoint(body.workspaceId, body.url, body.events || []);
        const { secret, ...safeEndpoint } = endpoint;
        return { ...safeEndpoint, secret };
    }
    async listEndpoints(req, workspaceId) {
        const userId = await this.getUserId(req);
        if (!userId) {
            throw new common_1.UnauthorizedException("Authentication required");
        }
        if (!workspaceId) {
            throw new common_1.UnauthorizedException("workspaceId is required");
        }
        return this.service.listEndpoints(workspaceId);
    }
    async deleteEndpoint(req, endpointId) {
        const userId = await this.getUserId(req);
        if (!userId) {
            throw new common_1.UnauthorizedException("Authentication required");
        }
        await this.service.deleteEndpoint(endpointId);
        return { success: true };
    }
    async getUserId(req) {
        const auth = req.headers.authorization;
        if (!auth?.startsWith("Bearer "))
            return null;
        const identity = await this.betterAuthService.getSessionFromBearer(auth);
        return identity?.userId ?? null;
    }
};
exports.OutboundWebhookController = OutboundWebhookController;
__decorate([
    (0, common_1.Post)("endpoints"),
    __param(0, (0, common_1.Req)()),
    __param(1, (0, common_1.Body)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, Object]),
    __metadata("design:returntype", Promise)
], OutboundWebhookController.prototype, "registerEndpoint", null);
__decorate([
    (0, common_1.Get)("endpoints"),
    __param(0, (0, common_1.Req)()),
    __param(1, (0, common_1.Query)("workspaceId")),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, String]),
    __metadata("design:returntype", Promise)
], OutboundWebhookController.prototype, "listEndpoints", null);
__decorate([
    (0, common_1.Delete)("endpoints/:id"),
    __param(0, (0, common_1.Req)()),
    __param(1, (0, common_1.Param)("id")),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, String]),
    __metadata("design:returntype", Promise)
], OutboundWebhookController.prototype, "deleteEndpoint", null);
exports.OutboundWebhookController = OutboundWebhookController = OutboundWebhookController_1 = __decorate([
    (0, common_1.Controller)("webhooks"),
    __metadata("design:paramtypes", [outbound_webhook_service_1.OutboundWebhookService,
        better_auth_identity_service_1.BetterAuthService])
], OutboundWebhookController);
//# sourceMappingURL=outbound-webhook.controller.js.map