michangarrito/apps/whatsapp-service/dist/common/credentials-provider.service.js
rckrdmrd 48dea7a5d0 feat: Initial commit - michangarrito
Marketplace móvil para negocios locales mexicanos.

Estructura inicial:
- apps/backend (NestJS API)
- apps/frontend (React Web)
- apps/mobile (Expo/React Native)
- apps/mcp-server (Claude MCP Server)
- apps/whatsapp-service (WhatsApp Business API)
- database/ (PostgreSQL DDL)
- docs/ (Documentación)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-07 04:41:02 -06:00

182 lines
7.6 KiB
JavaScript

"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 __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
var CredentialsProviderService_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CredentialsProviderService = void 0;
const common_1 = require("@nestjs/common");
const config_1 = require("@nestjs/config");
const axios_1 = __importDefault(require("axios"));
let CredentialsProviderService = CredentialsProviderService_1 = class CredentialsProviderService {
constructor(configService) {
this.configService = configService;
this.logger = new common_1.Logger(CredentialsProviderService_1.name);
this.cache = new Map();
this.CACHE_TTL = 5 * 60 * 1000;
const backendUrl = this.configService.get('BACKEND_URL', 'http://localhost:3141/api/v1');
this.backendClient = axios_1.default.create({
baseURL: backendUrl,
timeout: 5000,
});
}
async getWhatsAppCredentials(tenantId) {
if (!tenantId) {
return this.getPlatformWhatsAppCredentials();
}
const cacheKey = `whatsapp:${tenantId}`;
const cached = this.getFromCache(cacheKey);
if (cached) {
return cached;
}
try {
const { data } = await this.backendClient.get(`/internal/integrations/${tenantId}/whatsapp`, {
headers: {
'X-Internal-Key': this.configService.get('INTERNAL_API_KEY'),
},
});
if (data.configured && data.credentials) {
const credentials = {
accessToken: data.credentials.accessToken,
phoneNumberId: data.credentials.phoneNumberId,
businessAccountId: data.credentials.businessAccountId,
verifyToken: data.credentials.verifyToken,
isFromPlatform: false,
tenantId,
};
this.setCache(cacheKey, credentials);
return credentials;
}
}
catch (error) {
this.logger.warn(`Failed to get WhatsApp credentials for tenant ${tenantId}, using platform: ${error.message}`);
}
return this.getPlatformWhatsAppCredentials();
}
getPlatformWhatsAppCredentials() {
return {
accessToken: this.configService.get('WHATSAPP_ACCESS_TOKEN'),
phoneNumberId: this.configService.get('WHATSAPP_PHONE_NUMBER_ID'),
businessAccountId: this.configService.get('WHATSAPP_BUSINESS_ACCOUNT_ID'),
verifyToken: this.configService.get('WHATSAPP_VERIFY_TOKEN'),
isFromPlatform: true,
};
}
async getLLMConfig(tenantId) {
if (!tenantId) {
return this.getPlatformLLMConfig();
}
const cacheKey = `llm:${tenantId}`;
const cached = this.getFromCache(cacheKey);
if (cached) {
return cached;
}
try {
const { data } = await this.backendClient.get(`/internal/integrations/${tenantId}/llm`, {
headers: {
'X-Internal-Key': this.configService.get('INTERNAL_API_KEY'),
},
});
if (data.configured && data.credentials) {
const config = {
apiKey: data.credentials.apiKey,
provider: data.provider,
model: data.config?.model || 'gpt-4o-mini',
maxTokens: data.config?.maxTokens || 1000,
temperature: data.config?.temperature || 0.7,
baseUrl: data.config?.baseUrl || this.getDefaultBaseUrl(data.provider),
systemPrompt: data.config?.systemPrompt,
isFromPlatform: false,
tenantId,
};
this.setCache(cacheKey, config);
return config;
}
}
catch (error) {
this.logger.warn(`Failed to get LLM config for tenant ${tenantId}, using platform: ${error.message}`);
}
return this.getPlatformLLMConfig();
}
getPlatformLLMConfig() {
const provider = this.configService.get('LLM_PROVIDER', 'openai');
return {
apiKey: this.configService.get('OPENAI_API_KEY') || this.configService.get('LLM_API_KEY'),
provider,
model: this.configService.get('LLM_MODEL', 'gpt-4o-mini'),
maxTokens: parseInt(this.configService.get('LLM_MAX_TOKENS', '1000')),
temperature: parseFloat(this.configService.get('LLM_TEMPERATURE', '0.7')),
baseUrl: this.configService.get('LLM_BASE_URL', this.getDefaultBaseUrl(provider)),
isFromPlatform: true,
};
}
async resolveTenantFromPhoneNumberId(phoneNumberId) {
const platformPhoneNumberId = this.configService.get('WHATSAPP_PHONE_NUMBER_ID');
if (phoneNumberId === platformPhoneNumberId) {
return null;
}
const cacheKey = `phoneid:${phoneNumberId}`;
const cached = this.getFromCache(cacheKey);
if (cached) {
return cached;
}
try {
const { data } = await this.backendClient.get(`/internal/integrations/resolve-tenant/${phoneNumberId}`, {
headers: {
'X-Internal-Key': this.configService.get('INTERNAL_API_KEY'),
},
});
if (data.tenantId) {
this.setCache(cacheKey, data.tenantId);
return data.tenantId;
}
}
catch (error) {
this.logger.warn(`Failed to resolve tenant for phoneNumberId ${phoneNumberId}`);
}
return null;
}
invalidateCache(tenantId) {
this.cache.delete(`whatsapp:${tenantId}`);
this.cache.delete(`llm:${tenantId}`);
}
getFromCache(key) {
const entry = this.cache.get(key);
if (entry && entry.expiresAt > Date.now()) {
return entry.data;
}
this.cache.delete(key);
return null;
}
setCache(key, data) {
this.cache.set(key, {
data,
expiresAt: Date.now() + this.CACHE_TTL,
});
}
getDefaultBaseUrl(provider) {
const defaults = {
openai: 'https://api.openai.com/v1',
openrouter: 'https://openrouter.ai/api/v1',
anthropic: 'https://api.anthropic.com/v1',
ollama: 'http://localhost:11434/v1',
azure_openai: '',
};
return defaults[provider] || 'https://api.openai.com/v1';
}
};
exports.CredentialsProviderService = CredentialsProviderService;
exports.CredentialsProviderService = CredentialsProviderService = CredentialsProviderService_1 = __decorate([
(0, common_1.Injectable)(),
__metadata("design:paramtypes", [config_1.ConfigService])
], CredentialsProviderService);
//# sourceMappingURL=credentials-provider.service.js.map