* Add Enrich to frontend * Naive backend implementation * Add work email check * Rename Enrich to Quick Action * Refactor logic to a separate service * Refacto to separate IntelligenceService * Small fixes * Missing Break statement * Address PR comments * Create company interface * Improve edge case handling * Use httpService instead of Axios * Fix server tests
53 lines
1.5 KiB
TypeScript
53 lines
1.5 KiB
TypeScript
import { HttpService } from '@nestjs/axios';
|
|
import { Injectable } from '@nestjs/common';
|
|
|
|
import { CompanyInteface } from 'src/core/quick-actions/interfaces/company.interface';
|
|
|
|
import { EnvironmentService } from 'src/integrations/environment/environment.service';
|
|
|
|
@Injectable()
|
|
export class IntelligenceService {
|
|
constructor(
|
|
private readonly environmentService: EnvironmentService,
|
|
private readonly httpService: HttpService,
|
|
) {}
|
|
|
|
async enrichCompany(domainName: string): Promise<CompanyInteface> {
|
|
const enrichedCompany = await this.httpService.axiosRef.get(
|
|
`https://companies.twenty.com/${domainName}`,
|
|
{
|
|
validateStatus: function () {
|
|
// This ensures the promise is always resolved, preventing axios from throwing an error
|
|
return true;
|
|
},
|
|
},
|
|
);
|
|
|
|
if (enrichedCompany.status !== 200) {
|
|
return {};
|
|
}
|
|
|
|
return {
|
|
linkedinLinkUrl: `https://linkedin.com/` + enrichedCompany.data.handle,
|
|
};
|
|
}
|
|
|
|
async completeWithAi(content: string) {
|
|
return this.httpService.axiosRef.post(
|
|
'https://openrouter.ai/api/v1/chat/completions',
|
|
{
|
|
headers: {
|
|
Authorization: `Bearer ${this.environmentService.getOpenRouterApiKey()}`,
|
|
'HTTP-Referer': `https://twenty.com`,
|
|
'X-Title': `Twenty CRM`,
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify({
|
|
model: 'mistralai/mixtral-8x7b-instruct',
|
|
messages: [{ role: 'user', content: content }],
|
|
}),
|
|
},
|
|
);
|
|
}
|
|
}
|