Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Use the partner country for program application filters.

Kiran Kbeceb9b620fe
diff --git a/apps/web/lib/program-applications/count-program-applications.ts b/apps/web/lib/program-applications/count-program-applications.ts
index 6931713249..0f616d26a3 100644
--- a/apps/web/lib/program-applications/count-program-applications.ts
+++ b/apps/web/lib/program-applications/count-program-applications.ts
@@ -1,5 +1,8 @@
-import { prisma } from "@/lib/prisma";
+import { isExactPartnerIdQuery } from "@/lib/api/partners/program-enrollment-query";
+import { prisma, sanitizeFullTextSearch } from "@/lib/prisma";
import { getProgramApplicationsCountQuerySchema } from "@/lib/zod/schemas/program-application";
+import { parseFilterValue } from "@dub/utils";
+import { Prisma, ProgramApplicationStatus } from "@prisma/client";
import * as z from "zod/v4";
import { buildProgramApplicationWhere } from "./program-application-where";
@@ -17,27 +20,24 @@ export async function countProgramApplications({
search,
groupBy,
}: CountProgramApplicationsParams) {
+ if (groupBy === "country") {
+ return byPartnerCountry({
+ programId,
+ groupId,
+ status,
+ search,
+ });
+ }
+
const where = buildProgramApplicationWhere({
programId,
groupId: groupBy === "groupId" ? undefined : groupId,
- country: groupBy === "country" ? undefined : country,
+ country,
status,
search,
});
- if (groupBy === "country") {
- return prisma.programApplication.groupBy({
- by: ["country"],
- where,
- _count: true,
- orderBy: {
- _count: {
- country: "desc",
- },
- },
- });
- }
-
+ // Find the count of program applications by groupId
if (groupBy === "groupId") {
return prisma.programApplication.groupBy({
by: ["groupId"],
@@ -51,7 +51,83 @@ export async function countProgramApplications({
});
}
+ // Find the absolute count of program applications
return prisma.programApplication.count({
where,
});
}
+
+type PartnerCountryCountRow = {
+ country: string | null;
+ _count: bigint;
+};
+
+async function byPartnerCountry({
+ programId,
+ groupId,
+ status = ProgramApplicationStatus.pending,
+ search,
+}: Pick<
+ CountProgramApplicationsParams,
+ "programId" | "groupId" | "status" | "search"
+>) {
+ const rows = await prisma.$queryRaw<PartnerCountryCountRow[]>(Prisma.sql`
+ SELECT
+ p.country AS country,
+ COUNT(*) AS _count
+ FROM ProgramApplication pa
+ INNER JOIN Partner p ON p.id = pa.partnerId
+ WHERE pa.programId = ${programId}
+ AND pa.status = ${status} ${groupIdSql(groupId)} ${searchSql(search)}
+ GROUP BY p.country
+ ORDER BY _count DESC
+ `);
+
+ return rows.map((row) => ({
+ country: row.country,
+ _count: Number(row._count),
+ }));
+}
+
+// Include or exclude applications in the given groups.
+function groupIdSql(groupId?: string) {
+ const parsed = parseFilterValue(groupId);
+
+ if (!parsed?.values.length) {
+ return Prisma.sql``;
+ }
+
+ const ids = Prisma.join(parsed.values);
+
+ return parsed.sqlOperator === "NOT IN"
+ ? Prisma.sql`AND pa.groupId NOT IN (${ids})`
+ : Prisma.sql`AND pa.groupId IN (${ids})`;
+}
+
+// Match an exact partner ID, or a name, email, or company search.
+function searchSql(search?: string) {
+ const query = search?.trim();
+
+ if (!query) {
+ return Prisma.sql``;
+ }
+
+ if (isExactPartnerIdQuery(query)) {
+ return Prisma.sql`AND pa.partnerId = ${query}`;
+ }
+
+ const like = `%${query}%`;
+ const fullTextQuery = sanitizeFullTextSearch(query);
+
+ if (!fullTextQuery) {
+ return Prisma.sql`AND (pa.name LIKE ${like} OR pa.email LIKE ${like})`;
+ }
+
+ return Prisma.sql`
+ AND (
+ pa.name LIKE ${like}
+ OR pa.email LIKE ${like}
+ OR MATCH(p.email, p.name, p.companyName) AGAINST (${fullTextQuery} IN NATURAL LANGUAGE MODE)
+ )
+ `;
+}
diff --git a/apps/web/lib/program-applications/list-program-applications.ts b/apps/web/lib/program-applications/list-program-applications.ts
index 41d24fd384..8ba61a4425 100644
--- a/apps/web/lib/program-applications/list-program-applications.ts
+++ b/apps/web/lib/program-applications/list-program-applications.ts
@@ -113,7 +113,7 @@ function transformApplication(application: ProgramApplicationWithPartner) {
...partner,
name: application.name,
email: application.email,
- country: application.country,
+ country: partner.country,
groupId: application.groupId,
status: application.status,
...polyfillSocialMediaFields(platforms),
diff --git a/apps/web/lib/program-applications/program-application-where.ts b/apps/web/lib/program-applications/program-application-where.ts
index f3cdfb1759..9a12f8f472 100644
--- a/apps/web/lib/program-applications/program-application-where.ts
+++ b/apps/web/lib/program-applications/program-application-where.ts
@@ -19,6 +19,22 @@ function applicationFieldFilter(
: { in: parsed.values };
}
+function buildCountryWhere(
+ country?: string,
+): Prisma.ProgramApplicationWhereInput | undefined {
+ const countryFilter = applicationFieldFilter(country);
+
+ if (!countryFilter) {
+ return undefined;
+ }
+
+ return {
+ partner: {
+ country: countryFilter,
+ },
+ };
+}
+
type ProgramApplicationWhereParams = Omit<
z.infer<typeof getProgramApplicationsCountQuerySchema>,
"groupBy"
@@ -65,14 +81,14 @@ export function buildProgramApplicationWhere({
}: ProgramApplicationWhereParams): Prisma.ProgramApplicationWhereInput {
const query = search?.trim();
const groupIdFilter = applicationFieldFilter(groupId);
- const countryFilter = applicationFieldFilter(country);
+ const countryWhere = buildCountryWhere(country);
return {
programId,
status,
partnerId: { not: null },
...(groupIdFilter && { groupId: groupIdFilter }),
- ...(countryFilter && { country: countryFilter }),
+ ...countryWhere,
...(query ? buildSearchWhere(query) : {}),
};
}