chore: leaderboard table

This commit is contained in:
Ephraim Atta-Duncan
2024-09-19 13:08:29 +00:00
parent 670393d0d0
commit 894e857826
6 changed files with 147 additions and 331 deletions

View File

@ -1,15 +1,11 @@
import { Prisma } from '@prisma/client';
import { prisma } from '@documenso/prisma';
import { Prisma } from '@documenso/prisma/client';
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;
id: number;
name: string;
signingVolume: number;
createdAt: Date;
};
export type GetSigningVolumeOptions = {
@ -18,111 +14,101 @@ export type GetSigningVolumeOptions = {
perPage?: number;
};
export const getSigningVolume = async ({
export async function getSigningVolume({
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];
}: GetSigningVolumeOptions) {
const whereClause = Prisma.validator<Prisma.SubscriptionWhereInput>()({
status: 'ACTIVE',
OR: [
{
User: {
OR: [
{ name: { contains: search, mode: 'insensitive' } },
{ email: { contains: search, mode: 'insensitive' } },
],
},
},
{
team: {
name: { contains: search, mode: 'insensitive' },
},
},
],
});
const totalPages = Math.ceil(Number(totalCount[0].count) / perPage);
const [subscriptions, totalCount] = await Promise.all([
prisma.subscription.findMany({
where: whereClause,
select: {
id: true,
createdAt: true,
User: {
select: {
id: true,
name: true,
email: true,
Document: {
where: {
status: 'COMPLETED',
deletedAt: null,
},
select: {
id: true,
},
},
},
},
team: {
select: {
id: true,
name: true,
document: {
where: {
status: 'COMPLETED',
deletedAt: null,
},
select: {
id: true,
},
},
},
},
},
orderBy: [
{
User: {
Document: {
_count: 'desc',
},
},
},
],
skip: Math.max(page - 1, 0) * perPage,
take: perPage,
}),
prisma.subscription.count({
where: whereClause,
}),
]);
return { signingVolume: results, totalPages };
};
const leaderboardWithVolume: SigningVolume[] = subscriptions.map((subscription) => {
const name =
subscription.User?.name || subscription.team?.name || subscription.User?.email || 'Unknown';
const signingVolume =
(subscription.User?.Document.length || 0) + (subscription.team?.document.length || 0);
return {
id: subscription.id,
name,
signingVolume,
createdAt: subscription.createdAt,
};
});
return {
leaderboard: leaderboardWithVolume,
totalPages: Math.ceil(totalCount / perPage),
};
}