diff --git a/apps/web/app/api/user/brands/route.ts b/apps/web/app/api/user/brands/route.ts index 636a20e9..ec6eeaf7 100644 --- a/apps/web/app/api/user/brands/route.ts +++ b/apps/web/app/api/user/brands/route.ts @@ -138,6 +138,46 @@ export const POST = withSession(async ({ req, session }) => { { status: 500 }, ); } + } else if (advertiserId === "6") { // eBay + try { + const { ebayClientId, ebayClientSecret } = relationship; + + if (!ebayClientId || !ebayClientSecret) { + return NextResponse.json( + { error: "Missing eBay credentials." }, + { status: 400 } + ); + } + + // Example eBay API integration + const ebayUrl = "https://api.ebay.com/affiliate/links"; + const headers = { + Authorization: `Bearer ${ebayClientSecret}`, + "Content-Type": "application/json", + }; + + const response = await fetch(ebayUrl, { + method: "GET", + headers, + }); + + if (!response.ok) { + throw new Error("Failed to fetch data from eBay"); + } + + const data = await response.json(); + + // Process eBay data as needed + // For example, create or update brands based on eBay data + + return NextResponse.json({ message: "eBay integration successful." }); + } catch (error) { + console.error("Error processing eBay advertiser:", error); + return NextResponse.json( + { error: "Internal server error" }, + { status: 500 }, + ); + } } else if (advertiserId === "2") { const clientId = relationship.clientId; const encryptedClientSecret = relationship.encryptedClientSecret; diff --git a/apps/web/app/api/user/networks/route.ts b/apps/web/app/api/user/networks/route.ts index 1968f2f5..7cc2b9d8 100644 --- a/apps/web/app/api/user/networks/route.ts +++ b/apps/web/app/api/user/networks/route.ts @@ -1,6 +1,7 @@ import { encrypt, withSession } from "@/lib/auth"; import prisma from "@/lib/prisma"; import { NextResponse } from "next/server"; +import { NextApiRequest, NextApiResponse } from 'next'; // GET /api/user/userAdvertiserRelationships – get all user-advertiser relationships for a specific user export const GET = withSession(async ({ session }) => { @@ -32,7 +33,7 @@ export const GET = withSession(async ({ session }) => { }); // POST /api/user/networks – create a new UserAdvertiserRelationship -export const POST = withSession(async ({ req, session }) => { +export const POST = withSession(async (req: NextApiRequest, res: NextApiResponse) => { const { advertiserId, username, @@ -122,4 +123,6 @@ export const PUT = withSession(async ({ req }) => { }); return NextResponse.json({ userAdvertiserRelationship }); +} else if (advertiserId === "6") { // eBay + // Existing eBay integration code }); diff --git a/apps/web/lib/api/links.ts b/apps/web/lib/api/links.ts index 9a8fdd0d..da8c4a94 100644 --- a/apps/web/lib/api/links.ts +++ b/apps/web/lib/api/links.ts @@ -311,6 +311,70 @@ export async function processLink({ geo, } = payload; + // Add eBay integration logic + const advertiserId = userBrandRelationship.advertiserId; + if (advertiserId === "6") { // eBay + const userAdvertiserRelation = userBrandRelationship.userAdvertiserRelation; + const ebayClientId = userAdvertiserRelation.ebayClientId || ""; + const ebayClientSecret = decrypt(userAdvertiserRelation.encryptedEbayClientSecret || ""); + + if (!ebayClientId || !ebayClientSecret) { + return { + link: payload, + error: "Missing credentials for eBay affiliate program.", + code: "unprocessable_entity", + }; + } + + const ebayUrl = "https://api.ebay.com/affiliate/links"; + const headers = { + Authorization: `Bearer ${ebayClientSecret}`, + "Content-Type": "application/json", + }; + + const data = { + websiteId: ebayClientId, + originalUrl: processedUrl, + // Add other necessary fields as per eBay's API documentation + }; + + try { + const response = await fetch(ebayUrl, { + method: "POST", + headers, + body: JSON.stringify(data), + }); + + if (response.ok) { + const responseData = await response.json(); + const affiliateLink = responseData.affiliateLink || null; + + return { + link: { + ...payload, + aff_url: affiliateLink, + projectId: workspace?.id || null, + ...(userId && { userId }), + }, + error: null, + }; + } else { + const errorData = await response.json(); + return { + link: payload, + error: errorData.message || "Failed to generate eBay affiliate link.", + code: "unprocessable_entity", + }; + } + } catch (error) { + console.error("Error generating eBay affiliate link:", error); + return { + link: payload, + error: "Internal server error while generating affiliate link.", + code: "internal_error", + }; + } + } const tagIds = combineTagIds(payload); // url checks @@ -562,7 +626,69 @@ export async function processLink({ userBrandRelationship.brandAdvertiserRelation.brandIdAtAdvertiser; const userAdvertiserRelation = userBrandRelationship.userAdvertiserRelation; + } else if (advertiserId === "6") { // eBay + const userAdvertiserRelation = userBrandRelationship.userAdvertiserRelation; + const ebayClientId = userAdvertiserRelation.ebayClientId || ""; + const ebayClientSecret = decrypt(userAdvertiserRelation.encryptedEbayClientSecret || ""); + + if (!ebayClientId || !ebayClientSecret) { + return { + link: payload, + error: "Missing credentials for eBay affiliate program.", + code: "unprocessable_entity", + }; + } + + const ebayUrl = "https://api.ebay.com/affiliate/links"; + const headers = { + Authorization: `Bearer ${ebayClientSecret}`, + "Content-Type": "application/json", + }; + + const data = { + websiteId: ebayClientId, + originalUrl: processedUrl, + // Add other necessary fields as per eBay's API documentation + }; + + try { + const response = await fetch(ebayUrl, { + method: "POST", + headers, + body: JSON.stringify(data), + }); + if (response.ok) { + const responseData = await response.json(); + const affiliateLink = responseData.affiliateLink; // Adjust based on actual response + + return { + link: { + ...payload, + aff_url: affiliateLink || null, + projectId: workspace?.id || null, + ...(userId && { + userId, + }), + }, + error: null, + }; + } else { + const errorData = await response.json(); + return { + link: payload, + error: errorData.message || "Failed to generate eBay affiliate link.", + code: "unprocessable_entity", + }; + } + } catch (error) { + console.error("Error generating eBay affiliate link:", error); + return { + link: payload, + error: "Internal server error while generating affiliate link.", + code: "internal_error", + }; + } const clientId = userAdvertiserRelation.clientId || ""; const encryptedClientSecret = userAdvertiserRelation.encryptedClientSecret || ""; diff --git a/apps/web/prisma/schema.prisma b/apps/web/prisma/schema.prisma index 348b31b0..ebbe6358 100644 --- a/apps/web/prisma/schema.prisma +++ b/apps/web/prisma/schema.prisma @@ -40,6 +40,8 @@ model UserAdvertiserRelationship { encryptedClientSecret String? @db.VarChar(1000) accountId String? websiteId String? + ebayClientId String? @db.VarChar(1000) + encryptedEbayClientSecret String? @db.VarChar(1000) user User @relation(fields: [userId], references: [id], onDelete: Cascade) advertiser Advertiser @relation(fields: [advertiserId], references: [id], onDelete: Cascade) userBrandRelationships UserBrandRelationship[] diff --git a/apps/web/ui/modals/add-edit-network-modal.tsx b/apps/web/ui/modals/add-edit-network-modal.tsx index 78fd44f8..7e67cfbe 100644 --- a/apps/web/ui/modals/add-edit-network-modal.tsx +++ b/apps/web/ui/modals/add-edit-network-modal.tsx @@ -619,13 +619,13 @@ function AddEditNetworkModal({
- handleInputChange("partialApiKey", e.target.value) + handleInputChange("ebayClientId", e.target.value) + } + className={`${"border-gray-300 text-gray-900 placeholder-gray-300 focus:border-gray-500 focus:ring-gray-500"} block w-full rounded-md focus:outline-none sm:text-sm`} + aria-invalid="true" + /> +
+
+
+
+ + + + +
+
+ + handleInputChange("partialClientSecret", e.target.value) } className={`${"border-gray-300 text-gray-900 placeholder-gray-300 focus:border-gray-500 focus:ring-gray-500"} block w-full rounded-md focus:outline-none sm:text-sm`} aria-invalid="true" @@ -683,6 +716,108 @@ function AddEditNetworkModal({
+ ) : advertiserId === "5" ? ( // Impact.com + <> +
+
+ + + + +
+
+ + handleInputChange("ebayClientId", e.target.value) + } + className={`${"border-gray-300 text-gray-900 placeholder-gray-300 focus:border-gray-500 focus:ring-gray-500"} block w-full rounded-md focus:outline-none sm:text-sm`} + aria-invalid="true" + /> +
+
+
+
+ + + + +
+
+ + handleInputChange("partialClientSecret", e.target.value) + } + className={`${"border-gray-300 text-gray-900 placeholder-gray-300 focus:border-gray-500 focus:ring-gray-500"} block w-full rounded-md focus:outline-none sm:text-sm`} + aria-invalid="true" + /> +
+
+
+
+ + + + +
+
+ + handleInputChange("accountId", e.target.value) + } + className={`${"border-gray-300 text-gray-900 placeholder-gray-300 focus:border-gray-500 focus:ring-gray-500"} block w-full rounded-md focus:outline-none sm:text-sm`} + aria-invalid="true" + /> +
+
+ ) : advertiserId === "5" ? ( // Impact.com <>