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.

Fix rewardful importer

Steven Tey6682bde53c9c
diff --git a/apps/web/lib/actions/partners/set-rewardful-token.ts b/apps/web/lib/actions/partners/set-rewardful-token.ts
index ab37646a1a..195b1da47d 100644
--- a/apps/web/lib/actions/partners/set-rewardful-token.ts
+++ b/apps/web/lib/actions/partners/set-rewardful-token.ts
@@ -1,5 +1,6 @@
"use server";
+import { RewardfulApi } from "@/lib/rewardful/api";
import { rewardfulImporter } from "@/lib/rewardful/importer";
import { z } from "zod";
import { getProgramOrThrow } from "../../api/programs/get-program-or-throw";
@@ -22,6 +23,14 @@ export const setRewardfulTokenAction = authActionClient
programId,
});
+ const rewardfulApi = new RewardfulApi({ token });
+ try {
+ await rewardfulApi.listCampaigns();
+ } catch (error) {
+ console.error(error);
+ throw new Error("Invalid Rewardful token");
+ }
+
await rewardfulImporter.setCredentials(programId, {
userId: user.id,
token,
diff --git a/apps/web/lib/rewardful/api.ts b/apps/web/lib/rewardful/api.ts
index d383a4d43e..3d9e25a95c 100644
--- a/apps/web/lib/rewardful/api.ts
+++ b/apps/web/lib/rewardful/api.ts
@@ -17,7 +17,7 @@ class RewardfulApiError extends DubApiError {
}
export class RewardfulApi {
- private readonly baseUrl = "https://api.getrewardful.com";
+ private readonly baseUrl = "https://api.getrewardful.com/v1";
private readonly token: string;
constructor({ token }: { token: string }) {
@@ -31,14 +31,16 @@ export class RewardfulApi {
},
});
+ if (!response.ok) {
+ const error = await response.text();
+ console.log("Rewardful API Error:", error);
+ throw new RewardfulApiError(error);
+ }
+
const data = await response.json();
console.debug("Rewardful API Response:", { url, data });
- if (!response.ok) {
- throw new RewardfulApiError(data.error);
- }
-
return data as T;
}
@@ -49,7 +51,11 @@ export class RewardfulApi {
}
async listCampaigns() {
- return this.fetch<RewardfulCampaign[]>(`${this.baseUrl}/campaigns`);
+ const { data } = await this.fetch<{ data: RewardfulCampaign[] }>(
+ `${this.baseUrl}/campaigns`,
+ );
+
+ return data;
}
async listAffiliates({
@@ -59,9 +65,11 @@ export class RewardfulApi {
campaignId: string;
page?: number;
}) {
- return this.fetch<RewardfulAffiliate[]>(
+ const { data } = await this.fetch<{ data: RewardfulAffiliate[] }>(
`${this.baseUrl}/affiliates?expand[]=links&page=${page}&limit=${PAGE_LIMIT}&campaign_id=${campaignId}`,
);
+
+ return data;
}
async listReferrals({ page = 1 }: { page?: number }) {
@@ -72,8 +80,10 @@ export class RewardfulApi {
searchParams.append("page", page.toString());
searchParams.append("limit", PAGE_LIMIT.toString());
- return this.fetch<RewardfulReferral[]>(
+ const { data } = await this.fetch<{ data: RewardfulReferral[] }>(
`${this.baseUrl}/referrals?${searchParams.toString()}`,
);
+
+ return data;
}
}
diff --git a/apps/web/lib/rewardful/import-affiliates.ts b/apps/web/lib/rewardful/import-affiliates.ts
index dc4ed823ad..acdf4461e6 100644
--- a/apps/web/lib/rewardful/import-affiliates.ts
+++ b/apps/web/lib/rewardful/import-affiliates.ts
@@ -46,7 +46,13 @@ export async function importAffiliates({
}
const activeAffiliates = affiliates.filter(
- (affiliate) => affiliate.state === "active",
+ (affiliate) =>
+ // only active affiliates
+ affiliate.state === "active" &&
+ // have more than 1 lead or joined in the last 6 months
+ (affiliate.leads > 0 ||
+ new Date(affiliate.created_at) >
+ new Date(Date.now() - 6 * 30 * 24 * 60 * 60 * 1000)),
);
if (activeAffiliates.length > 0) {
@@ -95,9 +101,7 @@ async function createPartnerAndLinks({
name: `${affiliate.first_name} ${affiliate.last_name}`,
email: affiliate.email,
},
- update: {
- //
- },
+ update: {},
});
await prisma.programEnrollment.upsert({
diff --git a/apps/web/lib/rewardful/import-campaign.ts b/apps/web/lib/rewardful/import-campaign.ts
index eca67a9e97..e7ed4353f7 100644
--- a/apps/web/lib/rewardful/import-campaign.ts
+++ b/apps/web/lib/rewardful/import-campaign.ts
@@ -4,11 +4,12 @@ import { RewardfulApi } from "./api";
import { rewardfulImporter } from "./importer";
export async function importCampaign({ programId }: { programId: string }) {
- const { token } = await rewardfulImporter.getCredentials(programId);
+ const { token, campaignId } =
+ await rewardfulImporter.getCredentials(programId);
const rewardfulApi = new RewardfulApi({ token });
- const campaign = await rewardfulApi.retrieveCampaign(programId);
+ const campaign = await rewardfulApi.retrieveCampaign(campaignId);
const {
url,
diff --git a/apps/web/ui/modals/import-rewardful-modal.tsx b/apps/web/ui/modals/import-rewardful-modal.tsx
index 950ba98992..b32d269121 100644
--- a/apps/web/ui/modals/import-rewardful-modal.tsx
+++ b/apps/web/ui/modals/import-rewardful-modal.tsx
@@ -71,10 +71,11 @@ function ImportRewardfulModal({
isLoading: isLoadingCampaigns,
mutate,
} = useSWRImmutable<RewardfulCampaign[]>(
- workspaceId &&
- showImportRewardfulModal &&
+ showImportRewardfulModal &&
+ program?.id &&
+ workspaceId &&
step === "campaigns" &&
- `/api/mock/rewardful/campaigns?workspaceId=${workspaceId}`,
+ `/api/programs/${program.id}/rewardful/campaigns?workspaceId=${workspaceId}`,
fetcher,
);