"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); } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.IntegrationsController = void 0; const common_1 = require("@nestjs/common"); const swagger_1 = require("@nestjs/swagger"); const jwt_auth_guard_1 = require("../../auth/guards/jwt-auth.guard"); const tenant_integrations_service_1 = require("../services/tenant-integrations.service"); const integration_credentials_dto_1 = require("../dto/integration-credentials.dto"); const tenant_integration_credential_entity_1 = require("../entities/tenant-integration-credential.entity"); let IntegrationsController = class IntegrationsController { constructor(integrationsService) { this.integrationsService = integrationsService; } async getStatus(req) { return this.integrationsService.getIntegrationStatus(req.user.tenantId); } async getWhatsAppConfig(req) { const credential = await this.integrationsService.getCredential(req.user.tenantId, tenant_integration_credential_entity_1.IntegrationType.WHATSAPP, tenant_integration_credential_entity_1.IntegrationProvider.META); if (!credential) { return { configured: false, usesPlatformNumber: true, message: 'Usando número de plataforma compartido', }; } return { configured: true, usesPlatformNumber: false, isVerified: credential.isVerified, lastVerifiedAt: credential.lastVerifiedAt, hasAccessToken: !!credential.credentials?.['accessToken'], phoneNumberId: credential.credentials?.['phoneNumberId'], }; } async upsertWhatsAppCredentials(req, dto) { const credential = await this.integrationsService.upsertCredential(req.user.tenantId, tenant_integration_credential_entity_1.IntegrationType.WHATSAPP, tenant_integration_credential_entity_1.IntegrationProvider.META, { accessToken: dto.credentials.accessToken, phoneNumberId: dto.credentials.phoneNumberId, businessAccountId: dto.credentials.businessAccountId, verifyToken: dto.credentials.verifyToken, }, {}, req.user.sub); if (dto.credentials.phoneNumberId) { await this.integrationsService.registerWhatsAppNumber(req.user.tenantId, dto.credentials.phoneNumberId, dto.phoneNumber, dto.displayName); } return { success: true, message: 'Credenciales de WhatsApp configuradas', id: credential.id, }; } async deleteWhatsAppCredentials(req) { await this.integrationsService.deleteCredential(req.user.tenantId, tenant_integration_credential_entity_1.IntegrationType.WHATSAPP, tenant_integration_credential_entity_1.IntegrationProvider.META); } async getLLMConfig(req) { const credentials = await this.integrationsService.getCredentials(req.user.tenantId); const llmCred = credentials.find((c) => c.integrationType === tenant_integration_credential_entity_1.IntegrationType.LLM && c.isActive); if (!llmCred) { return { configured: false, usesPlatformDefault: true, message: 'Usando configuración LLM de plataforma', }; } return { configured: true, usesPlatformDefault: false, provider: llmCred.provider, isVerified: llmCred.isVerified, config: { model: llmCred.config?.['model'], maxTokens: llmCred.config?.['maxTokens'], temperature: llmCred.config?.['temperature'], hasSystemPrompt: !!llmCred.config?.['systemPrompt'], }, hasApiKey: !!llmCred.credentials?.['apiKey'], }; } async upsertLLMCredentials(req, dto) { const credential = await this.integrationsService.upsertCredential(req.user.tenantId, tenant_integration_credential_entity_1.IntegrationType.LLM, dto.provider, { apiKey: dto.credentials.apiKey }, dto.config || {}, req.user.sub); return { success: true, message: 'Credenciales de LLM configuradas', id: credential.id, provider: dto.provider, }; } async deleteLLMCredentials(req, provider) { await this.integrationsService.deleteCredential(req.user.tenantId, tenant_integration_credential_entity_1.IntegrationType.LLM, provider); } async getAllCredentials(req) { const credentials = await this.integrationsService.getCredentials(req.user.tenantId); return credentials.map((c) => ({ id: c.id, integrationType: c.integrationType, provider: c.provider, hasCredentials: !!c.credentials && Object.keys(c.credentials).length > 0, isActive: c.isActive, isVerified: c.isVerified, lastVerifiedAt: c.lastVerifiedAt, verificationError: c.verificationError, config: c.config, createdAt: c.createdAt, updatedAt: c.updatedAt, })); } async createCredential(req, dto) { const credential = await this.integrationsService.upsertCredential(req.user.tenantId, dto.integrationType, dto.provider, dto.credentials, dto.config, req.user.sub); return { success: true, message: 'Credencial creada', id: credential.id, }; } async toggleCredential(req, type, provider, body) { const credential = await this.integrationsService.toggleCredential(req.user.tenantId, type, provider, body.isActive); return { success: true, isActive: credential.isActive, }; } }; exports.IntegrationsController = IntegrationsController; __decorate([ (0, common_1.Get)('status'), (0, swagger_1.ApiOperation)({ summary: 'Obtener estado de todas las integraciones del tenant' }), (0, swagger_1.ApiResponse)({ status: 200, type: integration_credentials_dto_1.IntegrationStatusResponseDto }), __param(0, (0, common_1.Request)()), __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", Promise) ], IntegrationsController.prototype, "getStatus", null); __decorate([ (0, common_1.Get)('whatsapp'), (0, swagger_1.ApiOperation)({ summary: 'Obtener configuración de WhatsApp' }), __param(0, (0, common_1.Request)()), __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", Promise) ], IntegrationsController.prototype, "getWhatsAppConfig", null); __decorate([ (0, common_1.Put)('whatsapp'), (0, swagger_1.ApiOperation)({ summary: 'Configurar credenciales de WhatsApp propias' }), __param(0, (0, common_1.Request)()), __param(1, (0, common_1.Body)()), __metadata("design:type", Function), __metadata("design:paramtypes", [Object, integration_credentials_dto_1.UpsertWhatsAppCredentialsDto]), __metadata("design:returntype", Promise) ], IntegrationsController.prototype, "upsertWhatsAppCredentials", null); __decorate([ (0, common_1.Delete)('whatsapp'), (0, common_1.HttpCode)(common_1.HttpStatus.NO_CONTENT), (0, swagger_1.ApiOperation)({ summary: 'Eliminar credenciales de WhatsApp (volver a usar plataforma)' }), __param(0, (0, common_1.Request)()), __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", Promise) ], IntegrationsController.prototype, "deleteWhatsAppCredentials", null); __decorate([ (0, common_1.Get)('llm'), (0, swagger_1.ApiOperation)({ summary: 'Obtener configuración de LLM' }), __param(0, (0, common_1.Request)()), __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", Promise) ], IntegrationsController.prototype, "getLLMConfig", null); __decorate([ (0, common_1.Put)('llm'), (0, swagger_1.ApiOperation)({ summary: 'Configurar credenciales de LLM propias' }), __param(0, (0, common_1.Request)()), __param(1, (0, common_1.Body)()), __metadata("design:type", Function), __metadata("design:paramtypes", [Object, integration_credentials_dto_1.UpsertLLMCredentialsDto]), __metadata("design:returntype", Promise) ], IntegrationsController.prototype, "upsertLLMCredentials", null); __decorate([ (0, common_1.Delete)('llm/:provider'), (0, common_1.HttpCode)(common_1.HttpStatus.NO_CONTENT), (0, swagger_1.ApiOperation)({ summary: 'Eliminar credenciales de LLM (volver a usar plataforma)' }), __param(0, (0, common_1.Request)()), __param(1, (0, common_1.Param)('provider')), __metadata("design:type", Function), __metadata("design:paramtypes", [Object, String]), __metadata("design:returntype", Promise) ], IntegrationsController.prototype, "deleteLLMCredentials", null); __decorate([ (0, common_1.Get)('credentials'), (0, swagger_1.ApiOperation)({ summary: 'Obtener todas las credenciales del tenant' }), __param(0, (0, common_1.Request)()), __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", Promise) ], IntegrationsController.prototype, "getAllCredentials", null); __decorate([ (0, common_1.Post)('credentials'), (0, swagger_1.ApiOperation)({ summary: 'Crear credencial de integración genérica' }), __param(0, (0, common_1.Request)()), __param(1, (0, common_1.Body)()), __metadata("design:type", Function), __metadata("design:paramtypes", [Object, integration_credentials_dto_1.CreateIntegrationCredentialDto]), __metadata("design:returntype", Promise) ], IntegrationsController.prototype, "createCredential", null); __decorate([ (0, common_1.Put)('credentials/:type/:provider/toggle'), (0, swagger_1.ApiOperation)({ summary: 'Activar/desactivar una credencial' }), __param(0, (0, common_1.Request)()), __param(1, (0, common_1.Param)('type')), __param(2, (0, common_1.Param)('provider')), __param(3, (0, common_1.Body)()), __metadata("design:type", Function), __metadata("design:paramtypes", [Object, String, String, Object]), __metadata("design:returntype", Promise) ], IntegrationsController.prototype, "toggleCredential", null); exports.IntegrationsController = IntegrationsController = __decorate([ (0, swagger_1.ApiTags)('Integrations'), (0, swagger_1.ApiBearerAuth)(), (0, common_1.UseGuards)(jwt_auth_guard_1.JwtAuthGuard), (0, common_1.Controller)('integrations'), __metadata("design:paramtypes", [tenant_integrations_service_1.TenantIntegrationsService]) ], IntegrationsController); //# sourceMappingURL=integrations.controller.js.map