mirror of
https://github.com/documenso/documenso.git
synced 2025-11-21 04:01:45 +10:00
Compare commits
9 Commits
feat/unlin
...
b3ed80d721
| Author | SHA1 | Date | |
|---|---|---|---|
| b3ed80d721 | |||
| b3cb750470 | |||
| 1e52493144 | |||
| ab95e80987 | |||
| 1780a5c262 | |||
| cb9bf407f7 | |||
| 9350c53c7d | |||
| ffce7a2c81 | |||
| 353bdce86b |
692
CODE_STYLE.md
Normal file
692
CODE_STYLE.md
Normal file
@ -0,0 +1,692 @@
|
|||||||
|
# Documenso Code Style Guide
|
||||||
|
|
||||||
|
This document captures the code style, patterns, and conventions used in the Documenso codebase. It covers both enforceable rules and subjective "taste" elements that make our code consistent and maintainable.
|
||||||
|
|
||||||
|
## Table of Contents
|
||||||
|
|
||||||
|
1. [General Principles](#general-principles)
|
||||||
|
2. [TypeScript Conventions](#typescript-conventions)
|
||||||
|
3. [Imports & Dependencies](#imports--dependencies)
|
||||||
|
4. [Functions & Methods](#functions--methods)
|
||||||
|
5. [React & Components](#react--components)
|
||||||
|
6. [Error Handling](#error-handling)
|
||||||
|
7. [Async/Await Patterns](#asyncawait-patterns)
|
||||||
|
8. [Whitespace & Formatting](#whitespace--formatting)
|
||||||
|
9. [Naming Conventions](#naming-conventions)
|
||||||
|
10. [Pattern Matching](#pattern-matching)
|
||||||
|
11. [Database & Prisma](#database--prisma)
|
||||||
|
12. [TRPC Patterns](#trpc-patterns)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## General Principles
|
||||||
|
|
||||||
|
- **Functional over Object-Oriented**: Prefer functional programming patterns over classes
|
||||||
|
- **Explicit over Implicit**: Be explicit about types, return values, and error cases
|
||||||
|
- **Early Returns**: Use guard clauses and early returns to reduce nesting
|
||||||
|
- **Immutability**: Favor `const` over `let`; avoid mutation where possible
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## TypeScript Conventions
|
||||||
|
|
||||||
|
### Type Definitions
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// ✅ Prefer `type` over `interface`
|
||||||
|
type CreateDocumentOptions = {
|
||||||
|
templateId: number;
|
||||||
|
userId: number;
|
||||||
|
recipients: Recipient[];
|
||||||
|
};
|
||||||
|
|
||||||
|
// ❌ Avoid interfaces unless absolutely necessary
|
||||||
|
interface CreateDocumentOptions {
|
||||||
|
templateId: number;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Type Imports
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// ✅ Use `type` keyword for type-only imports
|
||||||
|
import type { Document, Recipient } from '@prisma/client';
|
||||||
|
import { DocumentStatus } from '@prisma/client';
|
||||||
|
|
||||||
|
// Types in function signatures
|
||||||
|
export const findDocuments = async ({ userId, teamId }: FindDocumentsOptions) => {
|
||||||
|
// ...
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
### Inline Types for Function Parameters
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// ✅ Extract inline types to named types
|
||||||
|
type FinalRecipient = Pick<Recipient, 'name' | 'email' | 'role' | 'authOptions'> & {
|
||||||
|
templateRecipientId: number;
|
||||||
|
fields: Field[];
|
||||||
|
};
|
||||||
|
|
||||||
|
const finalRecipients: FinalRecipient[] = [];
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Imports & Dependencies
|
||||||
|
|
||||||
|
### Import Organization
|
||||||
|
|
||||||
|
Imports should be organized in the following order with blank lines between groups:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// 1. React imports
|
||||||
|
import { useCallback, useEffect, useMemo } from 'react';
|
||||||
|
|
||||||
|
// 2. Third-party library imports (alphabetically)
|
||||||
|
import { zodResolver } from '@hookform/resolvers/zod';
|
||||||
|
import { Trans } from '@lingui/react/macro';
|
||||||
|
import type { Document, Recipient } from '@prisma/client';
|
||||||
|
import { DocumentStatus, RecipientRole } from '@prisma/client';
|
||||||
|
import { match } from 'ts-pattern';
|
||||||
|
|
||||||
|
// 3. Internal package imports (from @documenso/*)
|
||||||
|
import { AppError } from '@documenso/lib/errors/app-error';
|
||||||
|
import { prisma } from '@documenso/prisma';
|
||||||
|
import { Button } from '@documenso/ui/primitives/button';
|
||||||
|
|
||||||
|
// 4. Relative imports
|
||||||
|
import { getTeamById } from '../team/get-team';
|
||||||
|
import type { FindResultResponse } from './types';
|
||||||
|
```
|
||||||
|
|
||||||
|
### Destructuring Imports
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// ✅ Destructure specific exports
|
||||||
|
// ✅ Use type imports for types
|
||||||
|
import type { Document } from '@prisma/client';
|
||||||
|
|
||||||
|
import { Button } from '@documenso/ui/primitives/button';
|
||||||
|
import { Input } from '@documenso/ui/primitives/input';
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Functions & Methods
|
||||||
|
|
||||||
|
### Arrow Functions
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// ✅ Always use arrow functions for functions
|
||||||
|
export const createDocument = async ({
|
||||||
|
userId,
|
||||||
|
title,
|
||||||
|
}: CreateDocumentOptions) => {
|
||||||
|
// ...
|
||||||
|
};
|
||||||
|
|
||||||
|
// ✅ Callbacks and handlers
|
||||||
|
const onSubmit = useCallback(async () => {
|
||||||
|
// ...
|
||||||
|
}, [dependencies]);
|
||||||
|
|
||||||
|
// ❌ Avoid regular function declarations
|
||||||
|
function createDocument() {
|
||||||
|
// ...
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Function Parameters
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// ✅ Use destructured object parameters for multiple params
|
||||||
|
export const findDocuments = async ({
|
||||||
|
userId,
|
||||||
|
teamId,
|
||||||
|
status = ExtendedDocumentStatus.ALL,
|
||||||
|
page = 1,
|
||||||
|
perPage = 10,
|
||||||
|
}: FindDocumentsOptions) => {
|
||||||
|
// ...
|
||||||
|
};
|
||||||
|
|
||||||
|
// ✅ Destructure on separate line when needed
|
||||||
|
const onFormSubmit = form.handleSubmit(onSubmit);
|
||||||
|
|
||||||
|
// ✅ Deconstruct nested properties explicitly
|
||||||
|
const { user } = ctx;
|
||||||
|
const { templateId } = input;
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## React & Components
|
||||||
|
|
||||||
|
### Component Definition
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// ✅ Use const with arrow function
|
||||||
|
export const AddSignersFormPartial = ({
|
||||||
|
documentFlow,
|
||||||
|
recipients,
|
||||||
|
fields,
|
||||||
|
onSubmit,
|
||||||
|
}: AddSignersFormProps) => {
|
||||||
|
// ...
|
||||||
|
};
|
||||||
|
|
||||||
|
// ❌ Never use classes
|
||||||
|
class MyComponent extends React.Component {
|
||||||
|
// ...
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Hooks
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// ✅ Group related hooks together with blank line separation
|
||||||
|
const { _ } = useLingui();
|
||||||
|
const { toast } = useToast();
|
||||||
|
|
||||||
|
const { currentStep, totalSteps, previousStep } = useStep();
|
||||||
|
|
||||||
|
const form = useForm<TFormSchema>({
|
||||||
|
resolver: zodResolver(ZFormSchema),
|
||||||
|
defaultValues: {
|
||||||
|
// ...
|
||||||
|
},
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
### Event Handlers
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// ✅ Use arrow functions with descriptive names
|
||||||
|
const onFormSubmit = async () => {
|
||||||
|
await form.trigger();
|
||||||
|
// ...
|
||||||
|
};
|
||||||
|
|
||||||
|
const onFieldCopy = useCallback(
|
||||||
|
(event?: KeyboardEvent | null) => {
|
||||||
|
event?.preventDefault();
|
||||||
|
// ...
|
||||||
|
},
|
||||||
|
[dependencies],
|
||||||
|
);
|
||||||
|
|
||||||
|
// ✅ Inline handlers for simple operations
|
||||||
|
<Button onClick={() => setOpen(false)}>Close</Button>
|
||||||
|
```
|
||||||
|
|
||||||
|
### State Management
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// ✅ Descriptive state names with auxiliary verbs
|
||||||
|
const [isLoading, setIsLoading] = useState(false);
|
||||||
|
const [hasError, setHasError] = useState(false);
|
||||||
|
const [showAdvancedSettings, setShowAdvancedSettings] = useState(false);
|
||||||
|
|
||||||
|
// ✅ Complex state in single useState when related
|
||||||
|
const [coords, setCoords] = useState({
|
||||||
|
x: 0,
|
||||||
|
y: 0,
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Error Handling
|
||||||
|
|
||||||
|
### Try-Catch Blocks
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// ✅ Use try-catch for operations that might fail
|
||||||
|
try {
|
||||||
|
const document = await getDocumentById({
|
||||||
|
documentId: Number(documentId),
|
||||||
|
userId: user.id,
|
||||||
|
});
|
||||||
|
|
||||||
|
return {
|
||||||
|
status: 200,
|
||||||
|
body: document,
|
||||||
|
};
|
||||||
|
} catch (err) {
|
||||||
|
return {
|
||||||
|
status: 404,
|
||||||
|
body: {
|
||||||
|
message: 'Document not found',
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Throwing Errors
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// ✅ Use AppError for application errors
|
||||||
|
throw new AppError(AppErrorCode.NOT_FOUND, {
|
||||||
|
message: 'Template not found',
|
||||||
|
});
|
||||||
|
|
||||||
|
// ✅ Use descriptive error messages
|
||||||
|
if (!template) {
|
||||||
|
throw new AppError(AppErrorCode.NOT_FOUND, {
|
||||||
|
message: `Template with ID ${templateId} not found`,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Error Parsing on Frontend
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// ✅ Parse errors on the frontend
|
||||||
|
try {
|
||||||
|
await updateOrganisation({ organisationId, data });
|
||||||
|
} catch (err) {
|
||||||
|
const error = AppError.parseError(err);
|
||||||
|
console.error(error);
|
||||||
|
|
||||||
|
toast({
|
||||||
|
title: t`An error occurred`,
|
||||||
|
description: error.message,
|
||||||
|
variant: 'destructive',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Async/Await Patterns
|
||||||
|
|
||||||
|
### Async Function Definitions
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// ✅ Mark async functions clearly
|
||||||
|
export const createDocument = async ({
|
||||||
|
userId,
|
||||||
|
title,
|
||||||
|
}: Options): Promise<Document> => {
|
||||||
|
// ...
|
||||||
|
};
|
||||||
|
|
||||||
|
// ✅ Use await for promises
|
||||||
|
const document = await prisma.document.create({ data });
|
||||||
|
|
||||||
|
// ✅ Use Promise.all for parallel operations
|
||||||
|
const [document, recipients] = await Promise.all([
|
||||||
|
getDocumentById({ documentId }),
|
||||||
|
getRecipientsForDocument({ documentId }),
|
||||||
|
]);
|
||||||
|
```
|
||||||
|
|
||||||
|
### Void for Fire-and-Forget
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// ✅ Use void for intentionally unwaited promises
|
||||||
|
void handleAutoSave();
|
||||||
|
|
||||||
|
// ✅ Or in event handlers
|
||||||
|
onClick={() => void onFormSubmit()}
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Whitespace & Formatting
|
||||||
|
|
||||||
|
### Blank Lines Between Concepts
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// ✅ Blank line after imports
|
||||||
|
import { prisma } from '@documenso/prisma';
|
||||||
|
|
||||||
|
export const findDocuments = async () => {
|
||||||
|
// ...
|
||||||
|
};
|
||||||
|
|
||||||
|
// ✅ Blank line between logical sections
|
||||||
|
const user = await prisma.user.findFirst({ where: { id: userId } });
|
||||||
|
|
||||||
|
let team = null;
|
||||||
|
|
||||||
|
if (teamId !== undefined) {
|
||||||
|
team = await getTeamById({ userId, teamId });
|
||||||
|
}
|
||||||
|
|
||||||
|
// ✅ Blank line before return statements
|
||||||
|
const result = await someOperation();
|
||||||
|
|
||||||
|
return result;
|
||||||
|
```
|
||||||
|
|
||||||
|
### Function/Method Spacing
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// ✅ No blank lines between chained methods in same operation
|
||||||
|
const documents = await prisma.document
|
||||||
|
.findMany({ where: { userId } })
|
||||||
|
.then((docs) => docs.map(maskTokens));
|
||||||
|
|
||||||
|
// ✅ Blank line between different operations
|
||||||
|
const document = await createDocument({ userId });
|
||||||
|
|
||||||
|
await sendDocument({ documentId: document.id });
|
||||||
|
|
||||||
|
return document;
|
||||||
|
```
|
||||||
|
|
||||||
|
### Object and Array Formatting
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// ✅ Multi-line when complex
|
||||||
|
const options = {
|
||||||
|
userId,
|
||||||
|
teamId,
|
||||||
|
status: ExtendedDocumentStatus.ALL,
|
||||||
|
page: 1,
|
||||||
|
};
|
||||||
|
|
||||||
|
// ✅ Single line when simple
|
||||||
|
const coords = { x: 0, y: 0 };
|
||||||
|
|
||||||
|
// ✅ Array items on separate lines when objects
|
||||||
|
const recipients = [
|
||||||
|
{
|
||||||
|
name: 'John',
|
||||||
|
email: 'john@example.com',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'Jane',
|
||||||
|
email: 'jane@example.com',
|
||||||
|
},
|
||||||
|
];
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Naming Conventions
|
||||||
|
|
||||||
|
### Variables
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// ✅ camelCase for variables and functions
|
||||||
|
const documentId = 123;
|
||||||
|
const onSubmit = () => {};
|
||||||
|
|
||||||
|
// ✅ Descriptive names with auxiliary verbs for booleans
|
||||||
|
const isLoading = false;
|
||||||
|
const hasError = false;
|
||||||
|
const canEdit = true;
|
||||||
|
const shouldRender = true;
|
||||||
|
|
||||||
|
// ✅ Prefix with $ for DOM elements
|
||||||
|
const $page = document.querySelector('.page');
|
||||||
|
const $inputRef = useRef<HTMLInputElement>(null);
|
||||||
|
```
|
||||||
|
|
||||||
|
### Types and Schemas
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// ✅ PascalCase for types
|
||||||
|
type CreateDocumentOptions = {
|
||||||
|
userId: number;
|
||||||
|
};
|
||||||
|
|
||||||
|
// ✅ Prefix Zod schemas with Z
|
||||||
|
const ZCreateDocumentSchema = z.object({
|
||||||
|
title: z.string(),
|
||||||
|
});
|
||||||
|
|
||||||
|
// ✅ Prefix type from Zod schema with T
|
||||||
|
type TCreateDocumentSchema = z.infer<typeof ZCreateDocumentSchema>;
|
||||||
|
```
|
||||||
|
|
||||||
|
### Constants
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// ✅ UPPER_SNAKE_CASE for true constants
|
||||||
|
const DEFAULT_DOCUMENT_DATE_FORMAT = 'dd/MM/yyyy';
|
||||||
|
const MAX_FILE_SIZE = 1024 * 1024 * 5;
|
||||||
|
|
||||||
|
// ✅ camelCase for const variables that aren't "constants"
|
||||||
|
const userId = await getUserId();
|
||||||
|
```
|
||||||
|
|
||||||
|
### Functions
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// ✅ Verb-based names for functions
|
||||||
|
const createDocument = async () => {};
|
||||||
|
const findDocuments = async () => {};
|
||||||
|
const updateDocument = async () => {};
|
||||||
|
const deleteDocument = async () => {};
|
||||||
|
|
||||||
|
// ✅ On prefix for event handlers
|
||||||
|
const onSubmit = () => {};
|
||||||
|
const onClick = () => {};
|
||||||
|
const onFieldCopy = () => {}; // 'on' is also acceptable
|
||||||
|
```
|
||||||
|
|
||||||
|
### Clarity Over Brevity
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// ✅ Prefer descriptive names over abbreviations
|
||||||
|
const superLongMethodThatIsCorrect = () => {};
|
||||||
|
const recipientAuthenticationOptions = {};
|
||||||
|
const documentMetadata = {};
|
||||||
|
|
||||||
|
// ❌ Avoid abbreviations that sacrifice clarity
|
||||||
|
const supLongMethThatIsCorrect = () => {};
|
||||||
|
const recipAuthOpts = {};
|
||||||
|
const docMeta = {};
|
||||||
|
|
||||||
|
// ✅ Common abbreviations that are widely understood are acceptable
|
||||||
|
const userId = 123;
|
||||||
|
const htmlElement = document.querySelector('div');
|
||||||
|
const apiResponse = await fetch('/api');
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Pattern Matching
|
||||||
|
|
||||||
|
### Using ts-pattern
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import { match } from 'ts-pattern';
|
||||||
|
|
||||||
|
// ✅ Use match for complex conditionals
|
||||||
|
const result = match(status)
|
||||||
|
.with(ExtendedDocumentStatus.DRAFT, () => ({
|
||||||
|
status: 'draft',
|
||||||
|
}))
|
||||||
|
.with(ExtendedDocumentStatus.PENDING, () => ({
|
||||||
|
status: 'pending',
|
||||||
|
}))
|
||||||
|
.with(ExtendedDocumentStatus.COMPLETED, () => ({
|
||||||
|
status: 'completed',
|
||||||
|
}))
|
||||||
|
.exhaustive();
|
||||||
|
|
||||||
|
// ✅ Use .otherwise() for default case when not exhaustive
|
||||||
|
const value = match(type)
|
||||||
|
.with('text', () => 'Text field')
|
||||||
|
.with('number', () => 'Number field')
|
||||||
|
.otherwise(() => 'Unknown field');
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Database & Prisma
|
||||||
|
|
||||||
|
### Query Structure
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// ✅ Destructure commonly used fields
|
||||||
|
const { id, email, name } = user;
|
||||||
|
|
||||||
|
// ✅ Use select to limit returned fields
|
||||||
|
const user = await prisma.user.findFirst({
|
||||||
|
where: { id: userId },
|
||||||
|
select: {
|
||||||
|
id: true,
|
||||||
|
email: true,
|
||||||
|
name: true,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
// ✅ Use include for relations
|
||||||
|
const document = await prisma.document.findFirst({
|
||||||
|
where: { id: documentId },
|
||||||
|
include: {
|
||||||
|
recipients: true,
|
||||||
|
fields: true,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
### Transactions
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// ✅ Use transactions for related operations
|
||||||
|
return await prisma.$transaction(async (tx) => {
|
||||||
|
const document = await tx.document.create({ data });
|
||||||
|
|
||||||
|
await tx.field.createMany({ data: fieldsData });
|
||||||
|
|
||||||
|
await tx.documentAuditLog.create({ data: auditData });
|
||||||
|
|
||||||
|
return document;
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
### Where Clauses
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// ✅ Build complex where clauses separately
|
||||||
|
const whereClause: Prisma.DocumentWhereInput = {
|
||||||
|
AND: [
|
||||||
|
{ userId: user.id },
|
||||||
|
{ deletedAt: null },
|
||||||
|
{ status: { in: [DocumentStatus.DRAFT, DocumentStatus.PENDING] } },
|
||||||
|
],
|
||||||
|
};
|
||||||
|
|
||||||
|
const documents = await prisma.document.findMany({
|
||||||
|
where: whereClause,
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## TRPC Patterns
|
||||||
|
|
||||||
|
### Router Structure
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// ✅ Destructure context and input at start
|
||||||
|
.query(async ({ input, ctx }) => {
|
||||||
|
const { teamId } = ctx;
|
||||||
|
const { templateId } = input;
|
||||||
|
|
||||||
|
ctx.logger.info({
|
||||||
|
input: { templateId },
|
||||||
|
});
|
||||||
|
|
||||||
|
return await getTemplateById({
|
||||||
|
id: templateId,
|
||||||
|
userId: ctx.user.id,
|
||||||
|
teamId,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
### Request/Response Schemas
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// ✅ Name schemas clearly
|
||||||
|
const ZCreateDocumentRequestSchema = z.object({
|
||||||
|
title: z.string(),
|
||||||
|
recipients: z.array(ZRecipientSchema),
|
||||||
|
});
|
||||||
|
|
||||||
|
const ZCreateDocumentResponseSchema = z.object({
|
||||||
|
documentId: z.number(),
|
||||||
|
status: z.string(),
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
### Error Handling in TRPC
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// ✅ Catch and transform errors appropriately
|
||||||
|
try {
|
||||||
|
const result = await createDocument({ userId, data });
|
||||||
|
|
||||||
|
return result;
|
||||||
|
} catch (err) {
|
||||||
|
return AppError.toRestAPIError(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ✅ Or throw AppError directly
|
||||||
|
if (!template) {
|
||||||
|
throw new AppError(AppErrorCode.NOT_FOUND, {
|
||||||
|
message: 'Template not found',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Additional Patterns
|
||||||
|
|
||||||
|
### Optional Chaining
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// ✅ Use optional chaining for potentially undefined values
|
||||||
|
const email = user?.email;
|
||||||
|
const recipientToken = recipient?.token ?? '';
|
||||||
|
|
||||||
|
// ✅ Use nullish coalescing for defaults
|
||||||
|
const pageSize = perPage ?? 10;
|
||||||
|
const status = documentStatus ?? DocumentStatus.DRAFT;
|
||||||
|
```
|
||||||
|
|
||||||
|
### Array Operations
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// ✅ Use functional array methods
|
||||||
|
const activeRecipients = recipients.filter((r) => r.signingStatus === 'SIGNED');
|
||||||
|
const recipientEmails = recipients.map((r) => r.email);
|
||||||
|
const hasSignedRecipients = recipients.some((r) => r.signingStatus === 'SIGNED');
|
||||||
|
|
||||||
|
// ✅ Use find instead of filter + [0]
|
||||||
|
const recipient = recipients.find((r) => r.id === recipientId);
|
||||||
|
```
|
||||||
|
|
||||||
|
### Conditional Rendering
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// ✅ Use && for conditional rendering
|
||||||
|
{isLoading && <Loader />}
|
||||||
|
|
||||||
|
// ✅ Use ternary for either/or
|
||||||
|
{isLoading ? <Loader /> : <Content />}
|
||||||
|
|
||||||
|
// ✅ Extract complex conditions to variables
|
||||||
|
const shouldShowAdvanced = isAdmin && hasPermission && !isDisabled;
|
||||||
|
{shouldShowAdvanced && <AdvancedSettings />}
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## When in Doubt
|
||||||
|
|
||||||
|
- **Consistency**: Follow the patterns you see in similar files
|
||||||
|
- **Readability**: Favor code that's easy to read over clever one-liners
|
||||||
|
- **Explicitness**: Be explicit rather than implicit
|
||||||
|
- **Whitespace**: Use blank lines to separate logical sections
|
||||||
|
- **Early Returns**: Use guard clauses to reduce nesting
|
||||||
|
- **Functional**: Prefer functional patterns over imperative ones
|
||||||
@ -0,0 +1,218 @@
|
|||||||
|
import { useEffect, useState } from 'react';
|
||||||
|
|
||||||
|
import { zodResolver } from '@hookform/resolvers/zod';
|
||||||
|
import { useLingui } from '@lingui/react/macro';
|
||||||
|
import { Trans } from '@lingui/react/macro';
|
||||||
|
import { OrganisationMemberRole } from '@prisma/client';
|
||||||
|
import type * as DialogPrimitive from '@radix-ui/react-dialog';
|
||||||
|
import { useForm } from 'react-hook-form';
|
||||||
|
import { useNavigate } from 'react-router';
|
||||||
|
import { match } from 'ts-pattern';
|
||||||
|
import { z } from 'zod';
|
||||||
|
|
||||||
|
import { getHighestOrganisationRoleInGroup } from '@documenso/lib/utils/organisations';
|
||||||
|
import { trpc } from '@documenso/trpc/react';
|
||||||
|
import type { TGetAdminOrganisationResponse } from '@documenso/trpc/server/admin-router/get-admin-organisation.types';
|
||||||
|
import { Button } from '@documenso/ui/primitives/button';
|
||||||
|
import {
|
||||||
|
Dialog,
|
||||||
|
DialogContent,
|
||||||
|
DialogDescription,
|
||||||
|
DialogFooter,
|
||||||
|
DialogHeader,
|
||||||
|
DialogTitle,
|
||||||
|
DialogTrigger,
|
||||||
|
} from '@documenso/ui/primitives/dialog';
|
||||||
|
import {
|
||||||
|
Form,
|
||||||
|
FormControl,
|
||||||
|
FormField,
|
||||||
|
FormItem,
|
||||||
|
FormLabel,
|
||||||
|
FormMessage,
|
||||||
|
} from '@documenso/ui/primitives/form/form';
|
||||||
|
import {
|
||||||
|
Select,
|
||||||
|
SelectContent,
|
||||||
|
SelectItem,
|
||||||
|
SelectTrigger,
|
||||||
|
SelectValue,
|
||||||
|
} from '@documenso/ui/primitives/select';
|
||||||
|
import { useToast } from '@documenso/ui/primitives/use-toast';
|
||||||
|
|
||||||
|
export type AdminOrganisationMemberUpdateDialogProps = {
|
||||||
|
trigger?: React.ReactNode;
|
||||||
|
organisationId: string;
|
||||||
|
organisationMember: TGetAdminOrganisationResponse['members'][number];
|
||||||
|
isOwner: boolean;
|
||||||
|
} & Omit<DialogPrimitive.DialogProps, 'children'>;
|
||||||
|
|
||||||
|
const ZUpdateOrganisationMemberFormSchema = z.object({
|
||||||
|
role: z.enum(['OWNER', 'ADMIN', 'MANAGER', 'MEMBER']),
|
||||||
|
});
|
||||||
|
|
||||||
|
type ZUpdateOrganisationMemberSchema = z.infer<typeof ZUpdateOrganisationMemberFormSchema>;
|
||||||
|
|
||||||
|
export const AdminOrganisationMemberUpdateDialog = ({
|
||||||
|
trigger,
|
||||||
|
organisationId,
|
||||||
|
organisationMember,
|
||||||
|
isOwner,
|
||||||
|
...props
|
||||||
|
}: AdminOrganisationMemberUpdateDialogProps) => {
|
||||||
|
const [open, setOpen] = useState(false);
|
||||||
|
|
||||||
|
const { t } = useLingui();
|
||||||
|
const { toast } = useToast();
|
||||||
|
const navigate = useNavigate();
|
||||||
|
|
||||||
|
// Determine the current role value for the form
|
||||||
|
const currentRoleValue = isOwner
|
||||||
|
? 'OWNER'
|
||||||
|
: getHighestOrganisationRoleInGroup(
|
||||||
|
organisationMember.organisationGroupMembers.map((ogm) => ogm.group),
|
||||||
|
);
|
||||||
|
const organisationMemberName = organisationMember.user.name ?? organisationMember.user.email;
|
||||||
|
|
||||||
|
const form = useForm<ZUpdateOrganisationMemberSchema>({
|
||||||
|
resolver: zodResolver(ZUpdateOrganisationMemberFormSchema),
|
||||||
|
defaultValues: {
|
||||||
|
role: currentRoleValue,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const { mutateAsync: updateOrganisationMemberRole } =
|
||||||
|
trpc.admin.organisationMember.updateRole.useMutation();
|
||||||
|
|
||||||
|
const onFormSubmit = async ({ role }: ZUpdateOrganisationMemberSchema) => {
|
||||||
|
try {
|
||||||
|
await updateOrganisationMemberRole({
|
||||||
|
organisationId,
|
||||||
|
userId: organisationMember.userId,
|
||||||
|
role,
|
||||||
|
});
|
||||||
|
|
||||||
|
const roleLabel = match(role)
|
||||||
|
.with('OWNER', () => t`Owner`)
|
||||||
|
.with(OrganisationMemberRole.ADMIN, () => t`Admin`)
|
||||||
|
.with(OrganisationMemberRole.MANAGER, () => t`Manager`)
|
||||||
|
.with(OrganisationMemberRole.MEMBER, () => t`Member`)
|
||||||
|
.exhaustive();
|
||||||
|
|
||||||
|
toast({
|
||||||
|
title: t`Success`,
|
||||||
|
description:
|
||||||
|
role === 'OWNER'
|
||||||
|
? t`Ownership transferred to ${organisationMemberName}.`
|
||||||
|
: t`Updated ${organisationMemberName} to ${roleLabel}.`,
|
||||||
|
duration: 5000,
|
||||||
|
});
|
||||||
|
|
||||||
|
setOpen(false);
|
||||||
|
|
||||||
|
// Refresh the page to show updated data
|
||||||
|
await navigate(0);
|
||||||
|
} catch (err) {
|
||||||
|
console.error(err);
|
||||||
|
|
||||||
|
toast({
|
||||||
|
title: t`An unknown error occurred`,
|
||||||
|
description: t`We encountered an unknown error while attempting to update this organisation member. Please try again later.`,
|
||||||
|
variant: 'destructive',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!open) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
form.reset({
|
||||||
|
role: currentRoleValue,
|
||||||
|
});
|
||||||
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||||
|
}, [open, currentRoleValue, form]);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Dialog
|
||||||
|
{...props}
|
||||||
|
open={open}
|
||||||
|
onOpenChange={(value) => !form.formState.isSubmitting && setOpen(value)}
|
||||||
|
>
|
||||||
|
<DialogTrigger onClick={(e) => e.stopPropagation()} asChild>
|
||||||
|
{trigger ?? (
|
||||||
|
<Button variant="secondary">
|
||||||
|
<Trans>Update role</Trans>
|
||||||
|
</Button>
|
||||||
|
)}
|
||||||
|
</DialogTrigger>
|
||||||
|
|
||||||
|
<DialogContent position="center">
|
||||||
|
<DialogHeader>
|
||||||
|
<DialogTitle>
|
||||||
|
<Trans>Update organisation member</Trans>
|
||||||
|
</DialogTitle>
|
||||||
|
|
||||||
|
<DialogDescription className="mt-4">
|
||||||
|
<Trans>
|
||||||
|
You are currently updating{' '}
|
||||||
|
<span className="font-bold">{organisationMemberName}.</span>
|
||||||
|
</Trans>
|
||||||
|
</DialogDescription>
|
||||||
|
</DialogHeader>
|
||||||
|
|
||||||
|
<Form {...form}>
|
||||||
|
<form onSubmit={form.handleSubmit(onFormSubmit)}>
|
||||||
|
<fieldset className="flex h-full flex-col" disabled={form.formState.isSubmitting}>
|
||||||
|
<FormField
|
||||||
|
control={form.control}
|
||||||
|
name="role"
|
||||||
|
render={({ field }) => (
|
||||||
|
<FormItem className="w-full">
|
||||||
|
<FormLabel required>
|
||||||
|
<Trans>Role</Trans>
|
||||||
|
</FormLabel>
|
||||||
|
<FormControl>
|
||||||
|
<Select {...field} onValueChange={field.onChange}>
|
||||||
|
<SelectTrigger className="text-muted-foreground">
|
||||||
|
<SelectValue />
|
||||||
|
</SelectTrigger>
|
||||||
|
|
||||||
|
<SelectContent className="w-full" position="popper">
|
||||||
|
<SelectItem value="OWNER">
|
||||||
|
<Trans>Owner</Trans>
|
||||||
|
</SelectItem>
|
||||||
|
<SelectItem value={OrganisationMemberRole.ADMIN}>
|
||||||
|
<Trans>Admin</Trans>
|
||||||
|
</SelectItem>
|
||||||
|
<SelectItem value={OrganisationMemberRole.MANAGER}>
|
||||||
|
<Trans>Manager</Trans>
|
||||||
|
</SelectItem>
|
||||||
|
<SelectItem value={OrganisationMemberRole.MEMBER}>
|
||||||
|
<Trans>Member</Trans>
|
||||||
|
</SelectItem>
|
||||||
|
</SelectContent>
|
||||||
|
</Select>
|
||||||
|
</FormControl>
|
||||||
|
<FormMessage />
|
||||||
|
</FormItem>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
|
||||||
|
<DialogFooter className="mt-4">
|
||||||
|
<Button type="button" variant="secondary" onClick={() => setOpen(false)}>
|
||||||
|
<Trans>Cancel</Trans>
|
||||||
|
</Button>
|
||||||
|
|
||||||
|
<Button type="submit" loading={form.formState.isSubmitting}>
|
||||||
|
<Trans>Update</Trans>
|
||||||
|
</Button>
|
||||||
|
</DialogFooter>
|
||||||
|
</fieldset>
|
||||||
|
</form>
|
||||||
|
</Form>
|
||||||
|
</DialogContent>
|
||||||
|
</Dialog>
|
||||||
|
);
|
||||||
|
};
|
||||||
@ -34,6 +34,7 @@ import { Input } from '@documenso/ui/primitives/input';
|
|||||||
import { Tooltip, TooltipContent, TooltipTrigger } from '@documenso/ui/primitives/tooltip';
|
import { Tooltip, TooltipContent, TooltipTrigger } from '@documenso/ui/primitives/tooltip';
|
||||||
import { useToast } from '@documenso/ui/primitives/use-toast';
|
import { useToast } from '@documenso/ui/primitives/use-toast';
|
||||||
|
|
||||||
|
import { AdminOrganisationMemberUpdateDialog } from '~/components/dialogs/admin-organisation-member-update-dialog';
|
||||||
import { GenericErrorLayout } from '~/components/general/generic-error-layout';
|
import { GenericErrorLayout } from '~/components/general/generic-error-layout';
|
||||||
import { SettingsHeader } from '~/components/general/settings-header';
|
import { SettingsHeader } from '~/components/general/settings-header';
|
||||||
|
|
||||||
@ -71,23 +72,6 @@ export default function OrganisationGroupSettingsPage({ params }: Route.Componen
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const { mutateAsync: promoteToOwner, isPending: isPromotingToOwner } =
|
|
||||||
trpc.admin.organisationMember.promoteToOwner.useMutation({
|
|
||||||
onSuccess: () => {
|
|
||||||
toast({
|
|
||||||
title: t`Success`,
|
|
||||||
description: t`Member promoted to owner successfully`,
|
|
||||||
});
|
|
||||||
},
|
|
||||||
onError: () => {
|
|
||||||
toast({
|
|
||||||
title: t`Error`,
|
|
||||||
description: t`We couldn't promote the member to owner. Please try again.`,
|
|
||||||
variant: 'destructive',
|
|
||||||
});
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
const teamsColumns = useMemo(() => {
|
const teamsColumns = useMemo(() => {
|
||||||
return [
|
return [
|
||||||
{
|
{
|
||||||
@ -120,23 +104,24 @@ export default function OrganisationGroupSettingsPage({ params }: Route.Componen
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
header: t`Actions`,
|
header: t`Actions`,
|
||||||
cell: ({ row }) => (
|
cell: ({ row }) => {
|
||||||
<div className="flex justify-end space-x-2">
|
const isOwner = row.original.userId === organisation?.ownerUserId;
|
||||||
<Button
|
|
||||||
variant="outline"
|
return (
|
||||||
disabled={row.original.userId === organisation?.ownerUserId}
|
<div className="flex justify-end space-x-2">
|
||||||
loading={isPromotingToOwner}
|
<AdminOrganisationMemberUpdateDialog
|
||||||
onClick={async () =>
|
trigger={
|
||||||
promoteToOwner({
|
<Button variant="outline">
|
||||||
organisationId,
|
<Trans>Update role</Trans>
|
||||||
userId: row.original.userId,
|
</Button>
|
||||||
})
|
}
|
||||||
}
|
organisationId={organisationId}
|
||||||
>
|
organisationMember={row.original}
|
||||||
<Trans>Promote to owner</Trans>
|
isOwner={isOwner}
|
||||||
</Button>
|
/>
|
||||||
</div>
|
</div>
|
||||||
),
|
);
|
||||||
|
},
|
||||||
},
|
},
|
||||||
] satisfies DataTableColumnDef<TGetAdminOrganisationResponse['members'][number]>[];
|
] satisfies DataTableColumnDef<TGetAdminOrganisationResponse['members'][number]>[];
|
||||||
}, [organisation]);
|
}, [organisation]);
|
||||||
|
|||||||
13
package-lock.json
generated
13
package-lock.json
generated
@ -19,6 +19,7 @@
|
|||||||
"inngest-cli": "^0.29.1",
|
"inngest-cli": "^0.29.1",
|
||||||
"luxon": "^3.5.0",
|
"luxon": "^3.5.0",
|
||||||
"mupdf": "^1.0.0",
|
"mupdf": "^1.0.0",
|
||||||
|
"pdf2json": "^4.0.0",
|
||||||
"react": "^18",
|
"react": "^18",
|
||||||
"typescript": "5.6.2",
|
"typescript": "5.6.2",
|
||||||
"zod": "3.24.1"
|
"zod": "3.24.1"
|
||||||
@ -27198,6 +27199,18 @@
|
|||||||
"dev": true,
|
"dev": true,
|
||||||
"license": "MIT"
|
"license": "MIT"
|
||||||
},
|
},
|
||||||
|
"node_modules/pdf2json": {
|
||||||
|
"version": "4.0.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/pdf2json/-/pdf2json-4.0.0.tgz",
|
||||||
|
"integrity": "sha512-WkezNsLK8sGpuFC7+PPP0DsXROwdoOxmXPBTtUWWkCwCi/Vi97MRC52Ly6FWIJjOKIywpm/L2oaUgSrmtU+7ZQ==",
|
||||||
|
"license": "Apache-2.0",
|
||||||
|
"bin": {
|
||||||
|
"pdf2json": "bin/pdf2json.js"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">=20.18.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/pdfjs-dist": {
|
"node_modules/pdfjs-dist": {
|
||||||
"version": "3.11.174",
|
"version": "3.11.174",
|
||||||
"resolved": "https://registry.npmjs.org/pdfjs-dist/-/pdfjs-dist-3.11.174.tgz",
|
"resolved": "https://registry.npmjs.org/pdfjs-dist/-/pdfjs-dist-3.11.174.tgz",
|
||||||
|
|||||||
@ -74,6 +74,7 @@
|
|||||||
"inngest-cli": "^0.29.1",
|
"inngest-cli": "^0.29.1",
|
||||||
"luxon": "^3.5.0",
|
"luxon": "^3.5.0",
|
||||||
"mupdf": "^1.0.0",
|
"mupdf": "^1.0.0",
|
||||||
|
"pdf2json": "^4.0.0",
|
||||||
"react": "^18",
|
"react": "^18",
|
||||||
"typescript": "5.6.2",
|
"typescript": "5.6.2",
|
||||||
"zod": "3.24.1"
|
"zod": "3.24.1"
|
||||||
|
|||||||
@ -68,15 +68,29 @@ test('[ADMIN]: promote member to owner', async ({ page }) => {
|
|||||||
// Test promoting a MEMBER to owner
|
// Test promoting a MEMBER to owner
|
||||||
const memberRow = page.getByRole('row', { name: memberUser.email });
|
const memberRow = page.getByRole('row', { name: memberUser.email });
|
||||||
|
|
||||||
// Find and click the "Promote to owner" button for the member
|
// Find and click the "Update role" button for the member
|
||||||
const promoteButton = memberRow.getByRole('button', { name: 'Promote to owner' });
|
const updateRoleButton = memberRow.getByRole('button', {
|
||||||
await expect(promoteButton).toBeVisible();
|
name: 'Update role',
|
||||||
await expect(promoteButton).not.toBeDisabled();
|
});
|
||||||
|
await expect(updateRoleButton).toBeVisible();
|
||||||
|
await expect(updateRoleButton).not.toBeDisabled();
|
||||||
|
|
||||||
await promoteButton.click();
|
await updateRoleButton.click();
|
||||||
|
|
||||||
// Verify success toast appears
|
// Wait for dialog to open and select Owner role
|
||||||
await expect(page.getByText('Member promoted to owner successfully').first()).toBeVisible();
|
await expect(page.getByRole('dialog')).toBeVisible();
|
||||||
|
|
||||||
|
// Find and click the select trigger - it's a button with role="combobox"
|
||||||
|
await page.getByRole('dialog').locator('button[role="combobox"]').click();
|
||||||
|
|
||||||
|
// Select "Owner" from the dropdown options
|
||||||
|
await page.getByRole('option', { name: 'Owner' }).click();
|
||||||
|
|
||||||
|
// Click Update button
|
||||||
|
await page.getByRole('dialog').getByRole('button', { name: 'Update' }).click();
|
||||||
|
|
||||||
|
// Wait for dialog to close (indicates success)
|
||||||
|
await expect(page.getByRole('dialog')).not.toBeVisible({ timeout: 10_000 });
|
||||||
|
|
||||||
// Reload the page to see the changes
|
// Reload the page to see the changes
|
||||||
await page.reload();
|
await page.reload();
|
||||||
@ -89,12 +103,18 @@ test('[ADMIN]: promote member to owner', async ({ page }) => {
|
|||||||
const previousOwnerRow = page.getByRole('row', { name: ownerUser.email });
|
const previousOwnerRow = page.getByRole('row', { name: ownerUser.email });
|
||||||
await expect(previousOwnerRow.getByRole('status').filter({ hasText: 'Owner' })).not.toBeVisible();
|
await expect(previousOwnerRow.getByRole('status').filter({ hasText: 'Owner' })).not.toBeVisible();
|
||||||
|
|
||||||
// Verify that the promote button is now disabled for the new owner
|
// Verify that the Update role button exists for the new owner and shows Owner as current role
|
||||||
const newOwnerPromoteButton = newOwnerRow.getByRole('button', { name: 'Promote to owner' });
|
const newOwnerUpdateButton = newOwnerRow.getByRole('button', {
|
||||||
await expect(newOwnerPromoteButton).toBeDisabled();
|
name: 'Update role',
|
||||||
|
});
|
||||||
|
await expect(newOwnerUpdateButton).toBeVisible();
|
||||||
|
|
||||||
// Test that we can't promote the current owner (button should be disabled)
|
// Verify clicking it shows the dialog with Owner already selected
|
||||||
await expect(newOwnerPromoteButton).toHaveAttribute('disabled');
|
await newOwnerUpdateButton.click();
|
||||||
|
await expect(page.getByRole('dialog')).toBeVisible();
|
||||||
|
|
||||||
|
// Close the dialog without making changes
|
||||||
|
await page.getByRole('button', { name: 'Cancel' }).click();
|
||||||
});
|
});
|
||||||
|
|
||||||
test('[ADMIN]: promote manager to owner', async ({ page }) => {
|
test('[ADMIN]: promote manager to owner', async ({ page }) => {
|
||||||
@ -130,10 +150,26 @@ test('[ADMIN]: promote manager to owner', async ({ page }) => {
|
|||||||
|
|
||||||
// Promote the manager to owner
|
// Promote the manager to owner
|
||||||
const managerRow = page.getByRole('row', { name: managerUser.email });
|
const managerRow = page.getByRole('row', { name: managerUser.email });
|
||||||
const promoteButton = managerRow.getByRole('button', { name: 'Promote to owner' });
|
const updateRoleButton = managerRow.getByRole('button', {
|
||||||
|
name: 'Update role',
|
||||||
|
});
|
||||||
|
|
||||||
await promoteButton.click();
|
await updateRoleButton.click();
|
||||||
await expect(page.getByText('Member promoted to owner successfully').first()).toBeVisible();
|
|
||||||
|
// Wait for dialog to open and select Owner role
|
||||||
|
await expect(page.getByRole('dialog')).toBeVisible();
|
||||||
|
|
||||||
|
// Find and click the select trigger - it's a button with role="combobox"
|
||||||
|
await page.getByRole('dialog').locator('button[role="combobox"]').click();
|
||||||
|
|
||||||
|
// Select "Owner" from the dropdown options
|
||||||
|
await page.getByRole('option', { name: 'Owner' }).click();
|
||||||
|
|
||||||
|
// Click Update button
|
||||||
|
await page.getByRole('dialog').getByRole('button', { name: 'Update' }).click();
|
||||||
|
|
||||||
|
// Wait for dialog to close (indicates success)
|
||||||
|
await expect(page.getByRole('dialog')).not.toBeVisible({ timeout: 10_000 });
|
||||||
|
|
||||||
// Reload and verify the change
|
// Reload and verify the change
|
||||||
await page.reload();
|
await page.reload();
|
||||||
@ -173,14 +209,27 @@ test('[ADMIN]: promote admin member to owner', async ({ page }) => {
|
|||||||
|
|
||||||
// Promote the admin member to owner
|
// Promote the admin member to owner
|
||||||
const adminMemberRow = page.getByRole('row', { name: adminMemberUser.email });
|
const adminMemberRow = page.getByRole('row', { name: adminMemberUser.email });
|
||||||
const promoteButton = adminMemberRow.getByRole('button', { name: 'Promote to owner' });
|
const updateRoleButton = adminMemberRow.getByRole('button', {
|
||||||
|
name: 'Update role',
|
||||||
await promoteButton.click();
|
|
||||||
|
|
||||||
await expect(page.getByText('Member promoted to owner successfully').first()).toBeVisible({
|
|
||||||
timeout: 10_000,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
await updateRoleButton.click();
|
||||||
|
|
||||||
|
// Wait for dialog to open and select Owner role
|
||||||
|
await expect(page.getByRole('dialog')).toBeVisible();
|
||||||
|
|
||||||
|
// Find and click the select trigger - it's a button with role="combobox"
|
||||||
|
await page.getByRole('dialog').locator('button[role="combobox"]').click();
|
||||||
|
|
||||||
|
// Select "Owner" from the dropdown options
|
||||||
|
await page.getByRole('option', { name: 'Owner' }).click();
|
||||||
|
|
||||||
|
// Click Update button
|
||||||
|
await page.getByRole('dialog').getByRole('button', { name: 'Update' }).click();
|
||||||
|
|
||||||
|
// Wait for dialog to close (indicates success)
|
||||||
|
await expect(page.getByRole('dialog')).not.toBeVisible({ timeout: 10_000 });
|
||||||
|
|
||||||
// Reload and verify the change
|
// Reload and verify the change
|
||||||
await page.reload();
|
await page.reload();
|
||||||
await expect(adminMemberRow.getByRole('status').filter({ hasText: 'Owner' })).toBeVisible();
|
await expect(adminMemberRow.getByRole('status').filter({ hasText: 'Owner' })).toBeVisible();
|
||||||
@ -249,11 +298,25 @@ test('[ADMIN]: verify role hierarchy after promotion', async ({ page }) => {
|
|||||||
await expect(memberRow.getByRole('status').filter({ hasText: 'Owner' })).not.toBeVisible();
|
await expect(memberRow.getByRole('status').filter({ hasText: 'Owner' })).not.toBeVisible();
|
||||||
|
|
||||||
// Promote member to owner
|
// Promote member to owner
|
||||||
const promoteButton = memberRow.getByRole('button', { name: 'Promote to owner' });
|
const updateRoleButton = memberRow.getByRole('button', {
|
||||||
await promoteButton.click();
|
name: 'Update role',
|
||||||
await expect(page.getByText('Member promoted to owner successfully').first()).toBeVisible({
|
|
||||||
timeout: 10_000,
|
|
||||||
});
|
});
|
||||||
|
await updateRoleButton.click();
|
||||||
|
|
||||||
|
// Wait for dialog to open and select Owner role
|
||||||
|
await expect(page.getByRole('dialog')).toBeVisible();
|
||||||
|
|
||||||
|
// Find and click the select trigger - it's a button with role="combobox"
|
||||||
|
await page.getByRole('dialog').locator('button[role="combobox"]').click();
|
||||||
|
|
||||||
|
// Select "Owner" from the dropdown options
|
||||||
|
await page.getByRole('option', { name: 'Owner' }).click();
|
||||||
|
|
||||||
|
// Click Update button
|
||||||
|
await page.getByRole('dialog').getByRole('button', { name: 'Update' }).click();
|
||||||
|
|
||||||
|
// Wait for dialog to close (indicates success)
|
||||||
|
await expect(page.getByRole('dialog')).not.toBeVisible({ timeout: 10_000 });
|
||||||
|
|
||||||
// Reload page to see updated state
|
// Reload page to see updated state
|
||||||
await page.reload();
|
await page.reload();
|
||||||
@ -262,9 +325,11 @@ test('[ADMIN]: verify role hierarchy after promotion', async ({ page }) => {
|
|||||||
memberRow = page.getByRole('row', { name: memberUser.email });
|
memberRow = page.getByRole('row', { name: memberUser.email });
|
||||||
await expect(memberRow.getByRole('status').filter({ hasText: 'Owner' })).toBeVisible();
|
await expect(memberRow.getByRole('status').filter({ hasText: 'Owner' })).toBeVisible();
|
||||||
|
|
||||||
// Verify the promote button is now disabled for the new owner
|
// Verify the Update role button exists and shows Owner as current role
|
||||||
const newOwnerPromoteButton = memberRow.getByRole('button', { name: 'Promote to owner' });
|
const newOwnerUpdateButton = memberRow.getByRole('button', {
|
||||||
await expect(newOwnerPromoteButton).toBeDisabled();
|
name: 'Update role',
|
||||||
|
});
|
||||||
|
await expect(newOwnerUpdateButton).toBeVisible();
|
||||||
|
|
||||||
// Sign in as the newly promoted user to verify they have owner permissions
|
// Sign in as the newly promoted user to verify they have owner permissions
|
||||||
await apiSignin({
|
await apiSignin({
|
||||||
@ -336,28 +401,56 @@ test('[ADMIN]: multiple promotions in sequence', async ({ page }) => {
|
|||||||
|
|
||||||
// First promotion: Member 1 becomes owner
|
// First promotion: Member 1 becomes owner
|
||||||
let member1Row = page.getByRole('row', { name: member1User.email });
|
let member1Row = page.getByRole('row', { name: member1User.email });
|
||||||
let promoteButton1 = member1Row.getByRole('button', { name: 'Promote to owner' });
|
let updateRoleButton1 = member1Row.getByRole('button', {
|
||||||
await promoteButton1.click();
|
name: 'Update role',
|
||||||
await expect(page.getByText('Member promoted to owner successfully').first()).toBeVisible({
|
|
||||||
timeout: 10_000,
|
|
||||||
});
|
});
|
||||||
|
await updateRoleButton1.click();
|
||||||
|
|
||||||
|
// Wait for dialog to open and select Owner role
|
||||||
|
await expect(page.getByRole('dialog')).toBeVisible();
|
||||||
|
|
||||||
|
// Find and click the select trigger - it's a button with role="combobox"
|
||||||
|
await page.getByRole('dialog').locator('button[role="combobox"]').click();
|
||||||
|
|
||||||
|
// Select "Owner" from the dropdown options
|
||||||
|
await page.getByRole('option', { name: 'Owner' }).click();
|
||||||
|
|
||||||
|
// Click Update button
|
||||||
|
await page.getByRole('dialog').getByRole('button', { name: 'Update' }).click();
|
||||||
|
|
||||||
|
// Wait for dialog to close (indicates success)
|
||||||
|
await expect(page.getByRole('dialog')).not.toBeVisible({ timeout: 10_000 });
|
||||||
|
|
||||||
await page.reload();
|
await page.reload();
|
||||||
|
|
||||||
// Verify Member 1 is now owner and button is disabled
|
// Verify Member 1 is now owner
|
||||||
member1Row = page.getByRole('row', { name: member1User.email });
|
member1Row = page.getByRole('row', { name: member1User.email });
|
||||||
await expect(member1Row.getByRole('status').filter({ hasText: 'Owner' })).toBeVisible();
|
await expect(member1Row.getByRole('status').filter({ hasText: 'Owner' })).toBeVisible();
|
||||||
promoteButton1 = member1Row.getByRole('button', { name: 'Promote to owner' });
|
updateRoleButton1 = member1Row.getByRole('button', { name: 'Update role' });
|
||||||
await expect(promoteButton1).toBeDisabled();
|
await expect(updateRoleButton1).toBeVisible();
|
||||||
|
|
||||||
// Second promotion: Member 2 becomes the new owner
|
// Second promotion: Member 2 becomes the new owner
|
||||||
const member2Row = page.getByRole('row', { name: member2User.email });
|
const member2Row = page.getByRole('row', { name: member2User.email });
|
||||||
const promoteButton2 = member2Row.getByRole('button', { name: 'Promote to owner' });
|
const updateRoleButton2 = member2Row.getByRole('button', {
|
||||||
await expect(promoteButton2).not.toBeDisabled();
|
name: 'Update role',
|
||||||
await promoteButton2.click();
|
|
||||||
await expect(page.getByText('Member promoted to owner successfully').first()).toBeVisible({
|
|
||||||
timeout: 10_000,
|
|
||||||
});
|
});
|
||||||
|
await expect(updateRoleButton2).toBeVisible();
|
||||||
|
await updateRoleButton2.click();
|
||||||
|
|
||||||
|
// Wait for dialog to open and select Owner role
|
||||||
|
await expect(page.getByRole('dialog')).toBeVisible();
|
||||||
|
|
||||||
|
// Find and click the select trigger - it's a button with role="combobox"
|
||||||
|
await page.getByRole('dialog').locator('button[role="combobox"]').click();
|
||||||
|
|
||||||
|
// Select "Owner" from the dropdown options
|
||||||
|
await page.getByRole('option', { name: 'Owner' }).click();
|
||||||
|
|
||||||
|
// Click Update button
|
||||||
|
await page.getByRole('dialog').getByRole('button', { name: 'Update' }).click();
|
||||||
|
|
||||||
|
// Wait for dialog to close (indicates success)
|
||||||
|
await expect(page.getByRole('dialog')).not.toBeVisible({ timeout: 10_000 });
|
||||||
|
|
||||||
await page.reload();
|
await page.reload();
|
||||||
|
|
||||||
@ -365,9 +458,11 @@ test('[ADMIN]: multiple promotions in sequence', async ({ page }) => {
|
|||||||
await expect(member2Row.getByRole('status').filter({ hasText: 'Owner' })).toBeVisible();
|
await expect(member2Row.getByRole('status').filter({ hasText: 'Owner' })).toBeVisible();
|
||||||
await expect(member1Row.getByRole('status').filter({ hasText: 'Owner' })).not.toBeVisible();
|
await expect(member1Row.getByRole('status').filter({ hasText: 'Owner' })).not.toBeVisible();
|
||||||
|
|
||||||
// Verify Member 1's promote button is now enabled again
|
// Verify Member 1's Update role button is still visible
|
||||||
const newPromoteButton1 = member1Row.getByRole('button', { name: 'Promote to owner' });
|
const newUpdateButton1 = member1Row.getByRole('button', {
|
||||||
await expect(newPromoteButton1).not.toBeDisabled();
|
name: 'Update role',
|
||||||
|
});
|
||||||
|
await expect(newUpdateButton1).toBeVisible();
|
||||||
});
|
});
|
||||||
|
|
||||||
test('[ADMIN]: verify organisation access after ownership change', async ({ page }) => {
|
test('[ADMIN]: verify organisation access after ownership change', async ({ page }) => {
|
||||||
@ -402,11 +497,25 @@ test('[ADMIN]: verify organisation access after ownership change', async ({ page
|
|||||||
});
|
});
|
||||||
|
|
||||||
const memberRow = page.getByRole('row', { name: memberUser.email });
|
const memberRow = page.getByRole('row', { name: memberUser.email });
|
||||||
const promoteButton = memberRow.getByRole('button', { name: 'Promote to owner' });
|
const updateRoleButton = memberRow.getByRole('button', {
|
||||||
await promoteButton.click();
|
name: 'Update role',
|
||||||
await expect(page.getByText('Member promoted to owner successfully').first()).toBeVisible({
|
|
||||||
timeout: 10_000,
|
|
||||||
});
|
});
|
||||||
|
await updateRoleButton.click();
|
||||||
|
|
||||||
|
// Wait for dialog to open and select Owner role
|
||||||
|
await expect(page.getByRole('dialog')).toBeVisible();
|
||||||
|
|
||||||
|
// Find and click the select trigger - it's a button with role="combobox"
|
||||||
|
await page.getByRole('dialog').locator('button[role="combobox"]').click();
|
||||||
|
|
||||||
|
// Select "Owner" from the dropdown options
|
||||||
|
await page.getByRole('option', { name: 'Owner' }).click();
|
||||||
|
|
||||||
|
// Click Update button
|
||||||
|
await page.getByRole('dialog').getByRole('button', { name: 'Update' }).click();
|
||||||
|
|
||||||
|
// Wait for dialog to close (indicates success)
|
||||||
|
await expect(page.getByRole('dialog')).not.toBeVisible({ timeout: 10_000 });
|
||||||
|
|
||||||
// Test that the new owner can access organisation settings
|
// Test that the new owner can access organisation settings
|
||||||
await apiSignin({
|
await apiSignin({
|
||||||
@ -1,7 +1,5 @@
|
|||||||
import { EnvelopeType, TeamMemberRole } from '@prisma/client';
|
|
||||||
import type { Prisma, User } from '@prisma/client';
|
import type { Prisma, User } from '@prisma/client';
|
||||||
import { SigningStatus } from '@prisma/client';
|
import { DocumentVisibility, EnvelopeType, SigningStatus, TeamMemberRole } from '@prisma/client';
|
||||||
import { DocumentVisibility } from '@prisma/client';
|
|
||||||
import { DateTime } from 'luxon';
|
import { DateTime } from 'luxon';
|
||||||
import { match } from 'ts-pattern';
|
import { match } from 'ts-pattern';
|
||||||
|
|
||||||
@ -215,13 +213,14 @@ const getTeamCounts = async (options: GetTeamCountsOption) => {
|
|||||||
],
|
],
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const rootPageFilter = folderId === undefined ? { folderId: null } : {};
|
||||||
|
|
||||||
let ownerCountsWhereInput: Prisma.EnvelopeWhereInput = {
|
let ownerCountsWhereInput: Prisma.EnvelopeWhereInput = {
|
||||||
type: EnvelopeType.DOCUMENT,
|
type: EnvelopeType.DOCUMENT,
|
||||||
userId: userIdWhereClause,
|
userId: userIdWhereClause,
|
||||||
createdAt,
|
createdAt,
|
||||||
teamId,
|
teamId,
|
||||||
deletedAt: null,
|
deletedAt: null,
|
||||||
folderId,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let notSignedCountsGroupByArgs = null;
|
let notSignedCountsGroupByArgs = null;
|
||||||
@ -265,8 +264,16 @@ const getTeamCounts = async (options: GetTeamCountsOption) => {
|
|||||||
|
|
||||||
ownerCountsWhereInput = {
|
ownerCountsWhereInput = {
|
||||||
...ownerCountsWhereInput,
|
...ownerCountsWhereInput,
|
||||||
...visibilityFiltersWhereInput,
|
AND: [
|
||||||
...searchFilter,
|
...(Array.isArray(visibilityFiltersWhereInput.AND)
|
||||||
|
? visibilityFiltersWhereInput.AND
|
||||||
|
: visibilityFiltersWhereInput.AND
|
||||||
|
? [visibilityFiltersWhereInput.AND]
|
||||||
|
: []),
|
||||||
|
searchFilter,
|
||||||
|
rootPageFilter,
|
||||||
|
folderId ? { folderId } : {},
|
||||||
|
],
|
||||||
};
|
};
|
||||||
|
|
||||||
if (teamEmail) {
|
if (teamEmail) {
|
||||||
@ -285,6 +292,7 @@ const getTeamCounts = async (options: GetTeamCountsOption) => {
|
|||||||
},
|
},
|
||||||
],
|
],
|
||||||
deletedAt: null,
|
deletedAt: null,
|
||||||
|
AND: [searchFilter, rootPageFilter, folderId ? { folderId } : {}],
|
||||||
};
|
};
|
||||||
|
|
||||||
notSignedCountsGroupByArgs = {
|
notSignedCountsGroupByArgs = {
|
||||||
@ -296,7 +304,6 @@ const getTeamCounts = async (options: GetTeamCountsOption) => {
|
|||||||
type: EnvelopeType.DOCUMENT,
|
type: EnvelopeType.DOCUMENT,
|
||||||
userId: userIdWhereClause,
|
userId: userIdWhereClause,
|
||||||
createdAt,
|
createdAt,
|
||||||
folderId,
|
|
||||||
status: ExtendedDocumentStatus.PENDING,
|
status: ExtendedDocumentStatus.PENDING,
|
||||||
recipients: {
|
recipients: {
|
||||||
some: {
|
some: {
|
||||||
@ -306,6 +313,7 @@ const getTeamCounts = async (options: GetTeamCountsOption) => {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
deletedAt: null,
|
deletedAt: null,
|
||||||
|
AND: [searchFilter, rootPageFilter, folderId ? { folderId } : {}],
|
||||||
},
|
},
|
||||||
} satisfies Prisma.EnvelopeGroupByArgs;
|
} satisfies Prisma.EnvelopeGroupByArgs;
|
||||||
|
|
||||||
@ -318,7 +326,6 @@ const getTeamCounts = async (options: GetTeamCountsOption) => {
|
|||||||
type: EnvelopeType.DOCUMENT,
|
type: EnvelopeType.DOCUMENT,
|
||||||
userId: userIdWhereClause,
|
userId: userIdWhereClause,
|
||||||
createdAt,
|
createdAt,
|
||||||
folderId,
|
|
||||||
OR: [
|
OR: [
|
||||||
{
|
{
|
||||||
status: ExtendedDocumentStatus.PENDING,
|
status: ExtendedDocumentStatus.PENDING,
|
||||||
@ -342,6 +349,7 @@ const getTeamCounts = async (options: GetTeamCountsOption) => {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
|
AND: [searchFilter, rootPageFilter, folderId ? { folderId } : {}],
|
||||||
},
|
},
|
||||||
} satisfies Prisma.EnvelopeGroupByArgs;
|
} satisfies Prisma.EnvelopeGroupByArgs;
|
||||||
}
|
}
|
||||||
|
|||||||
482
packages/lib/server-only/pdf/auto-place-fields.ts
Normal file
482
packages/lib/server-only/pdf/auto-place-fields.ts
Normal file
@ -0,0 +1,482 @@
|
|||||||
|
import { PDFDocument, rgb } from '@cantoo/pdf-lib';
|
||||||
|
import type { Recipient } from '@prisma/client';
|
||||||
|
import { EnvelopeType, FieldType, RecipientRole } from '@prisma/client';
|
||||||
|
import PDFParser from 'pdf2json';
|
||||||
|
import { match } from 'ts-pattern';
|
||||||
|
|
||||||
|
import { AppError, AppErrorCode } from '@documenso/lib/errors/app-error';
|
||||||
|
import { getEnvelopeWhereInput } from '@documenso/lib/server-only/envelope/get-envelope-by-id';
|
||||||
|
import { createEnvelopeFields } from '@documenso/lib/server-only/field/create-envelope-fields';
|
||||||
|
import { createDocumentRecipients } from '@documenso/lib/server-only/recipient/create-document-recipients';
|
||||||
|
import { createTemplateRecipients } from '@documenso/lib/server-only/recipient/create-template-recipients';
|
||||||
|
import { type TFieldAndMeta, ZFieldAndMetaSchema } from '@documenso/lib/types/field-meta';
|
||||||
|
import type { ApiRequestMetadata } from '@documenso/lib/universal/extract-request-metadata';
|
||||||
|
import type { EnvelopeIdOptions } from '@documenso/lib/utils/envelope';
|
||||||
|
import { mapSecondaryIdToTemplateId } from '@documenso/lib/utils/envelope';
|
||||||
|
import { generateRecipientPlaceholder } from '@documenso/lib/utils/templates';
|
||||||
|
import { prisma } from '@documenso/prisma';
|
||||||
|
|
||||||
|
import { getPageSize } from './get-page-size';
|
||||||
|
|
||||||
|
type TextPosition = {
|
||||||
|
text: string;
|
||||||
|
x: number;
|
||||||
|
y: number;
|
||||||
|
w: number;
|
||||||
|
};
|
||||||
|
|
||||||
|
type CharIndexMapping = {
|
||||||
|
textPosIndex: number;
|
||||||
|
};
|
||||||
|
|
||||||
|
type PlaceholderInfo = {
|
||||||
|
placeholder: string;
|
||||||
|
recipient: string;
|
||||||
|
fieldAndMeta: TFieldAndMeta;
|
||||||
|
page: number;
|
||||||
|
x: number;
|
||||||
|
y: number;
|
||||||
|
width: number;
|
||||||
|
height: number;
|
||||||
|
pageWidth: number;
|
||||||
|
pageHeight: number;
|
||||||
|
};
|
||||||
|
|
||||||
|
type FieldToCreate = TFieldAndMeta & {
|
||||||
|
recipientId: number;
|
||||||
|
pageNumber: number;
|
||||||
|
pageX: number;
|
||||||
|
pageY: number;
|
||||||
|
width: number;
|
||||||
|
height: number;
|
||||||
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
Questions for later:
|
||||||
|
- Does it handle multi-page PDFs? ✅ YES! ✅
|
||||||
|
- Does it handle multiple recipients on the same page? ✅ YES! ✅
|
||||||
|
- Does it handle multiple recipients on multiple pages? ✅ YES! ✅
|
||||||
|
- What happens with incorrect placeholders? E.g. those containing non-accepted properties.
|
||||||
|
- The placeholder data is dynamic. How to handle this parsing? Perhaps we need to do it similar to the fieldMeta parsing.
|
||||||
|
- Need to handle envelopes with multiple items.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
Parse field type string to FieldType enum.
|
||||||
|
Normalizes the input (uppercase, trim) and validates it's a valid field type.
|
||||||
|
This ensures we handle case variations and whitespace, and provides clear error messages.
|
||||||
|
*/
|
||||||
|
const parseFieldType = (fieldTypeString: string): FieldType => {
|
||||||
|
const normalizedType = fieldTypeString.toUpperCase().trim();
|
||||||
|
|
||||||
|
return match(normalizedType)
|
||||||
|
.with('SIGNATURE', () => FieldType.SIGNATURE)
|
||||||
|
.with('FREE_SIGNATURE', () => FieldType.FREE_SIGNATURE)
|
||||||
|
.with('INITIALS', () => FieldType.INITIALS)
|
||||||
|
.with('NAME', () => FieldType.NAME)
|
||||||
|
.with('EMAIL', () => FieldType.EMAIL)
|
||||||
|
.with('DATE', () => FieldType.DATE)
|
||||||
|
.with('TEXT', () => FieldType.TEXT)
|
||||||
|
.with('NUMBER', () => FieldType.NUMBER)
|
||||||
|
.with('RADIO', () => FieldType.RADIO)
|
||||||
|
.with('CHECKBOX', () => FieldType.CHECKBOX)
|
||||||
|
.with('DROPDOWN', () => FieldType.DROPDOWN)
|
||||||
|
.otherwise(() => {
|
||||||
|
throw new AppError(AppErrorCode.INVALID_BODY, {
|
||||||
|
message: `Invalid field type: ${fieldTypeString}`,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
Transform raw field metadata from placeholder format to schema format.
|
||||||
|
Users should provide properly capitalized property names (e.g., readOnly, fontSize, textAlign).
|
||||||
|
Converts string values to proper types (booleans, numbers).
|
||||||
|
*/
|
||||||
|
const parseFieldMeta = (
|
||||||
|
rawFieldMeta: Record<string, string>,
|
||||||
|
fieldType: FieldType,
|
||||||
|
): Record<string, unknown> | undefined => {
|
||||||
|
if (fieldType === FieldType.SIGNATURE || fieldType === FieldType.FREE_SIGNATURE) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Object.keys(rawFieldMeta).length === 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const fieldTypeString = String(fieldType).toLowerCase();
|
||||||
|
|
||||||
|
const parsedFieldMeta: Record<string, boolean | number | string> = {
|
||||||
|
type: fieldTypeString,
|
||||||
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
rawFieldMeta is an object with string keys and string values.
|
||||||
|
It contains string values because the PDF parser returns the values as strings.
|
||||||
|
|
||||||
|
E.g. { required: 'true', fontSize: '12', maxValue: '100', minValue: '0', characterLimit: '100' }
|
||||||
|
*/
|
||||||
|
const rawFieldMetaEntries = Object.entries(rawFieldMeta);
|
||||||
|
|
||||||
|
for (const entry of rawFieldMetaEntries) {
|
||||||
|
const [key, value] = entry;
|
||||||
|
|
||||||
|
if (key === 'readOnly' || key === 'required') {
|
||||||
|
parsedFieldMeta[key] = value === 'true';
|
||||||
|
} else if (
|
||||||
|
key === 'fontSize' ||
|
||||||
|
key === 'maxValue' ||
|
||||||
|
key === 'minValue' ||
|
||||||
|
key === 'characterLimit'
|
||||||
|
) {
|
||||||
|
const numValue = Number(value);
|
||||||
|
|
||||||
|
if (!Number.isNaN(numValue)) {
|
||||||
|
parsedFieldMeta[key] = numValue;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
parsedFieldMeta[key] = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return parsedFieldMeta;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const extractPlaceholdersFromPDF = async (pdf: Buffer): Promise<PlaceholderInfo[]> => {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
const parser = new PDFParser(null, true);
|
||||||
|
|
||||||
|
parser.on('pdfParser_dataError', (errData) => {
|
||||||
|
reject(errData);
|
||||||
|
});
|
||||||
|
|
||||||
|
parser.on('pdfParser_dataReady', (pdfData) => {
|
||||||
|
const placeholders: PlaceholderInfo[] = [];
|
||||||
|
|
||||||
|
pdfData.Pages.forEach((page, pageIndex) => {
|
||||||
|
/*
|
||||||
|
pdf2json returns the PDF page content as an array of characters.
|
||||||
|
We need to concatenate the characters to get the full text.
|
||||||
|
We also need to get the position of the text so we can place the placeholders in the correct position.
|
||||||
|
|
||||||
|
Page dimensions from PDF2JSON are in "page units" (relative coordinates)
|
||||||
|
*/
|
||||||
|
const pageWidth = page.Width;
|
||||||
|
const pageHeight = page.Height;
|
||||||
|
|
||||||
|
let pageText = '';
|
||||||
|
const textPositions: TextPosition[] = [];
|
||||||
|
const charIndexToTextPos: CharIndexMapping[] = [];
|
||||||
|
|
||||||
|
page.Texts.forEach((text) => {
|
||||||
|
/*
|
||||||
|
R is an array that contains objects with each character.
|
||||||
|
The decodedText contains only the character, without any other information.
|
||||||
|
|
||||||
|
textPositions stores each character and its position on the page.
|
||||||
|
*/
|
||||||
|
const decodedText = text.R.map((run) => decodeURIComponent(run.T)).join('');
|
||||||
|
|
||||||
|
for (let i = 0; i < decodedText.length; i++) {
|
||||||
|
charIndexToTextPos.push({
|
||||||
|
textPosIndex: textPositions.length,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
pageText += decodedText;
|
||||||
|
|
||||||
|
textPositions.push({
|
||||||
|
text: decodedText,
|
||||||
|
x: text.x,
|
||||||
|
y: text.y,
|
||||||
|
w: text.w || 0,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
const placeholderMatches = pageText.matchAll(/{{([^}]+)}}/g);
|
||||||
|
|
||||||
|
for (const match of placeholderMatches) {
|
||||||
|
const placeholder = match[0];
|
||||||
|
const placeholderData = match[1].split(',').map((part) => part.trim());
|
||||||
|
|
||||||
|
const [fieldTypeString, recipient, ...fieldMetaData] = placeholderData;
|
||||||
|
|
||||||
|
const rawFieldMeta = Object.fromEntries(fieldMetaData.map((meta) => meta.split('=')));
|
||||||
|
|
||||||
|
const fieldType = parseFieldType(fieldTypeString);
|
||||||
|
const parsedFieldMeta = parseFieldMeta(rawFieldMeta, fieldType);
|
||||||
|
|
||||||
|
const fieldAndMeta: TFieldAndMeta = ZFieldAndMetaSchema.parse({
|
||||||
|
type: fieldType,
|
||||||
|
fieldMeta: parsedFieldMeta,
|
||||||
|
});
|
||||||
|
|
||||||
|
/*
|
||||||
|
Find the position of where the placeholder starts in the text
|
||||||
|
|
||||||
|
Then find the position of where the placeholder ends in the text by adding the length of the placeholder to the index of the placeholder.
|
||||||
|
*/
|
||||||
|
const matchIndex = match.index;
|
||||||
|
const placeholderLength = placeholder.length;
|
||||||
|
const placeholderEndIndex = matchIndex + placeholderLength;
|
||||||
|
|
||||||
|
const startCharInfo = charIndexToTextPos[matchIndex];
|
||||||
|
const endCharInfo = charIndexToTextPos[placeholderEndIndex - 1];
|
||||||
|
|
||||||
|
if (!startCharInfo || !endCharInfo) {
|
||||||
|
console.error('Could not find text position for placeholder', placeholder);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const startTextPos = textPositions[startCharInfo.textPosIndex];
|
||||||
|
const endTextPos = textPositions[endCharInfo.textPosIndex];
|
||||||
|
|
||||||
|
/*
|
||||||
|
PDF2JSON coordinates - these are in "page units" (relative coordinates)
|
||||||
|
Calculate width as the distance from start to end, plus a portion of the last character's width
|
||||||
|
Use 10% of the last character width to avoid extending too far beyond the placeholder
|
||||||
|
*/
|
||||||
|
const x = startTextPos.x;
|
||||||
|
const y = startTextPos.y;
|
||||||
|
const width = endTextPos.x + endTextPos.w * 0.1 - startTextPos.x;
|
||||||
|
|
||||||
|
placeholders.push({
|
||||||
|
placeholder,
|
||||||
|
recipient,
|
||||||
|
fieldAndMeta,
|
||||||
|
page: pageIndex + 1,
|
||||||
|
x,
|
||||||
|
y,
|
||||||
|
width,
|
||||||
|
height: 1,
|
||||||
|
pageWidth,
|
||||||
|
pageHeight,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
resolve(placeholders);
|
||||||
|
});
|
||||||
|
|
||||||
|
parser.parseBuffer(pdf);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
export const replacePlaceholdersInPDF = async (pdf: Buffer): Promise<Buffer> => {
|
||||||
|
const placeholders = await extractPlaceholdersFromPDF(pdf);
|
||||||
|
|
||||||
|
const pdfDoc = await PDFDocument.load(new Uint8Array(pdf));
|
||||||
|
const pages = pdfDoc.getPages();
|
||||||
|
|
||||||
|
for (const placeholder of placeholders) {
|
||||||
|
const pageIndex = placeholder.page - 1;
|
||||||
|
const page = pages[pageIndex];
|
||||||
|
|
||||||
|
const { width: pdfLibPageWidth, height: pdfLibPageHeight } = getPageSize(page);
|
||||||
|
|
||||||
|
/*
|
||||||
|
Convert PDF2JSON coordinates to pdf-lib coordinates:
|
||||||
|
|
||||||
|
PDF2JSON uses relative "page units":
|
||||||
|
- x, y, width, height are in page units
|
||||||
|
- Page dimensions (Width, Height) are also in page units
|
||||||
|
|
||||||
|
pdf-lib uses absolute points (1 point = 1/72 inch):
|
||||||
|
- Need to convert from page units to points
|
||||||
|
- Y-axis in pdf-lib is bottom-up (origin at bottom-left)
|
||||||
|
- Y-axis in PDF2JSON is top-down (origin at top-left)
|
||||||
|
|
||||||
|
Conversion formulas:
|
||||||
|
- x_points = (x / pageWidth) * pdfLibPageWidth
|
||||||
|
- y_points = pdfLibPageHeight - ((y / pageHeight) * pdfLibPageHeight)
|
||||||
|
- width_points = (width / pageWidth) * pdfLibPageWidth
|
||||||
|
- height_points = (height / pageHeight) * pdfLibPageHeight
|
||||||
|
*/
|
||||||
|
|
||||||
|
const xPoints = (placeholder.x / placeholder.pageWidth) * pdfLibPageWidth;
|
||||||
|
const yPoints = pdfLibPageHeight - (placeholder.y / placeholder.pageHeight) * pdfLibPageHeight;
|
||||||
|
const widthPoints = (placeholder.width / placeholder.pageWidth) * pdfLibPageWidth;
|
||||||
|
const heightPoints = (placeholder.height / placeholder.pageHeight) * pdfLibPageHeight;
|
||||||
|
|
||||||
|
page.drawRectangle({
|
||||||
|
x: xPoints,
|
||||||
|
y: yPoints - heightPoints, // Adjust for height since y is at baseline
|
||||||
|
width: widthPoints,
|
||||||
|
height: heightPoints,
|
||||||
|
color: rgb(1, 1, 1),
|
||||||
|
borderColor: rgb(1, 1, 1),
|
||||||
|
borderWidth: 2,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const modifiedPdfBytes = await pdfDoc.save();
|
||||||
|
|
||||||
|
return Buffer.from(modifiedPdfBytes);
|
||||||
|
};
|
||||||
|
|
||||||
|
const extractRecipientPlaceholder = (
|
||||||
|
placeholder: string,
|
||||||
|
): { email: string; recipientIndex: number } => {
|
||||||
|
const indexMatch = placeholder.match(/^r(\d+)$/i);
|
||||||
|
|
||||||
|
if (!indexMatch) {
|
||||||
|
throw new AppError(AppErrorCode.INVALID_BODY, {
|
||||||
|
message: `Invalid recipient placeholder format: ${placeholder}. Expected format: r1, r2, r3, etc.`,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
email: `recipient.${indexMatch[1]}@documenso.com`,
|
||||||
|
recipientIndex: Number(indexMatch[1]),
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
export const insertFieldsFromPlaceholdersInPDF = async (
|
||||||
|
pdf: Buffer,
|
||||||
|
userId: number,
|
||||||
|
teamId: number,
|
||||||
|
envelopeId: EnvelopeIdOptions,
|
||||||
|
requestMetadata: ApiRequestMetadata,
|
||||||
|
): Promise<Buffer> => {
|
||||||
|
const placeholders = await extractPlaceholdersFromPDF(pdf);
|
||||||
|
|
||||||
|
if (placeholders.length === 0) {
|
||||||
|
return pdf;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
A structure that maps the recipient email to the recipient index.
|
||||||
|
Example: 'recipient.1@documenso.com' => 1
|
||||||
|
*/
|
||||||
|
const recipientEmailToIndex = new Map<string, number>();
|
||||||
|
|
||||||
|
for (const placeholder of placeholders) {
|
||||||
|
const { email, recipientIndex } = extractRecipientPlaceholder(placeholder.recipient);
|
||||||
|
|
||||||
|
recipientEmailToIndex.set(email, recipientIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
Create a list of recipients to create.
|
||||||
|
Example: [{ email: 'recipient.1@documenso.com', name: 'Recipient 1', role: 'SIGNER', signingOrder: 1 }]
|
||||||
|
*/
|
||||||
|
const recipientsToCreate = Array.from(
|
||||||
|
recipientEmailToIndex.entries(),
|
||||||
|
([email, recipientIndex]) => {
|
||||||
|
const placeholderInfo = generateRecipientPlaceholder(recipientIndex);
|
||||||
|
|
||||||
|
return {
|
||||||
|
email,
|
||||||
|
name: placeholderInfo.name,
|
||||||
|
role: RecipientRole.SIGNER,
|
||||||
|
signingOrder: recipientIndex,
|
||||||
|
};
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
const { envelopeWhereInput } = await getEnvelopeWhereInput({
|
||||||
|
id: envelopeId,
|
||||||
|
userId,
|
||||||
|
teamId,
|
||||||
|
type: null,
|
||||||
|
});
|
||||||
|
|
||||||
|
const envelope = await prisma.envelope.findFirst({
|
||||||
|
where: envelopeWhereInput,
|
||||||
|
select: {
|
||||||
|
id: true,
|
||||||
|
type: true,
|
||||||
|
secondaryId: true,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!envelope) {
|
||||||
|
throw new AppError(AppErrorCode.NOT_FOUND, {
|
||||||
|
message: 'Envelope not found',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
let createdRecipients: Pick<Recipient, 'id' | 'email'>[];
|
||||||
|
|
||||||
|
if (envelope.type === EnvelopeType.DOCUMENT) {
|
||||||
|
const { recipients } = await createDocumentRecipients({
|
||||||
|
userId,
|
||||||
|
teamId,
|
||||||
|
id: envelopeId,
|
||||||
|
recipients: recipientsToCreate,
|
||||||
|
requestMetadata,
|
||||||
|
});
|
||||||
|
|
||||||
|
createdRecipients = recipients;
|
||||||
|
} else if (envelope.type === EnvelopeType.TEMPLATE) {
|
||||||
|
const templateId =
|
||||||
|
envelopeId.type === 'templateId'
|
||||||
|
? envelopeId.id
|
||||||
|
: mapSecondaryIdToTemplateId(envelope.secondaryId);
|
||||||
|
|
||||||
|
const { recipients } = await createTemplateRecipients({
|
||||||
|
userId,
|
||||||
|
teamId,
|
||||||
|
templateId,
|
||||||
|
recipients: recipientsToCreate,
|
||||||
|
});
|
||||||
|
|
||||||
|
createdRecipients = recipients;
|
||||||
|
} else {
|
||||||
|
throw new AppError(AppErrorCode.INVALID_BODY, {
|
||||||
|
message: `Invalid envelope type: ${envelope.type}`,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const fieldsToCreate: FieldToCreate[] = [];
|
||||||
|
|
||||||
|
for (const placeholder of placeholders) {
|
||||||
|
/*
|
||||||
|
Convert PDF2JSON coordinates to percentage-based coordinates (0-100)
|
||||||
|
The UI expects positionX and positionY as percentages, not absolute points
|
||||||
|
PDF2JSON uses relative coordinates: x/pageWidth and y/pageHeight give us the percentage
|
||||||
|
*/
|
||||||
|
const xPercent = (placeholder.x / placeholder.pageWidth) * 100;
|
||||||
|
const yPercent = (placeholder.y / placeholder.pageHeight) * 100;
|
||||||
|
|
||||||
|
const widthPercent = (placeholder.width / placeholder.pageWidth) * 100;
|
||||||
|
const heightPercent = (placeholder.height / placeholder.pageHeight) * 100;
|
||||||
|
|
||||||
|
const { email } = extractRecipientPlaceholder(placeholder.recipient);
|
||||||
|
const normalizedEmail = email.toLowerCase();
|
||||||
|
const recipient = createdRecipients.find((r) => r.email.toLowerCase() === normalizedEmail);
|
||||||
|
|
||||||
|
if (!recipient) {
|
||||||
|
throw new AppError(AppErrorCode.INVALID_BODY, {
|
||||||
|
message: `Could not find recipient ID for placeholder: ${placeholder.placeholder}`,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const recipientId = recipient.id;
|
||||||
|
|
||||||
|
// Default height percentage if too small (use 2% as a reasonable default)
|
||||||
|
const finalHeightPercent = heightPercent > 0.01 ? heightPercent : 2;
|
||||||
|
|
||||||
|
fieldsToCreate.push({
|
||||||
|
...placeholder.fieldAndMeta,
|
||||||
|
recipientId,
|
||||||
|
pageNumber: placeholder.page,
|
||||||
|
pageX: xPercent,
|
||||||
|
pageY: yPercent,
|
||||||
|
width: widthPercent,
|
||||||
|
height: finalHeightPercent,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
await createEnvelopeFields({
|
||||||
|
userId,
|
||||||
|
teamId,
|
||||||
|
id: envelopeId,
|
||||||
|
fields: fieldsToCreate,
|
||||||
|
requestMetadata,
|
||||||
|
});
|
||||||
|
|
||||||
|
return pdf;
|
||||||
|
};
|
||||||
@ -1,5 +1,6 @@
|
|||||||
import { PDFDocument } from '@cantoo/pdf-lib';
|
import { PDFDocument } from '@cantoo/pdf-lib';
|
||||||
|
|
||||||
|
import { replacePlaceholdersInPDF } from './auto-place-fields';
|
||||||
import { flattenAnnotations } from './flatten-annotations';
|
import { flattenAnnotations } from './flatten-annotations';
|
||||||
import { flattenForm, removeOptionalContentGroups } from './flatten-form';
|
import { flattenForm, removeOptionalContentGroups } from './flatten-form';
|
||||||
|
|
||||||
@ -13,6 +14,7 @@ export const normalizePdf = async (pdf: Buffer) => {
|
|||||||
removeOptionalContentGroups(pdfDoc);
|
removeOptionalContentGroups(pdfDoc);
|
||||||
await flattenForm(pdfDoc);
|
await flattenForm(pdfDoc);
|
||||||
flattenAnnotations(pdfDoc);
|
flattenAnnotations(pdfDoc);
|
||||||
|
const pdfWithoutPlaceholders = await replacePlaceholdersInPDF(pdf);
|
||||||
|
|
||||||
return Buffer.from(await pdfDoc.save());
|
return pdfWithoutPlaceholders;
|
||||||
};
|
};
|
||||||
|
|||||||
@ -39,6 +39,11 @@ export const getAdminOrganisation = async ({ organisationId }: GetOrganisationOp
|
|||||||
teams: true,
|
teams: true,
|
||||||
members: {
|
members: {
|
||||||
include: {
|
include: {
|
||||||
|
organisationGroupMembers: {
|
||||||
|
include: {
|
||||||
|
group: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
user: {
|
user: {
|
||||||
select: {
|
select: {
|
||||||
id: true,
|
id: true,
|
||||||
|
|||||||
@ -3,6 +3,8 @@ import { z } from 'zod';
|
|||||||
import { ZOrganisationSchema } from '@documenso/lib/types/organisation';
|
import { ZOrganisationSchema } from '@documenso/lib/types/organisation';
|
||||||
import OrganisationClaimSchema from '@documenso/prisma/generated/zod/modelSchema/OrganisationClaimSchema';
|
import OrganisationClaimSchema from '@documenso/prisma/generated/zod/modelSchema/OrganisationClaimSchema';
|
||||||
import OrganisationGlobalSettingsSchema from '@documenso/prisma/generated/zod/modelSchema/OrganisationGlobalSettingsSchema';
|
import OrganisationGlobalSettingsSchema from '@documenso/prisma/generated/zod/modelSchema/OrganisationGlobalSettingsSchema';
|
||||||
|
import OrganisationGroupMemberSchema from '@documenso/prisma/generated/zod/modelSchema/OrganisationGroupMemberSchema';
|
||||||
|
import OrganisationGroupSchema from '@documenso/prisma/generated/zod/modelSchema/OrganisationGroupSchema';
|
||||||
import OrganisationMemberSchema from '@documenso/prisma/generated/zod/modelSchema/OrganisationMemberSchema';
|
import OrganisationMemberSchema from '@documenso/prisma/generated/zod/modelSchema/OrganisationMemberSchema';
|
||||||
import SubscriptionSchema from '@documenso/prisma/generated/zod/modelSchema/SubscriptionSchema';
|
import SubscriptionSchema from '@documenso/prisma/generated/zod/modelSchema/SubscriptionSchema';
|
||||||
import TeamSchema from '@documenso/prisma/generated/zod/modelSchema/TeamSchema';
|
import TeamSchema from '@documenso/prisma/generated/zod/modelSchema/TeamSchema';
|
||||||
@ -30,6 +32,18 @@ export const ZGetAdminOrganisationResponseSchema = ZOrganisationSchema.extend({
|
|||||||
email: true,
|
email: true,
|
||||||
name: true,
|
name: true,
|
||||||
}),
|
}),
|
||||||
|
organisationGroupMembers: z.array(
|
||||||
|
OrganisationGroupMemberSchema.pick({
|
||||||
|
id: true,
|
||||||
|
groupId: true,
|
||||||
|
}).extend({
|
||||||
|
group: OrganisationGroupSchema.pick({
|
||||||
|
id: true,
|
||||||
|
type: true,
|
||||||
|
organisationRole: true,
|
||||||
|
}),
|
||||||
|
}),
|
||||||
|
),
|
||||||
}).array(),
|
}).array(),
|
||||||
subscription: SubscriptionSchema.nullable(),
|
subscription: SubscriptionSchema.nullable(),
|
||||||
organisationClaim: OrganisationClaimSchema,
|
organisationClaim: OrganisationClaimSchema,
|
||||||
|
|||||||
@ -17,6 +17,7 @@ import { promoteMemberToOwnerRoute } from './promote-member-to-owner';
|
|||||||
import { resealDocumentRoute } from './reseal-document';
|
import { resealDocumentRoute } from './reseal-document';
|
||||||
import { resetTwoFactorRoute } from './reset-two-factor-authentication';
|
import { resetTwoFactorRoute } from './reset-two-factor-authentication';
|
||||||
import { updateAdminOrganisationRoute } from './update-admin-organisation';
|
import { updateAdminOrganisationRoute } from './update-admin-organisation';
|
||||||
|
import { updateOrganisationMemberRoleRoute } from './update-organisation-member-role';
|
||||||
import { updateRecipientRoute } from './update-recipient';
|
import { updateRecipientRoute } from './update-recipient';
|
||||||
import { updateSiteSettingRoute } from './update-site-setting';
|
import { updateSiteSettingRoute } from './update-site-setting';
|
||||||
import { updateSubscriptionClaimRoute } from './update-subscription-claim';
|
import { updateSubscriptionClaimRoute } from './update-subscription-claim';
|
||||||
@ -31,6 +32,7 @@ export const adminRouter = router({
|
|||||||
},
|
},
|
||||||
organisationMember: {
|
organisationMember: {
|
||||||
promoteToOwner: promoteMemberToOwnerRoute,
|
promoteToOwner: promoteMemberToOwnerRoute,
|
||||||
|
updateRole: updateOrganisationMemberRoleRoute,
|
||||||
},
|
},
|
||||||
claims: {
|
claims: {
|
||||||
find: findSubscriptionClaimsRoute,
|
find: findSubscriptionClaimsRoute,
|
||||||
|
|||||||
@ -0,0 +1,220 @@
|
|||||||
|
import { OrganisationGroupType, OrganisationMemberRole } from '@prisma/client';
|
||||||
|
|
||||||
|
import { AppError, AppErrorCode } from '@documenso/lib/errors/app-error';
|
||||||
|
import { generateDatabaseId } from '@documenso/lib/universal/id';
|
||||||
|
import { getHighestOrganisationRoleInGroup } from '@documenso/lib/utils/organisations';
|
||||||
|
import { prisma } from '@documenso/prisma';
|
||||||
|
|
||||||
|
import { adminProcedure } from '../trpc';
|
||||||
|
import {
|
||||||
|
ZUpdateOrganisationMemberRoleRequestSchema,
|
||||||
|
ZUpdateOrganisationMemberRoleResponseSchema,
|
||||||
|
} from './update-organisation-member-role.types';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Admin mutation to update organisation member role or transfer ownership.
|
||||||
|
*
|
||||||
|
* This mutation handles two scenarios:
|
||||||
|
* 1. When role='OWNER': Transfers organisation ownership and promotes to ADMIN
|
||||||
|
* 2. When role=ADMIN/MANAGER/MEMBER: Updates group membership
|
||||||
|
*
|
||||||
|
* Admin privileges bypass normal hierarchy restrictions.
|
||||||
|
*/
|
||||||
|
export const updateOrganisationMemberRoleRoute = adminProcedure
|
||||||
|
.input(ZUpdateOrganisationMemberRoleRequestSchema)
|
||||||
|
.output(ZUpdateOrganisationMemberRoleResponseSchema)
|
||||||
|
.mutation(async ({ input, ctx }) => {
|
||||||
|
const { organisationId, userId, role } = input;
|
||||||
|
|
||||||
|
ctx.logger.info({
|
||||||
|
input: {
|
||||||
|
organisationId,
|
||||||
|
userId,
|
||||||
|
role,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const organisation = await prisma.organisation.findUnique({
|
||||||
|
where: {
|
||||||
|
id: organisationId,
|
||||||
|
},
|
||||||
|
include: {
|
||||||
|
groups: {
|
||||||
|
where: {
|
||||||
|
type: OrganisationGroupType.INTERNAL_ORGANISATION,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
members: {
|
||||||
|
where: {
|
||||||
|
userId,
|
||||||
|
},
|
||||||
|
include: {
|
||||||
|
organisationGroupMembers: {
|
||||||
|
include: {
|
||||||
|
group: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!organisation) {
|
||||||
|
throw new AppError(AppErrorCode.NOT_FOUND, {
|
||||||
|
message: 'Organisation not found',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const [member] = organisation.members;
|
||||||
|
|
||||||
|
if (!member) {
|
||||||
|
throw new AppError(AppErrorCode.NOT_FOUND, {
|
||||||
|
message: 'User is not a member of this organisation',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const currentOrganisationRole = getHighestOrganisationRoleInGroup(
|
||||||
|
member.organisationGroupMembers.flatMap((member) => member.group),
|
||||||
|
);
|
||||||
|
|
||||||
|
if (role === 'OWNER') {
|
||||||
|
if (organisation.ownerUserId === userId) {
|
||||||
|
throw new AppError(AppErrorCode.INVALID_REQUEST, {
|
||||||
|
message: 'User is already the owner of this organisation',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const currentMemberGroup = organisation.groups.find(
|
||||||
|
(group) => group.organisationRole === currentOrganisationRole,
|
||||||
|
);
|
||||||
|
|
||||||
|
const adminGroup = organisation.groups.find(
|
||||||
|
(group) => group.organisationRole === OrganisationMemberRole.ADMIN,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!currentMemberGroup) {
|
||||||
|
ctx.logger.error({
|
||||||
|
message: '[CRITICAL]: Missing internal group',
|
||||||
|
organisationId,
|
||||||
|
userId,
|
||||||
|
role: currentOrganisationRole,
|
||||||
|
});
|
||||||
|
|
||||||
|
throw new AppError(AppErrorCode.UNKNOWN_ERROR, {
|
||||||
|
message: 'Current member group not found',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!adminGroup) {
|
||||||
|
ctx.logger.error({
|
||||||
|
message: '[CRITICAL]: Missing internal group',
|
||||||
|
organisationId,
|
||||||
|
userId,
|
||||||
|
targetRole: 'ADMIN',
|
||||||
|
});
|
||||||
|
|
||||||
|
throw new AppError(AppErrorCode.UNKNOWN_ERROR, {
|
||||||
|
message: 'Admin group not found',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
await prisma.$transaction(async (tx) => {
|
||||||
|
await tx.organisation.update({
|
||||||
|
where: {
|
||||||
|
id: organisationId,
|
||||||
|
},
|
||||||
|
data: {
|
||||||
|
ownerUserId: userId,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (currentOrganisationRole !== OrganisationMemberRole.ADMIN) {
|
||||||
|
await tx.organisationGroupMember.delete({
|
||||||
|
where: {
|
||||||
|
organisationMemberId_groupId: {
|
||||||
|
organisationMemberId: member.id,
|
||||||
|
groupId: currentMemberGroup.id,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
await tx.organisationGroupMember.create({
|
||||||
|
data: {
|
||||||
|
id: generateDatabaseId('group_member'),
|
||||||
|
organisationMemberId: member.id,
|
||||||
|
groupId: adminGroup.id,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const targetRole = role as OrganisationMemberRole;
|
||||||
|
|
||||||
|
if (currentOrganisationRole === targetRole) {
|
||||||
|
throw new AppError(AppErrorCode.INVALID_REQUEST, {
|
||||||
|
message: 'User already has this role',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (userId === organisation.ownerUserId && targetRole !== OrganisationMemberRole.ADMIN) {
|
||||||
|
throw new AppError(AppErrorCode.INVALID_REQUEST, {
|
||||||
|
message: 'Organisation owner must be an admin. Transfer ownership first.',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const currentMemberGroup = organisation.groups.find(
|
||||||
|
(group) => group.organisationRole === currentOrganisationRole,
|
||||||
|
);
|
||||||
|
|
||||||
|
const newMemberGroup = organisation.groups.find(
|
||||||
|
(group) => group.organisationRole === targetRole,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!currentMemberGroup) {
|
||||||
|
ctx.logger.error({
|
||||||
|
message: '[CRITICAL]: Missing internal group',
|
||||||
|
organisationId,
|
||||||
|
userId,
|
||||||
|
role: currentOrganisationRole,
|
||||||
|
});
|
||||||
|
|
||||||
|
throw new AppError(AppErrorCode.UNKNOWN_ERROR, {
|
||||||
|
message: 'Current member group not found',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!newMemberGroup) {
|
||||||
|
ctx.logger.error({
|
||||||
|
message: '[CRITICAL]: Missing internal group',
|
||||||
|
organisationId,
|
||||||
|
userId,
|
||||||
|
targetRole,
|
||||||
|
});
|
||||||
|
|
||||||
|
throw new AppError(AppErrorCode.UNKNOWN_ERROR, {
|
||||||
|
message: 'New member group not found',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
await prisma.$transaction(async (tx) => {
|
||||||
|
await tx.organisationGroupMember.delete({
|
||||||
|
where: {
|
||||||
|
organisationMemberId_groupId: {
|
||||||
|
organisationMemberId: member.id,
|
||||||
|
groupId: currentMemberGroup.id,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
await tx.organisationGroupMember.create({
|
||||||
|
data: {
|
||||||
|
id: generateDatabaseId('group_member'),
|
||||||
|
organisationMemberId: member.id,
|
||||||
|
groupId: newMemberGroup.id,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
@ -0,0 +1,30 @@
|
|||||||
|
import { OrganisationMemberRole } from '@prisma/client';
|
||||||
|
import { z } from 'zod';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Admin-only role selection that includes OWNER as a special case.
|
||||||
|
* OWNER is not a database role but triggers ownership transfer.
|
||||||
|
*/
|
||||||
|
export const ZAdminRoleSelection = z.enum([
|
||||||
|
'OWNER',
|
||||||
|
OrganisationMemberRole.ADMIN,
|
||||||
|
OrganisationMemberRole.MANAGER,
|
||||||
|
OrganisationMemberRole.MEMBER,
|
||||||
|
]);
|
||||||
|
|
||||||
|
export type TAdminRoleSelection = z.infer<typeof ZAdminRoleSelection>;
|
||||||
|
|
||||||
|
export const ZUpdateOrganisationMemberRoleRequestSchema = z.object({
|
||||||
|
organisationId: z.string().min(1),
|
||||||
|
userId: z.number().min(1),
|
||||||
|
role: ZAdminRoleSelection,
|
||||||
|
});
|
||||||
|
|
||||||
|
export const ZUpdateOrganisationMemberRoleResponseSchema = z.void();
|
||||||
|
|
||||||
|
export type TUpdateOrganisationMemberRoleRequest = z.infer<
|
||||||
|
typeof ZUpdateOrganisationMemberRoleRequestSchema
|
||||||
|
>;
|
||||||
|
export type TUpdateOrganisationMemberRoleResponse = z.infer<
|
||||||
|
typeof ZUpdateOrganisationMemberRoleResponseSchema
|
||||||
|
>;
|
||||||
Reference in New Issue
Block a user