Skip to content

Commit

Permalink
fix: batchDelete returns incorrect sync result (#1907)
Browse files Browse the repository at this point in the history
## Describe your changes
Sync can return incorrect deletion results. 
Fixing by getting directly getting the modified rows when `soft_delete =
true`

## Issue ticket number and link

https://linear.app/nango/issue/NAN-641/batchdelete-doesnt-return-the-correct-result

## Checklist before requesting a review (skip if just adding/editing
APIs & templates)
- [ ] I added tests, otherwise the reason is: 
- [ ] I added observability, otherwise the reason is:
- [ ] I added analytics, otherwise the reason is:
  • Loading branch information
TBonnin committed Mar 26, 2024
1 parent 452a806 commit 8ddd5e7
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
9 changes: 7 additions & 2 deletions packages/shared/lib/services/sync/data/data.service.ts
Expand Up @@ -35,13 +35,18 @@ export async function upsert(
try {
const encryptedRecords = encryptionManager.encryptDataRecords(recordsWithoutDuplicates);

await schema().from(RECORDS_TABLE).insert(encryptedRecords).onConflict(['nango_connection_id', 'external_id', 'model']).merge();
const externalIds = await schema()
.from<DataRecord>(RECORDS_TABLE)
.insert(encryptedRecords)
.onConflict(['nango_connection_id', 'external_id', 'model'])
.merge()
.returning('external_id');

if (softDelete) {
return {
success: true,
summary: {
deletedKeys: [...addedKeys, ...updatedKeys],
deletedKeys: externalIds.map(({ external_id }) => external_id),
addedKeys: [],
updatedKeys: []
}
Expand Down
Expand Up @@ -164,7 +164,7 @@ describe('Running sync', () => {
{ id: '1', name: 'a' },
{ id: '2', name: 'b' }
];
const expectedResult = { added: 0, updated: 0, deleted: 0 };
const expectedResult = { added: 0, updated: 0, deleted: 2 };
const { records } = await verifySyncRun(rawRecords, rawRecords, false, expectedResult, softDelete);
expect(records).lengthOf(2);
records.forEach((record) => {
Expand Down

0 comments on commit 8ddd5e7

Please sign in to comment.