chore: add initial query

This commit is contained in:
Ephraim Atta-Duncan
2024-09-17 16:04:13 +00:00
parent 7644c0d855
commit 08d94cf2b2
9 changed files with 496 additions and 5 deletions

View File

@ -0,0 +1,49 @@
import { prisma } from '@documenso/prisma';
export const getSigningVolume = async () => {
const results = await prisma.$queryRaw`
WITH paying_customers AS (
SELECT DISTINCT
COALESCE(s."userId", t."ownerUserId") AS customer_id,
CASE
WHEN s."userId" IS NOT NULL THEN 'User'
ELSE 'Team'
END AS customer_type,
COALESCE(s."createdAt", t."createdAt") AS customer_created_at
FROM "Subscription" s
FULL OUTER JOIN "Team" t ON s."teamId" = t.id
WHERE s.status = 'ACTIVE'
),
document_counts AS (
SELECT
COALESCE(d."userId", t."ownerUserId") AS customer_id,
COUNT(DISTINCT d.id) AS total_documents,
COUNT(DISTINCT CASE WHEN d.status = 'COMPLETED' THEN d.id END) AS completed_documents
FROM "Document" d
LEFT JOIN "Team" t ON d."teamId" = t.id
GROUP BY COALESCE(d."userId", t."ownerUserId")
)
SELECT
pc.customer_id,
pc.customer_type,
pc.customer_created_at,
COALESCE(dc.total_documents, 0) AS total_documents,
COALESCE(dc.completed_documents, 0) AS completed_documents,
CASE
WHEN pc.customer_type = 'User' THEN u.email
ELSE te.email
END AS customer_email,
CASE
WHEN pc.customer_type = 'User' THEN u.name
ELSE t.name
END AS customer_name
FROM paying_customers pc
LEFT JOIN document_counts dc ON pc.customer_id = dc.customer_id
LEFT JOIN "User" u ON pc.customer_id = u.id AND pc.customer_type = 'User'
LEFT JOIN "Team" t ON pc.customer_id = t."ownerUserId" AND pc.customer_type = 'Team'
LEFT JOIN "TeamEmail" te ON t.id = te."teamId"
ORDER BY dc.completed_documents DESC NULLS LAST, pc.customer_created_at DESC
`;
return results;
};

File diff suppressed because one or more lines are too long

View File

@ -1791,6 +1791,10 @@ msgstr ""
msgid "Last used"
msgstr ""
#: apps/web/src/app/(dashboard)/admin/nav.tsx:93
msgid "Leaderboard"
msgstr ""
#: apps/web/src/components/(teams)/dialogs/leave-team-dialog.tsx:111
#: apps/web/src/components/(teams)/tables/current-user-teams-data-table.tsx:117
msgid "Leave"
@ -2804,6 +2808,10 @@ msgstr ""
msgid "Signing up..."
msgstr ""
#: apps/web/src/app/(dashboard)/admin/leaderboard/page.tsx:18
msgid "Signing Volume"
msgstr ""
#: apps/web/src/app/(profile)/p/[url]/page.tsx:109
msgid "Since {0}"
msgstr ""
@ -2812,7 +2820,7 @@ msgstr ""
msgid "Site Banner"
msgstr ""
#: apps/web/src/app/(dashboard)/admin/nav.tsx:93
#: apps/web/src/app/(dashboard)/admin/nav.tsx:107
#: apps/web/src/app/(dashboard)/admin/site-settings/page.tsx:26
msgid "Site Settings"
msgstr ""

File diff suppressed because one or more lines are too long

View File

@ -1809,6 +1809,10 @@ msgstr "Last updated at"
msgid "Last used"
msgstr "Last used"
#: apps/web/src/app/(dashboard)/admin/nav.tsx:93
msgid "Leaderboard"
msgstr "Leaderboard"
#: apps/web/src/components/(teams)/dialogs/leave-team-dialog.tsx:111
#: apps/web/src/components/(teams)/tables/current-user-teams-data-table.tsx:117
msgid "Leave"
@ -2826,6 +2830,10 @@ msgstr "Signing in..."
msgid "Signing up..."
msgstr "Signing up..."
#: apps/web/src/app/(dashboard)/admin/leaderboard/page.tsx:18
msgid "Signing Volume"
msgstr "Signing Volume"
#: apps/web/src/app/(profile)/p/[url]/page.tsx:109
msgid "Since {0}"
msgstr "Since {0}"
@ -2834,7 +2842,7 @@ msgstr "Since {0}"
msgid "Site Banner"
msgstr "Site Banner"
#: apps/web/src/app/(dashboard)/admin/nav.tsx:93
#: apps/web/src/app/(dashboard)/admin/nav.tsx:107
#: apps/web/src/app/(dashboard)/admin/site-settings/page.tsx:26
msgid "Site Settings"
msgstr "Site Settings"