|
| 1 | +import { NextRequest, NextResponse } from "next/server"; |
| 2 | +import DomainModel, { Domain } from "@models/Domain"; |
| 3 | +import EmailEventModel from "@models/EmailEvent"; |
| 4 | +import UserModel from "@models/User"; |
| 5 | +import SequenceModel from "@models/Sequence"; |
| 6 | +import { Constants, Sequence, User } from "@courselit/common-models"; |
| 7 | +import { error } from "@/services/logger"; |
| 8 | +import { jwtUtils } from "@courselit/utils"; |
| 9 | + |
| 10 | +export const dynamic = "force-dynamic"; |
| 11 | + |
| 12 | +function getJwtSecret(): string { |
| 13 | + const jwtSecret = process.env.PIXEL_SIGNING_SECRET; |
| 14 | + if (!jwtSecret) { |
| 15 | + throw new Error("PIXEL_SIGNING_SECRET is not defined"); |
| 16 | + } |
| 17 | + return jwtSecret; |
| 18 | +} |
| 19 | + |
| 20 | +export async function GET(req: NextRequest) { |
| 21 | + if (!process.env.PIXEL_SIGNING_SECRET) { |
| 22 | + error( |
| 23 | + "PIXEL_SIGNING_SECRET environment variable is not defined. No click tracking is done.", |
| 24 | + ); |
| 25 | + return NextResponse.redirect(new URL("/", req.url)); |
| 26 | + } |
| 27 | + |
| 28 | + try { |
| 29 | + const domainName = req.headers.get("domain"); |
| 30 | + const domain = await DomainModel.findOne<Domain>({ |
| 31 | + name: domainName, |
| 32 | + }); |
| 33 | + if (!domain) { |
| 34 | + error(`Domain not found: ${domainName}`); |
| 35 | + return NextResponse.redirect(new URL("/", req.url)); |
| 36 | + } |
| 37 | + |
| 38 | + const { searchParams } = new URL(req.url); |
| 39 | + const d = searchParams.get("d"); |
| 40 | + if (!d) { |
| 41 | + error("Missing data query parameter"); |
| 42 | + return NextResponse.redirect(new URL("/", req.url)); |
| 43 | + } |
| 44 | + |
| 45 | + const jwtSecret = getJwtSecret(); |
| 46 | + const payload = jwtUtils.verifyToken(d, jwtSecret); |
| 47 | + const { userId, sequenceId, emailId, link, index } = payload as any; |
| 48 | + |
| 49 | + if (!userId || !sequenceId || !emailId || !link) { |
| 50 | + error(`Invalid payload: Not all required fields are present`, { |
| 51 | + payload, |
| 52 | + }); |
| 53 | + return NextResponse.redirect(new URL("/", req.url)); |
| 54 | + } |
| 55 | + |
| 56 | + const sequence = await SequenceModel.findOne<Sequence>({ |
| 57 | + domain: domain._id, |
| 58 | + sequenceId, |
| 59 | + }); |
| 60 | + const user = await UserModel.findOne<User>({ |
| 61 | + domain: domain._id, |
| 62 | + userId, |
| 63 | + }); |
| 64 | + const email = sequence?.emails.find((e) => e.emailId === emailId); |
| 65 | + |
| 66 | + if (sequence && user && email) { |
| 67 | + await EmailEventModel.create({ |
| 68 | + domain: domain._id, |
| 69 | + sequenceId, |
| 70 | + userId, |
| 71 | + emailId, |
| 72 | + action: Constants.EmailEventAction.CLICK, |
| 73 | + link, |
| 74 | + linkIndex: index, |
| 75 | + }); |
| 76 | + } |
| 77 | + |
| 78 | + // Redirect to the original URL |
| 79 | + const decodedUrl = decodeURIComponent(link); |
| 80 | + return NextResponse.redirect(decodedUrl); |
| 81 | + } catch (err) { |
| 82 | + error(`Invalid click data`, { |
| 83 | + fileName: "click.route.ts", |
| 84 | + stack: err.stack, |
| 85 | + }); |
| 86 | + // Always redirect to home page on error |
| 87 | + return NextResponse.redirect(new URL("/", req.url)); |
| 88 | + } |
| 89 | +} |
0 commit comments