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.

Add sortBy saleAmount

Steven Teyba5429b15a85
diff --git a/apps/web/app/api/links/route.ts b/apps/web/app/api/links/route.ts
index 6f635622d2..e4303499ea 100644
--- a/apps/web/app/api/links/route.ts
+++ b/apps/web/app/api/links/route.ts
@@ -20,45 +20,15 @@ export const GET = withWorkspace(
async ({ req, headers, workspace }) => {
const searchParams = getSearchParamsWithArray(req.url);
- const {
- domain,
- tagId,
- tagIds,
- search,
- sort,
- page,
- pageSize,
- userId,
- showArchived,
- withTags,
- includeUser,
- includeWebhooks,
- includeDashboard,
- linkIds,
- tenantId,
- } = getLinksQuerySchemaExtended.parse(searchParams);
+ const params = getLinksQuerySchemaExtended.parse(searchParams);
- if (domain) {
- await getDomainOrThrow({ workspace, domain });
+ if (params.domain) {
+ await getDomainOrThrow({ workspace, domain: params.domain });
}
const response = await getLinksForWorkspace({
workspaceId: workspace.id,
- domain,
- tagId,
- tagIds,
- search,
- sort,
- page,
- pageSize,
- userId,
- showArchived,
- withTags,
- includeUser,
- includeWebhooks,
- includeDashboard,
- linkIds,
- tenantId,
+ ...params,
});
return NextResponse.json(response, {
diff --git a/apps/web/lib/api/links/get-links-for-workspace.ts b/apps/web/lib/api/links/get-links-for-workspace.ts
index 4deeebfdd8..53c087d30e 100644
--- a/apps/web/lib/api/links/get-links-for-workspace.ts
+++ b/apps/web/lib/api/links/get-links-for-workspace.ts
@@ -11,7 +11,8 @@ export async function getLinksForWorkspace({
tagIds,
tagNames,
search,
- sort = "createdAt",
+ sortBy,
+ sortOrder,
page,
pageSize,
userId,
@@ -85,7 +86,7 @@ export async function getLinksForWorkspace({
dashboard: includeDashboard,
},
orderBy: {
- [sort]: "desc",
+ [sortBy]: sortOrder,
},
take: pageSize,
skip: (page - 1) * pageSize,
diff --git a/apps/web/lib/zod/schemas/links.ts b/apps/web/lib/zod/schemas/links.ts
index 1d530d354d..b4633a8fc5 100644
--- a/apps/web/lib/zod/schemas/links.ts
+++ b/apps/web/lib/zod/schemas/links.ts
@@ -78,17 +78,35 @@ const LinksQuerySchema = z.object({
.openapi({ deprecated: true }),
});
-export const getLinksQuerySchema = LinksQuerySchema.merge(
- z.object({
- sort: z
- .enum(["createdAt", "clicks", "lastClicked"])
- .optional()
- .default("createdAt")
- .describe(
- "The field to sort the links by. The default is `createdAt`, and sort order is always descending.",
- ),
+const sortBy = z
+ .enum(["createdAt", "clicks", "saleAmount", "lastClicked"])
+ .optional()
+ .default("createdAt")
+ .describe("The field to sort the links by. The default is `createdAt`.");
+
+const sortParams = z.object({
+ sortBy,
+ sortOrder: z
+ .enum(["asc", "desc"])
+ .optional()
+ .default("desc")
+ .describe("The sort order. The default is `desc`."),
+ sort: sortBy
+ .openapi({ deprecated: true })
+ .describe("DEPRECATED. Use `sortBy` instead."),
+});
+
+const getLinksQuerySchemaBase = LinksQuerySchema.merge(sortParams).merge(
+ getPaginationQuerySchema({ pageSize: 100 }),
+);
+
+export const getLinksQuerySchema = getLinksQuerySchemaBase.transform(
+ (data) => ({
+ ...data,
+ // If sortBy is not provided but sort is, use sort value
+ sortBy: data.sortBy === "createdAt" && data.sort ? data.sort : data.sortBy,
}),
-).merge(getPaginationQuerySchema({ pageSize: 100 }));
+);
export const getLinksCountQuerySchema = LinksQuerySchema.merge(
z.object({
@@ -99,7 +117,7 @@ export const getLinksCountQuerySchema = LinksQuerySchema.merge(
}),
);
-export const linksExportQuerySchema = getLinksQuerySchema
+export const linksExportQuerySchema = getLinksQuerySchemaBase
.omit({ page: true, pageSize: true })
.merge(
z.object({
@@ -603,19 +621,25 @@ export const getLinkInfoQuerySchema = domainKeySchema.partial().merge(
}),
);
-export const getLinksQuerySchemaExtended = getLinksQuerySchema.merge(
- z.object({
- // Only Dub UI uses the following query parameters
- includeUser: booleanQuerySchema.default("false"),
- includeWebhooks: booleanQuerySchema.default("false"),
- includeDashboard: booleanQuerySchema.default("false"),
- linkIds: z
- .union([z.string(), z.array(z.string())])
- .transform((v) => (Array.isArray(v) ? v : v.split(",")))
- .optional()
- .describe("Link IDs to filter by."),
- }),
-);
+export const getLinksQuerySchemaExtended = getLinksQuerySchemaBase
+ .merge(
+ z.object({
+ // Only Dub UI uses the following query parameters
+ includeUser: booleanQuerySchema.default("false"),
+ includeWebhooks: booleanQuerySchema.default("false"),
+ includeDashboard: booleanQuerySchema.default("false"),
+ linkIds: z
+ .union([z.string(), z.array(z.string())])
+ .transform((v) => (Array.isArray(v) ? v : v.split(",")))
+ .optional()
+ .describe("Link IDs to filter by."),
+ }),
+ )
+ .transform((data) => ({
+ ...data,
+ // If sortBy is not provided but sort is, use sort value
+ sortBy: data.sortBy === "createdAt" && data.sort ? data.sort : data.sortBy,
+ }));
export const linkEventSchema = LinkSchema.extend({
// here we use string because url can be empty