Skip to content

Commit

Permalink
⚡ Updated sync loggers
Browse files Browse the repository at this point in the history
  • Loading branch information
naelob committed Sep 15, 2024
1 parent daea3e5 commit 086d8a7
Show file tree
Hide file tree
Showing 104 changed files with 247 additions and 317 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -210,25 +210,34 @@ export class IngestDataService {

// insert the files in our s3 bucket so we can process them for our RAG
if (vertical === 'filestorage' && commonObject === 'file') {
const filesInfo: FileInfo[] = data
.filter((file: FileStorageFile) => file.file_url && file.mime_type)
.map((file: FileStorageFile) => ({
id: file.id_fs_file,
url: file.file_url,
provider: integrationId,
s3Key: `${projectId}/${linkedUserId}/${
file.id_fs_file
}.${getFileExtensionFromMimeType(file.mime_type)}`,
fileType: getFileExtensionFromMimeType(file.mime_type),
}));
try {
const filesInfo: FileInfo[] = data
.filter((file: FileStorageFile) => file.file_url && file.mime_type)
.map((file: FileStorageFile) => ({
id: file.id_fs_file,
url: file.file_url,
provider: integrationId,
s3Key: `${projectId}/${linkedUserId}/${
file.id_fs_file
}.${getFileExtensionFromMimeType(file.mime_type)}`,
fileType: getFileExtensionFromMimeType(file.mime_type),
}));

if (filesInfo.length > 0) {
console.log('During sync, found files to process for RAG...');
await this.ragService.queueDocumentProcessing(
filesInfo,
projectId,
linkedUserId,
if (filesInfo.length > 0) {
this.logger.log('During sync, found files to process for RAG...');
await this.ragService.queueDocumentProcessing(
filesInfo,
projectId,
linkedUserId,
);
}
} catch (ragError) {
this.logger.error(
`Error processing files for RAG: ${ragError.message}`,
ragError.stack,
);
// Optionally, you could create an event to log this error
// await this.prisma.events.create({...});
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ export class ServiceRegistry {
getService(integrationId: string): IConnectionService {
const service = this.serviceMap.get(integrationId);
if (!service) {
throw new ReferenceError(
`Service not found for integration ID: ${integrationId}`,
);
return null;
}
return service;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ export class ServiceRegistry {
getService(integrationId: string): IConnectionService {
const service = this.serviceMap.get(integrationId);
if (!service) {
throw new ReferenceError(
`Service not found for integration ID: ${integrationId}`,
);
return null;
}
return service;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ export class ServiceRegistry {
getService(integrationId: string): IConnectionService {
const service = this.serviceMap.get(integrationId);
if (!service) {
throw new ReferenceError(
`Service not found for integration ID: ${integrationId}`,
);
return null;
}
return service;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ export class ServiceRegistry {
getService(integrationId: string): IConnectionService {
const service = this.serviceMap.get(integrationId);
if (!service) {
throw new ReferenceError(
`Service not found for integration ID: ${integrationId}`,
);
return null;
}
return service;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ export class ServiceRegistry {
getService(integrationId: string): IConnectionService {
const service = this.serviceMap.get(integrationId);
if (!service) {
throw new ReferenceError(
`Service not found for integration ID: ${integrationId}`,
);
return null;
}
return service;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,19 +190,19 @@ export class GoogleDriveConnectionService extends AbstractBaseConnectionService
async handleTokenRefresh(opts: RefreshParams) {
try {
const { connectionId, refreshToken, projectId } = opts;

const CREDENTIALS = (await this.cService.getCredentials(
projectId,
this.type,
)) as OAuth2AuthData;

const formData = new URLSearchParams({
grant_type: 'refresh_token',
refresh_token: this.cryptoService.decrypt(refreshToken),
client_id: CREDENTIALS.CLIENT_ID,
client_secret: CREDENTIALS.CLIENT_SECRET,
});

const res = await axios.post(
`https://oauth2.googleapis.com/token`,
formData.toString(),
Expand All @@ -216,20 +216,22 @@ export class GoogleDriveConnectionService extends AbstractBaseConnectionService
},
);
const data: GoogleDriveOAuthResponse = res.data;

// Prepare the update data
const updateData: any = {
access_token: this.cryptoService.encrypt(data.access_token),
expiration_timestamp: new Date(
new Date().getTime() + Number(data.expires_in) * 1000,
),
};

// Only update the refresh token if a new one is provided
if (data.refresh_token) {
updateData.refresh_token = this.cryptoService.encrypt(data.refresh_token);
updateData.refresh_token = this.cryptoService.encrypt(
data.refresh_token,
);
}

await this.prisma.connections.update({
where: {
id_connection: connectionId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ export class ServiceRegistry {
getService(integrationId: string): IConnectionService {
const service = this.serviceMap.get(integrationId);
if (!service) {
throw new ReferenceError(
`Service not found for integration ID: ${integrationId}`,
);
return null;
}
return service;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ export class ServiceRegistry {
getService(integrationId: string): IConnectionService {
const service = this.serviceMap.get(integrationId);
if (!service) {
throw new ReferenceError(
`Service not found for integration ID: ${integrationId}`,
);
return null;
}
return service;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ export class ServiceRegistry {
getService(integrationId: string): IConnectionService {
const service = this.serviceMap.get(integrationId);
if (!service) {
throw new ReferenceError(
`Service not found for integration ID: ${integrationId}`,
);
return null;
}
return service;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ export class ServiceRegistry {
getService(integrationId: string): IConnectionService {
const service = this.serviceMap.get(integrationId);
if (!service) {
throw new ReferenceError(
`Service not found for integration ID: ${integrationId}`,
);
return null;
}
return service;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ export class ServiceRegistry {
getService(integrationId: string): IConnectionService {
const service = this.serviceMap.get(integrationId);
if (!service) {
throw new ReferenceError(
`Service not found for integration ID: ${integrationId}`,
);
return null;
}
return service;
}
Expand Down
3 changes: 2 additions & 1 deletion packages/api/src/@core/sync/sync.processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ export class SyncProcessor {
try {
const service = this.registry.getService(vertical, commonObject);
if (!service) {
throw new Error(
this.logger.warn(
`No service found for vertical ${vertical} and common object ${commonObject}`,
);
return { status: 'skipped', reason: 'No service found' };
}

await service.kickstartSync(projectId);
Expand Down
35 changes: 35 additions & 0 deletions packages/api/src/@core/utils/exception.filter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import {
ExceptionFilter,
Catch,
ArgumentsHost,
HttpException,
HttpStatus,
} from '@nestjs/common';
import { HttpAdapterHost } from '@nestjs/core';

@Catch()
export class AllExceptionsFilter implements ExceptionFilter {
constructor(private readonly httpAdapterHost: HttpAdapterHost) {}

catch(exception: unknown, host: ArgumentsHost): void {
const { httpAdapter } = this.httpAdapterHost;
const ctx = host.switchToHttp();

const httpStatus =
exception instanceof HttpException
? exception.getStatus()
: HttpStatus.INTERNAL_SERVER_ERROR;

const responseBody = {
statusCode: httpStatus,
timestamp: new Date().toISOString(),
path: httpAdapter.getRequestUrl(ctx.getRequest()),
message:
exception instanceof Error
? exception.message
: 'Internal server error',
};

httpAdapter.reply(ctx.getResponse(), responseBody, httpStatus);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class ServiceRegistry {
getService(integrationId: string): IAccountService {
const service = this.serviceMap.get(integrationId);
if (!service) {
throw new ReferenceError();
return null;
}
return service;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ export class ServiceRegistry {
getService(integrationId: string): IAddressService {
const service = this.serviceMap.get(integrationId);
if (!service) {
throw new ReferenceError(
`Service not found for integration ID: ${integrationId}`,
);
return null;
}
return service;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ export class ServiceRegistry {
getService(integrationId: string): IAttachmentService {
const service = this.serviceMap.get(integrationId);
if (!service) {
throw new ReferenceError(
`Service not found for integration ID: ${integrationId}`,
);
return null;
}
return service;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ export class ServiceRegistry {
getService(integrationId: string): IBalanceSheetService {
const service = this.serviceMap.get(integrationId);
if (!service) {
throw new ReferenceError(
`Service not found for integration ID: ${integrationId}`,
);
return null;
}
return service;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ export class ServiceRegistry {
getService(integrationId: string): ICashflowStatementService {
const service = this.serviceMap.get(integrationId);
if (!service) {
throw new ReferenceError(
`Service not found for integration ID: ${integrationId}`,
);
return null;
}
return service;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ export class ServiceRegistry {
getService(integrationId: string): ICompanyInfoService {
const service = this.serviceMap.get(integrationId);
if (!service) {
throw new ReferenceError(
`Service not found for integration ID: ${integrationId}`,
);
return null;
}
return service;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ export class ServiceRegistry {
getService(integrationId: string): IContactService {
const service = this.serviceMap.get(integrationId);
if (!service) {
throw new ReferenceError(
`Service not found for integration ID: ${integrationId}`,
);
return null;
}
return service;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ export class ServiceRegistry {
getService(integrationId: string): ICreditNoteService {
const service = this.serviceMap.get(integrationId);
if (!service) {
throw new ReferenceError(
`Service not found for integration ID: ${integrationId}`,
);
return null;
}
return service;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ export class ServiceRegistry {
getService(integrationId: string): IExpenseService {
const service = this.serviceMap.get(integrationId);
if (!service) {
throw new ReferenceError(
`Service not found for integration ID: ${integrationId}`,
);
return null;
}
return service;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ export class ServiceRegistry {
getService(integrationId: string): IIncomeStatementService {
const service = this.serviceMap.get(integrationId);
if (!service) {
throw new ReferenceError(
`Service not found for integration ID: ${integrationId}`,
);
return null;
}
return service;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ export class ServiceRegistry {
getService(integrationId: string): IInvoiceService {
const service = this.serviceMap.get(integrationId);
if (!service) {
throw new ReferenceError(
`Service not found for integration ID: ${integrationId}`,
);
return null;
}
return service;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ export class ServiceRegistry {
getService(integrationId: string): IItemService {
const service = this.serviceMap.get(integrationId);
if (!service) {
throw new ReferenceError(
`Service not found for integration ID: ${integrationId}`,
);
return null;
}
return service;
}
Expand Down
Loading

0 comments on commit 086d8a7

Please sign in to comment.