Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix(jobs): logs track_deletes results #3493

Merged
merged 2 commits into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/jobs/lib/execution/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ export async function handleSyncSuccess({ nangoProps }: { nangoProps: NangoProps
syncId: nangoProps.syncId,
generation: nangoProps.syncJobId
});
await logCtx.info(`${model}: "track_deletes" post deleted ${deletedKeys.length} records`);
}

const updatedResults: Record<string, SyncResult> = {
Expand Down
20 changes: 10 additions & 10 deletions packages/shared/lib/services/sync/job.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,12 @@ export const updateLatestJobSyncStatus = async (sync_id: string, status: SyncSta
*/
export const updateSyncJobResult = async (id: number, result: SyncResultByModel, model: string): Promise<SyncJob> => {
return await db.knex.transaction(async (trx) => {
const { result: existingResult } = await trx.from<SyncJob>(SYNC_JOB_TABLE).select('result').forUpdate().where({ id }).first();
const row = await trx.from<SyncJob>(SYNC_JOB_TABLE).select<Pick<SyncJob, 'result'>>('result').forUpdate().where({ id }).first();
if (!row) {
throw new Error('Failed to query sync job');
}

const { result: existingResult } = row;
if (!existingResult || Object.keys(existingResult).length === 0) {
const [updatedRow] = await trx
.from<SyncJob>(SYNC_JOB_TABLE)
Expand All @@ -133,21 +137,17 @@ export const updateSyncJobResult = async (id: number, result: SyncResultByModel,
const { added, updated, deleted } = existingResult[model] || { added: 0, updated: 0, deleted: 0 };

const incomingResult = result[model];
const finalResult = {
const deletedValue = Number(deleted) || 0;
const incomingDeletedValue = Number(incomingResult?.deleted) || 0;
const finalResult: SyncResultByModel = {
...existingResult,
[model]: {
added: Number(added) + Number(incomingResult?.added),
updated: Number(updated) + Number(incomingResult?.updated)
updated: Number(updated) + Number(incomingResult?.updated),
deleted: deletedValue + incomingDeletedValue
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why it was done conditionally, maybe it's wrong, let me know

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think this was from legacy as we didn't want to show deletes at all since that wasn't a thing in the initial implementation.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it also what we send back in the webhook? Can customers have logic not expecting deleted attributes?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We always send at least deleted: 0 in the webhook by default (see: webhooks/lib/sync.ts:101)
So I don't think it's an issue here

}
};

const deletedValue = Number(deleted) || 0;
const incomingDeletedValue = Number(incomingResult?.deleted) || 0;

if (deletedValue !== 0 || incomingDeletedValue !== 0) {
finalResult[model].deleted = deletedValue + incomingDeletedValue;
}

const [updatedRow] = await trx
.from<SyncJob>(SYNC_JOB_TABLE)
.where({ id, deleted: false })
Expand Down
Loading