Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

code improvements

Pedro Ladeira0f018ce865fc
diff --git a/apps/web/lib/lemonsqueezy/import-commissions.ts b/apps/web/lib/lemonsqueezy/import-commissions.ts
index 3b05c3a190..354779facb 100644
--- a/apps/web/lib/lemonsqueezy/import-commissions.ts
+++ b/apps/web/lib/lemonsqueezy/import-commissions.ts
@@ -333,6 +333,7 @@ async function processSaleEvents({
prisma.link.findMany({
where: {
domain,
+ programId: program.id,
key: {
in: affiliateIds,
},
diff --git a/apps/web/lib/lemonsqueezy/import-customers.ts b/apps/web/lib/lemonsqueezy/import-customers.ts
index 7196480f5a..68f074cc65 100644
--- a/apps/web/lib/lemonsqueezy/import-customers.ts
+++ b/apps/web/lib/lemonsqueezy/import-customers.ts
@@ -74,6 +74,7 @@ export async function importCustomers(payload: LemonSqueezyImportPayload) {
const links = await prisma.link.findMany({
where: {
domain: program.domain,
+ programId: program.id,
key: {
in: affiliateIds,
},
@@ -120,7 +121,7 @@ export async function importCustomers(payload: LemonSqueezyImportPayload) {
const customerChunks = chunk(newCustomers, 10);
for (const customerChunk of customerChunks) {
- await Promise.all(
+ await Promise.allSettled(
customerChunk.map((customer) => {
// Deterministic: first affiliate_id that has an imported partner link
const affiliateId = customer.affiliate_ids.find((id) =>
@@ -221,6 +222,16 @@ async function createCustomer({
timestamp: clickedAt.toISOString(),
});
+ if (!clickData) {
+ await logImportError({
+ ...commonImportLogInputs,
+ code: "CLICK_NOT_FOUND",
+ message: `Failed to record click for customer ${externalId}.`,
+ });
+
+ return;
+ }
+
const clickEvent = clickEventSchemaTB.parse({
...clickData,
bot: 0,
diff --git a/apps/web/lib/lemonsqueezy/import-partners.ts b/apps/web/lib/lemonsqueezy/import-partners.ts
index babffde3d8..0f9fa032fe 100644
--- a/apps/web/lib/lemonsqueezy/import-partners.ts
+++ b/apps/web/lib/lemonsqueezy/import-partners.ts
@@ -99,7 +99,7 @@ export async function importPartners(payload: LemonSqueezyImportPayload) {
}
if (activeAffiliates.length > 0) {
- await Promise.allSettled(
+ const results = await Promise.allSettled(
activeAffiliates.map((affiliate) =>
createPartnerAndLinks({
workspace,
@@ -111,6 +111,25 @@ export async function importPartners(payload: LemonSqueezyImportPayload) {
}),
),
);
+
+ const failures = results.flatMap((result, index) =>
+ result.status === "rejected"
+ ? [{ affiliate: activeAffiliates[index], reason: result.reason }]
+ : [],
+ );
+
+ if (failures.length > 0) {
+ await logImportError(
+ failures.map(({ affiliate, reason }) => ({
+ ...commonImportLogInputs,
+ entity_id: affiliate.id,
+ code: "PARTNER_NOT_FOUND" as const,
+ message: `Failed to import affiliate ${affiliate.id}: ${
+ reason instanceof Error ? reason.message : String(reason)
+ }`,
+ })),
+ );
+ }
}
if (notImportedAffiliates.length > 0) {
@@ -220,7 +239,7 @@ async function createPartnerAndLinks({
if (links.length > 0 && links.some((link) => link.key === affiliate.id)) {
console.log(
- `Partner ${partner.email} already has a link with key ${affiliate.id}, skipping...`,
+ `Partner ${partner.id} already has a link with key ${affiliate.id}, skipping...`,
);
return;
}
diff --git a/apps/web/ui/modals/import-lemonsqueezy-modal.tsx b/apps/web/ui/modals/import-lemonsqueezy-modal.tsx
index dfbba0d753..a24dfa1099 100644
--- a/apps/web/ui/modals/import-lemonsqueezy-modal.tsx
+++ b/apps/web/ui/modals/import-lemonsqueezy-modal.tsx
@@ -137,9 +137,11 @@ function TokenForm({
const { executeAsync, isPending } = useAction(setLemonSqueezyTokenAction, {
onSuccess: ({ data }) => {
- if (data?.stores) {
+ if (data?.stores?.length) {
setStores(data.stores);
setStep("select-store");
+ } else {
+ toast.error("No Lemon Squeezy stores are available for this API key.");
}
},
onError: ({ error }) => {