-
Notifications
You must be signed in to change notification settings - Fork 202
Incremental sync for Attio provider #701
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -9,9 +9,10 @@ import { EncryptionService } from '@@core/@core-services/encryption/encryption.s | |||||
| import { ApiResponse } from '@@core/utils/types'; | ||||||
| import { ICompanyService } from '@crm/company/types'; | ||||||
| import { ServiceRegistry } from '../registry.service'; | ||||||
| import { AttioCompanyInput, AttioCompanyOutput } from './types'; | ||||||
| import { AttioCompanyInput, AttioCompanyOutput, paginationType } from './types'; | ||||||
| import { SyncParam } from '@@core/utils/types/interface'; | ||||||
| import { OriginalCompanyOutput } from '@@core/utils/types/original/original.crm'; | ||||||
| import { v4 as uuidv4 } from 'uuid'; | ||||||
|
|
||||||
| @Injectable() | ||||||
| export class AttioService implements ICompanyService { | ||||||
|
|
@@ -74,20 +75,134 @@ export class AttioService implements ICompanyService { | |||||
| vertical: 'crm', | ||||||
| }, | ||||||
| }); | ||||||
| const resp = await axios.post( | ||||||
| `${connection.account_url}/v2/objects/companies/records/query`, | ||||||
| {}, | ||||||
| { | ||||||
| headers: { | ||||||
| 'Content-Type': 'application/json', | ||||||
| Authorization: `Bearer ${this.cryptoService.decrypt( | ||||||
| connection.access_token, | ||||||
| )}`, | ||||||
| }, | ||||||
|
|
||||||
| const paginationTrackInfo = await this.prisma.vertical_objects_sync_track_data.findFirst({ | ||||||
| where: { | ||||||
| id_connection: connection.id_connection, | ||||||
| vertical: 'crm', | ||||||
| provider_slug: 'attio', | ||||||
| object: 'company', | ||||||
| }, | ||||||
| ); | ||||||
| }); | ||||||
|
|
||||||
| let respData: AttioCompanyOutput[] = []; | ||||||
| let initialOffset: number = 0; | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider removing the type annotation. The type annotation Apply this diff to remove the type annotation: -let initialOffset: number = 0;
+let initialOffset = 0;Committable suggestion
Suggested change
ToolsBiome
|
||||||
|
|
||||||
| if (!paginationTrackInfo) { | ||||||
| // Intial sync | ||||||
| try { | ||||||
| while (true) { | ||||||
| const resp = await axios.post( | ||||||
| `${connection.account_url}/v2/objects/companies/records/query`, | ||||||
| { | ||||||
| "sorts": [ | ||||||
| { | ||||||
| "attribute": "created_at", | ||||||
| "direction": "asc" | ||||||
| } | ||||||
| ], | ||||||
| "offset": initialOffset, | ||||||
| "limit": 500 | ||||||
| }, | ||||||
| { | ||||||
| headers: { | ||||||
| accept: 'application/json', | ||||||
| 'Content-Type': 'application/json', | ||||||
| Authorization: `Bearer ${this.cryptoService.decrypt( | ||||||
| connection.access_token, | ||||||
| )}`, | ||||||
| }, | ||||||
| }, | ||||||
| ); | ||||||
|
|
||||||
|
|
||||||
| respData.push(...resp.data.data); | ||||||
| initialOffset = initialOffset + resp.data.data.length; | ||||||
|
|
||||||
| if (resp.data.data.length < 500) { | ||||||
| break; | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| catch (error) { | ||||||
| this.logger.log(`Error in initial sync ${error.message} and last offset is ${initialOffset}`); | ||||||
| } | ||||||
|
|
||||||
| } | ||||||
| else { | ||||||
| // Incremental sync | ||||||
| const currentPaginationData = paginationTrackInfo.data as paginationType; | ||||||
| initialOffset = currentPaginationData.offset; | ||||||
|
|
||||||
| try { | ||||||
| while (true) { | ||||||
| const resp = await axios.post( | ||||||
| `${connection.account_url}/v2/objects/companies/records/query`, | ||||||
| { | ||||||
| "sorts": [ | ||||||
| { | ||||||
| "attribute": "created_at", | ||||||
| "direction": "asc" | ||||||
| } | ||||||
| ], | ||||||
| "offset": initialOffset, | ||||||
| "limit": 500 | ||||||
| }, | ||||||
| { | ||||||
| headers: { | ||||||
| accept: 'application/json', | ||||||
| 'Content-Type': 'application/json', | ||||||
| Authorization: `Bearer ${this.cryptoService.decrypt( | ||||||
| connection.access_token, | ||||||
| )}` | ||||||
| } | ||||||
| } | ||||||
| ); | ||||||
|
|
||||||
| respData.push(...resp.data.data); | ||||||
| initialOffset = initialOffset + resp.data.data.length; | ||||||
|
|
||||||
| if (resp.data.data.length < 500) { | ||||||
| break; | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| catch (error) { | ||||||
| this.logger.log(`Error in incremental sync ${error.message} and last offset is ${initialOffset}`); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| // create or update records | ||||||
| if (paginationTrackInfo) { | ||||||
| await this.prisma.vertical_objects_sync_track_data.update({ | ||||||
| where: { | ||||||
| id_vertical_objects_sync_track_data: paginationTrackInfo.id_vertical_objects_sync_track_data, | ||||||
| }, | ||||||
| data: { | ||||||
| data: { | ||||||
| offset: initialOffset, | ||||||
| }, | ||||||
| }, | ||||||
| }); | ||||||
| } | ||||||
| else { | ||||||
| await this.prisma.vertical_objects_sync_track_data.create({ | ||||||
| data: { | ||||||
| id_vertical_objects_sync_track_data: uuidv4(), | ||||||
| vertical: 'crm', | ||||||
| provider_slug: 'attio', | ||||||
| object: 'company', | ||||||
| pagination_type: 'offset', | ||||||
| id_connection: connection.id_connection, | ||||||
| data: { | ||||||
| offset: initialOffset, | ||||||
| }, | ||||||
| }, | ||||||
| }); | ||||||
| } | ||||||
|
|
||||||
| return { | ||||||
| data: resp.data.data, | ||||||
| data: respData, | ||||||
| message: 'Attio companies retrieved', | ||||||
| statusCode: 200, | ||||||
| }; | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use
constinstead oflet.The variable
respDatais only assigned once during its initialization and is not reassigned throughout the code. Usingconstinstead ofletwould ensure thatrespDatacannot be accidentally reassigned, improving code quality and readability.Apply this diff to use
const:Committable suggestion
Tools
Biome
Consider removing the type annotation.
The type annotation
AttioCompanyOutput[]is not necessary as TypeScript can infer the type ofrespDatabased on its initialization to an empty array[]. Removing the type annotation would not affect the functionality of the code and would improve readability.Apply this diff to remove the type annotation:
Committable suggestion
Tools
Biome