"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 DiditWebhookController_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.DiditWebhookController = void 0;
const common_1 = require("@nestjs/common");
const didit_webhook_service_1 = require("./didit-webhook.service");
const kyc_verification_service_1 = require("./kyc-verification.service");
let DiditWebhookController = DiditWebhookController_1 = class DiditWebhookController {
    constructor(diditWebhookService, kycVerificationService) {
        this.diditWebhookService = diditWebhookService;
        this.kycVerificationService = kycVerificationService;
        this.logger = new common_1.Logger(DiditWebhookController_1.name);
    }
    async handleDiditWebhook(timestamp, signatureV2, signatureSimple, body) {
        const result = this.diditWebhookService.verify({ timestamp, signatureV2, signatureSimple }, body);
        const sessionId = body?.session_id;
        const status = body?.status;
        const webhookType = body?.webhook_type;
        const vendorData = body?.vendor_data;
        const sessionKind = body?.session_kind;
        this.logger.log(`Webhook verified (${result.method}) type=${webhookType} kind=${sessionKind} session=${sessionId} status=${status} vendor_data=${vendorData}`);
        try {
            await this.kycVerificationService.handleDiditWebhookPayload(body);
        }
        catch (err) {
            this.logger.warn(`Unable to persist KYC webhook session=${sessionId}: ${err?.message ?? err}`);
        }
        return { ok: true };
    }
};
exports.DiditWebhookController = DiditWebhookController;
__decorate([
    (0, common_1.Post)(),
    __param(0, (0, common_1.Headers)('x-timestamp')),
    __param(1, (0, common_1.Headers)('x-signature-v2')),
    __param(2, (0, common_1.Headers)('x-signature-simple')),
    __param(3, (0, common_1.Body)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, String, String, Object]),
    __metadata("design:returntype", Promise)
], DiditWebhookController.prototype, "handleDiditWebhook", null);
exports.DiditWebhookController = DiditWebhookController = DiditWebhookController_1 = __decorate([
    (0, common_1.Controller)('webhooks/didit'),
    __metadata("design:paramtypes", [didit_webhook_service_1.DiditWebhookService,
        kyc_verification_service_1.KycVerificationService])
], DiditWebhookController);
//# sourceMappingURL=didit-webhook.controller.js.map