2024-09-17 16:04:13 +00:00
|
|
|
import { prisma } from '@documenso/prisma';
|
2024-09-19 13:08:29 +00:00
|
|
|
import { Prisma } from '@documenso/prisma/client';
|
2024-09-17 16:04:13 +00:00
|
|
|
|
2024-09-19 10:40:22 +00:00
|
|
|
export type SigningVolume = {
|
2024-09-19 13:08:29 +00:00
|
|
|
id: number;
|
|
|
|
|
name: string;
|
|
|
|
|
signingVolume: number;
|
|
|
|
|
createdAt: Date;
|
2024-09-19 10:40:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export type GetSigningVolumeOptions = {
|
|
|
|
|
search?: string;
|
|
|
|
|
page?: number;
|
|
|
|
|
perPage?: number;
|
|
|
|
|
};
|
|
|
|
|
|
2024-09-19 13:08:29 +00:00
|
|
|
export async function getSigningVolume({
|
2024-09-19 10:40:22 +00:00
|
|
|
search = '',
|
|
|
|
|
page = 1,
|
|
|
|
|
perPage = 10,
|
2024-09-19 13:08:29 +00:00
|
|
|
}: 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' },
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
});
|
2024-09-19 10:40:22 +00:00
|
|
|
|
2024-09-19 13:08:29 +00:00
|
|
|
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,
|
|
|
|
|
}),
|
|
|
|
|
]);
|
2024-09-19 10:40:22 +00:00
|
|
|
|
2024-09-19 13:08:29 +00:00
|
|
|
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);
|
2024-09-19 10:40:22 +00:00
|
|
|
|
2024-09-19 13:08:29 +00:00
|
|
|
return {
|
|
|
|
|
id: subscription.id,
|
|
|
|
|
name,
|
|
|
|
|
signingVolume,
|
|
|
|
|
createdAt: subscription.createdAt,
|
|
|
|
|
};
|
2024-09-19 10:40:22 +00:00
|
|
|
});
|
|
|
|
|
|
2024-09-19 13:08:29 +00:00
|
|
|
return {
|
|
|
|
|
leaderboard: leaderboardWithVolume,
|
|
|
|
|
totalPages: Math.ceil(totalCount / perPage),
|
|
|
|
|
};
|
|
|
|
|
}
|