Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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`);
}
}
31 changes: 31 additions & 0 deletions apps/web/app/api/affiliate-networks/route.ts
Original file line number Diff line number Diff line change
@@ -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' });
}
}
10 changes: 10 additions & 0 deletions apps/web/lib/api/links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,14 @@ export async function processLink({
userId,
bulk = false,
skipKeyChecks = false, // only skip when key doesn't change (e.g. when editing a link)
advertiserId, // Include advertiserId
}: {
payload: LinkWithTagIdsProps;
workspace?: WorkspaceProps;
userId?: string;
bulk?: boolean;
skipKeyChecks?: boolean;
advertiserId?: string; // Add advertiserId to the function signature
}: {
payload: LinkWithTagIdsProps;
workspace?: WorkspaceProps;
Expand Down Expand Up @@ -501,6 +509,8 @@ export async function processLink({
brand: {
url: modifiedUrl,
},
advertiserId: payload.advertiserId, // Use selected advertiserId
},
},
include: {
brand: true,
Expand Down
18 changes: 18 additions & 0 deletions apps/web/ui/modals/add-edit-link-modal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ function AddEditLinkModal({
const [generatingKey, setGeneratingKey] = useState(false);
const [saving, setSaving] = useState(false);

const [advertisers, setAdvertisers] = useState<Advertiser[]>([]);
const [selectedAdvertiser, setSelectedAdvertiser] = useState<string | null>(null);

const {
allActiveDomains: domains,
primaryDomain,
Expand Down Expand Up @@ -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) || "";
Expand Down Expand Up @@ -329,6 +346,7 @@ function AddEditLinkModal({
const { user, tags, tagId, ...rest } = data;
const bodyData = {
...rest,
advertiserId: data.advertiserId, // Include advertiserId
// Map tags to tagIds
tagIds: tags.map(({ id }) => id),
};
Expand Down