-
Notifications
You must be signed in to change notification settings - Fork 2.8k
PartnerStack importer #2642
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
PartnerStack importer #2642
Changes from all commits
Commits
Show all changes
37 commits
Select commit
Hold shift + click to select a range
ef0e24e
wip
devkiran 7bc5cd7
add modal
devkiran 3bc2ab7
wip
devkiran 62d0866
wip
devkiran 083daeb
add modal
devkiran 59f4efc
Implement PartnerStack import functionality and update UI components.
devkiran d86dff3
Refactor PartnerStack import logic to use new schemas and API methods
devkiran cdca031
Update types.ts
devkiran 9eecfca
import links
devkiran 7113d82
Implement customer import functionality in PartnerStack API and updat…
devkiran c624301
Remove update-stripe-customers schema, add partnerStackCommission sch…
devkiran 0f34439
Implement importCommissions functionality in PartnerStack API, update…
devkiran 013068b
Merge branch 'main' into partnerstack
devkiran 57f246e
Refactor PartnerStack import to use public and secret keys
devkiran 3d347ba
fix import-links
devkiran 3f0bf93
fix import-customers
devkiran d56aa35
Update api.ts
devkiran 0437249
Update import-customers.ts
devkiran 68d5694
wip import commissions
devkiran fc691d6
Merge branch 'main' into partnerstack
devkiran d60c059
Add updateStripeCustomers functionality to PartnerStack import process
devkiran 7187aa4
format
devkiran 595bddd
Merge branch 'main' into partnerstack
devkiran b4f83a0
Update importer.ts
devkiran 1aedb3e
Update route.ts
devkiran fb1edc4
Update import-links.ts
devkiran 999687a
Update import-partnerstack-modal.tsx
devkiran a23d858
Update import-customers.ts
devkiran 866f3e5
Merge branch 'main' into partnerstack
steven-tey 2d1a255
Merge branch 'main' into partnerstack
steven-tey ff53932
Merge branch 'main' into partnerstack
steven-tey d244a44
Update import-partners.ts
devkiran 5f4219e
Update import-customers.ts
devkiran d0a0280
Update import-commissions.ts
devkiran cf751d0
Update import-customers.ts
devkiran 73cfd95
Update import-partners.ts
devkiran 39ede7f
hold → fraud, small change to source/target emails
steven-tey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| import { handleAndReturnErrorResponse } from "@/lib/api/errors"; | ||
| import { verifyQstashSignature } from "@/lib/cron/verify-qstash"; | ||
| import { importCommissions } from "@/lib/partnerstack/import-commissions"; | ||
| import { importCustomers } from "@/lib/partnerstack/import-customers"; | ||
| import { importLinks } from "@/lib/partnerstack/import-links"; | ||
| import { importPartners } from "@/lib/partnerstack/import-partners"; | ||
| import { partnerStackImportPayloadSchema } from "@/lib/partnerstack/schemas"; | ||
| import { updateStripeCustomers } from "@/lib/partnerstack/update-stripe-customers"; | ||
| import { NextResponse } from "next/server"; | ||
|
|
||
| export const dynamic = "force-dynamic"; | ||
|
|
||
| export async function POST(req: Request) { | ||
| try { | ||
| const rawBody = await req.text(); | ||
|
|
||
| await verifyQstashSignature({ | ||
| req, | ||
| rawBody, | ||
| }); | ||
|
|
||
| const payload = partnerStackImportPayloadSchema.parse(JSON.parse(rawBody)); | ||
|
|
||
| switch (payload.action) { | ||
| case "import-partners": | ||
| await importPartners(payload); | ||
| break; | ||
| case "import-links": | ||
| await importLinks(payload); | ||
| break; | ||
| case "import-customers": | ||
| await importCustomers(payload); | ||
| break; | ||
| case "import-commissions": | ||
| await importCommissions(payload); | ||
| break; | ||
| case "update-stripe-customers": | ||
| await updateStripeCustomers(payload); | ||
| break; | ||
| default: | ||
| throw new Error(`Unknown action: ${payload.action}`); | ||
| } | ||
|
|
||
| return NextResponse.json("OK"); | ||
| } catch (error) { | ||
| return handleAndReturnErrorResponse(error); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
apps/web/lib/actions/partners/start-partnerstack-import.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| "use server"; | ||
|
|
||
| import { getDefaultProgramIdOrThrow } from "@/lib/api/programs/get-default-program-id-or-throw"; | ||
| import { getProgramOrThrow } from "@/lib/api/programs/get-program-or-throw"; | ||
| import { PartnerStackApi } from "@/lib/partnerstack/api"; | ||
| import { partnerStackImporter } from "@/lib/partnerstack/importer"; | ||
| import { z } from "zod"; | ||
| import { authActionClient } from "../safe-action"; | ||
|
|
||
| const schema = z.object({ | ||
| workspaceId: z.string(), | ||
| publicKey: z.string().min(1), | ||
| secretKey: z.string().min(1), | ||
| }); | ||
|
|
||
| export const startPartnerStackImportAction = authActionClient | ||
| .schema(schema) | ||
| .action(async ({ ctx, parsedInput }) => { | ||
| const { workspace, user } = ctx; | ||
| const { publicKey, secretKey } = parsedInput; | ||
|
|
||
| const programId = getDefaultProgramIdOrThrow(workspace); | ||
|
|
||
| const program = await getProgramOrThrow({ | ||
| workspaceId: workspace.id, | ||
| programId, | ||
| }); | ||
|
|
||
| if (!program.domain) { | ||
| throw new Error("Program domain is not set."); | ||
| } | ||
|
|
||
| if (!program.url) { | ||
| throw new Error("Program URL is not set."); | ||
| } | ||
|
|
||
| const partnerStackApi = new PartnerStackApi({ | ||
| publicKey, | ||
| secretKey, | ||
| }); | ||
|
|
||
| await partnerStackApi.testConnection(); | ||
|
|
||
| await partnerStackImporter.setCredentials(workspace.id, { | ||
| publicKey, | ||
| secretKey, | ||
| }); | ||
|
|
||
| await partnerStackImporter.queue({ | ||
| programId, | ||
| userId: user.id, | ||
| action: "import-partners", | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| import { | ||
| partnerStackCommission, | ||
| partnerStackCustomer, | ||
| partnerStackLink, | ||
| partnerStackPartner, | ||
| } from "./schemas"; | ||
| import { | ||
| PartnerStackCommission, | ||
| PartnerStackCustomer, | ||
| PartnerStackLink, | ||
| PartnerStackListResponse, | ||
| PartnerStackPartner, | ||
| } from "./types"; | ||
|
|
||
| const PAGE_LIMIT = 100; | ||
|
|
||
| export class PartnerStackApi { | ||
| private readonly baseUrl = "https://api.partnerstack.com/api/v2"; | ||
| private readonly publicKey: string; | ||
| private readonly secretKey: string; | ||
|
|
||
| constructor({ | ||
| publicKey, | ||
| secretKey, | ||
| }: { | ||
| publicKey: string; | ||
| secretKey: string; | ||
| }) { | ||
| this.publicKey = publicKey; | ||
| this.secretKey = secretKey; | ||
| } | ||
|
|
||
| private async fetch<T>(path: string): Promise<T> { | ||
| const token = Buffer.from(`${this.publicKey}:${this.secretKey}`).toString( | ||
| "base64", | ||
| ); | ||
|
|
||
| const response = await fetch(`${this.baseUrl}${path}`, { | ||
| headers: { | ||
| Authorization: `Basic ${token}`, | ||
| }, | ||
| }); | ||
|
|
||
| if (!response.ok) { | ||
| const error = await response.json(); | ||
|
|
||
| console.error("PartnerStack API Error:", error); | ||
|
|
||
| throw new Error(error.message || "Unknown error from PartnerStack API."); | ||
| } | ||
|
|
||
| return await response.json(); | ||
| } | ||
|
|
||
| async testConnection() { | ||
| try { | ||
| await this.fetch("/customers?limit=1"); | ||
| return true; | ||
| } catch (error) { | ||
| throw new Error("Invalid PartnerStack API token."); | ||
| } | ||
| } | ||
|
|
||
| async listPartners({ startingAfter }: { startingAfter?: string }) { | ||
| const searchParams = new URLSearchParams(); | ||
| searchParams.append("approved_status", "approved"); | ||
| searchParams.append("limit", PAGE_LIMIT.toString()); | ||
|
|
||
| if (startingAfter) { | ||
| searchParams.append("starting_after", startingAfter); | ||
| } | ||
|
|
||
| const { | ||
| data: { items }, | ||
| } = await this.fetch<PartnerStackListResponse<PartnerStackPartner>>( | ||
| `/partnerships?${searchParams.toString()}`, | ||
| ); | ||
|
|
||
| return partnerStackPartner.array().parse(items); | ||
| } | ||
|
|
||
| async listLinks({ identifier }: { identifier: string }) { | ||
| const { | ||
| data: { items }, | ||
| } = await this.fetch<PartnerStackListResponse<PartnerStackLink>>( | ||
| `/links/partnership/${identifier}`, | ||
| ); | ||
|
|
||
| return partnerStackLink.array().parse(items); | ||
| } | ||
|
|
||
| async listCustomers({ startingAfter }: { startingAfter?: string }) { | ||
| const searchParams = new URLSearchParams(); | ||
| searchParams.append("limit", PAGE_LIMIT.toString()); | ||
|
|
||
| if (startingAfter) { | ||
| searchParams.append("starting_after", startingAfter); | ||
| } | ||
|
|
||
| const { | ||
| data: { items }, | ||
| } = await this.fetch<PartnerStackListResponse<PartnerStackCustomer>>( | ||
| `/customers?${searchParams.toString()}`, | ||
| ); | ||
|
|
||
| return partnerStackCustomer.array().parse(items); | ||
| } | ||
|
|
||
| async listCommissions({ startingAfter }: { startingAfter?: string }) { | ||
| const searchParams = new URLSearchParams(); | ||
| searchParams.append("limit", PAGE_LIMIT.toString()); | ||
|
|
||
| if (startingAfter) { | ||
| searchParams.append("starting_after", startingAfter); | ||
| } | ||
|
|
||
| const { | ||
| data: { items }, | ||
| } = await this.fetch<PartnerStackListResponse<PartnerStackCommission>>( | ||
| `/rewards?${searchParams.toString()}`, | ||
| ); | ||
|
|
||
| return partnerStackCommission.array().parse(items); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.