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.

feat: Improve Rewardful import with user context and dynamic link matching

Kiran Kb8e1798e8a34
diff --git a/apps/web/app/api/programs/[programId]/rewardful/import/route.ts b/apps/web/app/api/programs/[programId]/rewardful/import/route.ts
index 4f4ed25268..2c350a2c89 100644
--- a/apps/web/app/api/programs/[programId]/rewardful/import/route.ts
+++ b/apps/web/app/api/programs/[programId]/rewardful/import/route.ts
@@ -12,21 +12,24 @@ const schema = z.object({
// POST /api/programs/[programId]/rewardful/import - import a rewardful program
// This is entrypoint for the rewardful importer
-export const POST = withWorkspace(async ({ workspace, params, req }) => {
- const { programId } = params;
- const { apiKey, campaignId } = schema.parse(await parseRequestBody(req));
+export const POST = withWorkspace(
+ async ({ workspace, session, params, req }) => {
+ const { programId } = params;
+ const { apiKey, campaignId } = schema.parse(await parseRequestBody(req));
- const program = await getProgramOrThrow({
- workspaceId: workspace.id,
- programId,
- });
+ const program = await getProgramOrThrow({
+ workspaceId: workspace.id,
+ programId,
+ });
- // Start a background job to import
- await startRewardfulImport({
- programId,
- campaignId,
- apiKey,
- });
+ // Start a background job to import
+ await startRewardfulImport({
+ programId,
+ campaignId,
+ apiKey,
+ userId: session.user.id,
+ });
- return NextResponse.json(program);
-});
+ return NextResponse.json(program);
+ },
+);
diff --git a/apps/web/app/api/rewardful/affiliates/route.ts b/apps/web/app/api/rewardful/affiliates/route.ts
index 90a4ad7e55..64d72cd776 100644
--- a/apps/web/app/api/rewardful/affiliates/route.ts
+++ b/apps/web/app/api/rewardful/affiliates/route.ts
@@ -21,7 +21,7 @@ export async function GET(request: NextRequest) {
{
id: "eb844960-6c42-4a3b-8009-f588a42d8506",
url: "http://www.example.com/?via=adam",
- token: "adam",
+ token: "ref1",
visitors: 100,
leads: 42,
conversions: 18,
@@ -43,7 +43,7 @@ export async function GET(request: NextRequest) {
{
id: "cd123456-7890-4def-b123-456789abcdef",
url: "http://www.example.com/?via=sarah",
- token: "sarah",
+ token: "ref2",
visitors: 250,
leads: 85,
conversions: 30,
@@ -57,7 +57,7 @@ export async function GET(request: NextRequest) {
first_name: "Michael",
last_name: "Brown",
- state: "inactive",
+ state: "active",
visitors: 150,
leads: 35,
conversions: 12,
@@ -65,7 +65,7 @@ export async function GET(request: NextRequest) {
{
id: "ef123456-7890-4abc-def1-23456789abcd",
url: "http://www.example.com/?via=michael",
- token: "michael",
+ token: "ref3",
visitors: 150,
leads: 35,
conversions: 12,
@@ -87,7 +87,7 @@ export async function GET(request: NextRequest) {
{
id: "gh123456-7890-4ijk-lmno-pqrstuvwxyz1",
url: "http://www.example.com/?via=emily",
- token: "emily",
+ token: "ref4",
visitors: 300,
leads: 120,
conversions: 45,
@@ -109,7 +109,7 @@ export async function GET(request: NextRequest) {
{
id: "ij123456-7890-4klm-nopq-rstuvwxyz123",
url: "http://www.example.com/?via=david",
- token: "david",
+ token: "ref5",
visitors: 180,
leads: 60,
conversions: 25,
@@ -131,7 +131,7 @@ export async function GET(request: NextRequest) {
{
id: "kl123456-7890-4mno-pqrs-tuvwxyz12345",
url: "http://www.example.com/?via=lisa",
- token: "lisa",
+ token: "ref6",
visitors: 220,
leads: 75,
conversions: 28,
@@ -145,7 +145,7 @@ export async function GET(request: NextRequest) {
first_name: "James",
last_name: "Anderson",
- state: "inactive",
+ state: "active",
visitors: 90,
leads: 20,
conversions: 8,
@@ -153,7 +153,7 @@ export async function GET(request: NextRequest) {
{
id: "mn123456-7890-4opq-rstu-vwxyz123456",
url: "http://www.example.com/?via=james",
- token: "james",
+ token: "ref7",
visitors: 90,
leads: 20,
conversions: 8,
@@ -175,7 +175,7 @@ export async function GET(request: NextRequest) {
{
id: "op123456-7890-4qrs-tuv-wxyz1234567",
url: "http://www.example.com/?via=emma",
- token: "emma",
+ token: "ref8",
visitors: 280,
leads: 95,
conversions: 40,
@@ -197,7 +197,7 @@ export async function GET(request: NextRequest) {
{
id: "qr123456-7890-4stu-vwxy-z123456789",
url: "http://www.example.com/?via=robert",
- token: "robert",
+ token: "ref9",
visitors: 160,
leads: 55,
conversions: 22,
@@ -219,7 +219,7 @@ export async function GET(request: NextRequest) {
{
id: "st123456-7890-4uvw-xyz1-234567890ab",
url: "http://www.example.com/?via=olivia",
- token: "olivia",
+ token: "ref10",
visitors: 200,
leads: 70,
conversions: 32,
diff --git a/apps/web/lib/rewardful/api.ts b/apps/web/lib/rewardful/api.ts
index e5467786fd..d73fcd9618 100644
--- a/apps/web/lib/rewardful/api.ts
+++ b/apps/web/lib/rewardful/api.ts
@@ -11,6 +11,26 @@ class RewardfulApiError extends DubApiError {
}
}
+interface ImportConfig {
+ apiKey: string;
+ campaignId: string;
+ userId: string;
+}
+
+export async function getImportConfig(programId: string) {
+ const config = await redis.get(`rewardful:import:${programId}`);
+
+ if (!config) {
+ throw new Error("Rewardful import data not found.", {
+ cause: {
+ programId,
+ },
+ });
+ }
+
+ return config as ImportConfig;
+}
+
export class RewardfulApi {
private readonly programId: string; // Dub program id
// private readonly baseUrl = "https://api.getrewardful.com/v1";
@@ -20,22 +40,8 @@ export class RewardfulApi {
this.programId = programId;
}
- private async fetchApiKey() {
- const apiKey = await redis.get(`rewardful:import:${this.programId}`);
-
- if (!apiKey) {
- throw new Error("Rewardful API key not found.", {
- cause: {
- programId: this.programId,
- },
- });
- }
-
- return apiKey;
- }
-
private async getAuthHeader() {
- const apiKey = await this.fetchApiKey();
+ const { apiKey } = await getImportConfig(this.programId);
return {
Authorization: `Basic ${Buffer.from(`${apiKey}:`).toString("base64")}`,
diff --git a/apps/web/lib/rewardful/importer.ts b/apps/web/lib/rewardful/importer.ts
index edbe94f55c..f83d1e8096 100644
--- a/apps/web/lib/rewardful/importer.ts
+++ b/apps/web/lib/rewardful/importer.ts
@@ -11,7 +11,7 @@ import { recordClick } from "../tinybird/record-click";
import { recordLead } from "../tinybird/record-lead";
import { WorkspaceProps } from "../types";
import { clickEventSchemaTB } from "../zod/schemas/clicks";
-import { RewardfulApi } from "./api";
+import { getImportConfig, RewardfulApi } from "./api";
import { RewardfulAffiliate, RewardfulReferral } from "./types";
const MAX_BATCHES = 5;
@@ -27,16 +27,19 @@ export async function startRewardfulImport({
programId,
apiKey,
campaignId,
+ userId,
}: {
programId: string;
apiKey: string;
campaignId: string;
+ userId: string;
}) {
await redis.set(
`rewardful:import:${programId}`,
{
apiKey,
campaignId,
+ userId,
},
{
ex: CACHE_EXPIRY,
@@ -150,11 +153,7 @@ async function createPartnerAndLinks({
return;
}
- const session = {
- user: {
- id: "cm1ypncqa0000tc44pfgxp6qs",
- },
- };
+ const { userId } = await getImportConfig(program.id);
const links = affiliate.links;
@@ -171,7 +170,7 @@ async function createPartnerAndLinks({
trackConversion: true,
folderId: program.defaultFolderId,
},
- userId: session.user.id,
+ userId,
skipProgramChecks: true,
workspace: workspace as WorkspaceProps,
});
@@ -257,10 +256,10 @@ async function createReferral({
}) {
const link = await prisma.link.findFirst({
where: {
- id: "link_0ntBrVm4VqPSpcLaLTbNAV5y", // TODO: Update with proper query
- // key: referral.link.token,
- // domain: program.domain!,
- // programId: program.id,
+ // id: "link_0ntBrVm4VqPSpcLaLTbNAV5y", // TODO: Update with proper query
+ key: referral.link.token,
+ domain: program.domain!,
+ programId: program.id,
},
});