Skip to content

Commit

Permalink
fix customer import logic
Browse files Browse the repository at this point in the history
  • Loading branch information
emilwidlund committed Feb 20, 2025
1 parent 3b19d0e commit 400e559
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "polar-migrate",
"version": "0.1.7",
"version": "0.1.8",
"license": "Apache-2.0",
"bin": "bin/cli.js",
"type": "module",
Expand Down
4 changes: 1 addition & 3 deletions src/cli.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,7 @@ meow(
});
}),
);
} catch (e) {
console.error(e);
}
} catch (e) {}

const customers = await customersMessage(
importCustomers(polar, store, organization),
Expand Down
36 changes: 33 additions & 3 deletions src/customers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ const importCustomer = async (
},
});
} catch (error) {
console.error("Failed to create customer:", error);
return null;
}
};
Expand All @@ -37,12 +36,43 @@ export const importCustomers = async (
filter: {
storeId: store.id,
},
page: {
number: 1,
size: 50,
},
});

const allCustomers = [];
let currentPage = 1;
const lastPage = customers.data?.meta.page.lastPage ?? 1;

// Get first page results
if (customers.data?.data) {
allCustomers.push(...customers.data.data);
}

// Get remaining pages
while (currentPage < lastPage) {
currentPage++;
const nextPage = await listCustomers({
filter: {
storeId: store.id,
},
page: {
number: currentPage,
size: 50,
},
});

if (nextPage.data?.data) {
allCustomers.push(...nextPage.data.data);
}
}

return promiseAllInBatches(
(customer) => importCustomer(polar, customer, organization),
customers.data?.data ?? [],
20,
allCustomers,
50,
);
};

Expand Down

0 comments on commit 400e559

Please sign in to comment.