mirror of
https://github.com/documenso/documenso.git
synced 2025-11-13 00:03:33 +10:00
Compare commits
4 Commits
feat/chat-
...
feat/marke
| Author | SHA1 | Date | |
|---|---|---|---|
| cf84844d9b | |||
| 37a2ae87a6 | |||
| f964cda8ec | |||
| 73a445035c |
@ -15,11 +15,6 @@ NEXT_PRIVATE_DATABASE_URL="postgres://documenso:password@127.0.0.1:54320/documen
|
|||||||
# Defines the URL to use for the database when running migrations and other commands that won't work with a connection pool.
|
# Defines the URL to use for the database when running migrations and other commands that won't work with a connection pool.
|
||||||
NEXT_PRIVATE_DIRECT_DATABASE_URL="postgres://documenso:password@127.0.0.1:54320/documenso"
|
NEXT_PRIVATE_DIRECT_DATABASE_URL="postgres://documenso:password@127.0.0.1:54320/documenso"
|
||||||
|
|
||||||
# [[E2E Tests]]
|
|
||||||
E2E_TEST_AUTHENTICATE_USERNAME="Test User"
|
|
||||||
E2E_TEST_AUTHENTICATE_USER_EMAIL="testuser@mail.com"
|
|
||||||
E2E_TEST_AUTHENTICATE_USER_PASSWORD="test_password"
|
|
||||||
|
|
||||||
# [[STORAGE]]
|
# [[STORAGE]]
|
||||||
# OPTIONAL: Defines the storage transport to use. Available options: database (default) | s3
|
# OPTIONAL: Defines the storage transport to use. Available options: database (default) | s3
|
||||||
NEXT_PUBLIC_UPLOAD_TRANSPORT="database"
|
NEXT_PUBLIC_UPLOAD_TRANSPORT="database"
|
||||||
@ -73,7 +68,6 @@ NEXT_PRIVATE_STRIPE_API_KEY=
|
|||||||
NEXT_PRIVATE_STRIPE_WEBHOOK_SECRET=
|
NEXT_PRIVATE_STRIPE_WEBHOOK_SECRET=
|
||||||
NEXT_PUBLIC_STRIPE_COMMUNITY_PLAN_MONTHLY_PRICE_ID=
|
NEXT_PUBLIC_STRIPE_COMMUNITY_PLAN_MONTHLY_PRICE_ID=
|
||||||
NEXT_PUBLIC_STRIPE_COMMUNITY_PLAN_YEARLY_PRICE_ID=
|
NEXT_PUBLIC_STRIPE_COMMUNITY_PLAN_YEARLY_PRICE_ID=
|
||||||
NEXT_PUBLIC_STRIPE_FREE_PLAN_ID=
|
|
||||||
|
|
||||||
# [[FEATURES]]
|
# [[FEATURES]]
|
||||||
# OPTIONAL: Leave blank to disable PostHog and feature flags.
|
# OPTIONAL: Leave blank to disable PostHog and feature flags.
|
||||||
|
|||||||
51
.github/workflows/e2e-tests.yml
vendored
51
.github/workflows/e2e-tests.yml
vendored
@ -1,51 +0,0 @@
|
|||||||
name: Playwright Tests
|
|
||||||
on:
|
|
||||||
push:
|
|
||||||
branches: [feat/refresh]
|
|
||||||
pull_request:
|
|
||||||
branches: [feat/refresh]
|
|
||||||
jobs:
|
|
||||||
e2e_tests:
|
|
||||||
timeout-minutes: 60
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
services:
|
|
||||||
postgres:
|
|
||||||
image: postgres
|
|
||||||
env:
|
|
||||||
POSTGRES_USER: postgres
|
|
||||||
POSTGRES_PASSWORD: postgres
|
|
||||||
options: >-
|
|
||||||
--health-cmd pg_isready
|
|
||||||
--health-interval 10s
|
|
||||||
--health-timeout 5s
|
|
||||||
--health-retries 5
|
|
||||||
ports:
|
|
||||||
- 5432:5432
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v3
|
|
||||||
- uses: actions/setup-node@v3
|
|
||||||
with:
|
|
||||||
node-version: 18
|
|
||||||
- name: Install dependencies
|
|
||||||
run: npm ci
|
|
||||||
- name: Copy env
|
|
||||||
run: cp .env.example .env
|
|
||||||
- name: Install Playwright Browsers
|
|
||||||
run: npx playwright install --with-deps
|
|
||||||
- name: Generate Prisma Client
|
|
||||||
run: npm run prisma:generate -w @documenso/prisma
|
|
||||||
- name: Create the database
|
|
||||||
run: npm run prisma:migrate-dev
|
|
||||||
- name: Run Playwright tests
|
|
||||||
run: npm run ci
|
|
||||||
- uses: actions/upload-artifact@v3
|
|
||||||
if: always()
|
|
||||||
with:
|
|
||||||
name: playwright-report
|
|
||||||
path: playwright-report/
|
|
||||||
retention-days: 30
|
|
||||||
env:
|
|
||||||
NEXT_PRIVATE_DATABASE_URL: postgresql://postgres:postgres@localhost:5432/documenso
|
|
||||||
NEXT_PRIVATE_DIRECT_DATABASE_URL: postgresql://postgres:postgres@localhost:5432/documenso
|
|
||||||
TURBO_TOKEN: ${{ secrets.TURBO_TOKEN }}
|
|
||||||
TURBO_TEAM: ${{ vars.TURBO_TEAM }}
|
|
||||||
1
.gitignore
vendored
1
.gitignore
vendored
@ -31,7 +31,6 @@ yarn-error.log*
|
|||||||
|
|
||||||
# turbo
|
# turbo
|
||||||
.turbo
|
.turbo
|
||||||
.turbo-cookie
|
|
||||||
|
|
||||||
# vercel
|
# vercel
|
||||||
.vercel
|
.vercel
|
||||||
|
|||||||
@ -2,12 +2,8 @@
|
|||||||
const path = require('path');
|
const path = require('path');
|
||||||
const { withContentlayer } = require('next-contentlayer');
|
const { withContentlayer } = require('next-contentlayer');
|
||||||
|
|
||||||
const ENV_FILES = ['.env', '.env.local', `.env.${process.env.NODE_ENV || 'development'}`];
|
require('dotenv').config({
|
||||||
|
path: path.join(__dirname, '../../.env.local'),
|
||||||
ENV_FILES.forEach((file) => {
|
|
||||||
require('dotenv').config({
|
|
||||||
path: path.join(__dirname, `../../${file}`),
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
/** @type {import('next').NextConfig} */
|
/** @type {import('next').NextConfig} */
|
||||||
@ -26,14 +22,6 @@ const config = {
|
|||||||
transform: 'lucide-react/dist/esm/icons/{{ kebabCase member }}',
|
transform: 'lucide-react/dist/esm/icons/{{ kebabCase member }}',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
webpack: (config, { isServer }) => {
|
|
||||||
// fixes: Module not found: Can’t resolve ‘../build/Release/canvas.node’
|
|
||||||
if (isServer) {
|
|
||||||
config.resolve.alias.canvas = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return config;
|
|
||||||
},
|
|
||||||
async headers() {
|
async headers() {
|
||||||
return [
|
return [
|
||||||
{
|
{
|
||||||
|
|||||||
@ -21,7 +21,7 @@
|
|||||||
"framer-motion": "^10.12.8",
|
"framer-motion": "^10.12.8",
|
||||||
"lucide-react": "^0.279.0",
|
"lucide-react": "^0.279.0",
|
||||||
"micro": "^10.0.1",
|
"micro": "^10.0.1",
|
||||||
"next": "13.5.4",
|
"next": "13.4.19",
|
||||||
"next-auth": "4.22.3",
|
"next-auth": "4.22.3",
|
||||||
"next-contentlayer": "^0.3.4",
|
"next-contentlayer": "^0.3.4",
|
||||||
"next-plausible": "^3.10.1",
|
"next-plausible": "^3.10.1",
|
||||||
@ -34,7 +34,7 @@
|
|||||||
"react-icons": "^4.11.0",
|
"react-icons": "^4.11.0",
|
||||||
"recharts": "^2.7.2",
|
"recharts": "^2.7.2",
|
||||||
"sharp": "0.32.5",
|
"sharp": "0.32.5",
|
||||||
"typescript": "5.2.2",
|
"typescript": "5.1.6",
|
||||||
"zod": "^3.21.4"
|
"zod": "^3.21.4"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
|||||||
1
apps/marketing/process-env.d.ts
vendored
1
apps/marketing/process-env.d.ts
vendored
@ -7,7 +7,6 @@ declare namespace NodeJS {
|
|||||||
|
|
||||||
NEXT_PUBLIC_STRIPE_COMMUNITY_PLAN_MONTHLY_PRICE_ID: string;
|
NEXT_PUBLIC_STRIPE_COMMUNITY_PLAN_MONTHLY_PRICE_ID: string;
|
||||||
NEXT_PUBLIC_STRIPE_COMMUNITY_PLAN_YEARLY_PRICE_ID: string;
|
NEXT_PUBLIC_STRIPE_COMMUNITY_PLAN_YEARLY_PRICE_ID: string;
|
||||||
NEXT_PUBLIC_STRIPE_FREE_PLAN_ID?: string;
|
|
||||||
|
|
||||||
NEXT_PRIVATE_STRIPE_API_KEY: string;
|
NEXT_PRIVATE_STRIPE_API_KEY: string;
|
||||||
NEXT_PRIVATE_STRIPE_WEBHOOK_SECRET: string;
|
NEXT_PRIVATE_STRIPE_WEBHOOK_SECRET: string;
|
||||||
|
|||||||
@ -2,8 +2,6 @@
|
|||||||
|
|
||||||
import React, { useEffect, useState } from 'react';
|
import React, { useEffect, useState } from 'react';
|
||||||
|
|
||||||
import { usePathname } from 'next/navigation';
|
|
||||||
|
|
||||||
import { cn } from '@documenso/ui/lib/utils';
|
import { cn } from '@documenso/ui/lib/utils';
|
||||||
|
|
||||||
import { Footer } from '~/components/(marketing)/footer';
|
import { Footer } from '~/components/(marketing)/footer';
|
||||||
@ -15,7 +13,6 @@ export type MarketingLayoutProps = {
|
|||||||
|
|
||||||
export default function MarketingLayout({ children }: MarketingLayoutProps) {
|
export default function MarketingLayout({ children }: MarketingLayoutProps) {
|
||||||
const [scrollY, setScrollY] = useState(0);
|
const [scrollY, setScrollY] = useState(0);
|
||||||
const pathname = usePathname();
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const onScroll = () => {
|
const onScroll = () => {
|
||||||
@ -28,11 +25,7 @@ export default function MarketingLayout({ children }: MarketingLayoutProps) {
|
|||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div
|
<div className="relative max-w-[100vw] overflow-y-auto overflow-x-hidden pt-20 md:pt-28">
|
||||||
className={cn('relative max-w-[100vw] pt-20 md:pt-28', {
|
|
||||||
'overflow-y-auto overflow-x-hidden': pathname !== '/singleplayer',
|
|
||||||
})}
|
|
||||||
>
|
|
||||||
<div
|
<div
|
||||||
className={cn('fixed left-0 top-0 z-50 w-full bg-transparent', {
|
className={cn('fixed left-0 top-0 z-50 w-full bg-transparent', {
|
||||||
'bg-background/50 backdrop-blur-md': scrollY > 5,
|
'bg-background/50 backdrop-blur-md': scrollY > 5,
|
||||||
|
|||||||
@ -27,10 +27,7 @@ import { createSinglePlayerDocument } from '~/components/(marketing)/single-play
|
|||||||
|
|
||||||
type SinglePlayerModeStep = 'fields' | 'sign';
|
type SinglePlayerModeStep = 'fields' | 'sign';
|
||||||
|
|
||||||
// !: This entire file is a hack to get around failed prerendering of
|
export default function SinglePlayerModePage() {
|
||||||
// !: the Single Player Mode page. This regression was introduced during
|
|
||||||
// !: the upgrade of Next.js to v13.5.x.
|
|
||||||
export const SinglePlayerClient = () => {
|
|
||||||
const analytics = useAnalytics();
|
const analytics = useAnalytics();
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
|
|
||||||
@ -133,7 +130,7 @@ export const SinglePlayerClient = () => {
|
|||||||
signer: data.email,
|
signer: data.email,
|
||||||
});
|
});
|
||||||
|
|
||||||
router.push(`/singleplayer/${documentToken}/success`);
|
router.push(`/single-player-mode/${documentToken}/success`);
|
||||||
} catch {
|
} catch {
|
||||||
toast({
|
toast({
|
||||||
title: 'Something went wrong',
|
title: 'Something went wrong',
|
||||||
@ -244,4 +241,4 @@ export const SinglePlayerClient = () => {
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
};
|
}
|
||||||
@ -1,10 +0,0 @@
|
|||||||
import { SinglePlayerClient } from './client';
|
|
||||||
|
|
||||||
export const revalidate = 0;
|
|
||||||
|
|
||||||
// !: This entire file is a hack to get around failed prerendering of
|
|
||||||
// !: the Single Player Mode page. This regression was introduced during
|
|
||||||
// !: the upgrade of Next.js to v13.5.x.
|
|
||||||
export default function SingleplayerPage() {
|
|
||||||
return <SinglePlayerClient />;
|
|
||||||
}
|
|
||||||
@ -30,7 +30,7 @@ import { claimPlan } from '~/api/claim-plan/fetcher';
|
|||||||
import { FormErrorMessage } from '../form/form-error-message';
|
import { FormErrorMessage } from '../form/form-error-message';
|
||||||
|
|
||||||
export const ZClaimPlanDialogFormSchema = z.object({
|
export const ZClaimPlanDialogFormSchema = z.object({
|
||||||
name: z.string().trim().min(3, { message: 'Please enter a valid name.' }),
|
name: z.string().min(3),
|
||||||
email: z.string().email(),
|
email: z.string().email(),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@ -23,12 +23,12 @@ const SOCIAL_LINKS = [
|
|||||||
|
|
||||||
const FOOTER_LINKS = [
|
const FOOTER_LINKS = [
|
||||||
{ href: '/pricing', text: 'Pricing' },
|
{ href: '/pricing', text: 'Pricing' },
|
||||||
{ href: '/singleplayer', text: 'Singleplayer' },
|
{ href: '/single-player-mode', text: 'Single Player Mode' },
|
||||||
{ href: '/blog', text: 'Blog' },
|
{ href: '/blog', text: 'Blog' },
|
||||||
{ href: '/open', text: 'Open' },
|
{ href: '/open', text: 'Open' },
|
||||||
{ href: 'https://shop.documenso.com', text: 'Shop', target: '_blank' },
|
{ href: 'https://shop.documenso.com', text: 'Shop', target: '_blank' },
|
||||||
{ href: 'https://status.documenso.com', text: 'Status', target: '_blank' },
|
{ href: 'https://status.documenso.com', text: 'Status', target: '_blank' },
|
||||||
{ href: 'mailto:support@documenso.com', text: 'Support', target: '_blank' },
|
{ href: 'mailto:support@documenso.com', text: 'Support' },
|
||||||
{ href: '/privacy', text: 'Privacy' },
|
{ href: '/privacy', text: 'Privacy' },
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|||||||
@ -35,7 +35,7 @@ export const Header = ({ className, ...props }: HeaderProps) => {
|
|||||||
|
|
||||||
{isSinglePlayerModeMarketingEnabled && (
|
{isSinglePlayerModeMarketingEnabled && (
|
||||||
<Link
|
<Link
|
||||||
href="/singleplayer"
|
href="/single-player-mode"
|
||||||
className="bg-primary dark:text-background rounded-full px-2 py-1 text-xs font-semibold sm:px-3"
|
className="bg-primary dark:text-background rounded-full px-2 py-1 text-xs font-semibold sm:px-3"
|
||||||
>
|
>
|
||||||
Try now!
|
Try now!
|
||||||
|
|||||||
@ -134,9 +134,9 @@ export const Hero = ({ className, ...props }: HeroProps) => {
|
|||||||
variants={HeroTitleVariants}
|
variants={HeroTitleVariants}
|
||||||
initial="initial"
|
initial="initial"
|
||||||
animate="animate"
|
animate="animate"
|
||||||
className="border-primary bg-background hover:bg-muted mx-auto mt-8 w-60 rounded-xl border transition-colors duration-300"
|
className="border-primary bg-background hover:bg-muted mx-auto mt-8 w-60 rounded-xl border transition duration-300"
|
||||||
>
|
>
|
||||||
<Link href="/singleplayer" className="block px-4 py-2 text-center">
|
<Link href="/single-player-mode" className="block px-4 py-2 text-center">
|
||||||
<h2 className="text-muted-foreground text-xs font-semibold">
|
<h2 className="text-muted-foreground text-xs font-semibold">
|
||||||
Introducing Single Player Mode
|
Introducing Single Player Mode
|
||||||
</h2>
|
</h2>
|
||||||
|
|||||||
@ -17,8 +17,8 @@ export type MobileNavigationProps = {
|
|||||||
|
|
||||||
export const MENU_NAVIGATION_LINKS = [
|
export const MENU_NAVIGATION_LINKS = [
|
||||||
{
|
{
|
||||||
href: '/singleplayer',
|
href: '/single-player-mode',
|
||||||
text: 'Singleplayer',
|
text: 'Single Player Mode',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
href: '/blog',
|
href: '/blog',
|
||||||
@ -39,7 +39,6 @@ export const MENU_NAVIGATION_LINKS = [
|
|||||||
{
|
{
|
||||||
href: 'mailto:support@documenso.com',
|
href: 'mailto:support@documenso.com',
|
||||||
text: 'Support',
|
text: 'Support',
|
||||||
target: '_blank',
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
href: '/privacy',
|
href: '/privacy',
|
||||||
@ -79,7 +78,7 @@ export const MobileNavigation = ({ isMenuOpen, onMenuOpenChange }: MobileNavigat
|
|||||||
staggerChildren: 0.03,
|
staggerChildren: 0.03,
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{MENU_NAVIGATION_LINKS.map(({ href, text, target }) => (
|
{MENU_NAVIGATION_LINKS.map(({ href, text }) => (
|
||||||
<motion.div
|
<motion.div
|
||||||
key={href}
|
key={href}
|
||||||
variants={{
|
variants={{
|
||||||
@ -101,7 +100,6 @@ export const MobileNavigation = ({ isMenuOpen, onMenuOpenChange }: MobileNavigat
|
|||||||
className="text-foreground hover:text-foreground/80 text-2xl font-semibold"
|
className="text-foreground hover:text-foreground/80 text-2xl font-semibold"
|
||||||
href={href}
|
href={href}
|
||||||
onClick={() => handleMenuItemClick()}
|
onClick={() => handleMenuItemClick()}
|
||||||
target={target}
|
|
||||||
>
|
>
|
||||||
{text}
|
{text}
|
||||||
</Link>
|
</Link>
|
||||||
|
|||||||
@ -31,7 +31,7 @@ import { FormErrorMessage } from '../form/form-error-message';
|
|||||||
const ZWidgetFormSchema = z
|
const ZWidgetFormSchema = z
|
||||||
.object({
|
.object({
|
||||||
email: z.string().email({ message: 'Please enter a valid email address.' }),
|
email: z.string().email({ message: 'Please enter a valid email address.' }),
|
||||||
name: z.string().trim().min(3, { message: 'Please enter a valid name.' }),
|
name: z.string().min(3, { message: 'Please enter a valid name.' }),
|
||||||
})
|
})
|
||||||
.and(
|
.and(
|
||||||
z.union([
|
z.union([
|
||||||
@ -41,7 +41,7 @@ const ZWidgetFormSchema = z
|
|||||||
}),
|
}),
|
||||||
z.object({
|
z.object({
|
||||||
signatureDataUrl: z.null().or(z.string().max(0)),
|
signatureDataUrl: z.null().or(z.string().max(0)),
|
||||||
signatureText: z.string().trim().min(1),
|
signatureText: z.string().min(1),
|
||||||
}),
|
}),
|
||||||
]),
|
]),
|
||||||
);
|
);
|
||||||
|
|||||||
@ -41,7 +41,7 @@ export default async function handler(
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
if (user && user.Subscription) {
|
if (user && user.Subscription.length > 0) {
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
redirectUrl: `${process.env.NEXT_PUBLIC_WEBAPP_URL}/login`,
|
redirectUrl: `${process.env.NEXT_PUBLIC_WEBAPP_URL}/login`,
|
||||||
});
|
});
|
||||||
|
|||||||
@ -2,12 +2,8 @@
|
|||||||
const path = require('path');
|
const path = require('path');
|
||||||
const { version } = require('./package.json');
|
const { version } = require('./package.json');
|
||||||
|
|
||||||
const ENV_FILES = ['.env', '.env.local', `.env.${process.env.NODE_ENV || 'development'}`];
|
require('dotenv').config({
|
||||||
|
path: path.join(__dirname, '../../.env.local'),
|
||||||
ENV_FILES.forEach((file) => {
|
|
||||||
require('dotenv').config({
|
|
||||||
path: path.join(__dirname, `../../${file}`),
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
/** @type {import('next').NextConfig} */
|
/** @type {import('next').NextConfig} */
|
||||||
@ -33,14 +29,6 @@ const config = {
|
|||||||
transform: 'lucide-react/dist/esm/icons/{{ kebabCase member }}',
|
transform: 'lucide-react/dist/esm/icons/{{ kebabCase member }}',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
webpack: (config, { isServer }) => {
|
|
||||||
// fixes: Module not found: Can’t resolve ‘../build/Release/canvas.node’
|
|
||||||
if (isServer) {
|
|
||||||
config.resolve.alias.canvas = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return config;
|
|
||||||
},
|
|
||||||
async rewrites() {
|
async rewrites() {
|
||||||
return [
|
return [
|
||||||
{
|
{
|
||||||
@ -49,32 +37,6 @@ const config = {
|
|||||||
},
|
},
|
||||||
];
|
];
|
||||||
},
|
},
|
||||||
async redirects() {
|
|
||||||
return [
|
|
||||||
{
|
|
||||||
permanent: true,
|
|
||||||
source: '/documents/:id/sign',
|
|
||||||
destination: '/sign/:token',
|
|
||||||
has: [
|
|
||||||
{
|
|
||||||
type: 'query',
|
|
||||||
key: 'token',
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
permanent: true,
|
|
||||||
source: '/documents/:id/signed',
|
|
||||||
destination: '/sign/:token',
|
|
||||||
has: [
|
|
||||||
{
|
|
||||||
type: 'query',
|
|
||||||
key: 'token',
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
];
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = config;
|
module.exports = config;
|
||||||
|
|||||||
@ -25,7 +25,7 @@
|
|||||||
"lucide-react": "^0.279.0",
|
"lucide-react": "^0.279.0",
|
||||||
"luxon": "^3.4.0",
|
"luxon": "^3.4.0",
|
||||||
"micro": "^10.0.1",
|
"micro": "^10.0.1",
|
||||||
"next": "13.5.4",
|
"next": "13.4.19",
|
||||||
"next-auth": "4.22.3",
|
"next-auth": "4.22.3",
|
||||||
"next-plausible": "^3.10.1",
|
"next-plausible": "^3.10.1",
|
||||||
"next-themes": "^0.2.1",
|
"next-themes": "^0.2.1",
|
||||||
@ -40,7 +40,7 @@
|
|||||||
"react-rnd": "^10.4.1",
|
"react-rnd": "^10.4.1",
|
||||||
"sharp": "0.32.5",
|
"sharp": "0.32.5",
|
||||||
"ts-pattern": "^5.0.5",
|
"ts-pattern": "^5.0.5",
|
||||||
"typescript": "5.2.2",
|
"typescript": "5.1.6",
|
||||||
"zod": "^3.21.4"
|
"zod": "^3.21.4"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
|||||||
1
apps/web/process-env.d.ts
vendored
1
apps/web/process-env.d.ts
vendored
@ -7,7 +7,6 @@ declare namespace NodeJS {
|
|||||||
|
|
||||||
NEXT_PUBLIC_STRIPE_COMMUNITY_PLAN_MONTHLY_PRICE_ID: string;
|
NEXT_PUBLIC_STRIPE_COMMUNITY_PLAN_MONTHLY_PRICE_ID: string;
|
||||||
NEXT_PUBLIC_STRIPE_COMMUNITY_PLAN_YEARLY_PRICE_ID: string;
|
NEXT_PUBLIC_STRIPE_COMMUNITY_PLAN_YEARLY_PRICE_ID: string;
|
||||||
NEXT_PUBLIC_STRIPE_FREE_PLAN_ID?: string;
|
|
||||||
|
|
||||||
NEXT_PRIVATE_STRIPE_API_KEY: string;
|
NEXT_PRIVATE_STRIPE_API_KEY: string;
|
||||||
NEXT_PRIVATE_STRIPE_WEBHOOK_SECRET: string;
|
NEXT_PRIVATE_STRIPE_WEBHOOK_SECRET: string;
|
||||||
|
|||||||
@ -1,106 +0,0 @@
|
|||||||
'use client';
|
|
||||||
|
|
||||||
import { useTransition } from 'react';
|
|
||||||
|
|
||||||
import Link from 'next/link';
|
|
||||||
|
|
||||||
import { Loader } from 'lucide-react';
|
|
||||||
|
|
||||||
import { useUpdateSearchParams } from '@documenso/lib/client-only/hooks/use-update-search-params';
|
|
||||||
import { FindResultSet } from '@documenso/lib/types/find-result-set';
|
|
||||||
import { recipientInitials } from '@documenso/lib/utils/recipient-formatter';
|
|
||||||
import { Document, User } from '@documenso/prisma/client';
|
|
||||||
import { Avatar, AvatarFallback } from '@documenso/ui/primitives/avatar';
|
|
||||||
import { DataTable } from '@documenso/ui/primitives/data-table';
|
|
||||||
import { DataTablePagination } from '@documenso/ui/primitives/data-table-pagination';
|
|
||||||
|
|
||||||
import { DocumentStatus } from '~/components/formatter/document-status';
|
|
||||||
import { LocaleDate } from '~/components/formatter/locale-date';
|
|
||||||
|
|
||||||
export type DocumentsDataTableProps = {
|
|
||||||
results: FindResultSet<
|
|
||||||
Document & {
|
|
||||||
User: Pick<User, 'id' | 'name' | 'email'>;
|
|
||||||
}
|
|
||||||
>;
|
|
||||||
};
|
|
||||||
|
|
||||||
export const DocumentsDataTable = ({ results }: DocumentsDataTableProps) => {
|
|
||||||
const [isPending, startTransition] = useTransition();
|
|
||||||
|
|
||||||
const updateSearchParams = useUpdateSearchParams();
|
|
||||||
|
|
||||||
const onPaginationChange = (page: number, perPage: number) => {
|
|
||||||
startTransition(() => {
|
|
||||||
updateSearchParams({
|
|
||||||
page,
|
|
||||||
perPage,
|
|
||||||
});
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div className="relative">
|
|
||||||
<DataTable
|
|
||||||
columns={[
|
|
||||||
{
|
|
||||||
header: 'Created',
|
|
||||||
accessorKey: 'createdAt',
|
|
||||||
cell: ({ row }) => <LocaleDate date={row.original.createdAt} />,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
header: 'Title',
|
|
||||||
accessorKey: 'title',
|
|
||||||
cell: ({ row }) => {
|
|
||||||
return (
|
|
||||||
<div className="block max-w-[5rem] truncate font-medium md:max-w-[10rem]">
|
|
||||||
{row.original.title}
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
header: 'Owner',
|
|
||||||
accessorKey: 'owner',
|
|
||||||
cell: ({ row }) => {
|
|
||||||
return (
|
|
||||||
<Link href={`/admin/users/${row.original.User.id}`}>
|
|
||||||
<Avatar className="dark:border-border h-12 w-12 border-2 border-solid border-white">
|
|
||||||
<AvatarFallback className="text-gray-400">
|
|
||||||
<span className="text-sm">
|
|
||||||
{recipientInitials(row.original.User.name ?? '')}
|
|
||||||
</span>
|
|
||||||
</AvatarFallback>
|
|
||||||
</Avatar>
|
|
||||||
</Link>
|
|
||||||
);
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
header: 'Last updated',
|
|
||||||
accessorKey: 'updatedAt',
|
|
||||||
cell: ({ row }) => <LocaleDate date={row.original.updatedAt} />,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
header: 'Status',
|
|
||||||
accessorKey: 'status',
|
|
||||||
cell: ({ row }) => <DocumentStatus status={row.original.status} />,
|
|
||||||
},
|
|
||||||
]}
|
|
||||||
data={results.data}
|
|
||||||
perPage={results.perPage}
|
|
||||||
currentPage={results.currentPage}
|
|
||||||
totalPages={results.totalPages}
|
|
||||||
onPaginationChange={onPaginationChange}
|
|
||||||
>
|
|
||||||
{(table) => <DataTablePagination additionalInformation="VisibleCount" table={table} />}
|
|
||||||
</DataTable>
|
|
||||||
|
|
||||||
{isPending && (
|
|
||||||
<div className="absolute inset-0 flex items-center justify-center bg-white/50">
|
|
||||||
<Loader className="h-8 w-8 animate-spin text-gray-500" />
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
};
|
|
||||||
@ -1,29 +0,0 @@
|
|||||||
import { findDocuments } from '@documenso/lib/server-only/admin/get-all-documents';
|
|
||||||
|
|
||||||
import { DocumentsDataTable } from './data-table';
|
|
||||||
|
|
||||||
export type DocumentsPageProps = {
|
|
||||||
searchParams?: {
|
|
||||||
page?: string;
|
|
||||||
perPage?: string;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
export default async function Documents({ searchParams = {} }: DocumentsPageProps) {
|
|
||||||
const page = Number(searchParams.page) || 1;
|
|
||||||
const perPage = Number(searchParams.perPage) || 20;
|
|
||||||
|
|
||||||
const results = await findDocuments({
|
|
||||||
page,
|
|
||||||
perPage,
|
|
||||||
});
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div>
|
|
||||||
<h2 className="text-4xl font-semibold">Manage documents</h2>
|
|
||||||
<div className="mt-8">
|
|
||||||
<DocumentsDataTable results={results} />
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@ -5,7 +5,7 @@ import { HTMLAttributes } from 'react';
|
|||||||
import Link from 'next/link';
|
import Link from 'next/link';
|
||||||
import { usePathname } from 'next/navigation';
|
import { usePathname } from 'next/navigation';
|
||||||
|
|
||||||
import { BarChart3, FileStack, User2, Wallet2 } from 'lucide-react';
|
import { BarChart3, User2 } from 'lucide-react';
|
||||||
|
|
||||||
import { cn } from '@documenso/ui/lib/utils';
|
import { cn } from '@documenso/ui/lib/utils';
|
||||||
import { Button } from '@documenso/ui/primitives/button';
|
import { Button } from '@documenso/ui/primitives/button';
|
||||||
@ -37,40 +37,10 @@ export const AdminNav = ({ className, ...props }: AdminNavProps) => {
|
|||||||
'justify-start md:w-full',
|
'justify-start md:w-full',
|
||||||
pathname?.startsWith('/admin/users') && 'bg-secondary',
|
pathname?.startsWith('/admin/users') && 'bg-secondary',
|
||||||
)}
|
)}
|
||||||
asChild
|
disabled
|
||||||
>
|
>
|
||||||
<Link href="/admin/users">
|
|
||||||
<User2 className="mr-2 h-5 w-5" />
|
<User2 className="mr-2 h-5 w-5" />
|
||||||
Users
|
Users (Coming Soon)
|
||||||
</Link>
|
|
||||||
</Button>
|
|
||||||
|
|
||||||
<Button
|
|
||||||
variant="ghost"
|
|
||||||
className={cn(
|
|
||||||
'justify-start md:w-full',
|
|
||||||
pathname?.startsWith('/admin/documents') && 'bg-secondary',
|
|
||||||
)}
|
|
||||||
asChild
|
|
||||||
>
|
|
||||||
<Link href="/admin/documents">
|
|
||||||
<FileStack className="mr-2 h-5 w-5" />
|
|
||||||
Documents
|
|
||||||
</Link>
|
|
||||||
</Button>
|
|
||||||
|
|
||||||
<Button
|
|
||||||
variant="ghost"
|
|
||||||
className={cn(
|
|
||||||
'justify-start md:w-full',
|
|
||||||
pathname?.startsWith('/admin/subscriptions') && 'bg-secondary',
|
|
||||||
)}
|
|
||||||
asChild
|
|
||||||
>
|
|
||||||
<Link href="/admin/subscriptions">
|
|
||||||
<Wallet2 className="mr-2 h-5 w-5" />
|
|
||||||
Subscriptions
|
|
||||||
</Link>
|
|
||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|||||||
@ -1,65 +0,0 @@
|
|||||||
import Link from 'next/link';
|
|
||||||
|
|
||||||
import { findSubscriptions } from '@documenso/lib/server-only/admin/get-all-subscriptions';
|
|
||||||
import {
|
|
||||||
Table,
|
|
||||||
TableBody,
|
|
||||||
TableCell,
|
|
||||||
TableHead,
|
|
||||||
TableHeader,
|
|
||||||
TableRow,
|
|
||||||
} from '@documenso/ui/primitives/table';
|
|
||||||
|
|
||||||
export default async function Subscriptions() {
|
|
||||||
const subscriptions = await findSubscriptions();
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div>
|
|
||||||
<h2 className="text-4xl font-semibold">Manage subscriptions</h2>
|
|
||||||
<div className="mt-8">
|
|
||||||
<Table>
|
|
||||||
<TableHeader>
|
|
||||||
<TableRow>
|
|
||||||
<TableHead>ID</TableHead>
|
|
||||||
<TableHead>Status</TableHead>
|
|
||||||
<TableHead>Created At</TableHead>
|
|
||||||
<TableHead>Ends On</TableHead>
|
|
||||||
<TableHead>User ID</TableHead>
|
|
||||||
</TableRow>
|
|
||||||
</TableHeader>
|
|
||||||
<TableBody>
|
|
||||||
{subscriptions.map((subscription, index) => (
|
|
||||||
<TableRow key={index}>
|
|
||||||
<TableCell>{subscription.id}</TableCell>
|
|
||||||
<TableCell>{subscription.status}</TableCell>
|
|
||||||
<TableCell>
|
|
||||||
{subscription.createdAt
|
|
||||||
? new Date(subscription.createdAt).toLocaleDateString(undefined, {
|
|
||||||
weekday: 'long',
|
|
||||||
year: 'numeric',
|
|
||||||
month: 'long',
|
|
||||||
day: 'numeric',
|
|
||||||
})
|
|
||||||
: 'N/A'}
|
|
||||||
</TableCell>
|
|
||||||
<TableCell>
|
|
||||||
{subscription.periodEnd
|
|
||||||
? new Date(subscription.periodEnd).toLocaleDateString(undefined, {
|
|
||||||
weekday: 'long',
|
|
||||||
year: 'numeric',
|
|
||||||
month: 'long',
|
|
||||||
day: 'numeric',
|
|
||||||
})
|
|
||||||
: 'N/A'}
|
|
||||||
</TableCell>
|
|
||||||
<TableCell>
|
|
||||||
<Link href={`/admin/users/${subscription.userId}`}>{subscription.userId}</Link>
|
|
||||||
</TableCell>
|
|
||||||
</TableRow>
|
|
||||||
))}
|
|
||||||
</TableBody>
|
|
||||||
</Table>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@ -1,141 +0,0 @@
|
|||||||
'use client';
|
|
||||||
|
|
||||||
import { useRouter } from 'next/navigation';
|
|
||||||
|
|
||||||
import { zodResolver } from '@hookform/resolvers/zod';
|
|
||||||
import { useForm } from 'react-hook-form';
|
|
||||||
import { z } from 'zod';
|
|
||||||
|
|
||||||
import { trpc } from '@documenso/trpc/react';
|
|
||||||
import { ZUpdateProfileMutationByAdminSchema } from '@documenso/trpc/server/admin-router/schema';
|
|
||||||
import { Button } from '@documenso/ui/primitives/button';
|
|
||||||
import { Combobox } from '@documenso/ui/primitives/combobox';
|
|
||||||
import {
|
|
||||||
Form,
|
|
||||||
FormControl,
|
|
||||||
FormField,
|
|
||||||
FormItem,
|
|
||||||
FormLabel,
|
|
||||||
FormMessage,
|
|
||||||
} from '@documenso/ui/primitives/form/form';
|
|
||||||
import { Input } from '@documenso/ui/primitives/input';
|
|
||||||
import { useToast } from '@documenso/ui/primitives/use-toast';
|
|
||||||
|
|
||||||
const ZUserFormSchema = ZUpdateProfileMutationByAdminSchema.omit({ id: true });
|
|
||||||
|
|
||||||
type TUserFormSchema = z.infer<typeof ZUserFormSchema>;
|
|
||||||
|
|
||||||
export default function UserPage({ params }: { params: { id: number } }) {
|
|
||||||
const { toast } = useToast();
|
|
||||||
const router = useRouter();
|
|
||||||
|
|
||||||
const { data: user } = trpc.profile.getUser.useQuery(
|
|
||||||
{
|
|
||||||
id: Number(params.id),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
enabled: !!params.id,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
const roles = user?.roles ?? [];
|
|
||||||
|
|
||||||
const { mutateAsync: updateUserMutation } = trpc.admin.updateUser.useMutation();
|
|
||||||
|
|
||||||
const form = useForm<TUserFormSchema>({
|
|
||||||
resolver: zodResolver(ZUserFormSchema),
|
|
||||||
values: {
|
|
||||||
name: user?.name ?? '',
|
|
||||||
email: user?.email ?? '',
|
|
||||||
roles: user?.roles ?? [],
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
const onSubmit = async ({ name, email, roles }: TUserFormSchema) => {
|
|
||||||
try {
|
|
||||||
await updateUserMutation({
|
|
||||||
id: Number(user?.id),
|
|
||||||
name,
|
|
||||||
email,
|
|
||||||
roles,
|
|
||||||
});
|
|
||||||
|
|
||||||
router.refresh();
|
|
||||||
|
|
||||||
toast({
|
|
||||||
title: 'Profile updated',
|
|
||||||
description: 'Your profile has been updated.',
|
|
||||||
duration: 5000,
|
|
||||||
});
|
|
||||||
} catch (e) {
|
|
||||||
toast({
|
|
||||||
title: 'Error',
|
|
||||||
description: 'An error occurred while updating your profile.',
|
|
||||||
variant: 'destructive',
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div>
|
|
||||||
<h2 className="text-4xl font-semibold">Manage {user?.name}'s profile</h2>
|
|
||||||
<Form {...form}>
|
|
||||||
<form onSubmit={form.handleSubmit(onSubmit)}>
|
|
||||||
<fieldset className="mt-6 flex w-full flex-col gap-y-4">
|
|
||||||
<FormField
|
|
||||||
control={form.control}
|
|
||||||
name="name"
|
|
||||||
render={({ field }) => (
|
|
||||||
<FormItem>
|
|
||||||
<FormLabel className="text-muted-foreground">Name</FormLabel>
|
|
||||||
<FormControl>
|
|
||||||
<Input type="text" {...field} value={field.value ?? ''} />
|
|
||||||
</FormControl>
|
|
||||||
<FormMessage />
|
|
||||||
</FormItem>
|
|
||||||
)}
|
|
||||||
/>
|
|
||||||
<FormField
|
|
||||||
control={form.control}
|
|
||||||
name="email"
|
|
||||||
render={({ field }) => (
|
|
||||||
<FormItem>
|
|
||||||
<FormLabel className="text-muted-foreground">Email</FormLabel>
|
|
||||||
<FormControl>
|
|
||||||
<Input type="text" {...field} />
|
|
||||||
</FormControl>
|
|
||||||
<FormMessage />
|
|
||||||
</FormItem>
|
|
||||||
)}
|
|
||||||
/>
|
|
||||||
|
|
||||||
<FormField
|
|
||||||
control={form.control}
|
|
||||||
name="roles"
|
|
||||||
render={({ field: { onChange } }) => (
|
|
||||||
<FormItem>
|
|
||||||
<fieldset className="flex flex-col gap-2">
|
|
||||||
<FormLabel className="text-muted-foreground">Roles</FormLabel>
|
|
||||||
<FormControl>
|
|
||||||
<Combobox
|
|
||||||
listValues={roles}
|
|
||||||
onChange={(values: string[]) => onChange(values)}
|
|
||||||
/>
|
|
||||||
</FormControl>
|
|
||||||
<FormMessage />
|
|
||||||
</fieldset>
|
|
||||||
</FormItem>
|
|
||||||
)}
|
|
||||||
/>
|
|
||||||
|
|
||||||
<div className="mt-4">
|
|
||||||
<Button type="submit" loading={form.formState.isSubmitting}>
|
|
||||||
Update user
|
|
||||||
</Button>
|
|
||||||
</div>
|
|
||||||
</fieldset>
|
|
||||||
</form>
|
|
||||||
</Form>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@ -1,143 +0,0 @@
|
|||||||
'use client';
|
|
||||||
|
|
||||||
import { useEffect, useState, useTransition } from 'react';
|
|
||||||
|
|
||||||
import Link from 'next/link';
|
|
||||||
|
|
||||||
import { Edit, Loader } from 'lucide-react';
|
|
||||||
|
|
||||||
import { useDebouncedValue } from '@documenso/lib/client-only/hooks/use-debounced-value';
|
|
||||||
import { useUpdateSearchParams } from '@documenso/lib/client-only/hooks/use-update-search-params';
|
|
||||||
import { Document, Role, Subscription } from '@documenso/prisma/client';
|
|
||||||
import { Button } from '@documenso/ui/primitives/button';
|
|
||||||
import { DataTable } from '@documenso/ui/primitives/data-table';
|
|
||||||
import { DataTablePagination } from '@documenso/ui/primitives/data-table-pagination';
|
|
||||||
import { Input } from '@documenso/ui/primitives/input';
|
|
||||||
|
|
||||||
type UserData = {
|
|
||||||
id: number;
|
|
||||||
name: string | null;
|
|
||||||
email: string;
|
|
||||||
roles: Role[];
|
|
||||||
Subscription?: SubscriptionLite | null;
|
|
||||||
Document: DocumentLite[];
|
|
||||||
};
|
|
||||||
|
|
||||||
type SubscriptionLite = Pick<
|
|
||||||
Subscription,
|
|
||||||
'id' | 'status' | 'planId' | 'priceId' | 'createdAt' | 'periodEnd'
|
|
||||||
>;
|
|
||||||
|
|
||||||
type DocumentLite = Pick<Document, 'id'>;
|
|
||||||
|
|
||||||
type UsersDataTableProps = {
|
|
||||||
users: UserData[];
|
|
||||||
totalPages: number;
|
|
||||||
perPage: number;
|
|
||||||
page: number;
|
|
||||||
};
|
|
||||||
|
|
||||||
export const UsersDataTable = ({ users, totalPages, perPage, page }: UsersDataTableProps) => {
|
|
||||||
const [isPending, startTransition] = useTransition();
|
|
||||||
const updateSearchParams = useUpdateSearchParams();
|
|
||||||
const [searchString, setSearchString] = useState('');
|
|
||||||
const debouncedSearchString = useDebouncedValue(searchString, 1000);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
startTransition(() => {
|
|
||||||
updateSearchParams({
|
|
||||||
search: debouncedSearchString,
|
|
||||||
page: 1,
|
|
||||||
perPage,
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}, [debouncedSearchString]);
|
|
||||||
|
|
||||||
const onPaginationChange = (page: number, perPage: number) => {
|
|
||||||
startTransition(() => {
|
|
||||||
updateSearchParams({
|
|
||||||
page,
|
|
||||||
perPage,
|
|
||||||
});
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
|
||||||
setSearchString(e.target.value);
|
|
||||||
};
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div className="relative">
|
|
||||||
<Input
|
|
||||||
className="my-6 flex flex-row gap-4"
|
|
||||||
type="text"
|
|
||||||
placeholder="Search by name or email"
|
|
||||||
value={searchString}
|
|
||||||
onChange={handleChange}
|
|
||||||
/>
|
|
||||||
<DataTable
|
|
||||||
columns={[
|
|
||||||
{
|
|
||||||
header: 'ID',
|
|
||||||
accessorKey: 'id',
|
|
||||||
cell: ({ row }) => <div>{row.original.id}</div>,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
header: 'Name',
|
|
||||||
accessorKey: 'name',
|
|
||||||
cell: ({ row }) => <div>{row.original.name}</div>,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
header: 'Email',
|
|
||||||
accessorKey: 'email',
|
|
||||||
cell: ({ row }) => <div>{row.original.email}</div>,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
header: 'Roles',
|
|
||||||
accessorKey: 'roles',
|
|
||||||
cell: ({ row }) => row.original.roles.join(', '),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
header: 'Subscription',
|
|
||||||
accessorKey: 'subscription',
|
|
||||||
cell: ({ row }) => row.original.Subscription?.status ?? 'NONE',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
header: 'Documents',
|
|
||||||
accessorKey: 'documents',
|
|
||||||
cell: ({ row }) => {
|
|
||||||
return <div>{row.original.Document.length}</div>;
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
header: '',
|
|
||||||
accessorKey: 'edit',
|
|
||||||
cell: ({ row }) => {
|
|
||||||
return (
|
|
||||||
<Button className="w-24" asChild>
|
|
||||||
<Link href={`/admin/users/${row.original.id}`}>
|
|
||||||
<Edit className="-ml-1 mr-2 h-4 w-4" />
|
|
||||||
Edit
|
|
||||||
</Link>
|
|
||||||
</Button>
|
|
||||||
);
|
|
||||||
},
|
|
||||||
},
|
|
||||||
]}
|
|
||||||
data={users}
|
|
||||||
perPage={perPage}
|
|
||||||
currentPage={page}
|
|
||||||
totalPages={totalPages}
|
|
||||||
onPaginationChange={onPaginationChange}
|
|
||||||
>
|
|
||||||
{(table) => <DataTablePagination additionalInformation="VisibleCount" table={table} />}
|
|
||||||
</DataTable>
|
|
||||||
|
|
||||||
{isPending && (
|
|
||||||
<div className="absolute inset-0 flex items-center justify-center bg-white/50">
|
|
||||||
<Loader className="h-8 w-8 animate-spin text-gray-500" />
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
};
|
|
||||||
@ -1,9 +0,0 @@
|
|||||||
'use server';
|
|
||||||
|
|
||||||
import { findUsers } from '@documenso/lib/server-only/user/get-all-users';
|
|
||||||
|
|
||||||
export async function search(search: string, page: number, perPage: number) {
|
|
||||||
const results = await findUsers({ username: search, email: search, page, perPage });
|
|
||||||
|
|
||||||
return results;
|
|
||||||
}
|
|
||||||
@ -1,25 +0,0 @@
|
|||||||
import { UsersDataTable } from './data-table-users';
|
|
||||||
import { search } from './fetch-users.actions';
|
|
||||||
|
|
||||||
type AdminManageUsersProps = {
|
|
||||||
searchParams?: {
|
|
||||||
search?: string;
|
|
||||||
page?: number;
|
|
||||||
perPage?: number;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
export default async function AdminManageUsers({ searchParams = {} }: AdminManageUsersProps) {
|
|
||||||
const page = Number(searchParams.page) || 1;
|
|
||||||
const perPage = Number(searchParams.perPage) || 10;
|
|
||||||
const searchString = searchParams.search || '';
|
|
||||||
|
|
||||||
const { users, totalPages } = await search(searchString, page, perPage);
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div>
|
|
||||||
<h2 className="text-4xl font-semibold">Manage users</h2>
|
|
||||||
<UsersDataTable users={users} totalPages={totalPages} page={page} perPage={perPage} />
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@ -6,12 +6,9 @@ import { Edit, Pencil, Share } from 'lucide-react';
|
|||||||
import { useSession } from 'next-auth/react';
|
import { useSession } from 'next-auth/react';
|
||||||
import { match } from 'ts-pattern';
|
import { match } from 'ts-pattern';
|
||||||
|
|
||||||
import { useCopyShareLink } from '@documenso/lib/client-only/hooks/use-copy-share-link';
|
import { useCopyToClipboard } from '@documenso/lib/client-only/hooks/use-copy-to-clipboard';
|
||||||
import {
|
|
||||||
TOAST_DOCUMENT_SHARE_ERROR,
|
|
||||||
TOAST_DOCUMENT_SHARE_SUCCESS,
|
|
||||||
} from '@documenso/lib/constants/toast';
|
|
||||||
import { Document, DocumentStatus, Recipient, SigningStatus, User } from '@documenso/prisma/client';
|
import { Document, DocumentStatus, Recipient, SigningStatus, User } from '@documenso/prisma/client';
|
||||||
|
import { trpc } from '@documenso/trpc/react';
|
||||||
import { Button } from '@documenso/ui/primitives/button';
|
import { Button } from '@documenso/ui/primitives/button';
|
||||||
import { useToast } from '@documenso/ui/primitives/use-toast';
|
import { useToast } from '@documenso/ui/primitives/use-toast';
|
||||||
|
|
||||||
@ -24,18 +21,16 @@ export type DataTableActionButtonProps = {
|
|||||||
|
|
||||||
export const DataTableActionButton = ({ row }: DataTableActionButtonProps) => {
|
export const DataTableActionButton = ({ row }: DataTableActionButtonProps) => {
|
||||||
const { data: session } = useSession();
|
const { data: session } = useSession();
|
||||||
|
|
||||||
const { toast } = useToast();
|
const { toast } = useToast();
|
||||||
|
const [, copyToClipboard] = useCopyToClipboard();
|
||||||
const { createAndCopyShareLink, isCopyingShareLink } = useCopyShareLink({
|
|
||||||
onSuccess: () => toast(TOAST_DOCUMENT_SHARE_SUCCESS),
|
|
||||||
onError: () => toast(TOAST_DOCUMENT_SHARE_ERROR),
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!session) {
|
if (!session) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const { mutateAsync: createOrGetShareLink, isLoading: isCreatingShareLink } =
|
||||||
|
trpc.shareLink.createOrGetShareLink.useMutation();
|
||||||
|
|
||||||
const recipient = row.Recipient.find((recipient) => recipient.email === session.user.email);
|
const recipient = row.Recipient.find((recipient) => recipient.email === session.user.email);
|
||||||
|
|
||||||
const isOwner = row.User.id === session.user.id;
|
const isOwner = row.User.id === session.user.id;
|
||||||
@ -45,6 +40,20 @@ export const DataTableActionButton = ({ row }: DataTableActionButtonProps) => {
|
|||||||
const isComplete = row.status === DocumentStatus.COMPLETED;
|
const isComplete = row.status === DocumentStatus.COMPLETED;
|
||||||
const isSigned = recipient?.signingStatus === SigningStatus.SIGNED;
|
const isSigned = recipient?.signingStatus === SigningStatus.SIGNED;
|
||||||
|
|
||||||
|
const onShareClick = async () => {
|
||||||
|
const { slug } = await createOrGetShareLink({
|
||||||
|
token: recipient?.token,
|
||||||
|
documentId: row.id,
|
||||||
|
});
|
||||||
|
|
||||||
|
await copyToClipboard(`${process.env.NEXT_PUBLIC_WEBAPP_URL}/share/${slug}`).catch(() => null);
|
||||||
|
|
||||||
|
toast({
|
||||||
|
title: 'Copied to clipboard',
|
||||||
|
description: 'The sharing link has been copied to your clipboard.',
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
return match({
|
return match({
|
||||||
isOwner,
|
isOwner,
|
||||||
isRecipient,
|
isRecipient,
|
||||||
@ -70,17 +79,8 @@ export const DataTableActionButton = ({ row }: DataTableActionButtonProps) => {
|
|||||||
</Button>
|
</Button>
|
||||||
))
|
))
|
||||||
.otherwise(() => (
|
.otherwise(() => (
|
||||||
<Button
|
<Button className="w-24" loading={isCreatingShareLink} onClick={onShareClick}>
|
||||||
className="w-24"
|
{!isCreatingShareLink && <Share className="-ml-1 mr-2 h-4 w-4" />}
|
||||||
loading={isCopyingShareLink}
|
|
||||||
onClick={async () =>
|
|
||||||
createAndCopyShareLink({
|
|
||||||
token: recipient?.token,
|
|
||||||
documentId: row.id,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
>
|
|
||||||
{!isCopyingShareLink && <Share className="-ml-1 mr-2 h-4 w-4" />}
|
|
||||||
Share
|
Share
|
||||||
</Button>
|
</Button>
|
||||||
));
|
));
|
||||||
|
|||||||
@ -18,15 +18,12 @@ import {
|
|||||||
} from 'lucide-react';
|
} from 'lucide-react';
|
||||||
import { useSession } from 'next-auth/react';
|
import { useSession } from 'next-auth/react';
|
||||||
|
|
||||||
import { useCopyShareLink } from '@documenso/lib/client-only/hooks/use-copy-share-link';
|
import { useCopyToClipboard } from '@documenso/lib/client-only/hooks/use-copy-to-clipboard';
|
||||||
import {
|
|
||||||
TOAST_DOCUMENT_SHARE_ERROR,
|
|
||||||
TOAST_DOCUMENT_SHARE_SUCCESS,
|
|
||||||
} from '@documenso/lib/constants/toast';
|
|
||||||
import { getFile } from '@documenso/lib/universal/upload/get-file';
|
import { getFile } from '@documenso/lib/universal/upload/get-file';
|
||||||
import { Document, DocumentStatus, Recipient, User } from '@documenso/prisma/client';
|
import { Document, DocumentStatus, Recipient, User } from '@documenso/prisma/client';
|
||||||
import { DocumentWithData } from '@documenso/prisma/types/document-with-data';
|
import { DocumentWithData } from '@documenso/prisma/types/document-with-data';
|
||||||
import { trpc as trpcClient } from '@documenso/trpc/client';
|
import { trpc as trpcClient } from '@documenso/trpc/client';
|
||||||
|
import { trpc as trpcReact } from '@documenso/trpc/react';
|
||||||
import {
|
import {
|
||||||
DropdownMenu,
|
DropdownMenu,
|
||||||
DropdownMenuContent,
|
DropdownMenuContent,
|
||||||
@ -47,13 +44,8 @@ export type DataTableActionDropdownProps = {
|
|||||||
|
|
||||||
export const DataTableActionDropdown = ({ row }: DataTableActionDropdownProps) => {
|
export const DataTableActionDropdown = ({ row }: DataTableActionDropdownProps) => {
|
||||||
const { data: session } = useSession();
|
const { data: session } = useSession();
|
||||||
|
|
||||||
const { toast } = useToast();
|
const { toast } = useToast();
|
||||||
|
const [, copyToClipboard] = useCopyToClipboard();
|
||||||
const { createAndCopyShareLink, isCopyingShareLink } = useCopyShareLink({
|
|
||||||
onSuccess: () => toast(TOAST_DOCUMENT_SHARE_SUCCESS),
|
|
||||||
onError: () => toast(TOAST_DOCUMENT_SHARE_ERROR),
|
|
||||||
});
|
|
||||||
|
|
||||||
const [isDeleteDialogOpen, setDeleteDialogOpen] = useState(false);
|
const [isDeleteDialogOpen, setDeleteDialogOpen] = useState(false);
|
||||||
|
|
||||||
@ -61,16 +53,33 @@ export const DataTableActionDropdown = ({ row }: DataTableActionDropdownProps) =
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const { mutateAsync: createOrGetShareLink, isLoading: isCreatingShareLink } =
|
||||||
|
trpcReact.shareLink.createOrGetShareLink.useMutation();
|
||||||
|
|
||||||
const recipient = row.Recipient.find((recipient) => recipient.email === session.user.email);
|
const recipient = row.Recipient.find((recipient) => recipient.email === session.user.email);
|
||||||
|
|
||||||
const isOwner = row.User.id === session.user.id;
|
const isOwner = row.User.id === session.user.id;
|
||||||
// const isRecipient = !!recipient;
|
// const isRecipient = !!recipient;
|
||||||
const isDraft = row.status === DocumentStatus.DRAFT;
|
// const isDraft = row.status === DocumentStatus.DRAFT;
|
||||||
// const isPending = row.status === DocumentStatus.PENDING;
|
// const isPending = row.status === DocumentStatus.PENDING;
|
||||||
const isComplete = row.status === DocumentStatus.COMPLETED;
|
const isComplete = row.status === DocumentStatus.COMPLETED;
|
||||||
// const isSigned = recipient?.signingStatus === SigningStatus.SIGNED;
|
// const isSigned = recipient?.signingStatus === SigningStatus.SIGNED;
|
||||||
const isDocumentDeletable = isOwner && row.status === DocumentStatus.DRAFT;
|
const isDocumentDeletable = isOwner && row.status === DocumentStatus.DRAFT;
|
||||||
|
|
||||||
|
const onShareClick = async () => {
|
||||||
|
const { slug } = await createOrGetShareLink({
|
||||||
|
token: recipient?.token,
|
||||||
|
documentId: row.id,
|
||||||
|
});
|
||||||
|
|
||||||
|
await copyToClipboard(`${process.env.NEXT_PUBLIC_WEBAPP_URL}/share/${slug}`).catch(() => null);
|
||||||
|
|
||||||
|
toast({
|
||||||
|
title: 'Copied to clipboard',
|
||||||
|
description: 'The sharing link has been copied to your clipboard.',
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
const onDownloadClick = async () => {
|
const onDownloadClick = async () => {
|
||||||
let document: DocumentWithData | null = null;
|
let document: DocumentWithData | null = null;
|
||||||
|
|
||||||
@ -156,16 +165,8 @@ export const DataTableActionDropdown = ({ row }: DataTableActionDropdownProps) =
|
|||||||
Resend
|
Resend
|
||||||
</DropdownMenuItem>
|
</DropdownMenuItem>
|
||||||
|
|
||||||
<DropdownMenuItem
|
<DropdownMenuItem onClick={onShareClick}>
|
||||||
disabled={isDraft}
|
{isCreatingShareLink ? (
|
||||||
onClick={async () =>
|
|
||||||
createAndCopyShareLink({
|
|
||||||
token: recipient?.token,
|
|
||||||
documentId: row.id,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
>
|
|
||||||
{isCopyingShareLink ? (
|
|
||||||
<Loader className="mr-2 h-4 w-4" />
|
<Loader className="mr-2 h-4 w-4" />
|
||||||
) : (
|
) : (
|
||||||
<Share className="mr-2 h-4 w-4" />
|
<Share className="mr-2 h-4 w-4" />
|
||||||
|
|||||||
@ -2,15 +2,12 @@
|
|||||||
|
|
||||||
import { useState } from 'react';
|
import { useState } from 'react';
|
||||||
|
|
||||||
import Link from 'next/link';
|
|
||||||
import { useRouter } from 'next/navigation';
|
import { useRouter } from 'next/navigation';
|
||||||
|
|
||||||
import { Loader } from 'lucide-react';
|
import { Loader } from 'lucide-react';
|
||||||
|
|
||||||
import { useLimits } from '@documenso/ee/server-only/limits/provider/client';
|
|
||||||
import { createDocumentData } from '@documenso/lib/server-only/document-data/create-document-data';
|
import { createDocumentData } from '@documenso/lib/server-only/document-data/create-document-data';
|
||||||
import { putFile } from '@documenso/lib/universal/upload/put-file';
|
import { putFile } from '@documenso/lib/universal/upload/put-file';
|
||||||
import { TRPCClientError } from '@documenso/trpc/client';
|
|
||||||
import { trpc } from '@documenso/trpc/react';
|
import { trpc } from '@documenso/trpc/react';
|
||||||
import { cn } from '@documenso/ui/lib/utils';
|
import { cn } from '@documenso/ui/lib/utils';
|
||||||
import { DocumentDropzone } from '@documenso/ui/primitives/document-dropzone';
|
import { DocumentDropzone } from '@documenso/ui/primitives/document-dropzone';
|
||||||
@ -25,8 +22,6 @@ export const UploadDocument = ({ className }: UploadDocumentProps) => {
|
|||||||
|
|
||||||
const { toast } = useToast();
|
const { toast } = useToast();
|
||||||
|
|
||||||
const { quota, remaining } = useLimits();
|
|
||||||
|
|
||||||
const [isLoading, setIsLoading] = useState(false);
|
const [isLoading, setIsLoading] = useState(false);
|
||||||
|
|
||||||
const { mutateAsync: createDocument } = trpc.document.createDocument.useMutation();
|
const { mutateAsync: createDocument } = trpc.document.createDocument.useMutation();
|
||||||
@ -57,19 +52,11 @@ export const UploadDocument = ({ className }: UploadDocumentProps) => {
|
|||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
|
|
||||||
if (error instanceof TRPCClientError) {
|
|
||||||
toast({
|
|
||||||
title: 'Error',
|
|
||||||
description: error.message,
|
|
||||||
variant: 'destructive',
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
toast({
|
toast({
|
||||||
title: 'Error',
|
title: 'Error',
|
||||||
description: 'An error occurred while uploading your document.',
|
description: 'An error occurred while uploading your document.',
|
||||||
variant: 'destructive',
|
variant: 'destructive',
|
||||||
});
|
});
|
||||||
}
|
|
||||||
} finally {
|
} finally {
|
||||||
setIsLoading(false);
|
setIsLoading(false);
|
||||||
}
|
}
|
||||||
@ -77,46 +64,13 @@ export const UploadDocument = ({ className }: UploadDocumentProps) => {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={cn('relative', className)}>
|
<div className={cn('relative', className)}>
|
||||||
<DocumentDropzone
|
<DocumentDropzone className="min-h-[40vh]" onDrop={onFileDrop} />
|
||||||
className="min-h-[40vh]"
|
|
||||||
disabled={remaining.documents === 0}
|
|
||||||
onDrop={onFileDrop}
|
|
||||||
/>
|
|
||||||
|
|
||||||
<div className="absolute -bottom-6 right-0">
|
|
||||||
{remaining.documents > 0 && Number.isFinite(remaining.documents) && (
|
|
||||||
<p className="text-muted-foreground/60 text-xs">
|
|
||||||
{remaining.documents} of {quota.documents} documents remaining this month.
|
|
||||||
</p>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{isLoading && (
|
{isLoading && (
|
||||||
<div className="bg-background/50 absolute inset-0 flex items-center justify-center rounded-lg">
|
<div className="bg-background/50 absolute inset-0 flex items-center justify-center">
|
||||||
<Loader className="text-muted-foreground h-12 w-12 animate-spin" />
|
<Loader className="text-muted-foreground h-12 w-12 animate-spin" />
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
{remaining.documents === 0 && (
|
|
||||||
<div className="bg-background/60 absolute inset-0 flex items-center justify-center rounded-lg backdrop-blur-sm">
|
|
||||||
<div className="text-center">
|
|
||||||
<h2 className="text-muted-foreground/80 text-xl font-semibold">
|
|
||||||
You have reached your document limit.
|
|
||||||
</h2>
|
|
||||||
|
|
||||||
<p className="text-muted-foreground/60 mt-2 text-sm">
|
|
||||||
You can upload up to {quota.documents} documents per month on your current plan.
|
|
||||||
</p>
|
|
||||||
|
|
||||||
<Link
|
|
||||||
className="text-primary hover:text-primary/80 mt-6 block font-medium"
|
|
||||||
href="/settings/billing"
|
|
||||||
>
|
|
||||||
Upgrade your account to upload more documents.
|
|
||||||
</Link>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
@ -4,7 +4,6 @@ import { redirect } from 'next/navigation';
|
|||||||
|
|
||||||
import { getServerSession } from 'next-auth';
|
import { getServerSession } from 'next-auth';
|
||||||
|
|
||||||
import { LimitsProvider } from '@documenso/ee/server-only/limits/provider/server';
|
|
||||||
import { NEXT_AUTH_OPTIONS } from '@documenso/lib/next-auth/auth-options';
|
import { NEXT_AUTH_OPTIONS } from '@documenso/lib/next-auth/auth-options';
|
||||||
import { getRequiredServerComponentSession } from '@documenso/lib/next-auth/get-server-session';
|
import { getRequiredServerComponentSession } from '@documenso/lib/next-auth/get-server-session';
|
||||||
|
|
||||||
@ -29,13 +28,11 @@ export default async function AuthenticatedDashboardLayout({
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<NextAuthProvider session={session}>
|
<NextAuthProvider session={session}>
|
||||||
<LimitsProvider>
|
|
||||||
<Header user={user} />
|
<Header user={user} />
|
||||||
|
|
||||||
<main className="mt-8 pb-8 md:mt-12 md:pb-12">{children}</main>
|
<main className="mt-8 pb-8 md:mt-12 md:pb-12">{children}</main>
|
||||||
|
|
||||||
<RefreshOnFocus />
|
<RefreshOnFocus />
|
||||||
</LimitsProvider>
|
|
||||||
</NextAuthProvider>
|
</NextAuthProvider>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,133 +0,0 @@
|
|||||||
'use client';
|
|
||||||
|
|
||||||
import { useState } from 'react';
|
|
||||||
|
|
||||||
import { AnimatePresence, motion } from 'framer-motion';
|
|
||||||
|
|
||||||
import { PriceIntervals } from '@documenso/ee/server-only/stripe/get-prices-by-interval';
|
|
||||||
import { useIsMounted } from '@documenso/lib/client-only/hooks/use-is-mounted';
|
|
||||||
import { toHumanPrice } from '@documenso/lib/universal/stripe/to-human-price';
|
|
||||||
import { Button } from '@documenso/ui/primitives/button';
|
|
||||||
import { Card, CardContent, CardTitle } from '@documenso/ui/primitives/card';
|
|
||||||
import { Tabs, TabsList, TabsTrigger } from '@documenso/ui/primitives/tabs';
|
|
||||||
import { useToast } from '@documenso/ui/primitives/use-toast';
|
|
||||||
|
|
||||||
import { createCheckout } from './create-checkout.action';
|
|
||||||
|
|
||||||
type Interval = keyof PriceIntervals;
|
|
||||||
|
|
||||||
const INTERVALS: Interval[] = ['day', 'week', 'month', 'year'];
|
|
||||||
|
|
||||||
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
||||||
const isInterval = (value: unknown): value is Interval => INTERVALS.includes(value as Interval);
|
|
||||||
|
|
||||||
const FRIENDLY_INTERVALS: Record<Interval, string> = {
|
|
||||||
day: 'Daily',
|
|
||||||
week: 'Weekly',
|
|
||||||
month: 'Monthly',
|
|
||||||
year: 'Yearly',
|
|
||||||
};
|
|
||||||
|
|
||||||
const MotionCard = motion(Card);
|
|
||||||
|
|
||||||
export type BillingPlansProps = {
|
|
||||||
prices: PriceIntervals;
|
|
||||||
};
|
|
||||||
|
|
||||||
export const BillingPlans = ({ prices }: BillingPlansProps) => {
|
|
||||||
const { toast } = useToast();
|
|
||||||
|
|
||||||
const isMounted = useIsMounted();
|
|
||||||
|
|
||||||
const [interval, setInterval] = useState<Interval>('month');
|
|
||||||
const [isFetchingCheckoutSession, setIsFetchingCheckoutSession] = useState(false);
|
|
||||||
|
|
||||||
const onSubscribeClick = async (priceId: string) => {
|
|
||||||
try {
|
|
||||||
setIsFetchingCheckoutSession(true);
|
|
||||||
|
|
||||||
const url = await createCheckout({ priceId });
|
|
||||||
|
|
||||||
if (!url) {
|
|
||||||
throw new Error('Unable to create session');
|
|
||||||
}
|
|
||||||
|
|
||||||
window.open(url);
|
|
||||||
} catch (_err) {
|
|
||||||
toast({
|
|
||||||
title: 'Something went wrong',
|
|
||||||
description: 'An error occurred while trying to create a checkout session.',
|
|
||||||
variant: 'destructive',
|
|
||||||
});
|
|
||||||
} finally {
|
|
||||||
setIsFetchingCheckoutSession(false);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div>
|
|
||||||
<Tabs value={interval} onValueChange={(value) => isInterval(value) && setInterval(value)}>
|
|
||||||
<TabsList>
|
|
||||||
{INTERVALS.map(
|
|
||||||
(interval) =>
|
|
||||||
prices[interval].length > 0 && (
|
|
||||||
<TabsTrigger key={interval} className="min-w-[150px]" value={interval}>
|
|
||||||
{FRIENDLY_INTERVALS[interval]}
|
|
||||||
</TabsTrigger>
|
|
||||||
),
|
|
||||||
)}
|
|
||||||
</TabsList>
|
|
||||||
</Tabs>
|
|
||||||
|
|
||||||
<div className="mt-8 grid gap-8 lg:grid-cols-2 2xl:grid-cols-3">
|
|
||||||
<AnimatePresence mode="wait">
|
|
||||||
{prices[interval].map((price) => (
|
|
||||||
<MotionCard
|
|
||||||
key={price.id}
|
|
||||||
initial={{ opacity: isMounted ? 0 : 1, y: isMounted ? 20 : 0 }}
|
|
||||||
animate={{ opacity: 1, y: 0, transition: { duration: 0.3 } }}
|
|
||||||
exit={{ opacity: 0, transition: { duration: 0.3 } }}
|
|
||||||
>
|
|
||||||
<CardContent className="flex h-full flex-col p-6">
|
|
||||||
<CardTitle>{price.product.name}</CardTitle>
|
|
||||||
|
|
||||||
<div className="text-muted-foreground mt-2 text-lg font-medium">
|
|
||||||
${toHumanPrice(price.unit_amount ?? 0)} {price.currency.toUpperCase()}{' '}
|
|
||||||
<span className="text-xs">per {interval}</span>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className="text-muted-foreground mt-1.5 text-sm">
|
|
||||||
{price.product.description}
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{price.product.features && price.product.features.length > 0 && (
|
|
||||||
<div className="text-muted-foreground mt-4">
|
|
||||||
<div className="text-sm font-medium">Includes:</div>
|
|
||||||
|
|
||||||
<ul className="mt-1 divide-y text-sm">
|
|
||||||
{price.product.features.map((feature, index) => (
|
|
||||||
<li key={index} className="py-2">
|
|
||||||
{feature.name}
|
|
||||||
</li>
|
|
||||||
))}
|
|
||||||
</ul>
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
|
|
||||||
<div className="flex-1" />
|
|
||||||
|
|
||||||
<Button
|
|
||||||
className="mt-4"
|
|
||||||
loading={isFetchingCheckoutSession}
|
|
||||||
onClick={() => void onSubscribeClick(price.id)}
|
|
||||||
>
|
|
||||||
Subscribe
|
|
||||||
</Button>
|
|
||||||
</CardContent>
|
|
||||||
</MotionCard>
|
|
||||||
))}
|
|
||||||
</AnimatePresence>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
};
|
|
||||||
@ -1,55 +0,0 @@
|
|||||||
'use client';
|
|
||||||
|
|
||||||
import { useState } from 'react';
|
|
||||||
|
|
||||||
import { Button } from '@documenso/ui/primitives/button';
|
|
||||||
import { useToast } from '@documenso/ui/primitives/use-toast';
|
|
||||||
|
|
||||||
import { createBillingPortal } from './create-billing-portal.action';
|
|
||||||
|
|
||||||
export const BillingPortalButton = () => {
|
|
||||||
const { toast } = useToast();
|
|
||||||
|
|
||||||
const [isFetchingPortalUrl, setIsFetchingPortalUrl] = useState(false);
|
|
||||||
|
|
||||||
const handleFetchPortalUrl = async () => {
|
|
||||||
if (isFetchingPortalUrl) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
setIsFetchingPortalUrl(true);
|
|
||||||
|
|
||||||
try {
|
|
||||||
const sessionUrl = await createBillingPortal();
|
|
||||||
|
|
||||||
if (!sessionUrl) {
|
|
||||||
throw new Error('NO_SESSION');
|
|
||||||
}
|
|
||||||
|
|
||||||
window.open(sessionUrl, '_blank');
|
|
||||||
} catch (e) {
|
|
||||||
let description =
|
|
||||||
'We are unable to proceed to the billing portal at this time. Please try again, or contact support.';
|
|
||||||
|
|
||||||
if (e.message === 'CUSTOMER_NOT_FOUND') {
|
|
||||||
description =
|
|
||||||
'You do not currently have a customer record, this should not happen. Please contact support for assistance.';
|
|
||||||
}
|
|
||||||
|
|
||||||
toast({
|
|
||||||
title: 'Something went wrong',
|
|
||||||
description,
|
|
||||||
variant: 'destructive',
|
|
||||||
duration: 10000,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
setIsFetchingPortalUrl(false);
|
|
||||||
};
|
|
||||||
|
|
||||||
return (
|
|
||||||
<Button onClick={async () => handleFetchPortalUrl()} loading={isFetchingPortalUrl}>
|
|
||||||
Manage Subscription
|
|
||||||
</Button>
|
|
||||||
);
|
|
||||||
};
|
|
||||||
@ -1,48 +0,0 @@
|
|||||||
'use server';
|
|
||||||
|
|
||||||
import {
|
|
||||||
getStripeCustomerByEmail,
|
|
||||||
getStripeCustomerById,
|
|
||||||
} from '@documenso/ee/server-only/stripe/get-customer';
|
|
||||||
import { getPortalSession } from '@documenso/ee/server-only/stripe/get-portal-session';
|
|
||||||
import { getRequiredServerComponentSession } from '@documenso/lib/next-auth/get-server-session';
|
|
||||||
import { Stripe, stripe } from '@documenso/lib/server-only/stripe';
|
|
||||||
import { getSubscriptionByUserId } from '@documenso/lib/server-only/subscription/get-subscription-by-user-id';
|
|
||||||
|
|
||||||
export const createBillingPortal = async () => {
|
|
||||||
const { user } = await getRequiredServerComponentSession();
|
|
||||||
|
|
||||||
const existingSubscription = await getSubscriptionByUserId({ userId: user.id });
|
|
||||||
|
|
||||||
let stripeCustomer: Stripe.Customer | null = null;
|
|
||||||
|
|
||||||
// Find the Stripe customer for the current user subscription.
|
|
||||||
if (existingSubscription) {
|
|
||||||
stripeCustomer = await getStripeCustomerById(existingSubscription.customerId);
|
|
||||||
|
|
||||||
if (!stripeCustomer) {
|
|
||||||
throw new Error('Missing Stripe customer for subscription');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fallback to check if a Stripe customer already exists for the current user email.
|
|
||||||
if (!stripeCustomer) {
|
|
||||||
stripeCustomer = await getStripeCustomerByEmail(user.email);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create a Stripe customer if it does not exist for the current user.
|
|
||||||
if (!stripeCustomer) {
|
|
||||||
stripeCustomer = await stripe.customers.create({
|
|
||||||
name: user.name ?? undefined,
|
|
||||||
email: user.email,
|
|
||||||
metadata: {
|
|
||||||
userId: user.id,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return getPortalSession({
|
|
||||||
customerId: stripeCustomer.id,
|
|
||||||
returnUrl: `${process.env.NEXT_PUBLIC_WEBAPP_URL}/settings/billing`,
|
|
||||||
});
|
|
||||||
};
|
|
||||||
@ -1,59 +0,0 @@
|
|||||||
'use server';
|
|
||||||
|
|
||||||
import { getCheckoutSession } from '@documenso/ee/server-only/stripe/get-checkout-session';
|
|
||||||
import {
|
|
||||||
getStripeCustomerByEmail,
|
|
||||||
getStripeCustomerById,
|
|
||||||
} from '@documenso/ee/server-only/stripe/get-customer';
|
|
||||||
import { getPortalSession } from '@documenso/ee/server-only/stripe/get-portal-session';
|
|
||||||
import { getRequiredServerComponentSession } from '@documenso/lib/next-auth/get-server-session';
|
|
||||||
import { Stripe, stripe } from '@documenso/lib/server-only/stripe';
|
|
||||||
import { getSubscriptionByUserId } from '@documenso/lib/server-only/subscription/get-subscription-by-user-id';
|
|
||||||
|
|
||||||
export type CreateCheckoutOptions = {
|
|
||||||
priceId: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
export const createCheckout = async ({ priceId }: CreateCheckoutOptions) => {
|
|
||||||
const { user } = await getRequiredServerComponentSession();
|
|
||||||
|
|
||||||
const existingSubscription = await getSubscriptionByUserId({ userId: user.id });
|
|
||||||
|
|
||||||
let stripeCustomer: Stripe.Customer | null = null;
|
|
||||||
|
|
||||||
// Find the Stripe customer for the current user subscription.
|
|
||||||
if (existingSubscription) {
|
|
||||||
stripeCustomer = await getStripeCustomerById(existingSubscription.customerId);
|
|
||||||
|
|
||||||
if (!stripeCustomer) {
|
|
||||||
throw new Error('Missing Stripe customer for subscription');
|
|
||||||
}
|
|
||||||
|
|
||||||
return getPortalSession({
|
|
||||||
customerId: stripeCustomer.id,
|
|
||||||
returnUrl: `${process.env.NEXT_PUBLIC_WEBAPP_URL}/settings/billing`,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fallback to check if a Stripe customer already exists for the current user email.
|
|
||||||
if (!stripeCustomer) {
|
|
||||||
stripeCustomer = await getStripeCustomerByEmail(user.email);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create a Stripe customer if it does not exist for the current user.
|
|
||||||
if (!stripeCustomer) {
|
|
||||||
stripeCustomer = await stripe.customers.create({
|
|
||||||
name: user.name ?? undefined,
|
|
||||||
email: user.email,
|
|
||||||
metadata: {
|
|
||||||
userId: user.id,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return getCheckoutSession({
|
|
||||||
customerId: stripeCustomer.id,
|
|
||||||
priceId,
|
|
||||||
returnUrl: `${process.env.NEXT_PUBLIC_WEBAPP_URL}/settings/billing`,
|
|
||||||
});
|
|
||||||
};
|
|
||||||
@ -1,19 +1,16 @@
|
|||||||
|
import Link from 'next/link';
|
||||||
import { redirect } from 'next/navigation';
|
import { redirect } from 'next/navigation';
|
||||||
|
|
||||||
import { match } from 'ts-pattern';
|
import { createCustomer } from '@documenso/ee/server-only/stripe/create-customer';
|
||||||
|
import { getPortalSession } from '@documenso/ee/server-only/stripe/get-portal-session';
|
||||||
import { getPricesByInterval } from '@documenso/ee/server-only/stripe/get-prices-by-interval';
|
|
||||||
import { getProductByPriceId } from '@documenso/ee/server-only/stripe/get-product-by-price-id';
|
|
||||||
import { getRequiredServerComponentSession } from '@documenso/lib/next-auth/get-server-session';
|
import { getRequiredServerComponentSession } from '@documenso/lib/next-auth/get-server-session';
|
||||||
import { getServerComponentFlag } from '@documenso/lib/server-only/feature-flags/get-server-component-feature-flag';
|
import { getServerComponentFlag } from '@documenso/lib/server-only/feature-flags/get-server-component-feature-flag';
|
||||||
import { Stripe } from '@documenso/lib/server-only/stripe';
|
|
||||||
import { getSubscriptionByUserId } from '@documenso/lib/server-only/subscription/get-subscription-by-user-id';
|
import { getSubscriptionByUserId } from '@documenso/lib/server-only/subscription/get-subscription-by-user-id';
|
||||||
|
import { SubscriptionStatus } from '@documenso/prisma/client';
|
||||||
|
import { Button } from '@documenso/ui/primitives/button';
|
||||||
|
|
||||||
import { LocaleDate } from '~/components/formatter/locale-date';
|
import { LocaleDate } from '~/components/formatter/locale-date';
|
||||||
|
|
||||||
import { BillingPlans } from './billing-plans';
|
|
||||||
import { BillingPortalButton } from './billing-portal-button';
|
|
||||||
|
|
||||||
export default async function BillingSettingsPage() {
|
export default async function BillingSettingsPage() {
|
||||||
const { user } = await getRequiredServerComponentSession();
|
const { user } = await getRequiredServerComponentSession();
|
||||||
|
|
||||||
@ -24,73 +21,57 @@ export default async function BillingSettingsPage() {
|
|||||||
redirect('/settings/profile');
|
redirect('/settings/profile');
|
||||||
}
|
}
|
||||||
|
|
||||||
const [subscription, prices] = await Promise.all([
|
const subscription = await getSubscriptionByUserId({ userId: user.id }).then(async (sub) => {
|
||||||
getSubscriptionByUserId({ userId: user.id }),
|
if (sub) {
|
||||||
getPricesByInterval(),
|
return sub;
|
||||||
]);
|
|
||||||
|
|
||||||
let subscriptionProduct: Stripe.Product | null = null;
|
|
||||||
|
|
||||||
if (subscription?.priceId) {
|
|
||||||
subscriptionProduct = await getProductByPriceId({ priceId: subscription.priceId }).catch(
|
|
||||||
() => null,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const isMissingOrInactiveOrFreePlan = !subscription || subscription.status === 'INACTIVE';
|
// If we don't have a customer record, create one as well as an empty subscription.
|
||||||
|
return createCustomer({ user });
|
||||||
|
});
|
||||||
|
|
||||||
|
let billingPortalUrl = '';
|
||||||
|
|
||||||
|
if (subscription.customerId) {
|
||||||
|
billingPortalUrl = await getPortalSession({
|
||||||
|
customerId: subscription.customerId,
|
||||||
|
returnUrl: `${process.env.NEXT_PUBLIC_WEBAPP_URL}/settings/billing`,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<h3 className="text-lg font-medium">Billing</h3>
|
<h3 className="text-lg font-medium">Billing</h3>
|
||||||
|
|
||||||
<div className="text-muted-foreground mt-2 text-sm">
|
<p className="text-muted-foreground mt-2 text-sm">
|
||||||
{isMissingOrInactiveOrFreePlan && (
|
Your subscription is{' '}
|
||||||
<p>
|
{subscription.status !== SubscriptionStatus.INACTIVE ? 'active' : 'inactive'}.
|
||||||
You are currently on the <span className="font-semibold">Free Plan</span>.
|
{subscription?.periodEnd && (
|
||||||
</p>
|
<>
|
||||||
)}
|
|
||||||
|
|
||||||
{!isMissingOrInactiveOrFreePlan &&
|
|
||||||
match(subscription.status)
|
|
||||||
.with('ACTIVE', () => (
|
|
||||||
<p>
|
|
||||||
{subscriptionProduct ? (
|
|
||||||
<span>
|
|
||||||
You are currently subscribed to{' '}
|
|
||||||
<span className="font-semibold">{subscriptionProduct.name}</span>
|
|
||||||
</span>
|
|
||||||
) : (
|
|
||||||
<span>You currently have an active plan</span>
|
|
||||||
)}
|
|
||||||
|
|
||||||
{subscription.periodEnd && (
|
|
||||||
<span>
|
|
||||||
{' '}
|
{' '}
|
||||||
which is set to{' '}
|
Your next payment is due on{' '}
|
||||||
{subscription.cancelAtPeriodEnd ? (
|
<span className="font-semibold">
|
||||||
<span>
|
<LocaleDate date={subscription.periodEnd} />
|
||||||
end on{' '}
|
|
||||||
<LocaleDate className="font-semibold" date={subscription.periodEnd} />.
|
|
||||||
</span>
|
|
||||||
) : (
|
|
||||||
<span>
|
|
||||||
automatically renew on{' '}
|
|
||||||
<LocaleDate className="font-semibold" date={subscription.periodEnd} />.
|
|
||||||
</span>
|
|
||||||
)}
|
|
||||||
</span>
|
</span>
|
||||||
|
.
|
||||||
|
</>
|
||||||
)}
|
)}
|
||||||
</p>
|
</p>
|
||||||
))
|
|
||||||
.with('PAST_DUE', () => (
|
|
||||||
<p>Your current plan is past due. Please update your payment information.</p>
|
|
||||||
))
|
|
||||||
.otherwise(() => null)}
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<hr className="my-4" />
|
<hr className="my-4" />
|
||||||
|
|
||||||
{isMissingOrInactiveOrFreePlan ? <BillingPlans prices={prices} /> : <BillingPortalButton />}
|
{billingPortalUrl && (
|
||||||
|
<Button asChild>
|
||||||
|
<Link href={billingPortalUrl}>Manage Subscription</Link>
|
||||||
|
</Button>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{!billingPortalUrl && (
|
||||||
|
<p className="text-muted-foreground max-w-[60ch] text-base">
|
||||||
|
You do not currently have a customer record, this should not happen. Please contact
|
||||||
|
support for assistance.
|
||||||
|
</p>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,43 +0,0 @@
|
|||||||
import fs from 'fs/promises';
|
|
||||||
|
|
||||||
import { loadFileIntoPinecone } from '@documenso/lib/server-only/pinecone';
|
|
||||||
import { getFile } from '@documenso/lib/universal/upload/get-file';
|
|
||||||
import { DocumentDataType } from '@documenso/prisma/client';
|
|
||||||
import { Card, CardContent } from '@documenso/ui/primitives/card';
|
|
||||||
|
|
||||||
import { Chat } from './chat';
|
|
||||||
|
|
||||||
type ChatPDFProps = {
|
|
||||||
id: string;
|
|
||||||
type: DocumentDataType;
|
|
||||||
data: string;
|
|
||||||
initialData: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
export async function ChatPDF({ documentData }: { documentData: ChatPDFProps }) {
|
|
||||||
const docData = await getFile(documentData);
|
|
||||||
const fileName = `${documentData.id}}.pdf`;
|
|
||||||
|
|
||||||
try {
|
|
||||||
await fs.access(fileName, fs.constants.F_OK);
|
|
||||||
} catch (err) {
|
|
||||||
await fs.writeFile(fileName, docData);
|
|
||||||
}
|
|
||||||
await loadFileIntoPinecone(fileName);
|
|
||||||
|
|
||||||
return (
|
|
||||||
<Card className="my-8" gradient={true} degrees={200}>
|
|
||||||
<CardContent className="mt-8 flex flex-col">
|
|
||||||
<h2 className="text-foreground text-2xl font-semibold">Chat with the PDF</h2>
|
|
||||||
<p className="text-muted-foreground mt-2 text-sm">Ask any questions regarding the PDF</p>
|
|
||||||
<hr className="border-border mb-4 mt-4" />
|
|
||||||
<Chat />
|
|
||||||
<hr className="border-border mb-4 mt-4" />
|
|
||||||
<p className="text-muted-foreground text-sm italic">
|
|
||||||
Disclaimer: Never trust AI 100%. Always double check the documents yourself. Documenso is
|
|
||||||
not liable for any issue arising from you relying 100% on the AI.
|
|
||||||
</p>
|
|
||||||
</CardContent>
|
|
||||||
</Card>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@ -1,56 +0,0 @@
|
|||||||
'use client';
|
|
||||||
|
|
||||||
import { useChat } from 'ai/react';
|
|
||||||
|
|
||||||
import { cn } from '@documenso/ui/lib/utils';
|
|
||||||
import { Button } from '@documenso/ui/primitives/button';
|
|
||||||
import { Input } from '@documenso/ui/primitives/input';
|
|
||||||
|
|
||||||
type Props = {};
|
|
||||||
|
|
||||||
export function Chat({}: Props) {
|
|
||||||
const { input, handleInputChange, handleSubmit, messages } = useChat({
|
|
||||||
api: '/api/chat',
|
|
||||||
});
|
|
||||||
|
|
||||||
// continue https://youtu.be/bZFedu-0emE?si=2JGSJfSQ38aXSlp2&t=10941
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div>
|
|
||||||
<div className="flex flex-col gap-8">
|
|
||||||
<ul>
|
|
||||||
{messages.map((message, index) => (
|
|
||||||
<li
|
|
||||||
className={cn(
|
|
||||||
'flex',
|
|
||||||
message.role === 'user'
|
|
||||||
? 'mb-6 ml-10 mt-6 flex justify-end'
|
|
||||||
: 'mr-10 justify-start',
|
|
||||||
)}
|
|
||||||
key={index}
|
|
||||||
>
|
|
||||||
<span
|
|
||||||
className={
|
|
||||||
message.role === 'user'
|
|
||||||
? 'bg-background text-foreground group relative rounded-lg border-2 p-4 backdrop-blur-[2px]'
|
|
||||||
: 'bg-primary text-primary-foreground rounded-lg p-4 backdrop-blur-[2px]'
|
|
||||||
}
|
|
||||||
>
|
|
||||||
{message.content}
|
|
||||||
</span>
|
|
||||||
</li>
|
|
||||||
))}
|
|
||||||
</ul>
|
|
||||||
</div>
|
|
||||||
<form className="mb-2 mt-8 flex" onSubmit={handleSubmit}>
|
|
||||||
<Input
|
|
||||||
value={input}
|
|
||||||
className="mr-6 w-1/2"
|
|
||||||
onChange={handleInputChange}
|
|
||||||
placeholder="Ask away..."
|
|
||||||
/>
|
|
||||||
<Button type="submit">Send</Button>
|
|
||||||
</form>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@ -87,7 +87,7 @@ export const SigningForm = ({ document, recipient, fields }: SigningFormProps) =
|
|||||||
Please review the document before signing.
|
Please review the document before signing.
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<hr className="border-border mb-8 mt-4 h-8 w-full" />
|
<hr className="border-border mb-8 mt-4" />
|
||||||
|
|
||||||
<div className="-mx-2 flex flex-1 flex-col gap-4 overflow-y-auto px-2">
|
<div className="-mx-2 flex flex-1 flex-col gap-4 overflow-y-auto px-2">
|
||||||
<div className="flex flex-1 flex-col gap-y-4">
|
<div className="flex flex-1 flex-col gap-y-4">
|
||||||
@ -99,7 +99,7 @@ export const SigningForm = ({ document, recipient, fields }: SigningFormProps) =
|
|||||||
id="full-name"
|
id="full-name"
|
||||||
className="bg-background mt-2"
|
className="bg-background mt-2"
|
||||||
value={fullName}
|
value={fullName}
|
||||||
onChange={(e) => setFullName(e.target.value.trimStart())}
|
onChange={(e) => setFullName(e.target.value)}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|||||||
@ -125,7 +125,7 @@ export const NameField = ({ field, recipient }: NameFieldProps) => {
|
|||||||
type="text"
|
type="text"
|
||||||
className="mt-2"
|
className="mt-2"
|
||||||
value={localFullName}
|
value={localFullName}
|
||||||
onChange={(e) => setLocalFullName(e.target.value.trimStart())}
|
onChange={(e) => setLocalFullName(e.target.value)}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|||||||
@ -14,7 +14,6 @@ import { Card, CardContent } from '@documenso/ui/primitives/card';
|
|||||||
import { ElementVisible } from '@documenso/ui/primitives/element-visible';
|
import { ElementVisible } from '@documenso/ui/primitives/element-visible';
|
||||||
import { LazyPDFViewer } from '@documenso/ui/primitives/lazy-pdf-viewer';
|
import { LazyPDFViewer } from '@documenso/ui/primitives/lazy-pdf-viewer';
|
||||||
|
|
||||||
import { ChatPDF } from './chat-pdf';
|
|
||||||
import { DateField } from './date-field';
|
import { DateField } from './date-field';
|
||||||
import { EmailField } from './email-field';
|
import { EmailField } from './email-field';
|
||||||
import { SigningForm } from './form';
|
import { SigningForm } from './form';
|
||||||
@ -107,7 +106,6 @@ export default async function SigningPage({ params: { token } }: SigningPageProp
|
|||||||
.otherwise(() => null),
|
.otherwise(() => null),
|
||||||
)}
|
)}
|
||||||
</ElementVisible>
|
</ElementVisible>
|
||||||
<ChatPDF documentData={documentData} />
|
|
||||||
</div>
|
</div>
|
||||||
</SigningProvider>
|
</SigningProvider>
|
||||||
);
|
);
|
||||||
|
|||||||
@ -20,7 +20,7 @@ import { useToast } from '@documenso/ui/primitives/use-toast';
|
|||||||
import { FormErrorMessage } from '../form/form-error-message';
|
import { FormErrorMessage } from '../form/form-error-message';
|
||||||
|
|
||||||
export const ZProfileFormSchema = z.object({
|
export const ZProfileFormSchema = z.object({
|
||||||
name: z.string().trim().min(1, { message: 'Please enter a valid name.' }),
|
name: z.string().min(1),
|
||||||
signature: z.string().min(1, 'Signature Pad cannot be empty'),
|
signature: z.string().min(1, 'Signature Pad cannot be empty'),
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -117,8 +117,7 @@ export const ProfileForm = ({ className, user }: ProfileFormProps) => {
|
|||||||
name="signature"
|
name="signature"
|
||||||
render={({ field: { onChange } }) => (
|
render={({ field: { onChange } }) => (
|
||||||
<SignaturePad
|
<SignaturePad
|
||||||
className="h-44 w-full"
|
className="h-44 w-full rounded-lg border bg-white backdrop-blur-sm dark:border-[#e2d7c5] dark:bg-[#fcf8ee]"
|
||||||
containerClassName="rounded-lg border bg-background"
|
|
||||||
defaultValue={user.signature ?? undefined}
|
defaultValue={user.signature ?? undefined}
|
||||||
onChange={(v) => onChange(v ?? '')}
|
onChange={(v) => onChange(v ?? '')}
|
||||||
/>
|
/>
|
||||||
|
|||||||
@ -19,7 +19,7 @@ import { SignaturePad } from '@documenso/ui/primitives/signature-pad';
|
|||||||
import { useToast } from '@documenso/ui/primitives/use-toast';
|
import { useToast } from '@documenso/ui/primitives/use-toast';
|
||||||
|
|
||||||
export const ZSignUpFormSchema = z.object({
|
export const ZSignUpFormSchema = z.object({
|
||||||
name: z.string().trim().min(1, { message: 'Please enter a valid name.' }),
|
name: z.string().min(1),
|
||||||
email: z.string().email().min(1),
|
email: z.string().email().min(1),
|
||||||
password: z.string().min(6).max(72),
|
password: z.string().min(6).max(72),
|
||||||
signature: z.string().min(1, { message: 'We need your signature to sign documents' }),
|
signature: z.string().min(1, { message: 'We need your signature to sign documents' }),
|
||||||
@ -147,8 +147,7 @@ export const SignUpForm = ({ className }: SignUpFormProps) => {
|
|||||||
name="signature"
|
name="signature"
|
||||||
render={({ field: { onChange } }) => (
|
render={({ field: { onChange } }) => (
|
||||||
<SignaturePad
|
<SignaturePad
|
||||||
className="h-36 w-full"
|
className="mt-2 h-36 w-full rounded-lg border bg-white dark:border-[#e2d7c5] dark:bg-[#fcf8ee]"
|
||||||
containerClassName="mt-2 rounded-lg border bg-background"
|
|
||||||
onChange={(v) => onChange(v ?? '')}
|
onChange={(v) => onChange(v ?? '')}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
|
|||||||
@ -1,54 +0,0 @@
|
|||||||
import { Message, OpenAIStream, StreamingTextResponse } from 'ai';
|
|
||||||
import { Configuration, OpenAIApi } from 'openai-edge';
|
|
||||||
|
|
||||||
import { getContext } from '@documenso/lib/server-only/context';
|
|
||||||
|
|
||||||
export const runtime = 'edge';
|
|
||||||
|
|
||||||
const config = new Configuration({
|
|
||||||
apiKey: process.env.OPENAI_API_KEY!,
|
|
||||||
});
|
|
||||||
|
|
||||||
const openai = new OpenAIApi(config);
|
|
||||||
|
|
||||||
export default async function handler(request: Request) {
|
|
||||||
// console.log(request.method);
|
|
||||||
// request.json().then((data) => console.log(data));
|
|
||||||
// return Response.json({ message: 'world' });
|
|
||||||
|
|
||||||
try {
|
|
||||||
const data = await request.json();
|
|
||||||
const lastMessage = data.messages[data.messages.length - 1];
|
|
||||||
const context = await getContext(lastMessage.content);
|
|
||||||
console.log('context', context);
|
|
||||||
const prompt = {
|
|
||||||
role: 'system',
|
|
||||||
content: `AI assistant is a brand new, powerful, human-like artificial intelligence.
|
|
||||||
The traits of AI include expert knowledge, helpfulness, cleverness, and articulateness.
|
|
||||||
AI is a well-behaved and well-mannered individual.
|
|
||||||
AI is always friendly, kind, and inspiring, and he is eager to provide vivid and thoughtful responses to the user.
|
|
||||||
AI has the sum of all knowledge in their brain, and is able to accurately answer nearly any question about any topic in conversation.
|
|
||||||
AI assistant is a big fan of Pinecone and Vercel.
|
|
||||||
START CONTEXT BLOCK
|
|
||||||
${context}
|
|
||||||
END OF CONTEXT BLOCK
|
|
||||||
AI assistant will take into account any CONTEXT BLOCK that is provided in a conversation.
|
|
||||||
If the context does not provide the answer to question, the AI assistant will say, "I'm sorry, but I don't know the answer to that question".
|
|
||||||
AI assistant will not apologize for previous responses, but instead will indicated new information was gained.
|
|
||||||
AI assistant will not invent anything that is not drawn directly from the context.
|
|
||||||
`,
|
|
||||||
};
|
|
||||||
const response = await openai.createChatCompletion({
|
|
||||||
model: 'gpt-3.5-turbo',
|
|
||||||
messages: [prompt, ...data.messages.filter((message: Message) => message.role === 'user')],
|
|
||||||
stream: true,
|
|
||||||
});
|
|
||||||
|
|
||||||
const stream = OpenAIStream(response);
|
|
||||||
|
|
||||||
return new StreamingTextResponse(stream);
|
|
||||||
} catch (error) {
|
|
||||||
console.error('There was an error getting embeddings: ', error);
|
|
||||||
throw new Error('There was an error getting embeddings');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -41,7 +41,7 @@ export default async function handler(
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
if (user && user.Subscription) {
|
if (user && user.Subscription.length > 0) {
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
redirectUrl: `${process.env.NEXT_PUBLIC_WEBAPP_URL}/login`,
|
redirectUrl: `${process.env.NEXT_PUBLIC_WEBAPP_URL}/login`,
|
||||||
});
|
});
|
||||||
|
|||||||
@ -1,3 +0,0 @@
|
|||||||
import { limitsHandler } from '@documenso/ee/server-only/limits/handler';
|
|
||||||
|
|
||||||
export default limitsHandler;
|
|
||||||
@ -1,7 +1,197 @@
|
|||||||
import { stripeWebhookHandler } from '@documenso/ee/server-only/stripe/webhook/handler';
|
import { NextApiRequest, NextApiResponse } from 'next';
|
||||||
|
|
||||||
|
import { randomBytes } from 'crypto';
|
||||||
|
import { readFileSync } from 'fs';
|
||||||
|
import { buffer } from 'micro';
|
||||||
|
|
||||||
|
import { insertImageInPDF } from '@documenso/lib/server-only/pdf/insert-image-in-pdf';
|
||||||
|
import { insertTextInPDF } from '@documenso/lib/server-only/pdf/insert-text-in-pdf';
|
||||||
|
import { redis } from '@documenso/lib/server-only/redis';
|
||||||
|
import { Stripe, stripe } from '@documenso/lib/server-only/stripe';
|
||||||
|
import { prisma } from '@documenso/prisma';
|
||||||
|
import {
|
||||||
|
DocumentDataType,
|
||||||
|
DocumentStatus,
|
||||||
|
FieldType,
|
||||||
|
ReadStatus,
|
||||||
|
SendStatus,
|
||||||
|
SigningStatus,
|
||||||
|
} from '@documenso/prisma/client';
|
||||||
|
|
||||||
|
const log = (...args: unknown[]) => console.log('[stripe]', ...args);
|
||||||
|
|
||||||
export const config = {
|
export const config = {
|
||||||
api: { bodyParser: false },
|
api: { bodyParser: false },
|
||||||
};
|
};
|
||||||
|
|
||||||
export default stripeWebhookHandler;
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
|
// if (!process.env.NEXT_PUBLIC_ALLOW_SUBSCRIPTIONS) {
|
||||||
|
// return res.status(500).json({
|
||||||
|
// success: false,
|
||||||
|
// message: 'Subscriptions are not enabled',
|
||||||
|
// });
|
||||||
|
// }
|
||||||
|
|
||||||
|
const sig =
|
||||||
|
typeof req.headers['stripe-signature'] === 'string' ? req.headers['stripe-signature'] : '';
|
||||||
|
|
||||||
|
if (!sig) {
|
||||||
|
return res.status(400).json({
|
||||||
|
success: false,
|
||||||
|
message: 'No signature found in request',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
log('constructing body...');
|
||||||
|
const body = await buffer(req);
|
||||||
|
log('constructed body');
|
||||||
|
|
||||||
|
const event = stripe.webhooks.constructEvent(
|
||||||
|
body,
|
||||||
|
sig,
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion, turbo/no-undeclared-env-vars
|
||||||
|
process.env.NEXT_PRIVATE_STRIPE_WEBHOOK_SECRET!,
|
||||||
|
);
|
||||||
|
log('event-type:', event.type);
|
||||||
|
|
||||||
|
if (event.type === 'checkout.session.completed') {
|
||||||
|
// This is required since we don't want to create a guard for every event type
|
||||||
|
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
||||||
|
const session = event.data.object as Stripe.Checkout.Session;
|
||||||
|
|
||||||
|
if (session.metadata?.source === 'landing') {
|
||||||
|
const user = await prisma.user.findFirst({
|
||||||
|
where: {
|
||||||
|
id: Number(session.client_reference_id),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!user) {
|
||||||
|
return res.status(500).json({
|
||||||
|
success: false,
|
||||||
|
message: 'User not found',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const signatureText = session.metadata?.signatureText || user.name;
|
||||||
|
let signatureDataUrl = '';
|
||||||
|
|
||||||
|
if (session.metadata?.signatureDataUrl) {
|
||||||
|
const result = await redis.get<string>(`signature:${session.metadata.signatureDataUrl}`);
|
||||||
|
|
||||||
|
if (result) {
|
||||||
|
signatureDataUrl = result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const now = new Date();
|
||||||
|
|
||||||
|
const bytes64 = readFileSync('./public/documenso-supporter-pledge.pdf').toString('base64');
|
||||||
|
|
||||||
|
const { id: documentDataId } = await prisma.documentData.create({
|
||||||
|
data: {
|
||||||
|
type: DocumentDataType.BYTES_64,
|
||||||
|
data: bytes64,
|
||||||
|
initialData: bytes64,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const document = await prisma.document.create({
|
||||||
|
data: {
|
||||||
|
title: 'Documenso Supporter Pledge.pdf',
|
||||||
|
status: DocumentStatus.COMPLETED,
|
||||||
|
userId: user.id,
|
||||||
|
documentDataId,
|
||||||
|
},
|
||||||
|
include: {
|
||||||
|
documentData: true,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const { documentData } = document;
|
||||||
|
|
||||||
|
if (!documentData) {
|
||||||
|
throw new Error(`Document ${document.id} has no document data`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const recipient = await prisma.recipient.create({
|
||||||
|
data: {
|
||||||
|
name: user.name ?? '',
|
||||||
|
email: user.email,
|
||||||
|
token: randomBytes(16).toString('hex'),
|
||||||
|
signedAt: now,
|
||||||
|
readStatus: ReadStatus.OPENED,
|
||||||
|
sendStatus: SendStatus.SENT,
|
||||||
|
signingStatus: SigningStatus.SIGNED,
|
||||||
|
documentId: document.id,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const field = await prisma.field.create({
|
||||||
|
data: {
|
||||||
|
documentId: document.id,
|
||||||
|
recipientId: recipient.id,
|
||||||
|
type: FieldType.SIGNATURE,
|
||||||
|
page: 0,
|
||||||
|
positionX: 77,
|
||||||
|
positionY: 638,
|
||||||
|
inserted: false,
|
||||||
|
customText: '',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (signatureDataUrl) {
|
||||||
|
documentData.data = await insertImageInPDF(
|
||||||
|
documentData.data,
|
||||||
|
signatureDataUrl,
|
||||||
|
field.positionX.toNumber(),
|
||||||
|
field.positionY.toNumber(),
|
||||||
|
field.page,
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
documentData.data = await insertTextInPDF(
|
||||||
|
documentData.data,
|
||||||
|
signatureText ?? '',
|
||||||
|
field.positionX.toNumber(),
|
||||||
|
field.positionY.toNumber(),
|
||||||
|
field.page,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
await Promise.all([
|
||||||
|
prisma.signature.create({
|
||||||
|
data: {
|
||||||
|
fieldId: field.id,
|
||||||
|
recipientId: recipient.id,
|
||||||
|
signatureImageAsBase64: signatureDataUrl || undefined,
|
||||||
|
typedSignature: signatureDataUrl ? '' : signatureText,
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
prisma.document.update({
|
||||||
|
where: {
|
||||||
|
id: document.id,
|
||||||
|
},
|
||||||
|
data: {
|
||||||
|
documentData: {
|
||||||
|
update: {
|
||||||
|
data: documentData.data,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
message: 'Webhook received',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
log('Unhandled webhook event', event.type);
|
||||||
|
|
||||||
|
return res.status(400).json({
|
||||||
|
success: false,
|
||||||
|
message: 'Unhandled webhook event',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|||||||
@ -6,3 +6,7 @@ export default trpcNext.createNextApiHandler({
|
|||||||
router: appRouter,
|
router: appRouter,
|
||||||
createContext: async ({ req, res }) => createTrpcContext({ req, res }),
|
createContext: async ({ req, res }) => createTrpcContext({ req, res }),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// export default async function handler(_req: NextApiRequest, res: NextApiResponse) {
|
||||||
|
// res.json({ hello: 'world' });
|
||||||
|
// }
|
||||||
|
|||||||
9432
package-lock.json
generated
9432
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
13
package.json
13
package.json
@ -14,13 +14,9 @@
|
|||||||
"dx": "npm i && npm run dx:up && npm run prisma:migrate-dev",
|
"dx": "npm i && npm run dx:up && npm run prisma:migrate-dev",
|
||||||
"dx:up": "docker compose -f docker/compose-services.yml up -d",
|
"dx:up": "docker compose -f docker/compose-services.yml up -d",
|
||||||
"dx:down": "docker compose -f docker/compose-services.yml down",
|
"dx:down": "docker compose -f docker/compose-services.yml down",
|
||||||
"ci": "turbo run build test:e2e",
|
|
||||||
"prisma:generate": "npm run with:env -- npm run prisma:generate -w @documenso/prisma",
|
|
||||||
"prisma:migrate-dev": "npm run with:env -- npm run prisma:migrate-dev -w @documenso/prisma",
|
"prisma:migrate-dev": "npm run with:env -- npm run prisma:migrate-dev -w @documenso/prisma",
|
||||||
"prisma:migrate-deploy": "npm run with:env -- npm run prisma:migrate-deploy -w @documenso/prisma",
|
"prisma:migrate-deploy": "npm run with:env -- npm run prisma:migrate-deploy -w @documenso/prisma",
|
||||||
"prisma:studio": "npm run with:env -- npx prisma studio --schema packages/prisma/schema.prisma",
|
"with:env": "dotenv -e .env -e .env.local --"
|
||||||
"with:env": "dotenv -e .env -e .env.local --",
|
|
||||||
"reset:hard": "npm run clean && npm i && npm run prisma:generate"
|
|
||||||
},
|
},
|
||||||
"engines": {
|
"engines": {
|
||||||
"npm": ">=8.6.0",
|
"npm": ">=8.6.0",
|
||||||
@ -46,13 +42,6 @@
|
|||||||
"packages/*"
|
"packages/*"
|
||||||
],
|
],
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@pinecone-database/pinecone": "^1.1.1",
|
|
||||||
"@types/md5": "^2.3.4",
|
|
||||||
"ai": "^2.2.16",
|
|
||||||
"langchain": "^0.0.169",
|
|
||||||
"md5": "^2.3.0",
|
|
||||||
"openai-edge": "^1.2.2",
|
|
||||||
"pdf-parse": "^1.1.1",
|
|
||||||
"recharts": "^2.7.2"
|
"recharts": "^2.7.2"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
4
packages/app-tests/.gitignore
vendored
4
packages/app-tests/.gitignore
vendored
@ -1,4 +0,0 @@
|
|||||||
node_modules/
|
|
||||||
/test-results/
|
|
||||||
/playwright-report/
|
|
||||||
/playwright/.cache/
|
|
||||||
@ -1,55 +0,0 @@
|
|||||||
import { type Page, expect, test } from '@playwright/test';
|
|
||||||
|
|
||||||
import { deleteUser } from '@documenso/lib/server-only/user/delete-user';
|
|
||||||
|
|
||||||
test.use({ storageState: { cookies: [], origins: [] } });
|
|
||||||
|
|
||||||
/*
|
|
||||||
Using them sequentially so the 2nd test
|
|
||||||
uses the details from the 1st (registration) test
|
|
||||||
*/
|
|
||||||
test.describe.configure({ mode: 'serial' });
|
|
||||||
|
|
||||||
const username = process.env.E2E_TEST_AUTHENTICATE_USERNAME;
|
|
||||||
const email = process.env.E2E_TEST_AUTHENTICATE_USER_EMAIL;
|
|
||||||
const password = process.env.E2E_TEST_AUTHENTICATE_USER_PASSWORD;
|
|
||||||
|
|
||||||
test('user can sign up with email and password', async ({ page }: { page: Page }) => {
|
|
||||||
await page.goto('/signup');
|
|
||||||
await page.getByLabel('Name').fill(username);
|
|
||||||
await page.getByLabel('Email').fill(email);
|
|
||||||
await page.getByLabel('Password', { exact: true }).fill(password);
|
|
||||||
|
|
||||||
const canvas = page.locator('canvas');
|
|
||||||
const box = await canvas.boundingBox();
|
|
||||||
|
|
||||||
if (box) {
|
|
||||||
await page.mouse.move(box.x + box.width / 2, box.y + box.height / 2);
|
|
||||||
await page.mouse.down();
|
|
||||||
await page.mouse.move(box.x + box.width / 4, box.y + box.height / 4);
|
|
||||||
await page.mouse.up();
|
|
||||||
}
|
|
||||||
|
|
||||||
await page.getByRole('button', { name: 'Sign Up' }).click();
|
|
||||||
await page.waitForURL('/documents');
|
|
||||||
|
|
||||||
await expect(page).toHaveURL('/documents');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('user can login with user and password', async ({ page }: { page: Page }) => {
|
|
||||||
await page.goto('/signin');
|
|
||||||
await page.getByLabel('Email').fill(email);
|
|
||||||
await page.getByLabel('Password', { exact: true }).fill(password);
|
|
||||||
await page.getByRole('button', { name: 'Sign In' }).click();
|
|
||||||
|
|
||||||
await page.waitForURL('/documents');
|
|
||||||
await expect(page).toHaveURL('/documents');
|
|
||||||
});
|
|
||||||
|
|
||||||
test.afterAll('Teardown', async () => {
|
|
||||||
try {
|
|
||||||
await deleteUser({ email });
|
|
||||||
} catch (e) {
|
|
||||||
throw new Error(`Error deleting user: ${e}`);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
@ -1,21 +0,0 @@
|
|||||||
{
|
|
||||||
"name": "@documenso/app-tests",
|
|
||||||
"version": "1.0.0",
|
|
||||||
"license": "to-update",
|
|
||||||
"description": "",
|
|
||||||
"main": "index.js",
|
|
||||||
"scripts": {
|
|
||||||
"test:dev": "playwright test",
|
|
||||||
"test:e2e": "start-server-and-test \"(cd ../../apps/web && npm run start)\" http://localhost:3000 \"playwright test\""
|
|
||||||
},
|
|
||||||
"keywords": [],
|
|
||||||
"author": "",
|
|
||||||
"devDependencies": {
|
|
||||||
"@playwright/test": "^1.18.1",
|
|
||||||
"@types/node": "^20.8.2",
|
|
||||||
"@documenso/web": "*"
|
|
||||||
},
|
|
||||||
"dependencies": {
|
|
||||||
"start-server-and-test": "^2.0.1"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,77 +0,0 @@
|
|||||||
import { defineConfig, devices } from '@playwright/test';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Read environment variables from file.
|
|
||||||
* https://github.com/motdotla/dotenv
|
|
||||||
*/
|
|
||||||
// require('dotenv').config();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* See https://playwright.dev/docs/test-configuration.
|
|
||||||
*/
|
|
||||||
export default defineConfig({
|
|
||||||
testDir: './e2e',
|
|
||||||
/* Run tests in files in parallel */
|
|
||||||
fullyParallel: true,
|
|
||||||
/* Fail the build on CI if you accidentally left test.only in the source code. */
|
|
||||||
forbidOnly: !!process.env.CI,
|
|
||||||
/* Retry on CI only */
|
|
||||||
retries: process.env.CI ? 2 : 0,
|
|
||||||
/* Opt out of parallel tests on CI. */
|
|
||||||
workers: process.env.CI ? 1 : undefined,
|
|
||||||
/* Reporter to use. See https://playwright.dev/docs/test-reporters */
|
|
||||||
reporter: 'html',
|
|
||||||
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
|
|
||||||
use: {
|
|
||||||
/* Base URL to use in actions like `await page.goto('/')`. */
|
|
||||||
baseURL: 'http://localhost:3000',
|
|
||||||
|
|
||||||
/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
|
|
||||||
trace: 'on-first-retry',
|
|
||||||
},
|
|
||||||
|
|
||||||
/* Configure projects for major browsers */
|
|
||||||
projects: [
|
|
||||||
{
|
|
||||||
name: 'chromium',
|
|
||||||
use: { ...devices['Desktop Chrome'] },
|
|
||||||
},
|
|
||||||
|
|
||||||
// {
|
|
||||||
// name: 'firefox',
|
|
||||||
// use: { ...devices['Desktop Firefox'] },
|
|
||||||
// },
|
|
||||||
|
|
||||||
// {
|
|
||||||
// name: 'webkit',
|
|
||||||
// use: { ...devices['Desktop Safari'] },
|
|
||||||
// },
|
|
||||||
|
|
||||||
/* Test against mobile viewports. */
|
|
||||||
// {
|
|
||||||
// name: 'Mobile Chrome',
|
|
||||||
// use: { ...devices['Pixel 5'] },
|
|
||||||
// },
|
|
||||||
// {
|
|
||||||
// name: 'Mobile Safari',
|
|
||||||
// use: { ...devices['iPhone 12'] },
|
|
||||||
// },
|
|
||||||
|
|
||||||
/* Test against branded browsers. */
|
|
||||||
// {
|
|
||||||
// name: 'Microsoft Edge',
|
|
||||||
// use: { ...devices['Desktop Edge'], channel: 'msedge' },
|
|
||||||
// },
|
|
||||||
// {
|
|
||||||
// name: 'Google Chrome',
|
|
||||||
// use: { ...devices['Desktop Chrome'], channel: 'chrome' },
|
|
||||||
// },
|
|
||||||
],
|
|
||||||
|
|
||||||
/* Run your local dev server before starting the tests */
|
|
||||||
// webServer: {
|
|
||||||
// command: 'npm run start',
|
|
||||||
// url: 'http://127.0.0.1:3000',
|
|
||||||
// reuseExistingServer: !process.env.CI,
|
|
||||||
// },
|
|
||||||
});
|
|
||||||
@ -14,13 +14,6 @@
|
|||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@documenso/lib": "*",
|
"@documenso/lib": "*",
|
||||||
"@documenso/prisma": "*",
|
"@documenso/prisma": "*"
|
||||||
"luxon": "^3.4.0",
|
|
||||||
"micro": "^10.0.1",
|
|
||||||
"next": "13.5.4",
|
|
||||||
"next-auth": "4.22.3",
|
|
||||||
"react": "18.2.0",
|
|
||||||
"ts-pattern": "^5.0.5",
|
|
||||||
"zod": "^3.21.4"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,31 +0,0 @@
|
|||||||
import { APP_BASE_URL } from '@documenso/lib/constants/app';
|
|
||||||
|
|
||||||
import { FREE_PLAN_LIMITS } from './constants';
|
|
||||||
import { TLimitsResponseSchema, ZLimitsResponseSchema } from './schema';
|
|
||||||
|
|
||||||
export type GetLimitsOptions = {
|
|
||||||
headers?: Record<string, string>;
|
|
||||||
};
|
|
||||||
|
|
||||||
export const getLimits = async ({ headers }: GetLimitsOptions = {}) => {
|
|
||||||
const requestHeaders = headers ?? {};
|
|
||||||
|
|
||||||
const url = new URL(`${APP_BASE_URL}/api/limits`);
|
|
||||||
|
|
||||||
return fetch(url, {
|
|
||||||
headers: {
|
|
||||||
...requestHeaders,
|
|
||||||
},
|
|
||||||
next: {
|
|
||||||
revalidate: 60,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
.then(async (res) => res.json())
|
|
||||||
.then((res) => ZLimitsResponseSchema.parse(res))
|
|
||||||
.catch(() => {
|
|
||||||
return {
|
|
||||||
quota: FREE_PLAN_LIMITS,
|
|
||||||
remaining: FREE_PLAN_LIMITS,
|
|
||||||
} satisfies TLimitsResponseSchema;
|
|
||||||
});
|
|
||||||
};
|
|
||||||
@ -1,11 +0,0 @@
|
|||||||
import { TLimitsSchema } from './schema';
|
|
||||||
|
|
||||||
export const FREE_PLAN_LIMITS: TLimitsSchema = {
|
|
||||||
documents: 5,
|
|
||||||
recipients: 10,
|
|
||||||
};
|
|
||||||
|
|
||||||
export const SELFHOSTED_PLAN_LIMITS: TLimitsSchema = {
|
|
||||||
documents: Infinity,
|
|
||||||
recipients: Infinity,
|
|
||||||
};
|
|
||||||
@ -1,6 +0,0 @@
|
|||||||
export const ERROR_CODES: Record<string, string> = {
|
|
||||||
UNAUTHORIZED: 'You must be logged in to access this resource',
|
|
||||||
USER_FETCH_FAILED: 'An error occurred while fetching your user account',
|
|
||||||
SUBSCRIPTION_FETCH_FAILED: 'An error occurred while fetching your subscription',
|
|
||||||
UNKNOWN: 'An unknown error occurred',
|
|
||||||
};
|
|
||||||
@ -1,54 +0,0 @@
|
|||||||
import { NextApiRequest, NextApiResponse } from 'next';
|
|
||||||
|
|
||||||
import { getToken } from 'next-auth/jwt';
|
|
||||||
import { match } from 'ts-pattern';
|
|
||||||
|
|
||||||
import { withStaleWhileRevalidate } from '@documenso/lib/server-only/http/with-swr';
|
|
||||||
import { getFlag } from '@documenso/lib/universal/get-feature-flag';
|
|
||||||
|
|
||||||
import { SELFHOSTED_PLAN_LIMITS } from './constants';
|
|
||||||
import { ERROR_CODES } from './errors';
|
|
||||||
import { TLimitsErrorResponseSchema, TLimitsResponseSchema } from './schema';
|
|
||||||
import { getServerLimits } from './server';
|
|
||||||
|
|
||||||
export const limitsHandler = async (
|
|
||||||
req: NextApiRequest,
|
|
||||||
res: NextApiResponse<TLimitsResponseSchema | TLimitsErrorResponseSchema>,
|
|
||||||
) => {
|
|
||||||
try {
|
|
||||||
const token = await getToken({ req });
|
|
||||||
|
|
||||||
const isBillingEnabled = await getFlag('app_billing');
|
|
||||||
|
|
||||||
if (!isBillingEnabled) {
|
|
||||||
return withStaleWhileRevalidate<typeof res>(res).status(200).json({
|
|
||||||
quota: SELFHOSTED_PLAN_LIMITS,
|
|
||||||
remaining: SELFHOSTED_PLAN_LIMITS,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!token?.email) {
|
|
||||||
throw new Error(ERROR_CODES.UNAUTHORIZED);
|
|
||||||
}
|
|
||||||
|
|
||||||
const limits = await getServerLimits({ email: token.email });
|
|
||||||
|
|
||||||
return withStaleWhileRevalidate<typeof res>(res).status(200).json(limits);
|
|
||||||
} catch (err) {
|
|
||||||
console.error('error', err);
|
|
||||||
|
|
||||||
if (err instanceof Error) {
|
|
||||||
const status = match(err.message)
|
|
||||||
.with(ERROR_CODES.UNAUTHORIZED, () => 401)
|
|
||||||
.otherwise(() => 500);
|
|
||||||
|
|
||||||
return res.status(status).json({
|
|
||||||
error: ERROR_CODES[err.message] ?? ERROR_CODES.UNKNOWN,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
res.status(500).json({
|
|
||||||
error: ERROR_CODES.UNKNOWN,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
@ -1,53 +0,0 @@
|
|||||||
'use client';
|
|
||||||
|
|
||||||
import { createContext, useContext, useEffect, useState } from 'react';
|
|
||||||
|
|
||||||
import { getLimits } from '../client';
|
|
||||||
import { FREE_PLAN_LIMITS } from '../constants';
|
|
||||||
import { TLimitsResponseSchema } from '../schema';
|
|
||||||
|
|
||||||
export type LimitsContextValue = TLimitsResponseSchema;
|
|
||||||
|
|
||||||
const LimitsContext = createContext<LimitsContextValue | null>(null);
|
|
||||||
|
|
||||||
export const useLimits = () => {
|
|
||||||
const limits = useContext(LimitsContext);
|
|
||||||
|
|
||||||
if (!limits) {
|
|
||||||
throw new Error('useLimits must be used within a LimitsProvider');
|
|
||||||
}
|
|
||||||
|
|
||||||
return limits;
|
|
||||||
};
|
|
||||||
|
|
||||||
export type LimitsProviderProps = {
|
|
||||||
initialValue?: LimitsContextValue;
|
|
||||||
children?: React.ReactNode;
|
|
||||||
};
|
|
||||||
|
|
||||||
export const LimitsProvider = ({ initialValue, children }: LimitsProviderProps) => {
|
|
||||||
const defaultValue: TLimitsResponseSchema = {
|
|
||||||
quota: FREE_PLAN_LIMITS,
|
|
||||||
remaining: FREE_PLAN_LIMITS,
|
|
||||||
};
|
|
||||||
|
|
||||||
const [limits, setLimits] = useState(() => initialValue ?? defaultValue);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
void getLimits().then((limits) => setLimits(limits));
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
const onFocus = () => {
|
|
||||||
void getLimits().then((limits) => setLimits(limits));
|
|
||||||
};
|
|
||||||
|
|
||||||
window.addEventListener('focus', onFocus);
|
|
||||||
|
|
||||||
return () => {
|
|
||||||
window.removeEventListener('focus', onFocus);
|
|
||||||
};
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
return <LimitsContext.Provider value={limits}>{children}</LimitsContext.Provider>;
|
|
||||||
};
|
|
||||||
@ -1,18 +0,0 @@
|
|||||||
'use server';
|
|
||||||
|
|
||||||
import { headers } from 'next/headers';
|
|
||||||
|
|
||||||
import { getLimits } from '../client';
|
|
||||||
import { LimitsProvider as ClientLimitsProvider } from './client';
|
|
||||||
|
|
||||||
export type LimitsProviderProps = {
|
|
||||||
children?: React.ReactNode;
|
|
||||||
};
|
|
||||||
|
|
||||||
export const LimitsProvider = async ({ children }: LimitsProviderProps) => {
|
|
||||||
const requestHeaders = Object.fromEntries(headers().entries());
|
|
||||||
|
|
||||||
const limits = await getLimits({ headers: requestHeaders });
|
|
||||||
|
|
||||||
return <ClientLimitsProvider initialValue={limits}>{children}</ClientLimitsProvider>;
|
|
||||||
};
|
|
||||||
@ -1,28 +0,0 @@
|
|||||||
import { z } from 'zod';
|
|
||||||
|
|
||||||
// Not proud of the below but it's a way to deal with Infinity when returning JSON.
|
|
||||||
export const ZLimitsSchema = z.object({
|
|
||||||
documents: z
|
|
||||||
.preprocess((v) => (v === null ? Infinity : Number(v)), z.number())
|
|
||||||
.optional()
|
|
||||||
.default(0),
|
|
||||||
recipients: z
|
|
||||||
.preprocess((v) => (v === null ? Infinity : Number(v)), z.number())
|
|
||||||
.optional()
|
|
||||||
.default(0),
|
|
||||||
});
|
|
||||||
|
|
||||||
export type TLimitsSchema = z.infer<typeof ZLimitsSchema>;
|
|
||||||
|
|
||||||
export const ZLimitsResponseSchema = z.object({
|
|
||||||
quota: ZLimitsSchema,
|
|
||||||
remaining: ZLimitsSchema,
|
|
||||||
});
|
|
||||||
|
|
||||||
export type TLimitsResponseSchema = z.infer<typeof ZLimitsResponseSchema>;
|
|
||||||
|
|
||||||
export const ZLimitsErrorResponseSchema = z.object({
|
|
||||||
error: z.string(),
|
|
||||||
});
|
|
||||||
|
|
||||||
export type TLimitsErrorResponseSchema = z.infer<typeof ZLimitsErrorResponseSchema>;
|
|
||||||
@ -1,78 +0,0 @@
|
|||||||
import { DateTime } from 'luxon';
|
|
||||||
|
|
||||||
import { stripe } from '@documenso/lib/server-only/stripe';
|
|
||||||
import { getFlag } from '@documenso/lib/universal/get-feature-flag';
|
|
||||||
import { prisma } from '@documenso/prisma';
|
|
||||||
|
|
||||||
import { FREE_PLAN_LIMITS, SELFHOSTED_PLAN_LIMITS } from './constants';
|
|
||||||
import { ERROR_CODES } from './errors';
|
|
||||||
import { ZLimitsSchema } from './schema';
|
|
||||||
|
|
||||||
export type GetServerLimitsOptions = {
|
|
||||||
email?: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
export const getServerLimits = async ({ email }: GetServerLimitsOptions) => {
|
|
||||||
const isBillingEnabled = await getFlag('app_billing');
|
|
||||||
|
|
||||||
if (!isBillingEnabled) {
|
|
||||||
return {
|
|
||||||
quota: SELFHOSTED_PLAN_LIMITS,
|
|
||||||
remaining: SELFHOSTED_PLAN_LIMITS,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!email) {
|
|
||||||
throw new Error(ERROR_CODES.UNAUTHORIZED);
|
|
||||||
}
|
|
||||||
|
|
||||||
const user = await prisma.user.findFirst({
|
|
||||||
where: {
|
|
||||||
email,
|
|
||||||
},
|
|
||||||
include: {
|
|
||||||
Subscription: true,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!user) {
|
|
||||||
throw new Error(ERROR_CODES.USER_FETCH_FAILED);
|
|
||||||
}
|
|
||||||
|
|
||||||
let quota = structuredClone(FREE_PLAN_LIMITS);
|
|
||||||
let remaining = structuredClone(FREE_PLAN_LIMITS);
|
|
||||||
|
|
||||||
if (user.Subscription?.priceId) {
|
|
||||||
const { product } = await stripe.prices
|
|
||||||
.retrieve(user.Subscription.priceId, {
|
|
||||||
expand: ['product'],
|
|
||||||
})
|
|
||||||
.catch((err) => {
|
|
||||||
console.error(err);
|
|
||||||
throw err;
|
|
||||||
});
|
|
||||||
|
|
||||||
if (typeof product === 'string') {
|
|
||||||
throw new Error(ERROR_CODES.SUBSCRIPTION_FETCH_FAILED);
|
|
||||||
}
|
|
||||||
|
|
||||||
quota = ZLimitsSchema.parse('metadata' in product ? product.metadata : {});
|
|
||||||
remaining = structuredClone(quota);
|
|
||||||
}
|
|
||||||
|
|
||||||
const documents = await prisma.document.count({
|
|
||||||
where: {
|
|
||||||
userId: user.id,
|
|
||||||
createdAt: {
|
|
||||||
gte: DateTime.utc().startOf('month').toJSDate(),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
remaining.documents = Math.max(remaining.documents - documents, 0);
|
|
||||||
|
|
||||||
return {
|
|
||||||
quota,
|
|
||||||
remaining,
|
|
||||||
};
|
|
||||||
};
|
|
||||||
@ -1,32 +0,0 @@
|
|||||||
'use server';
|
|
||||||
|
|
||||||
import { stripe } from '@documenso/lib/server-only/stripe';
|
|
||||||
|
|
||||||
export type GetCheckoutSessionOptions = {
|
|
||||||
customerId: string;
|
|
||||||
priceId: string;
|
|
||||||
returnUrl: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
export const getCheckoutSession = async ({
|
|
||||||
customerId,
|
|
||||||
priceId,
|
|
||||||
returnUrl,
|
|
||||||
}: GetCheckoutSessionOptions) => {
|
|
||||||
'use server';
|
|
||||||
|
|
||||||
const session = await stripe.checkout.sessions.create({
|
|
||||||
customer: customerId,
|
|
||||||
mode: 'subscription',
|
|
||||||
line_items: [
|
|
||||||
{
|
|
||||||
price: priceId,
|
|
||||||
quantity: 1,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
success_url: `${returnUrl}?success=true`,
|
|
||||||
cancel_url: `${returnUrl}?canceled=true`,
|
|
||||||
});
|
|
||||||
|
|
||||||
return session.url;
|
|
||||||
};
|
|
||||||
@ -1,19 +0,0 @@
|
|||||||
import { stripe } from '@documenso/lib/server-only/stripe';
|
|
||||||
|
|
||||||
export const getStripeCustomerByEmail = async (email: string) => {
|
|
||||||
const foundStripeCustomers = await stripe.customers.list({
|
|
||||||
email,
|
|
||||||
});
|
|
||||||
|
|
||||||
return foundStripeCustomers.data[0] ?? null;
|
|
||||||
};
|
|
||||||
|
|
||||||
export const getStripeCustomerById = async (stripeCustomerId: string) => {
|
|
||||||
try {
|
|
||||||
const stripeCustomer = await stripe.customers.retrieve(stripeCustomerId);
|
|
||||||
|
|
||||||
return !stripeCustomer.deleted ? stripeCustomer : null;
|
|
||||||
} catch {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
@ -1,49 +0,0 @@
|
|||||||
import Stripe from 'stripe';
|
|
||||||
|
|
||||||
import { stripe } from '@documenso/lib/server-only/stripe';
|
|
||||||
|
|
||||||
// Utility type to handle usage of the `expand` option.
|
|
||||||
type PriceWithProduct = Stripe.Price & { product: Stripe.Product };
|
|
||||||
|
|
||||||
export type PriceIntervals = Record<Stripe.Price.Recurring.Interval, PriceWithProduct[]>;
|
|
||||||
|
|
||||||
export const getPricesByInterval = async () => {
|
|
||||||
let { data: prices } = await stripe.prices.search({
|
|
||||||
query: `active:'true' type:'recurring'`,
|
|
||||||
expand: ['data.product'],
|
|
||||||
limit: 100,
|
|
||||||
});
|
|
||||||
|
|
||||||
prices = prices.filter((price) => {
|
|
||||||
// We use `expand` to get the product, but it's not typed as part of the Price type.
|
|
||||||
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
||||||
const product = price.product as Stripe.Product;
|
|
||||||
|
|
||||||
// Filter out prices for products that are not active.
|
|
||||||
return product.active;
|
|
||||||
});
|
|
||||||
|
|
||||||
const intervals: PriceIntervals = {
|
|
||||||
day: [],
|
|
||||||
week: [],
|
|
||||||
month: [],
|
|
||||||
year: [],
|
|
||||||
};
|
|
||||||
|
|
||||||
// Add each price to the correct interval.
|
|
||||||
for (const price of prices) {
|
|
||||||
if (price.recurring?.interval) {
|
|
||||||
// We use `expand` to get the product, but it's not typed as part of the Price type.
|
|
||||||
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
||||||
intervals[price.recurring.interval].push(price as PriceWithProduct);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Order all prices by unit_amount.
|
|
||||||
intervals.day.sort((a, b) => Number(a.unit_amount) - Number(b.unit_amount));
|
|
||||||
intervals.week.sort((a, b) => Number(a.unit_amount) - Number(b.unit_amount));
|
|
||||||
intervals.month.sort((a, b) => Number(a.unit_amount) - Number(b.unit_amount));
|
|
||||||
intervals.year.sort((a, b) => Number(a.unit_amount) - Number(b.unit_amount));
|
|
||||||
|
|
||||||
return intervals;
|
|
||||||
};
|
|
||||||
@ -1,17 +0,0 @@
|
|||||||
import { stripe } from '@documenso/lib/server-only/stripe';
|
|
||||||
|
|
||||||
export type GetProductByPriceIdOptions = {
|
|
||||||
priceId: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
export const getProductByPriceId = async ({ priceId }: GetProductByPriceIdOptions) => {
|
|
||||||
const { product } = await stripe.prices.retrieve(priceId, {
|
|
||||||
expand: ['product'],
|
|
||||||
});
|
|
||||||
|
|
||||||
if (typeof product === 'string' || 'deleted' in product) {
|
|
||||||
throw new Error('Product not found');
|
|
||||||
}
|
|
||||||
|
|
||||||
return product;
|
|
||||||
};
|
|
||||||
@ -1,224 +0,0 @@
|
|||||||
import { NextApiRequest, NextApiResponse } from 'next';
|
|
||||||
|
|
||||||
import { buffer } from 'micro';
|
|
||||||
import { match } from 'ts-pattern';
|
|
||||||
|
|
||||||
import { Stripe, stripe } from '@documenso/lib/server-only/stripe';
|
|
||||||
import { getFlag } from '@documenso/lib/universal/get-feature-flag';
|
|
||||||
import { prisma } from '@documenso/prisma';
|
|
||||||
|
|
||||||
import { onSubscriptionDeleted } from './on-subscription-deleted';
|
|
||||||
import { onSubscriptionUpdated } from './on-subscription-updated';
|
|
||||||
|
|
||||||
type StripeWebhookResponse = {
|
|
||||||
success: boolean;
|
|
||||||
message: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
export const stripeWebhookHandler = async (
|
|
||||||
req: NextApiRequest,
|
|
||||||
res: NextApiResponse<StripeWebhookResponse>,
|
|
||||||
) => {
|
|
||||||
try {
|
|
||||||
const isBillingEnabled = await getFlag('app_billing');
|
|
||||||
|
|
||||||
if (!isBillingEnabled) {
|
|
||||||
return res.status(500).json({
|
|
||||||
success: false,
|
|
||||||
message: 'Billing is disabled',
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const signature =
|
|
||||||
typeof req.headers['stripe-signature'] === 'string' ? req.headers['stripe-signature'] : '';
|
|
||||||
|
|
||||||
if (!signature) {
|
|
||||||
return res.status(400).json({
|
|
||||||
success: false,
|
|
||||||
message: 'No signature found in request',
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const body = await buffer(req);
|
|
||||||
|
|
||||||
const event = stripe.webhooks.constructEvent(
|
|
||||||
body,
|
|
||||||
signature,
|
|
||||||
process.env.NEXT_PRIVATE_STRIPE_WEBHOOK_SECRET,
|
|
||||||
);
|
|
||||||
|
|
||||||
await match(event.type)
|
|
||||||
.with('checkout.session.completed', async () => {
|
|
||||||
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
||||||
const session = event.data.object as Stripe.Checkout.Session;
|
|
||||||
|
|
||||||
const userId = Number(session.client_reference_id);
|
|
||||||
const subscriptionId =
|
|
||||||
typeof session.subscription === 'string'
|
|
||||||
? session.subscription
|
|
||||||
: session.subscription?.id;
|
|
||||||
|
|
||||||
if (!subscriptionId || Number.isNaN(userId)) {
|
|
||||||
return res.status(500).json({
|
|
||||||
success: false,
|
|
||||||
message: 'Invalid session',
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const subscription = await stripe.subscriptions.retrieve(subscriptionId);
|
|
||||||
|
|
||||||
await onSubscriptionUpdated({ userId, subscription });
|
|
||||||
|
|
||||||
return res.status(200).json({
|
|
||||||
success: true,
|
|
||||||
message: 'Webhook received',
|
|
||||||
});
|
|
||||||
})
|
|
||||||
.with('customer.subscription.updated', async () => {
|
|
||||||
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
||||||
const subscription = event.data.object as Stripe.Subscription;
|
|
||||||
|
|
||||||
const customerId =
|
|
||||||
typeof subscription.customer === 'string'
|
|
||||||
? subscription.customer
|
|
||||||
: subscription.customer.id;
|
|
||||||
|
|
||||||
const result = await prisma.subscription.findFirst({
|
|
||||||
select: {
|
|
||||||
userId: true,
|
|
||||||
},
|
|
||||||
where: {
|
|
||||||
customerId,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!result?.userId) {
|
|
||||||
return res.status(500).json({
|
|
||||||
success: false,
|
|
||||||
message: 'User not found',
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
await onSubscriptionUpdated({ userId: result.userId, subscription });
|
|
||||||
|
|
||||||
return res.status(200).json({
|
|
||||||
success: true,
|
|
||||||
message: 'Webhook received',
|
|
||||||
});
|
|
||||||
})
|
|
||||||
.with('invoice.payment_succeeded', async () => {
|
|
||||||
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
||||||
const invoice = event.data.object as Stripe.Invoice;
|
|
||||||
|
|
||||||
if (invoice.billing_reason !== 'subscription_cycle') {
|
|
||||||
return res.status(200).json({
|
|
||||||
success: true,
|
|
||||||
message: 'Webhook received',
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const customerId =
|
|
||||||
typeof invoice.customer === 'string' ? invoice.customer : invoice.customer?.id;
|
|
||||||
|
|
||||||
const subscriptionId =
|
|
||||||
typeof invoice.subscription === 'string'
|
|
||||||
? invoice.subscription
|
|
||||||
: invoice.subscription?.id;
|
|
||||||
|
|
||||||
if (!customerId || !subscriptionId) {
|
|
||||||
return res.status(500).json({
|
|
||||||
success: false,
|
|
||||||
message: 'Invalid invoice',
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const subscription = await stripe.subscriptions.retrieve(subscriptionId);
|
|
||||||
|
|
||||||
const result = await prisma.subscription.findFirst({
|
|
||||||
select: {
|
|
||||||
userId: true,
|
|
||||||
},
|
|
||||||
where: {
|
|
||||||
customerId,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!result?.userId) {
|
|
||||||
return res.status(500).json({
|
|
||||||
success: false,
|
|
||||||
message: 'User not found',
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
await onSubscriptionUpdated({ userId: result.userId, subscription });
|
|
||||||
|
|
||||||
return res.status(200).json({
|
|
||||||
success: true,
|
|
||||||
message: 'Webhook received',
|
|
||||||
});
|
|
||||||
})
|
|
||||||
.with('invoice.payment_failed', async () => {
|
|
||||||
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
||||||
const invoice = event.data.object as Stripe.Invoice;
|
|
||||||
|
|
||||||
const customerId =
|
|
||||||
typeof invoice.customer === 'string' ? invoice.customer : invoice.customer?.id;
|
|
||||||
|
|
||||||
const subscriptionId =
|
|
||||||
typeof invoice.subscription === 'string'
|
|
||||||
? invoice.subscription
|
|
||||||
: invoice.subscription?.id;
|
|
||||||
|
|
||||||
if (!customerId || !subscriptionId) {
|
|
||||||
return res.status(500).json({
|
|
||||||
success: false,
|
|
||||||
message: 'Invalid invoice',
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const subscription = await stripe.subscriptions.retrieve(subscriptionId);
|
|
||||||
|
|
||||||
const result = await prisma.subscription.findFirst({
|
|
||||||
select: {
|
|
||||||
userId: true,
|
|
||||||
},
|
|
||||||
where: {
|
|
||||||
customerId,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!result?.userId) {
|
|
||||||
return res.status(500).json({
|
|
||||||
success: false,
|
|
||||||
message: 'User not found',
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
await onSubscriptionUpdated({ userId: result.userId, subscription });
|
|
||||||
})
|
|
||||||
.with('customer.subscription.deleted', async () => {
|
|
||||||
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
||||||
const subscription = event.data.object as Stripe.Subscription;
|
|
||||||
|
|
||||||
await onSubscriptionDeleted({ subscription });
|
|
||||||
|
|
||||||
return res.status(200).json({
|
|
||||||
success: true,
|
|
||||||
message: 'Webhook received',
|
|
||||||
});
|
|
||||||
})
|
|
||||||
.otherwise(() => {
|
|
||||||
return res.status(200).json({
|
|
||||||
success: true,
|
|
||||||
message: 'Webhook received',
|
|
||||||
});
|
|
||||||
});
|
|
||||||
} catch (err) {
|
|
||||||
console.error(err);
|
|
||||||
|
|
||||||
res.status(500).json({
|
|
||||||
success: false,
|
|
||||||
message: 'Unknown error',
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
@ -1,21 +0,0 @@
|
|||||||
import { Stripe } from '@documenso/lib/server-only/stripe';
|
|
||||||
import { prisma } from '@documenso/prisma';
|
|
||||||
import { SubscriptionStatus } from '@documenso/prisma/client';
|
|
||||||
|
|
||||||
export type OnSubscriptionDeletedOptions = {
|
|
||||||
subscription: Stripe.Subscription;
|
|
||||||
};
|
|
||||||
|
|
||||||
export const onSubscriptionDeleted = async ({ subscription }: OnSubscriptionDeletedOptions) => {
|
|
||||||
const customerId =
|
|
||||||
typeof subscription.customer === 'string' ? subscription.customer : subscription.customer?.id;
|
|
||||||
|
|
||||||
await prisma.subscription.update({
|
|
||||||
where: {
|
|
||||||
customerId,
|
|
||||||
},
|
|
||||||
data: {
|
|
||||||
status: SubscriptionStatus.INACTIVE,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
};
|
|
||||||
@ -1,44 +0,0 @@
|
|||||||
import { match } from 'ts-pattern';
|
|
||||||
|
|
||||||
import { Stripe } from '@documenso/lib/server-only/stripe';
|
|
||||||
import { prisma } from '@documenso/prisma';
|
|
||||||
import { SubscriptionStatus } from '@documenso/prisma/client';
|
|
||||||
|
|
||||||
export type OnSubscriptionUpdatedOptions = {
|
|
||||||
userId: number;
|
|
||||||
subscription: Stripe.Subscription;
|
|
||||||
};
|
|
||||||
|
|
||||||
export const onSubscriptionUpdated = async ({
|
|
||||||
userId,
|
|
||||||
subscription,
|
|
||||||
}: OnSubscriptionUpdatedOptions) => {
|
|
||||||
const customerId =
|
|
||||||
typeof subscription.customer === 'string' ? subscription.customer : subscription.customer?.id;
|
|
||||||
|
|
||||||
const status = match(subscription.status)
|
|
||||||
.with('active', () => SubscriptionStatus.ACTIVE)
|
|
||||||
.with('past_due', () => SubscriptionStatus.PAST_DUE)
|
|
||||||
.otherwise(() => SubscriptionStatus.INACTIVE);
|
|
||||||
|
|
||||||
await prisma.subscription.upsert({
|
|
||||||
where: {
|
|
||||||
customerId,
|
|
||||||
},
|
|
||||||
create: {
|
|
||||||
customerId,
|
|
||||||
status: status,
|
|
||||||
planId: subscription.id,
|
|
||||||
priceId: subscription.items.data[0].price.id,
|
|
||||||
periodEnd: new Date(subscription.current_period_end * 1000),
|
|
||||||
userId,
|
|
||||||
},
|
|
||||||
update: {
|
|
||||||
customerId,
|
|
||||||
status: status,
|
|
||||||
planId: subscription.id,
|
|
||||||
priceId: subscription.items.data[0].price.id,
|
|
||||||
periodEnd: new Date(subscription.current_period_end * 1000),
|
|
||||||
},
|
|
||||||
});
|
|
||||||
};
|
|
||||||
@ -1 +1 @@
|
|||||||
export { render } from '@react-email/components';
|
export { render, renderAsync } from '@react-email/components';
|
||||||
|
|||||||
@ -32,7 +32,7 @@ export const DocumentInviteEmailTemplate = ({
|
|||||||
assetBaseUrl = 'http://localhost:3002',
|
assetBaseUrl = 'http://localhost:3002',
|
||||||
customBody,
|
customBody,
|
||||||
}: DocumentInviteEmailTemplateProps) => {
|
}: DocumentInviteEmailTemplateProps) => {
|
||||||
const previewText = `${inviterName} has invited you to sign ${documentName}`;
|
const previewText = `Completed Document`;
|
||||||
|
|
||||||
const getAssetUrl = (path: string) => {
|
const getAssetUrl = (path: string) => {
|
||||||
return new URL(path, assetBaseUrl).toString();
|
return new URL(path, assetBaseUrl).toString();
|
||||||
|
|||||||
@ -7,8 +7,8 @@
|
|||||||
"clean": "rimraf node_modules"
|
"clean": "rimraf node_modules"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@typescript-eslint/eslint-plugin": "6.8.0",
|
"@typescript-eslint/eslint-plugin": "^5.59.2",
|
||||||
"@typescript-eslint/parser": "6.8.0",
|
"@typescript-eslint/parser": "^5.59.2",
|
||||||
"eslint": "^8.40.0",
|
"eslint": "^8.40.0",
|
||||||
"eslint-config-next": "13.4.19",
|
"eslint-config-next": "13.4.19",
|
||||||
"eslint-config-prettier": "^8.8.0",
|
"eslint-config-prettier": "^8.8.0",
|
||||||
@ -16,6 +16,6 @@
|
|||||||
"eslint-plugin-package-json": "^0.1.4",
|
"eslint-plugin-package-json": "^0.1.4",
|
||||||
"eslint-plugin-prettier": "^4.2.1",
|
"eslint-plugin-prettier": "^4.2.1",
|
||||||
"eslint-plugin-react": "^7.32.2",
|
"eslint-plugin-react": "^7.32.2",
|
||||||
"typescript": "5.2.2"
|
"typescript": "^5.1.6"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1,53 +0,0 @@
|
|||||||
import { trpc } from '@documenso/trpc/react';
|
|
||||||
import { TCreateOrGetShareLinkMutationSchema } from '@documenso/trpc/server/share-link-router/schema';
|
|
||||||
|
|
||||||
import { useCopyToClipboard } from './use-copy-to-clipboard';
|
|
||||||
|
|
||||||
export type UseCopyShareLinkOptions = {
|
|
||||||
onSuccess?: () => void;
|
|
||||||
onError?: () => void;
|
|
||||||
};
|
|
||||||
|
|
||||||
export function useCopyShareLink({ onSuccess, onError }: UseCopyShareLinkOptions) {
|
|
||||||
const [, copyToClipboard] = useCopyToClipboard();
|
|
||||||
|
|
||||||
const { mutateAsync: createOrGetShareLink, isLoading: isCreatingShareLink } =
|
|
||||||
trpc.shareLink.createOrGetShareLink.useMutation();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Copy a newly created, or pre-existing share link to the user's clipboard.
|
|
||||||
*
|
|
||||||
* @param payload The payload to create or get a share link.
|
|
||||||
*/
|
|
||||||
const createAndCopyShareLink = async (payload: TCreateOrGetShareLinkMutationSchema) => {
|
|
||||||
const valueToCopy = createOrGetShareLink(payload).then(
|
|
||||||
(result) => `${window.location.origin}/share/${result.slug}`,
|
|
||||||
);
|
|
||||||
|
|
||||||
await copyShareLink(valueToCopy);
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Copy a share link to the user's clipboard.
|
|
||||||
*
|
|
||||||
* @param shareLink Either the share link itself or a promise that returns a shared link.
|
|
||||||
*/
|
|
||||||
const copyShareLink = async (shareLink: Promise<string> | string) => {
|
|
||||||
try {
|
|
||||||
const isCopySuccess = await copyToClipboard(shareLink);
|
|
||||||
if (!isCopySuccess) {
|
|
||||||
throw new Error('Copy to clipboard failed');
|
|
||||||
}
|
|
||||||
|
|
||||||
onSuccess?.();
|
|
||||||
} catch (e) {
|
|
||||||
onError?.();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
return {
|
|
||||||
createAndCopyShareLink,
|
|
||||||
copyShareLink,
|
|
||||||
isCopyingShareLink: isCreatingShareLink,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
@ -1,28 +1,21 @@
|
|||||||
import { useState } from 'react';
|
import { useState } from 'react';
|
||||||
|
|
||||||
export type CopiedValue = string | null;
|
export type CopiedValue = string | null;
|
||||||
export type CopyFn = (_text: CopyValue, _blobType?: string) => Promise<boolean>;
|
export type CopyFn = (_text: string) => Promise<boolean>;
|
||||||
|
|
||||||
type CopyValue = Promise<string> | string;
|
|
||||||
|
|
||||||
export function useCopyToClipboard(): [CopiedValue, CopyFn] {
|
export function useCopyToClipboard(): [CopiedValue, CopyFn] {
|
||||||
const [copiedText, setCopiedText] = useState<CopiedValue>(null);
|
const [copiedText, setCopiedText] = useState<CopiedValue>(null);
|
||||||
|
|
||||||
const copy: CopyFn = async (text, blobType = 'text/plain') => {
|
const copy: CopyFn = async (text) => {
|
||||||
if (!navigator?.clipboard) {
|
if (!navigator?.clipboard) {
|
||||||
console.warn('Clipboard not supported');
|
console.warn('Clipboard not supported');
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
const isClipboardApiSupported = Boolean(typeof ClipboardItem && navigator.clipboard.write);
|
|
||||||
|
|
||||||
// Try to save to clipboard then save it in the state if worked
|
// Try to save to clipboard then save it in the state if worked
|
||||||
try {
|
try {
|
||||||
isClipboardApiSupported
|
await navigator.clipboard.writeText(text);
|
||||||
? await handleClipboardApiCopy(text, blobType)
|
setCopiedText(text);
|
||||||
: await handleWriteTextCopy(text);
|
|
||||||
|
|
||||||
setCopiedText(await text);
|
|
||||||
return true;
|
return true;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.warn('Copy failed', error);
|
console.warn('Copy failed', error);
|
||||||
@ -31,30 +24,5 @@ export function useCopyToClipboard(): [CopiedValue, CopyFn] {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
|
||||||
* Handle copying values to the clipboard using the ClipboardItem API.
|
|
||||||
*
|
|
||||||
* Works in all browsers except FireFox.
|
|
||||||
*
|
|
||||||
* https://caniuse.com/mdn-api_clipboarditem
|
|
||||||
*/
|
|
||||||
const handleClipboardApiCopy = async (value: CopyValue, blobType = 'text/plain') => {
|
|
||||||
try {
|
|
||||||
await navigator.clipboard.write([new ClipboardItem({ [blobType]: value })]);
|
|
||||||
} catch (e) {
|
|
||||||
// Fallback attempt.
|
|
||||||
await handleWriteTextCopy(value);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Handle copying values to the clipboard using `writeText`.
|
|
||||||
*
|
|
||||||
* Works in all browsers except Safari for async values.
|
|
||||||
*/
|
|
||||||
const handleWriteTextCopy = async (value: CopyValue) => {
|
|
||||||
await navigator.clipboard.writeText(await value);
|
|
||||||
};
|
|
||||||
|
|
||||||
return [copiedText, copy];
|
return [copiedText, copy];
|
||||||
}
|
}
|
||||||
|
|||||||
@ -60,17 +60,26 @@ export const calculateTextScaleSize = (
|
|||||||
*/
|
*/
|
||||||
export function useElementScaleSize(
|
export function useElementScaleSize(
|
||||||
container: { width: number; height: number },
|
container: { width: number; height: number },
|
||||||
text: string,
|
child: RefObject<HTMLElement | null>,
|
||||||
fontSize: number,
|
fontSize: number,
|
||||||
fontFamily: string,
|
fontFamily: string,
|
||||||
) {
|
) {
|
||||||
const [scalingFactor, setScalingFactor] = useState(1);
|
const [scalingFactor, setScalingFactor] = useState(1);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const scaleSize = calculateTextScaleSize(container, text, `${fontSize}px`, fontFamily);
|
if (!child.current) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const scaleSize = calculateTextScaleSize(
|
||||||
|
container,
|
||||||
|
child.current.innerText,
|
||||||
|
`${fontSize}px`,
|
||||||
|
fontFamily,
|
||||||
|
);
|
||||||
|
|
||||||
setScalingFactor(scaleSize);
|
setScalingFactor(scaleSize);
|
||||||
}, [text, container, fontFamily, fontSize]);
|
}, [child, container, fontFamily, fontSize]);
|
||||||
|
|
||||||
return scalingFactor;
|
return scalingFactor;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,13 +0,0 @@
|
|||||||
import { Toast } from '@documenso/ui/primitives/use-toast';
|
|
||||||
|
|
||||||
export const TOAST_DOCUMENT_SHARE_SUCCESS: Toast = {
|
|
||||||
title: 'Copied to clipboard',
|
|
||||||
description: 'The sharing link has been copied to your clipboard.',
|
|
||||||
} as const;
|
|
||||||
|
|
||||||
export const TOAST_DOCUMENT_SHARE_ERROR: Toast = {
|
|
||||||
variant: 'destructive',
|
|
||||||
title: 'Something went wrong',
|
|
||||||
description: 'The sharing link could not be created at this time. Please try again.',
|
|
||||||
duration: 5000,
|
|
||||||
};
|
|
||||||
@ -1,3 +1,5 @@
|
|||||||
import { Role, User } from '@documenso/prisma/client';
|
import { Role, User } from '@documenso/prisma/client';
|
||||||
|
|
||||||
export const isAdmin = (user: User) => user.roles.includes(Role.ADMIN);
|
const isAdmin = (user: User) => user.roles.includes(Role.ADMIN);
|
||||||
|
|
||||||
|
export { isAdmin };
|
||||||
|
|||||||
@ -28,13 +28,12 @@
|
|||||||
"bcrypt": "^5.1.0",
|
"bcrypt": "^5.1.0",
|
||||||
"luxon": "^3.4.0",
|
"luxon": "^3.4.0",
|
||||||
"nanoid": "^4.0.2",
|
"nanoid": "^4.0.2",
|
||||||
"next": "13.5.4",
|
"next": "13.4.19",
|
||||||
"next-auth": "4.22.3",
|
"next-auth": "4.22.3",
|
||||||
"pdf-lib": "^1.17.1",
|
"pdf-lib": "^1.17.1",
|
||||||
"react": "18.2.0",
|
"react": "18.2.0",
|
||||||
"stripe": "^12.7.0",
|
"stripe": "^12.7.0",
|
||||||
"ts-pattern": "^5.0.5",
|
"ts-pattern": "^5.0.5"
|
||||||
"zod": "^3.21.4"
|
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/bcrypt": "^5.0.0",
|
"@types/bcrypt": "^5.0.0",
|
||||||
|
|||||||
@ -1,55 +0,0 @@
|
|||||||
import { prisma } from '@documenso/prisma';
|
|
||||||
import { Prisma } from '@documenso/prisma/client';
|
|
||||||
|
|
||||||
export interface FindDocumentsOptions {
|
|
||||||
term?: string;
|
|
||||||
page?: number;
|
|
||||||
perPage?: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
export const findDocuments = async ({ term, page = 1, perPage = 10 }: FindDocumentsOptions) => {
|
|
||||||
const termFilters: Prisma.DocumentWhereInput | undefined = !term
|
|
||||||
? undefined
|
|
||||||
: {
|
|
||||||
title: {
|
|
||||||
contains: term,
|
|
||||||
mode: 'insensitive',
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
const [data, count] = await Promise.all([
|
|
||||||
prisma.document.findMany({
|
|
||||||
where: {
|
|
||||||
...termFilters,
|
|
||||||
},
|
|
||||||
skip: Math.max(page - 1, 0) * perPage,
|
|
||||||
take: perPage,
|
|
||||||
orderBy: {
|
|
||||||
createdAt: 'desc',
|
|
||||||
},
|
|
||||||
include: {
|
|
||||||
User: {
|
|
||||||
select: {
|
|
||||||
id: true,
|
|
||||||
name: true,
|
|
||||||
email: true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
Recipient: true,
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
prisma.document.count({
|
|
||||||
where: {
|
|
||||||
...termFilters,
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
]);
|
|
||||||
|
|
||||||
return {
|
|
||||||
data,
|
|
||||||
count,
|
|
||||||
currentPage: Math.max(page, 1),
|
|
||||||
perPage,
|
|
||||||
totalPages: Math.ceil(count / perPage),
|
|
||||||
};
|
|
||||||
};
|
|
||||||
@ -1,13 +0,0 @@
|
|||||||
import { prisma } from '@documenso/prisma';
|
|
||||||
|
|
||||||
export const findSubscriptions = async () => {
|
|
||||||
return prisma.subscription.findMany({
|
|
||||||
select: {
|
|
||||||
id: true,
|
|
||||||
status: true,
|
|
||||||
createdAt: true,
|
|
||||||
periodEnd: true,
|
|
||||||
userId: true,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
};
|
|
||||||
@ -9,8 +9,10 @@ export const getUsersWithSubscriptionsCount = async () => {
|
|||||||
return await prisma.user.count({
|
return await prisma.user.count({
|
||||||
where: {
|
where: {
|
||||||
Subscription: {
|
Subscription: {
|
||||||
|
some: {
|
||||||
status: SubscriptionStatus.ACTIVE,
|
status: SubscriptionStatus.ACTIVE,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
},
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
@ -1,28 +0,0 @@
|
|||||||
import { prisma } from '@documenso/prisma';
|
|
||||||
import { Role } from '@documenso/prisma/client';
|
|
||||||
|
|
||||||
export type UpdateUserOptions = {
|
|
||||||
id: number;
|
|
||||||
name: string | null | undefined;
|
|
||||||
email: string | undefined;
|
|
||||||
roles: Role[] | undefined;
|
|
||||||
};
|
|
||||||
|
|
||||||
export const updateUser = async ({ id, name, email, roles }: UpdateUserOptions) => {
|
|
||||||
await prisma.user.findFirstOrThrow({
|
|
||||||
where: {
|
|
||||||
id,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
return await prisma.user.update({
|
|
||||||
where: {
|
|
||||||
id,
|
|
||||||
},
|
|
||||||
data: {
|
|
||||||
name,
|
|
||||||
email,
|
|
||||||
roles,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
};
|
|
||||||
@ -1,35 +0,0 @@
|
|||||||
import { Pinecone } from '@pinecone-database/pinecone';
|
|
||||||
|
|
||||||
import { getEmbeddings } from './embeddings';
|
|
||||||
|
|
||||||
export async function getMatchesFromEmbeddings(embeddings: number[]) {
|
|
||||||
const pc = new Pinecone({
|
|
||||||
apiKey: process.env.PINECONE_API_KEY!,
|
|
||||||
environment: process.env.PINECONE_ENV!,
|
|
||||||
});
|
|
||||||
|
|
||||||
const pineconeIndex = pc.index('documenso-chat-with-pdf-test');
|
|
||||||
|
|
||||||
try {
|
|
||||||
const queryResult = await pineconeIndex.query({
|
|
||||||
topK: 5,
|
|
||||||
vector: embeddings,
|
|
||||||
includeMetadata: true,
|
|
||||||
});
|
|
||||||
|
|
||||||
return queryResult.matches || [];
|
|
||||||
} catch (error) {
|
|
||||||
console.error('There was an error getting matches from embeddings: ', error);
|
|
||||||
throw new Error('There was an error getting matches from embeddings');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function getContext(query: string) {
|
|
||||||
const queryEmbeddings = await getEmbeddings(query);
|
|
||||||
const matches = await getMatchesFromEmbeddings(queryEmbeddings);
|
|
||||||
|
|
||||||
const qualifyingMatches = matches.filter((match) => match.score && match.score > 0.7);
|
|
||||||
const docs = qualifyingMatches.map((match) => match.metadata?.text);
|
|
||||||
|
|
||||||
return docs.join('\n').substring(0, 3000);
|
|
||||||
}
|
|
||||||
@ -1,23 +0,0 @@
|
|||||||
import { Configuration, OpenAIApi } from 'openai-edge';
|
|
||||||
|
|
||||||
const config = new Configuration({
|
|
||||||
apiKey: process.env.OPENAI_API_KEY!,
|
|
||||||
});
|
|
||||||
|
|
||||||
const openai = new OpenAIApi(config);
|
|
||||||
|
|
||||||
export async function getEmbeddings(text: string) {
|
|
||||||
try {
|
|
||||||
const response = await openai.createEmbedding({
|
|
||||||
model: 'text-embedding-ada-002',
|
|
||||||
input: text.replace(/\n/g, ' '),
|
|
||||||
});
|
|
||||||
|
|
||||||
const result = await response.json();
|
|
||||||
|
|
||||||
return result.data[0].embedding;
|
|
||||||
} catch (error) {
|
|
||||||
console.error('There was an error getting embeddings: ', error);
|
|
||||||
throw new Error('There was an error getting embeddings');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,9 +0,0 @@
|
|||||||
import { NextRequest } from 'next/server';
|
|
||||||
|
|
||||||
export const toNextRequest = (req: Request) => {
|
|
||||||
const headers = Object.fromEntries(req.headers.entries());
|
|
||||||
|
|
||||||
return new NextRequest(req, {
|
|
||||||
headers: headers,
|
|
||||||
});
|
|
||||||
};
|
|
||||||
@ -1,28 +0,0 @@
|
|||||||
import { NextApiResponse } from 'next';
|
|
||||||
import { NextResponse } from 'next/server';
|
|
||||||
|
|
||||||
type NarrowedResponse<T> = T extends NextResponse
|
|
||||||
? NextResponse
|
|
||||||
: T extends NextApiResponse<infer U>
|
|
||||||
? NextApiResponse<U>
|
|
||||||
: never;
|
|
||||||
|
|
||||||
export const withStaleWhileRevalidate = <T>(
|
|
||||||
res: NarrowedResponse<T>,
|
|
||||||
cacheInSeconds = 60,
|
|
||||||
staleCacheInSeconds = 300,
|
|
||||||
) => {
|
|
||||||
if ('headers' in res) {
|
|
||||||
res.headers.set(
|
|
||||||
'Cache-Control',
|
|
||||||
`public, s-maxage=${cacheInSeconds}, stale-while-revalidate=${staleCacheInSeconds}`,
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
res.setHeader(
|
|
||||||
'Cache-Control',
|
|
||||||
`public, s-maxage=${cacheInSeconds}, stale-while-revalidate=${staleCacheInSeconds}`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return res;
|
|
||||||
};
|
|
||||||
@ -1,113 +0,0 @@
|
|||||||
import { Pinecone } from '@pinecone-database/pinecone';
|
|
||||||
import { PDFLoader } from 'langchain/document_loaders/fs/pdf';
|
|
||||||
import { RecursiveCharacterTextSplitter } from 'langchain/text_splitter';
|
|
||||||
import md5 from 'md5';
|
|
||||||
|
|
||||||
import { getEmbeddings } from './embeddings';
|
|
||||||
|
|
||||||
let pc: Pinecone | null = null;
|
|
||||||
|
|
||||||
// export type PDFPage = {
|
|
||||||
// pageContent: string;
|
|
||||||
// metadata: {
|
|
||||||
// source: string;
|
|
||||||
// pdf: {
|
|
||||||
// version: string;
|
|
||||||
// info: {
|
|
||||||
// pdfformatversion: string;
|
|
||||||
// isacroformpresent: boolean;
|
|
||||||
// isxfapresent: boolean;
|
|
||||||
// creator: string;
|
|
||||||
// producer: string;
|
|
||||||
// ceationdate: string;
|
|
||||||
// moddate: string;
|
|
||||||
// };
|
|
||||||
// metadata: null;
|
|
||||||
// totalPages: number;
|
|
||||||
// };
|
|
||||||
// loc: {
|
|
||||||
// pageNumber: number;
|
|
||||||
// };
|
|
||||||
// };
|
|
||||||
// };
|
|
||||||
|
|
||||||
export type PDFPage = unknown;
|
|
||||||
export const getPineconeClient = () => {
|
|
||||||
if (!pc) {
|
|
||||||
pc = new Pinecone({
|
|
||||||
apiKey: process.env.PINECONE_API_KEY!,
|
|
||||||
environment: process.env.PINECONE_ENV!,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return pc;
|
|
||||||
};
|
|
||||||
|
|
||||||
export async function loadFileIntoPinecone(file: string) {
|
|
||||||
if (!file) {
|
|
||||||
throw new Error('No file provided');
|
|
||||||
}
|
|
||||||
|
|
||||||
const loader = new PDFLoader(file);
|
|
||||||
const pages: PDFPage[] = await loader.load();
|
|
||||||
|
|
||||||
const documents = await Promise.all(pages.map(prepareDocument));
|
|
||||||
|
|
||||||
const vectors = await Promise.all(documents.flat().map(embedDocuments));
|
|
||||||
|
|
||||||
const client = getPineconeClient();
|
|
||||||
const pineconeIndex = client.index('documenso-chat-with-pdf-test');
|
|
||||||
|
|
||||||
try {
|
|
||||||
await pineconeIndex.upsert(vectors);
|
|
||||||
} catch (error) {
|
|
||||||
console.error('There was an error upserting vectors: ', error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function embedDocuments(doc) {
|
|
||||||
try {
|
|
||||||
const embeddings = await getEmbeddings(doc.pageContent);
|
|
||||||
const hash = md5(doc.pageContent);
|
|
||||||
|
|
||||||
return {
|
|
||||||
id: hash,
|
|
||||||
values: embeddings,
|
|
||||||
metadata: {
|
|
||||||
text: doc.metadata.text,
|
|
||||||
pageNumber: doc.metadata.pageNumber,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
} catch (error) {
|
|
||||||
console.error('There was an error embedding documents: ', error);
|
|
||||||
throw new Error('There was an error embedding documents');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export const truncateStringByBytes = (str: string, numBytes: number) => {
|
|
||||||
const encoder = new TextEncoder();
|
|
||||||
|
|
||||||
return new TextDecoder('utf-8').decode(encoder.encode(str).slice(0, numBytes));
|
|
||||||
};
|
|
||||||
|
|
||||||
async function prepareDocument(page: PDFPage) {
|
|
||||||
let { pageContent, metadata } = page;
|
|
||||||
pageContent = pageContent.replace(/\n/g, '');
|
|
||||||
|
|
||||||
const splitter = new RecursiveCharacterTextSplitter();
|
|
||||||
const docs = await splitter.splitDocuments([
|
|
||||||
{
|
|
||||||
pageContent,
|
|
||||||
metadata: {
|
|
||||||
pageNumber: metadata.loc.pageNumber,
|
|
||||||
text: truncateStringByBytes(pageContent, 36000),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
]);
|
|
||||||
|
|
||||||
return docs;
|
|
||||||
}
|
|
||||||
|
|
||||||
function convertToAscii(input: string) {
|
|
||||||
return input.replace(/[^\x00-\x7F]/g, '');
|
|
||||||
}
|
|
||||||
@ -1,4 +1,3 @@
|
|||||||
/// <reference types="./stripe.d.ts" />
|
|
||||||
import Stripe from 'stripe';
|
import Stripe from 'stripe';
|
||||||
|
|
||||||
export const stripe = new Stripe(process.env.NEXT_PRIVATE_STRIPE_API_KEY ?? '', {
|
export const stripe = new Stripe(process.env.NEXT_PRIVATE_STRIPE_API_KEY ?? '', {
|
||||||
|
|||||||
7
packages/lib/server-only/stripe/stripe.d.ts
vendored
7
packages/lib/server-only/stripe/stripe.d.ts
vendored
@ -1,7 +0,0 @@
|
|||||||
declare module 'stripe' {
|
|
||||||
namespace Stripe {
|
|
||||||
interface Product {
|
|
||||||
features?: Array<{ name: string }>;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -7,7 +7,7 @@ export type GetSubscriptionByUserIdOptions = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const getSubscriptionByUserId = async ({ userId }: GetSubscriptionByUserIdOptions) => {
|
export const getSubscriptionByUserId = async ({ userId }: GetSubscriptionByUserIdOptions) => {
|
||||||
return await prisma.subscription.findFirst({
|
return prisma.subscription.findFirst({
|
||||||
where: {
|
where: {
|
||||||
userId,
|
userId,
|
||||||
},
|
},
|
||||||
|
|||||||
@ -1,25 +0,0 @@
|
|||||||
import { prisma } from '@documenso/prisma';
|
|
||||||
|
|
||||||
export type DeleteUserOptions = {
|
|
||||||
email: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
export const deleteUser = async ({ email }: DeleteUserOptions) => {
|
|
||||||
const user = await prisma.user.findFirst({
|
|
||||||
where: {
|
|
||||||
email: {
|
|
||||||
contains: email,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!user) {
|
|
||||||
throw new Error(`User with email ${email} not found`);
|
|
||||||
}
|
|
||||||
|
|
||||||
return await prisma.user.delete({
|
|
||||||
where: {
|
|
||||||
id: user.id,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
};
|
|
||||||
@ -1,57 +0,0 @@
|
|||||||
import { prisma } from '@documenso/prisma';
|
|
||||||
import { Prisma } from '@documenso/prisma/client';
|
|
||||||
|
|
||||||
type GetAllUsersProps = {
|
|
||||||
username: string;
|
|
||||||
email: string;
|
|
||||||
page: number;
|
|
||||||
perPage: number;
|
|
||||||
};
|
|
||||||
|
|
||||||
export const findUsers = async ({
|
|
||||||
username = '',
|
|
||||||
email = '',
|
|
||||||
page = 1,
|
|
||||||
perPage = 10,
|
|
||||||
}: GetAllUsersProps) => {
|
|
||||||
const whereClause = Prisma.validator<Prisma.UserWhereInput>()({
|
|
||||||
OR: [
|
|
||||||
{
|
|
||||||
name: {
|
|
||||||
contains: username,
|
|
||||||
mode: 'insensitive',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
email: {
|
|
||||||
contains: email,
|
|
||||||
mode: 'insensitive',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
],
|
|
||||||
});
|
|
||||||
|
|
||||||
const [users, count] = await Promise.all([
|
|
||||||
await prisma.user.findMany({
|
|
||||||
include: {
|
|
||||||
Subscription: true,
|
|
||||||
Document: {
|
|
||||||
select: {
|
|
||||||
id: true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
where: whereClause,
|
|
||||||
skip: Math.max(page - 1, 0) * perPage,
|
|
||||||
take: perPage,
|
|
||||||
}),
|
|
||||||
await prisma.user.count({
|
|
||||||
where: whereClause,
|
|
||||||
}),
|
|
||||||
]);
|
|
||||||
|
|
||||||
return {
|
|
||||||
users,
|
|
||||||
totalPages: Math.ceil(count / perPage),
|
|
||||||
};
|
|
||||||
};
|
|
||||||
@ -1,3 +0,0 @@
|
|||||||
export const toHumanPrice = (price: number) => {
|
|
||||||
return Number(price / 100).toFixed(2);
|
|
||||||
};
|
|
||||||
@ -3,6 +3,8 @@ import { match } from 'ts-pattern';
|
|||||||
|
|
||||||
import { DocumentDataType } from '@documenso/prisma/client';
|
import { DocumentDataType } from '@documenso/prisma/client';
|
||||||
|
|
||||||
|
import { getPresignGetUrl } from './server-actions';
|
||||||
|
|
||||||
export type GetFileOptions = {
|
export type GetFileOptions = {
|
||||||
type: DocumentDataType;
|
type: DocumentDataType;
|
||||||
data: string;
|
data: string;
|
||||||
@ -31,8 +33,6 @@ const getFileFromBytes64 = (data: string) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const getFileFromS3 = async (key: string) => {
|
const getFileFromS3 = async (key: string) => {
|
||||||
const { getPresignGetUrl } = await import('./server-actions');
|
|
||||||
|
|
||||||
const { url } = await getPresignGetUrl(key);
|
const { url } = await getPresignGetUrl(key);
|
||||||
|
|
||||||
const response = await fetch(url, {
|
const response = await fetch(url, {
|
||||||
|
|||||||
@ -4,6 +4,7 @@ import { match } from 'ts-pattern';
|
|||||||
import { DocumentDataType } from '@documenso/prisma/client';
|
import { DocumentDataType } from '@documenso/prisma/client';
|
||||||
|
|
||||||
import { createDocumentData } from '../../server-only/document-data/create-document-data';
|
import { createDocumentData } from '../../server-only/document-data/create-document-data';
|
||||||
|
import { getPresignPostUrl } from './server-actions';
|
||||||
|
|
||||||
type File = {
|
type File = {
|
||||||
name: string;
|
name: string;
|
||||||
@ -33,8 +34,6 @@ const putFileInDatabase = async (file: File) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const putFileInS3 = async (file: File) => {
|
const putFileInS3 = async (file: File) => {
|
||||||
const { getPresignPostUrl } = await import('./server-actions');
|
|
||||||
|
|
||||||
const { url, key } = await getPresignPostUrl(file.name, file.type);
|
const { url, key } = await getPresignPostUrl(file.name, file.type);
|
||||||
|
|
||||||
const body = await file.arrayBuffer();
|
const body = await file.arrayBuffer();
|
||||||
|
|||||||
@ -6,6 +6,7 @@ import {
|
|||||||
PutObjectCommand,
|
PutObjectCommand,
|
||||||
S3Client,
|
S3Client,
|
||||||
} from '@aws-sdk/client-s3';
|
} from '@aws-sdk/client-s3';
|
||||||
|
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
|
||||||
import slugify from '@sindresorhus/slugify';
|
import slugify from '@sindresorhus/slugify';
|
||||||
import path from 'node:path';
|
import path from 'node:path';
|
||||||
|
|
||||||
@ -16,8 +17,6 @@ import { alphaid } from '../id';
|
|||||||
export const getPresignPostUrl = async (fileName: string, contentType: string) => {
|
export const getPresignPostUrl = async (fileName: string, contentType: string) => {
|
||||||
const client = getS3Client();
|
const client = getS3Client();
|
||||||
|
|
||||||
const { getSignedUrl } = await import('@aws-sdk/s3-request-presigner');
|
|
||||||
|
|
||||||
const { user } = await getServerComponentSession();
|
const { user } = await getServerComponentSession();
|
||||||
|
|
||||||
// Get the basename and extension for the file
|
// Get the basename and extension for the file
|
||||||
@ -45,8 +44,6 @@ export const getPresignPostUrl = async (fileName: string, contentType: string) =
|
|||||||
export const getAbsolutePresignPostUrl = async (key: string) => {
|
export const getAbsolutePresignPostUrl = async (key: string) => {
|
||||||
const client = getS3Client();
|
const client = getS3Client();
|
||||||
|
|
||||||
const { getSignedUrl } = await import('@aws-sdk/s3-request-presigner');
|
|
||||||
|
|
||||||
const putObjectCommand = new PutObjectCommand({
|
const putObjectCommand = new PutObjectCommand({
|
||||||
Bucket: process.env.NEXT_PRIVATE_UPLOAD_BUCKET,
|
Bucket: process.env.NEXT_PRIVATE_UPLOAD_BUCKET,
|
||||||
Key: key,
|
Key: key,
|
||||||
@ -62,8 +59,6 @@ export const getAbsolutePresignPostUrl = async (key: string) => {
|
|||||||
export const getPresignGetUrl = async (key: string) => {
|
export const getPresignGetUrl = async (key: string) => {
|
||||||
const client = getS3Client();
|
const client = getS3Client();
|
||||||
|
|
||||||
const { getSignedUrl } = await import('@aws-sdk/s3-request-presigner');
|
|
||||||
|
|
||||||
const getObjectCommand = new GetObjectCommand({
|
const getObjectCommand = new GetObjectCommand({
|
||||||
Bucket: process.env.NEXT_PRIVATE_UPLOAD_BUCKET,
|
Bucket: process.env.NEXT_PRIVATE_UPLOAD_BUCKET,
|
||||||
Key: key,
|
Key: key,
|
||||||
|
|||||||
@ -3,6 +3,8 @@ import { match } from 'ts-pattern';
|
|||||||
|
|
||||||
import { DocumentDataType } from '@documenso/prisma/client';
|
import { DocumentDataType } from '@documenso/prisma/client';
|
||||||
|
|
||||||
|
import { getAbsolutePresignPostUrl } from './server-actions';
|
||||||
|
|
||||||
export type UpdateFileOptions = {
|
export type UpdateFileOptions = {
|
||||||
type: DocumentDataType;
|
type: DocumentDataType;
|
||||||
oldData: string;
|
oldData: string;
|
||||||
@ -38,8 +40,6 @@ const updateFileWithBytes64 = (data: string) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const updateFileWithS3 = async (key: string, data: string) => {
|
const updateFileWithS3 = async (key: string, data: string) => {
|
||||||
const { getAbsolutePresignPostUrl } = await import('./server-actions');
|
|
||||||
|
|
||||||
const { url } = await getAbsolutePresignPostUrl(key);
|
const { url } = await getAbsolutePresignPostUrl(key);
|
||||||
|
|
||||||
const response = await fetch(url, {
|
const response = await fetch(url, {
|
||||||
|
|||||||
@ -1,17 +0,0 @@
|
|||||||
/*
|
|
||||||
Warnings:
|
|
||||||
|
|
||||||
- A unique constraint covering the columns `[userId]` on the table `Subscription` will be added. If there are existing duplicate values, this will fail.
|
|
||||||
- Made the column `customerId` on table `Subscription` required. This step will fail if there are existing NULL values in that column.
|
|
||||||
|
|
||||||
*/
|
|
||||||
|
|
||||||
DELETE FROM "Subscription"
|
|
||||||
WHERE "customerId" IS NULL;
|
|
||||||
|
|
||||||
-- AlterTable
|
|
||||||
ALTER TABLE "Subscription" ADD COLUMN "cancelAtPeriodEnd" BOOLEAN NOT NULL DEFAULT false,
|
|
||||||
ALTER COLUMN "customerId" SET NOT NULL;
|
|
||||||
|
|
||||||
-- CreateIndex
|
|
||||||
CREATE UNIQUE INDEX "Subscription_userId_key" ON "Subscription"("userId");
|
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user