Files
sign/packages/lib/server-only/admin/get-users-stats.ts

83 lines
2.0 KiB
TypeScript
Raw Normal View History

2024-04-05 17:49:32 +00:00
import { DateTime } from 'luxon';
2023-09-08 11:28:50 +03:00
import { prisma } from '@documenso/prisma';
2024-04-05 17:49:32 +00:00
import { DocumentStatus, SubscriptionStatus } from '@documenso/prisma/client';
2023-09-08 11:28:50 +03:00
export const getUsersCount = async () => {
return await prisma.user.count();
};
export const getUsersWithSubscriptionsCount = async () => {
return await prisma.user.count({
where: {
Subscription: {
some: {
status: SubscriptionStatus.ACTIVE,
},
2023-09-08 11:28:50 +03:00
},
},
});
};
2024-04-05 17:49:32 +00:00
export const getUserWithAtLeastOneDocumentPerMonth = async () => {
return await prisma.user.count({
where: {
Document: {
some: {
createdAt: {
gte: new Date(new Date().setMonth(new Date().getMonth() - 1)),
},
},
},
},
});
};
export const getUserWithAtLeastOneDocumentSignedPerMonth = async () => {
return await prisma.user.count({
where: {
Document: {
some: {
status: {
equals: DocumentStatus.COMPLETED,
},
2024-05-21 09:28:23 +00:00
completedAt: {
2024-04-05 17:49:32 +00:00
gte: new Date(new Date().setMonth(new Date().getMonth() - 1)),
},
},
},
},
});
};
export type GetUserWithDocumentMonthlyGrowth = Array<{
month: string;
count: number;
signed_count: number;
}>;
type GetUserWithDocumentMonthlyGrowthQueryResult = Array<{
month: Date;
count: bigint;
signed_count: bigint;
}>;
export const getUserWithSignedDocumentMonthlyGrowth = async () => {
const result = await prisma.$queryRaw<GetUserWithDocumentMonthlyGrowthQueryResult>`
SELECT
DATE_TRUNC('month', "Document"."createdAt") AS "month",
COUNT(DISTINCT "Document"."userId") as "count",
2024-05-21 22:44:44 +00:00
COUNT(DISTINCT CASE WHEN "Document"."status" = 'COMPLETED' THEN "Document"."userId" END) as "signed_count"
2024-04-05 17:49:32 +00:00
FROM "Document"
GROUP BY "month"
ORDER BY "month" DESC
LIMIT 12
`;
return result.map((row) => ({
month: DateTime.fromJSDate(row.month).toFormat('yyyy-MM'),
count: Number(row.count),
signed_count: Number(row.signed_count),
}));
};