mirror of
https://github.com/documenso/documenso.git
synced 2025-11-17 02:01:33 +10:00
chore: wip
This commit is contained in:
@ -28,103 +28,116 @@ export async function getSigningVolume({
|
|||||||
}: GetSigningVolumeOptions) {
|
}: GetSigningVolumeOptions) {
|
||||||
const skip = (page - 1) * perPage;
|
const skip = (page - 1) * perPage;
|
||||||
|
|
||||||
const baseUserQuery = {
|
// Find all unique customerIds from both personal and team subscriptions
|
||||||
OR: [
|
const activeCustomerIds = await prisma.$queryRaw<{ customerId: string }[]>`
|
||||||
{
|
SELECT DISTINCT "customerId"
|
||||||
subscriptions: {
|
FROM (
|
||||||
some: {
|
-- Get customerIds from users with active subscriptions
|
||||||
status: SubscriptionStatus.ACTIVE,
|
SELECT u."customerId"
|
||||||
},
|
FROM "User" u
|
||||||
},
|
JOIN "Subscription" s ON u.id = s."userId"
|
||||||
},
|
WHERE s.status = 'ACTIVE' AND u."customerId" IS NOT NULL
|
||||||
{
|
|
||||||
teamMembers: {
|
|
||||||
some: {
|
|
||||||
team: {
|
|
||||||
subscription: {
|
|
||||||
status: SubscriptionStatus.ACTIVE,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
],
|
|
||||||
...(search
|
|
||||||
? {
|
|
||||||
OR: [
|
|
||||||
{ name: { contains: search, mode: Prisma.QueryMode.insensitive } },
|
|
||||||
{ email: { contains: search, mode: Prisma.QueryMode.insensitive } },
|
|
||||||
],
|
|
||||||
}
|
|
||||||
: {}),
|
|
||||||
};
|
|
||||||
|
|
||||||
const results = await prisma.user.findMany({
|
UNION
|
||||||
where: baseUserQuery,
|
|
||||||
select: {
|
-- Get customerIds from teams with active subscriptions
|
||||||
id: true,
|
SELECT t."customerId"
|
||||||
name: true,
|
FROM "Team" t
|
||||||
email: true,
|
JOIN "Subscription" s ON t.id = s."teamId"
|
||||||
createdAt: true,
|
WHERE s.status = 'ACTIVE' AND t."customerId" IS NOT NULL
|
||||||
_count: {
|
) AS active_customers
|
||||||
|
${search ? Prisma.sql`WHERE "customerId" LIKE ${`%${search}%`}` : Prisma.empty}
|
||||||
|
`;
|
||||||
|
|
||||||
|
const totalCustomerCount = activeCustomerIds.length;
|
||||||
|
|
||||||
|
const paginatedCustomerIds = activeCustomerIds.slice(skip, skip + perPage);
|
||||||
|
|
||||||
|
const customerData = await Promise.all(
|
||||||
|
paginatedCustomerIds.map(async ({ customerId }) => {
|
||||||
|
const users = await prisma.user.findMany({
|
||||||
|
where: { customerId },
|
||||||
select: {
|
select: {
|
||||||
documents: {
|
id: true,
|
||||||
where: {
|
name: true,
|
||||||
status: DocumentStatus.COMPLETED,
|
email: true,
|
||||||
},
|
createdAt: true,
|
||||||
},
|
|
||||||
},
|
},
|
||||||
},
|
});
|
||||||
teamMembers: {
|
|
||||||
|
const teams = await prisma.team.findMany({
|
||||||
|
where: { customerId },
|
||||||
select: {
|
select: {
|
||||||
team: {
|
id: true,
|
||||||
select: {
|
name: true,
|
||||||
documents: {
|
createdAt: true,
|
||||||
where: {
|
|
||||||
status: DocumentStatus.COMPLETED,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
},
|
});
|
||||||
},
|
|
||||||
skip,
|
|
||||||
take: perPage,
|
|
||||||
orderBy: [
|
|
||||||
...(sortBy === 'name'
|
|
||||||
? [{ name: sortOrder }]
|
|
||||||
: sortBy === 'createdAt'
|
|
||||||
? [{ createdAt: sortOrder }]
|
|
||||||
: []),
|
|
||||||
],
|
|
||||||
});
|
|
||||||
|
|
||||||
const count = await prisma.user.count({
|
const userDocumentCount = await prisma.document.count({
|
||||||
where: baseUserQuery,
|
where: {
|
||||||
});
|
userId: { in: users.map((user) => user.id) },
|
||||||
|
status: DocumentStatus.COMPLETED,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
const transformedResults = results.map((user) => {
|
const teamDocumentCount = await prisma.document.count({
|
||||||
const personalDocuments = user._count.documents;
|
where: {
|
||||||
|
teamId: { in: teams.map((team) => team.id) },
|
||||||
|
status: DocumentStatus.COMPLETED,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
const teamDocuments = user.teamMembers.reduce(
|
const subscription = await prisma.subscription.findFirst({
|
||||||
(acc, member) => acc + member.team.documents.length,
|
where: {
|
||||||
0,
|
OR: [{ user: { customerId } }, { team: { customerId } }],
|
||||||
);
|
status: SubscriptionStatus.ACTIVE,
|
||||||
|
},
|
||||||
|
select: {
|
||||||
|
planId: true,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
const signingVolume = personalDocuments + teamDocuments;
|
const displayName = users[0]?.name || teams[0]?.name || customerId;
|
||||||
|
|
||||||
return {
|
const creationDates = [
|
||||||
id: user.id,
|
...users.map((user) => user.createdAt),
|
||||||
name: user.name,
|
...teams.map((team) => team.createdAt),
|
||||||
signingVolume,
|
].filter(Boolean);
|
||||||
createdAt: user.createdAt,
|
|
||||||
planId: '',
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
if (sortBy === 'signingVolume') {
|
const createdAt =
|
||||||
transformedResults.sort((a, b) => {
|
creationDates.length > 0
|
||||||
|
? new Date(Math.min(...creationDates.map((date) => date.getTime())))
|
||||||
|
: new Date();
|
||||||
|
|
||||||
|
return {
|
||||||
|
id: users[0]?.id || teams[0]?.id || 0,
|
||||||
|
customerId,
|
||||||
|
name: displayName,
|
||||||
|
signingVolume: userDocumentCount + teamDocumentCount,
|
||||||
|
createdAt,
|
||||||
|
planId: subscription?.planId || '',
|
||||||
|
};
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
// Sort the results by the requested sort criteria
|
||||||
|
const sortedResults = [...customerData];
|
||||||
|
|
||||||
|
if (sortBy === 'name') {
|
||||||
|
sortedResults.sort((a, b) => {
|
||||||
|
return sortOrder === 'desc'
|
||||||
|
? (b.name || '').localeCompare(a.name || '')
|
||||||
|
: (a.name || '').localeCompare(b.name || '');
|
||||||
|
});
|
||||||
|
} else if (sortBy === 'createdAt') {
|
||||||
|
sortedResults.sort((a, b) => {
|
||||||
|
return sortOrder === 'desc'
|
||||||
|
? b.createdAt.getTime() - a.createdAt.getTime()
|
||||||
|
: a.createdAt.getTime() - b.createdAt.getTime();
|
||||||
|
});
|
||||||
|
} else if (sortBy === 'signingVolume') {
|
||||||
|
sortedResults.sort((a, b) => {
|
||||||
return sortOrder === 'desc'
|
return sortOrder === 'desc'
|
||||||
? b.signingVolume - a.signingVolume
|
? b.signingVolume - a.signingVolume
|
||||||
: a.signingVolume - b.signingVolume;
|
: a.signingVolume - b.signingVolume;
|
||||||
@ -132,7 +145,7 @@ export async function getSigningVolume({
|
|||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
leaderboard: transformedResults,
|
leaderboard: sortedResults,
|
||||||
totalPages: Math.ceil(count / perPage),
|
totalPages: Math.ceil(totalCustomerCount / perPage),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user