Files
esign/packages/lib/server-only/user/get-monthly-completed-document.ts
zooqueen fb4e3df0e3 rename: @hanzo/sign-* → @hanzo/esign-* (full eSign brand consistency)
The product is branded Hanzo eSign (esign.hanzo.ai, repo hanzoai/esign).
Rename all workspace packages @hanzo/sign-* → @hanzo/esign-* (1052 files)
and align docker/Dockerfile turbo --scope/--filter to the actual remix
package name @hanzo/esign (was a pre-existing mismatch with -remix that
broke the build). Protected deps (@hanzo/insights, @hanzo/logo) untouched.
2026-06-21 19:56:09 -07:00

41 lines
1.4 KiB
TypeScript

import { DocumentStatus, EnvelopeType } from '@prisma/client';
import { DateTime } from 'luxon';
import { kyselyPrisma, monthTrunc, sql } from '@hanzo/esign-prisma';
export const getCompletedDocumentsMonthly = async () => {
// SQLite: truncate to month via strftime; enum columns are TEXT so values
// compare directly with no `::"Enum"` cast.
const monthExpr = monthTrunc('Envelope.updatedAt');
const qb = kyselyPrisma.$kysely
.selectFrom('Envelope')
.select(({ fn }) => [
monthExpr.as('month'),
fn.count('id').as('count'),
fn
.sum(fn.count('id'))
// Feels like a bug in the Kysely extension but I just can not do this orderBy in a type-safe manner
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions, @typescript-eslint/no-explicit-any
.over((ob) => ob.orderBy(monthExpr as any))
.as('cume_count'),
])
.where('Envelope.status', '=', sql.lit(DocumentStatus.COMPLETED))
.where('Envelope.type', '=', sql.lit(EnvelopeType.DOCUMENT))
.groupBy('month')
.orderBy('month', 'desc')
.limit(12);
const result = await qb.execute();
return result.map((row) => ({
month: DateTime.fromFormat(row.month, 'yyyy-MM-dd').toFormat('yyyy-MM'),
count: Number(row.count),
cume_count: Number(row.cume_count),
}));
};
export type GetCompletedDocumentsMonthlyResult = Awaited<
ReturnType<typeof getCompletedDocumentsMonthly>
>;