WIP - fix AI suggestions during call progress
This commit is contained in:
@@ -16,26 +16,45 @@ import { TenantId } from './tenant.decorator';
|
||||
export class TenantController {
|
||||
constructor(private readonly tenantDbService: TenantDatabaseService) {}
|
||||
|
||||
/**
|
||||
* Helper to find tenant by ID or domain
|
||||
*/
|
||||
private async findTenant(identifier: string) {
|
||||
const centralPrisma = getCentralPrisma();
|
||||
|
||||
// Check if identifier is a CUID (tenant ID) or a domain
|
||||
const isCUID = /^c[a-z0-9]{24}$/i.test(identifier);
|
||||
|
||||
if (isCUID) {
|
||||
// Look up by tenant ID directly
|
||||
return centralPrisma.tenant.findUnique({
|
||||
where: { id: identifier },
|
||||
select: { id: true, integrationsConfig: true },
|
||||
});
|
||||
} else {
|
||||
// Look up by domain
|
||||
const domainRecord = await centralPrisma.domain.findUnique({
|
||||
where: { domain: identifier },
|
||||
include: { tenant: { select: { id: true, integrationsConfig: true } } },
|
||||
});
|
||||
return domainRecord?.tenant;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get integrations configuration for the current tenant
|
||||
*/
|
||||
@Get('integrations')
|
||||
async getIntegrationsConfig(@TenantId() domain: string) {
|
||||
const centralPrisma = getCentralPrisma();
|
||||
|
||||
// Look up tenant by domain
|
||||
const domainRecord = await centralPrisma.domain.findUnique({
|
||||
where: { domain },
|
||||
include: { tenant: { select: { id: true, integrationsConfig: true } } },
|
||||
});
|
||||
async getIntegrationsConfig(@TenantId() tenantIdentifier: string) {
|
||||
const tenant = await this.findTenant(tenantIdentifier);
|
||||
|
||||
if (!domainRecord?.tenant || !domainRecord.tenant.integrationsConfig) {
|
||||
if (!tenant || !tenant.integrationsConfig) {
|
||||
return { data: null };
|
||||
}
|
||||
|
||||
// Decrypt the config
|
||||
const config = this.tenantDbService.decryptIntegrationsConfig(
|
||||
domainRecord.tenant.integrationsConfig as any,
|
||||
tenant.integrationsConfig as any,
|
||||
);
|
||||
|
||||
// Return config with sensitive fields masked
|
||||
@@ -49,31 +68,26 @@ export class TenantController {
|
||||
*/
|
||||
@Put('integrations')
|
||||
async updateIntegrationsConfig(
|
||||
@TenantId() domain: string,
|
||||
@TenantId() tenantIdentifier: string,
|
||||
@Body() body: { integrationsConfig: any },
|
||||
) {
|
||||
const { integrationsConfig } = body;
|
||||
|
||||
if (!domain) {
|
||||
throw new Error('Domain is missing from request');
|
||||
if (!tenantIdentifier) {
|
||||
throw new Error('Tenant identifier is missing from request');
|
||||
}
|
||||
|
||||
// Look up tenant by domain
|
||||
const centralPrisma = getCentralPrisma();
|
||||
const domainRecord = await centralPrisma.domain.findUnique({
|
||||
where: { domain },
|
||||
include: { tenant: { select: { id: true, integrationsConfig: true } } },
|
||||
});
|
||||
const tenant = await this.findTenant(tenantIdentifier);
|
||||
|
||||
if (!domainRecord?.tenant) {
|
||||
throw new Error(`Tenant with domain ${domain} not found`);
|
||||
if (!tenant) {
|
||||
throw new Error(`Tenant with identifier ${tenantIdentifier} not found`);
|
||||
}
|
||||
|
||||
// Merge with existing config to preserve masked values
|
||||
let finalConfig = integrationsConfig;
|
||||
if (domainRecord.tenant.integrationsConfig) {
|
||||
if (tenant.integrationsConfig) {
|
||||
const existingConfig = this.tenantDbService.decryptIntegrationsConfig(
|
||||
domainRecord.tenant.integrationsConfig as any,
|
||||
tenant.integrationsConfig as any,
|
||||
);
|
||||
|
||||
// Replace masked values with actual values from existing config
|
||||
@@ -86,8 +100,9 @@ export class TenantController {
|
||||
);
|
||||
|
||||
// Update in database
|
||||
const centralPrisma = getCentralPrisma();
|
||||
await centralPrisma.tenant.update({
|
||||
where: { id: domainRecord.tenant.id },
|
||||
where: { id: tenant.id },
|
||||
data: {
|
||||
integrationsConfig: encryptedConfig as any,
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user