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

30 lines
805 B
TypeScript
Raw Normal View History

2023-09-08 11:28:50 +03:00
import { prisma } from '@documenso/prisma';
import { ReadStatus, SendStatus, SigningStatus } from '@documenso/prisma/client';
export const getRecipientsStats = async () => {
const results = await prisma.recipient.groupBy({
by: ['readStatus', 'signingStatus', 'sendStatus'],
_count: true,
});
2023-09-08 12:39:13 +03:00
const stats = {
TOTAL_RECIPIENTS: 0,
[ReadStatus.OPENED]: 0,
[ReadStatus.NOT_OPENED]: 0,
[SigningStatus.SIGNED]: 0,
[SigningStatus.NOT_SIGNED]: 0,
[SendStatus.SENT]: 0,
[SendStatus.NOT_SENT]: 0,
2023-09-08 11:28:50 +03:00
};
2023-09-12 17:53:38 +10:00
2023-09-08 12:39:13 +03:00
results.forEach((result) => {
const { readStatus, signingStatus, sendStatus, _count } = result;
stats[readStatus] += _count;
stats[signingStatus] += _count;
stats[sendStatus] += _count;
stats.TOTAL_RECIPIENTS += _count;
});
2023-09-12 17:53:38 +10:00
2023-09-08 12:39:13 +03:00
return stats;
2023-09-08 11:28:50 +03:00
};