mirror of
https://github.com/documenso/documenso.git
synced 2026-07-26 18:04:55 +10:00
chore: tidy code
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/// <reference types="../types/next-auth.d.ts" />
|
||||
import { KyselyAdapter } from '@auth/kysely-adapter';
|
||||
import { PrismaAdapter } from '@next-auth/prisma-adapter';
|
||||
import { compare } from '@node-rs/bcrypt';
|
||||
import { verifyAuthenticationResponse } from '@simplewebauthn/server';
|
||||
import { DateTime } from 'luxon';
|
||||
@@ -24,11 +24,9 @@ import { ZAuthenticationResponseJSONSchema } from '../types/webauthn';
|
||||
import { extractNextAuthRequestMetadata } from '../universal/extract-request-metadata';
|
||||
import { getAuthenticatorOptions } from '../utils/authenticator';
|
||||
import { ErrorCode } from './error-codes';
|
||||
import { db } from './kysely-db/db';
|
||||
|
||||
export const NEXT_AUTH_OPTIONS: AuthOptions = {
|
||||
//@ts-expect-error - https://github.com/nextauthjs/next-auth/issues/8660
|
||||
adapter: KyselyAdapter(db),
|
||||
adapter: PrismaAdapter(prisma),
|
||||
secret: process.env.NEXTAUTH_SECRET ?? 'secret',
|
||||
session: {
|
||||
strategy: 'jwt',
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
import { KyselyAuth } from '@auth/kysely-adapter';
|
||||
import type { Codegen } from '@auth/kysely-adapter';
|
||||
import { PostgresDialect } from 'kysely';
|
||||
import { Pool } from 'pg';
|
||||
|
||||
import type { DB } from '@documenso/prisma/generated/types';
|
||||
|
||||
export const db = new KyselyAuth<DB, Codegen>({
|
||||
dialect: new PostgresDialect({
|
||||
pool: new Pool({
|
||||
connectionString: process.env.DATABASE_URL,
|
||||
}),
|
||||
}),
|
||||
});
|
||||
@@ -51,7 +51,7 @@
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/luxon": "^3.3.1",
|
||||
"@types/pg": "^8.11.4"
|
||||
"@types/pg": "^8.11.4",
|
||||
"@playwright/browser-chromium": "1.43.0"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import { sql } from 'kysely';
|
||||
import { DateTime } from 'luxon';
|
||||
|
||||
import { prisma } from '@documenso/prisma';
|
||||
import { kyselyPrisma } from '@documenso/prisma';
|
||||
import { DocumentStatus } from '@documenso/prisma/client';
|
||||
|
||||
export type GetCompletedDocumentsMonthlyResult = Array<{
|
||||
month: string;
|
||||
@@ -8,24 +10,27 @@ export type GetCompletedDocumentsMonthlyResult = Array<{
|
||||
cume_count: number;
|
||||
}>;
|
||||
|
||||
type GetCompletedDocumentsMonthlyQueryResult = Array<{
|
||||
month: Date;
|
||||
count: bigint;
|
||||
cume_count: bigint;
|
||||
}>;
|
||||
|
||||
export const getCompletedDocumentsMonthly = async () => {
|
||||
const result = await prisma.$queryRaw<GetCompletedDocumentsMonthlyQueryResult>`
|
||||
SELECT
|
||||
DATE_TRUNC('month', "updatedAt") AS "month",
|
||||
COUNT("id") as "count",
|
||||
SUM(COUNT("id")) OVER (ORDER BY DATE_TRUNC('month', "updatedAt")) as "cume_count"
|
||||
FROM "Document"
|
||||
WHERE "status" = 'COMPLETED'
|
||||
GROUP BY "month"
|
||||
ORDER BY "month" DESC
|
||||
LIMIT 12
|
||||
`;
|
||||
const qb = kyselyPrisma.$kysely
|
||||
.selectFrom('Document')
|
||||
.select(({ fn, ref }) => [
|
||||
fn<Date>('DATE_TRUNC', [sql.lit('MONTH'), ref('Document.updatedAt')]).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(fn('DATE_TRUNC', [sql.lit('MONTH'), ref('Document.updatedAt')]) as any),
|
||||
)
|
||||
.as('cume_count'),
|
||||
])
|
||||
.where(() => sql`"Document"."status" = ${DocumentStatus.COMPLETED}::"DocumentStatus"`)
|
||||
.groupBy('month')
|
||||
.orderBy('month', 'desc')
|
||||
.limit(12);
|
||||
|
||||
const result = await qb.execute();
|
||||
|
||||
return result.map((row) => ({
|
||||
month: DateTime.fromJSDate(row.month).toFormat('yyyy-MM'),
|
||||
|
||||
Reference in New Issue
Block a user