diff --git a/apps/web/app/api/affiliate-networks/brands/[domain]/advertisers/route.ts b/apps/web/app/api/affiliate-networks/brands/[domain]/advertisers/route.ts new file mode 100644 index 00000000..127eed5c --- /dev/null +++ b/apps/web/app/api/affiliate-networks/brands/[domain]/advertisers/route.ts @@ -0,0 +1,37 @@ +// apps/web/app/api/affiliate-networks/brands/[domain]/advertisers/route.ts + +import { NextApiRequest, NextApiResponse } from 'next'; +import prisma from '@/lib/prisma'; +import { getSession } from 'next-auth/react'; // Adjust import based on auth setup + +export default async function handler(req: NextApiRequest, res: NextApiResponse) { + const { domain } = req.query; + const session = await getSession({ req }); + if (!session || !session.user?.id) { + return res.status(401).json({ error: 'Unauthorized' }); + } + + if (req.method === 'GET') { + try { + const advertisers = await prisma.advertiser.findMany({ + where: { + userBrandRelationships: { + some: { + userId: session.user.id, + brand: { + url: domain as string, + }, + }, + }, + }, + }); + res.status(200).json({ advertisers }); + } catch (error) { + console.error('Error fetching advertisers:', error); + res.status(500).json({ error: 'Internal Server Error' }); + } + } else { + res.setHeader('Allow', ['GET']); + res.status(405).end(`Method ${req.method} Not Allowed`); + } +} \ No newline at end of file diff --git a/apps/web/app/api/affiliate-networks/route.ts b/apps/web/app/api/affiliate-networks/route.ts new file mode 100644 index 00000000..07c5d77e --- /dev/null +++ b/apps/web/app/api/affiliate-networks/route.ts @@ -0,0 +1,31 @@ +// apps/web/app/api/affiliate-networks/route.ts + +import { NextApiRequest, NextApiResponse } from 'next'; +import prisma from '@/lib/prisma'; +import { getSession } from 'next-auth/react'; // Adjust import based on auth setup + +export default async function handler(req: NextApiRequest, res: NextApiResponse) { + const session = await getSession({ req }); + if (!session || !session.user?.id) { + return res.status(401).json({ error: 'Unauthorized' }); + } + + try { + const brands = await prisma.brand.findMany({ + where: { + userBrandRelationships: { + some: { + userId: session.user.id, + }, + }, + }, + include: { + advertisers: true, // Ensure this relation is defined in Prisma schema + }, + }); + res.status(200).json({ brands }); + } catch (error) { + console.error('Error fetching affiliate networks:', error); + res.status(500).json({ error: 'Internal Server Error' }); + } +} \ No newline at end of file diff --git a/apps/web/lib/api/links.ts b/apps/web/lib/api/links.ts index a84e681b..2fb03971 100644 --- a/apps/web/lib/api/links.ts +++ b/apps/web/lib/api/links.ts @@ -501,6 +501,8 @@ export async function processLink({ brand: { url: modifiedUrl, }, + advertiserId: payload.advertiserId, // Use selected advertiserId + }, }, include: { brand: true, diff --git a/apps/web/ui/modals/add-edit-link-modal/index.tsx b/apps/web/ui/modals/add-edit-link-modal/index.tsx index 7180e1c1..108ea16f 100644 --- a/apps/web/ui/modals/add-edit-link-modal/index.tsx +++ b/apps/web/ui/modals/add-edit-link-modal/index.tsx @@ -80,6 +80,9 @@ function AddEditLinkModal({ const [generatingKey, setGeneratingKey] = useState(false); const [saving, setSaving] = useState(false); + const [advertisers, setAdvertisers] = useState([]); + const [selectedAdvertiser, setSelectedAdvertiser] = useState(null); + const { allActiveDomains: domains, primaryDomain, @@ -116,6 +119,20 @@ function AddEditLinkModal({ useEffect(() => { // when someone pastes a URL if (showAddEditLinkModal && url.length > 0) { + // Fetch advertisers based on selected brand + const fetchAdvertisers = async () => { + if (data.domain) { + const res = await fetch(`/api/affiliate-networks/brands/${data.domain}/advertisers`); + if (res.ok) { + const result = await res.json(); + setAdvertisers(result.advertisers); + } else { + setAdvertisers([]); + } + } + }; + fetchAdvertisers(); + // if it's a new link and there are matching default domains, set it as the domain if (!props && activeDefaultDomains) { const urlDomain = getDomainWithoutWWW(url) || ""; @@ -329,6 +346,7 @@ function AddEditLinkModal({ const { user, tags, tagId, ...rest } = data; const bodyData = { ...rest, + advertiserId: selectedAdvertiser, // Use the state bound to the dropdown // Map tags to tagIds tagIds: tags.map(({ id }) => id), }; @@ -486,6 +504,19 @@ function AddEditLinkModal({ ))} +