chore: leaderboard table

This commit is contained in:
Ephraim Atta-Duncan
2024-09-19 10:40:22 +00:00
parent 08d94cf2b2
commit 670393d0d0
10 changed files with 511 additions and 319 deletions

View File

@ -1,49 +1,128 @@
import { Prisma } from '@prisma/client';
import { prisma } from '@documenso/prisma';
export const getSigningVolume = async () => {
const results = await prisma.$queryRaw`
WITH paying_customers AS (
SELECT DISTINCT
COALESCE(s."userId", t."ownerUserId") AS customer_id,
CASE
WHEN s."userId" IS NOT NULL THEN 'User'
ELSE 'Team'
END AS customer_type,
COALESCE(s."createdAt", t."createdAt") AS customer_created_at
FROM "Subscription" s
FULL OUTER JOIN "Team" t ON s."teamId" = t.id
WHERE s.status = 'ACTIVE'
),
document_counts AS (
SELECT
COALESCE(d."userId", t."ownerUserId") AS customer_id,
COUNT(DISTINCT d.id) AS total_documents,
COUNT(DISTINCT CASE WHEN d.status = 'COMPLETED' THEN d.id END) AS completed_documents
FROM "Document" d
LEFT JOIN "Team" t ON d."teamId" = t.id
GROUP BY COALESCE(d."userId", t."ownerUserId")
)
SELECT
pc.customer_id,
pc.customer_type,
pc.customer_created_at,
COALESCE(dc.total_documents, 0) AS total_documents,
COALESCE(dc.completed_documents, 0) AS completed_documents,
CASE
WHEN pc.customer_type = 'User' THEN u.email
ELSE te.email
END AS customer_email,
CASE
WHEN pc.customer_type = 'User' THEN u.name
ELSE t.name
END AS customer_name
FROM paying_customers pc
LEFT JOIN document_counts dc ON pc.customer_id = dc.customer_id
LEFT JOIN "User" u ON pc.customer_id = u.id AND pc.customer_type = 'User'
LEFT JOIN "Team" t ON pc.customer_id = t."ownerUserId" AND pc.customer_type = 'Team'
LEFT JOIN "TeamEmail" te ON t.id = te."teamId"
ORDER BY dc.completed_documents DESC NULLS LAST, pc.customer_created_at DESC
`;
return results;
export type SigningVolume = {
customer_id: number;
customer_type: 'User' | 'Team';
customer_created_at: Date;
total_documents: bigint;
completed_documents: bigint;
customer_email: string;
customer_name: string;
};
export type GetSigningVolumeOptions = {
search?: string;
page?: number;
perPage?: number;
};
export const getSigningVolume = async ({
search = '',
page = 1,
perPage = 10,
}: GetSigningVolumeOptions = {}): Promise<{
signingVolume: SigningVolume[];
totalPages: number;
}> => {
const offset = (page - 1) * perPage;
const whereClause = search
? `AND (LOWER(COALESCE(u.name, t.name)) LIKE $1 OR LOWER(COALESCE(u.email, te.email)) LIKE $1)`
: '';
const searchParam = search ? [`%${search.toLowerCase()}%`] : [];
const [results, totalCount] = await prisma.$transaction(async (tx) => {
const signingVolumeQuery = tx.$queryRaw<SigningVolume[]>`
WITH paying_customers AS (
SELECT DISTINCT
COALESCE(s."userId", t."ownerUserId") AS customer_id,
CASE
WHEN s."userId" IS NOT NULL THEN 'User'
ELSE 'Team'
END AS customer_type,
COALESCE(s."createdAt", t."createdAt") AS customer_created_at
FROM "Subscription" s
FULL OUTER JOIN "Team" t ON s."teamId" = t.id
WHERE s.status = 'ACTIVE'
),
document_counts AS (
SELECT
COALESCE(d."userId", t."ownerUserId") AS customer_id,
COUNT(DISTINCT d.id) AS total_documents,
COUNT(DISTINCT CASE WHEN d.status = 'COMPLETED' THEN d.id END) AS completed_documents
FROM "Document" d
LEFT JOIN "Team" t ON d."teamId" = t.id
GROUP BY COALESCE(d."userId", t."ownerUserId")
)
SELECT
pc.customer_id,
pc.customer_type,
pc.customer_created_at,
COALESCE(dc.total_documents, 0) AS total_documents,
COALESCE(dc.completed_documents, 0) AS completed_documents,
CASE
WHEN pc.customer_type = 'User' THEN u.email
ELSE te.email
END AS customer_email,
CASE
WHEN pc.customer_type = 'User' THEN u.name
ELSE t.name
END AS customer_name
FROM paying_customers pc
LEFT JOIN document_counts dc ON pc.customer_id = dc.customer_id
LEFT JOIN "User" u ON pc.customer_id = u.id AND pc.customer_type = 'User'
LEFT JOIN "Team" t ON pc.customer_id = t."ownerUserId" AND pc.customer_type = 'Team'
LEFT JOIN "TeamEmail" te ON t.id = te."teamId"
WHERE 1=1 ${Prisma.raw(whereClause)}
ORDER BY dc.completed_documents DESC NULLS LAST, pc.customer_created_at DESC
LIMIT ${perPage} OFFSET ${offset}
`;
const totalCountQuery = tx.$queryRaw<[{ count: bigint }]>`
SELECT COUNT(*) as count
FROM (
WITH paying_customers AS (
SELECT DISTINCT
COALESCE(s."userId", t."ownerUserId") AS customer_id,
CASE
WHEN s."userId" IS NOT NULL THEN 'User'
ELSE 'Team'
END AS customer_type,
COALESCE(s."createdAt", t."createdAt") AS customer_created_at
FROM "Subscription" s
FULL OUTER JOIN "Team" t ON s."teamId" = t.id
WHERE s.status = 'ACTIVE'
)
SELECT
pc.customer_id,
CASE
WHEN pc.customer_type = 'User' THEN u.email
ELSE te.email
END AS customer_email,
CASE
WHEN pc.customer_type = 'User' THEN u.name
ELSE t.name
END AS customer_name
FROM paying_customers pc
LEFT JOIN "User" u ON pc.customer_id = u.id AND pc.customer_type = 'User'
LEFT JOIN "Team" t ON pc.customer_id = t."ownerUserId" AND pc.customer_type = 'Team'
LEFT JOIN "TeamEmail" te ON t.id = te."teamId"
WHERE 1=1 ${Prisma.raw(whereClause)}
) subquery
`;
const [signingVolumeResults, totalCountResults] = await Promise.all([
signingVolumeQuery,
totalCountQuery,
]);
return [signingVolumeResults, totalCountResults];
});
const totalPages = Math.ceil(Number(totalCount[0].count) / perPage);
return { signingVolume: results, totalPages };
};

File diff suppressed because one or more lines are too long

View File

@ -1000,6 +1000,11 @@ msgstr ""
msgid "Current plan: {0}"
msgstr ""
#: apps/web/src/app/(dashboard)/admin/leaderboard/data-table-leaderboard.tsx:94
#: apps/web/src/app/(dashboard)/admin/leaderboard/leaderboard-table.tsx:94
msgid "Customer Type"
msgstr ""
#: apps/web/src/app/(dashboard)/settings/billing/billing-plans.tsx:28
msgid "Daily"
msgstr ""
@ -1391,6 +1396,8 @@ msgid "Edit webhook"
msgstr ""
#: apps/web/src/app/(dashboard)/admin/documents/[id]/recipient-item.tsx:166
#: apps/web/src/app/(dashboard)/admin/leaderboard/data-table-leaderboard.tsx:68
#: apps/web/src/app/(dashboard)/admin/leaderboard/leaderboard-table.tsx:68
#: apps/web/src/app/(dashboard)/admin/users/[id]/page.tsx:114
#: apps/web/src/app/(dashboard)/admin/users/data-table-users.tsx:71
#: apps/web/src/app/(dashboard)/templates/use-template-dialog.tsx:213
@ -1983,6 +1990,8 @@ msgid "My templates"
msgstr ""
#: apps/web/src/app/(dashboard)/admin/documents/[id]/recipient-item.tsx:148
#: apps/web/src/app/(dashboard)/admin/leaderboard/data-table-leaderboard.tsx:55
#: apps/web/src/app/(dashboard)/admin/leaderboard/leaderboard-table.tsx:55
#: apps/web/src/app/(dashboard)/admin/users/[id]/page.tsx:99
#: apps/web/src/app/(dashboard)/admin/users/data-table-users.tsx:66
#: apps/web/src/app/(dashboard)/settings/security/passkeys/user-passkeys-data-table-actions.tsx:144
@ -2587,6 +2596,8 @@ msgstr ""
msgid "Search by document title"
msgstr ""
#: apps/web/src/app/(dashboard)/admin/leaderboard/data-table-leaderboard.tsx:133
#: apps/web/src/app/(dashboard)/admin/leaderboard/leaderboard-table.tsx:133
#: apps/web/src/app/(dashboard)/admin/users/data-table-users.tsx:144
msgid "Search by name or email"
msgstr ""
@ -2808,7 +2819,9 @@ msgstr ""
msgid "Signing up..."
msgstr ""
#: apps/web/src/app/(dashboard)/admin/leaderboard/page.tsx:18
#: apps/web/src/app/(dashboard)/admin/leaderboard/data-table-leaderboard.tsx:81
#: apps/web/src/app/(dashboard)/admin/leaderboard/leaderboard-table.tsx:81
#: apps/web/src/app/(dashboard)/admin/leaderboard/page.tsx:36
msgid "Signing Volume"
msgstr ""

File diff suppressed because one or more lines are too long

View File

@ -1000,6 +1000,11 @@ msgstr "Current Password"
msgid "Current plan: {0}"
msgstr "Current plan: {0}"
#: apps/web/src/app/(dashboard)/admin/leaderboard/data-table-leaderboard.tsx:94
#: apps/web/src/app/(dashboard)/admin/leaderboard/leaderboard-table.tsx:94
msgid "Customer Type"
msgstr "Customer Type"
#: apps/web/src/app/(dashboard)/settings/billing/billing-plans.tsx:28
msgid "Daily"
msgstr "Daily"
@ -1395,6 +1400,8 @@ msgid "Edit webhook"
msgstr "Edit webhook"
#: apps/web/src/app/(dashboard)/admin/documents/[id]/recipient-item.tsx:166
#: apps/web/src/app/(dashboard)/admin/leaderboard/data-table-leaderboard.tsx:68
#: apps/web/src/app/(dashboard)/admin/leaderboard/leaderboard-table.tsx:68
#: apps/web/src/app/(dashboard)/admin/users/[id]/page.tsx:114
#: apps/web/src/app/(dashboard)/admin/users/data-table-users.tsx:71
#: apps/web/src/app/(dashboard)/templates/use-template-dialog.tsx:213
@ -2001,6 +2008,8 @@ msgid "My templates"
msgstr "My templates"
#: apps/web/src/app/(dashboard)/admin/documents/[id]/recipient-item.tsx:148
#: apps/web/src/app/(dashboard)/admin/leaderboard/data-table-leaderboard.tsx:55
#: apps/web/src/app/(dashboard)/admin/leaderboard/leaderboard-table.tsx:55
#: apps/web/src/app/(dashboard)/admin/users/[id]/page.tsx:99
#: apps/web/src/app/(dashboard)/admin/users/data-table-users.tsx:66
#: apps/web/src/app/(dashboard)/settings/security/passkeys/user-passkeys-data-table-actions.tsx:144
@ -2605,6 +2614,8 @@ msgstr "Search"
msgid "Search by document title"
msgstr "Search by document title"
#: apps/web/src/app/(dashboard)/admin/leaderboard/data-table-leaderboard.tsx:133
#: apps/web/src/app/(dashboard)/admin/leaderboard/leaderboard-table.tsx:133
#: apps/web/src/app/(dashboard)/admin/users/data-table-users.tsx:144
msgid "Search by name or email"
msgstr "Search by name or email"
@ -2830,7 +2841,9 @@ msgstr "Signing in..."
msgid "Signing up..."
msgstr "Signing up..."
#: apps/web/src/app/(dashboard)/admin/leaderboard/page.tsx:18
#: apps/web/src/app/(dashboard)/admin/leaderboard/data-table-leaderboard.tsx:81
#: apps/web/src/app/(dashboard)/admin/leaderboard/leaderboard-table.tsx:81
#: apps/web/src/app/(dashboard)/admin/leaderboard/page.tsx:36
msgid "Signing Volume"
msgstr "Signing Volume"