Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: return fields in GET /documents/:id endpoint #1317

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

Etrenak
Copy link
Contributor

@Etrenak Etrenak commented Sep 2, 2024

Description

To be able to use the PATCH /api/v1/documents/{id}/fields/{fieldId} endpoint, we need to know the fields ID of a particular document. The issue #1178 suggested to create a new endpoint for this. To be consistent with the /templates endpoint, I propose in this PR to directly add the fields field to the /documents/:id endpoint.

Related Issue

This is related to #1178 but it might still be interesting to create a new endpoint which could also return the fieldMeta.

Changes Made

Added fields to ZSuccessfulGetDocumentResponseSchema in the API contract, and changed the implementation accordingly.

Testing Performed

  • Tested on a document with and without fields.
  • Full production build (npm run build)

Checklist

  • I have tested these changes locally and they work as expected.
  • I have added/updated tests that prove the effectiveness of these changes.
  • I have updated the documentation to reflect these changes, if applicable.
  • I have followed the project's coding style guidelines.
  • I have addressed the code review feedback from the previous submission, if applicable.

Summary by CodeRabbit

Summary by CodeRabbit

  • New Features

    • Enhanced API response to include additional field data associated with documents.
    • Introduced a new schema for field objects, improving data validation and structure in API responses.
  • Chores

    • Restructured schema definitions for better organization and readability.

Copy link

vercel bot commented Sep 2, 2024

@Etrenak is attempting to deploy a commit to the Documenso Team Team on Vercel.

A member of the Team first needs to authorize it.

Copy link
Contributor

coderabbitai bot commented Sep 2, 2024

Walkthrough

The changes introduce a new asynchronous function, getFieldsForDocument, in the ApiContractV1Implementation route handler, enabling the API to return additional field data related to a specified document. The schema definitions are updated to include a new ZFieldSchema for field objects and to modify the response schemas, such as ZSuccessfulDocumentResponseSchema, to incorporate an array of these fields, refining the structure of the API responses.

Changes

Files Change Summary
packages/api/v1/implementation.ts Added getFieldsForDocument function call in ApiContractV1Implementation, including fields in the response.
packages/api/v1/schema.ts Introduced ZFieldSchema for field object structure; modified ZSuccessfulDocumentResponseSchema and ZSuccessfulGetDocumentResponseSchema to include fields. Updated ZSuccessfulSigningResponseSchema to omit fields.

Possibly related PRs

Suggested labels

apps: web, apps: marketing

Poem

🐰 In the meadow where data flows,
New fields sprout, as knowledge grows.
With each document, more to see,
A richer world, for you and me!
Hopping high with joy, we cheer,
For changes bright, we hold so dear! 🌼


Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 7f314e1 and 2c54328.

Files selected for processing (1)
  • packages/api/v1/implementation.ts (3 hunks)
Files skipped from review as they are similar to previous changes (1)
  • packages/api/v1/implementation.ts

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    -- I pushed a fix in commit <commit_id>, please review it.
    -- Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    -- @coderabbitai generate unit testing code for this file.
    -- @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    -- @coderabbitai generate interesting stats about this repository and render them as a table.
    -- @coderabbitai read src/utils.ts and generate unit testing code.
    -- @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    -- @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 4c13176 and 389c530.

Files selected for processing (2)
  • packages/api/v1/implementation.ts (3 hunks)
  • packages/api/v1/schema.ts (3 hunks)
Additional comments not posted (4)
packages/api/v1/schema.ts (2)

37-51: LGTM!

The new ZFieldSchema declaration looks good. It correctly defines the structure of a field object using the z.object() method from the Zod validation library. The usage of z.nativeEnum(FieldType) for the type property is a good practice to restrict the allowed values to a predefined set.


64-73: LGTM!

The changes to the ZSuccessfulDocumentResponseSchema look good. The inclusion of the fields property as an array of objects conforming to the ZFieldSchema enhances the response schema by allowing it to encapsulate a collection of field objects. The usage of the pick method to select specific fields from ZFieldSchema is a good practice to keep the response schema concise and focused.

packages/api/v1/implementation.ts (2)

21-21: LGTM!

The import statement is syntactically correct and necessary for using the getFieldsForDocument function.


94-97: LGTM!

The code changes in the getDocument function are syntactically correct and enhance the API functionality by returning the fields associated with the document.

Also applies to: 107-107

@Mythie
Copy link
Collaborator

Mythie commented Sep 2, 2024

Hey there, it looks like you haven't accepted our contributor license agreement yet. In order for us to accept your pull request we ask that you please fill out the CLA:

https://documen.so/cla

@Etrenak Etrenak changed the title feat: return fields in GET /documents and GET /documents/:id endpoints feat: return fields in GET /documents/:id endpoint Sep 4, 2024
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 389c530 and e25fce4.

Files selected for processing (1)
  • packages/api/v1/schema.ts (4 hunks)
Files skipped from review as they are similar to previous changes (1)
  • packages/api/v1/schema.ts

@Mythie
Copy link
Collaborator

Mythie commented Sep 5, 2024

This one looks good, I'll just need to grab a moment to run through the testing on this one manually!

@Etrenak
Copy link
Contributor Author

Etrenak commented Sep 6, 2024

For my use case, I would like to be able to also get fieldMeta returned by this endpoint. However, when I tried to also return the fieldMeta, I encountered some typing issues.

That is, I simply added fieldMeta in the schema like this :

export const ZSuccessfulGetDocumentResponseSchema = ZSuccessfulDocumentResponseSchema.extend({
  recipients: z.lazy(() => z.array(ZSuccessfulRecipientResponseSchema)),
  fields: ZFieldSchema.pick({
    id: true,
    recipientId: true,
    type: true,
    page: true,
    positionX: true,
    positionY: true,
    width: true,
    height: true,
  }).extend({
    fieldMeta: ZFieldMetaSchema,
  }).array(),
});

But since fieldMeta in only known as a JsonValue in the prisma schema, we cannot simply return the result of the query to the database. Please tell me if this is OK to simply do the parsing of the database result in implementation.ts:getDocument into a correct field meta type ? I think it would be better to edit the database schema to indicate that the type is one of the advanced fields meta (Either TEXT meta, or DROPDOWN meta, ...), but it is certainly not the easiest way.

Do you know why it was chosen to store the field meta as a JsonValue in the database, and which of the two solutions I should prefer ?

EDIT : This can definitely be addressed in another PR

Copy link

github-actions bot commented Sep 6, 2024

Thank you for following the naming conventions for pull request titles! 💚🚀

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between e25fce4 and 35502c4.

Files selected for processing (2)
  • packages/api/v1/implementation.ts (3 hunks)
  • packages/api/v1/schema.ts (2 hunks)
Files skipped from review as they are similar to previous changes (2)
  • packages/api/v1/implementation.ts
  • packages/api/v1/schema.ts

@Etrenak
Copy link
Contributor Author

Etrenak commented Sep 6, 2024

So I ended up adding fieldMeta anyway. I hope I did not waste your time on previously testing the PR without it.

I also put back the definition of ZFieldSchema in its original position, since I learned what z.lazy() does (I'm not really familiar with TS). Using z.lazy() seems to mess up the OpenAPI schema generation though ☹️. It only displays fields: "string" instead of the correct schema, but that's not a big issue.

Feel free to tell me if you would like another implementation, my precedent comment still stands.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Outside diff range and nitpick comments (1)
packages/api/v1/implementation.ts (1)

100-117: Handle parsing errors and update API documentation.

The parsing of fieldMeta using ZFieldMetaSchema is a good way to ensure the response contains valid and typed metadata. However, consider the following suggestions:

  1. Add proper error handling for the parsing error. Instead of throwing a generic error, catch the parsing error and throw a more specific error indicating the issue with the field metadata.

  2. Log the parsing error for debugging purposes. This will help identify and fix issues with invalid field metadata stored in the database.

  3. Update the API documentation to reflect the new fields key in the response. Clients need to be aware of this change to handle the response correctly.

Here's an example of how you can handle the parsing error and log it:

  if (field.fieldMeta) {
    const result = ZFieldMetaSchema.safeParse(field.fieldMeta);
      
    if (!result.success) {
      const error = new Error(`Field meta parsing failed for field ${field.id}: ${result.error.message}`);
      console.error(error);
-     throw new Error('Field meta parsing failed for field '   field.id);
      throw error;
    }
      
    parsedMetaOrNull = result.data;
  }
Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 35502c4 and 7f314e1.

Files selected for processing (2)
  • packages/api/v1/implementation.ts (3 hunks)
  • packages/api/v1/schema.ts (2 hunks)
Additional comments not posted (3)
packages/api/v1/schema.ts (2)

58-73: LGTM!

The extension of the ZSuccessfulGetDocumentResponseSchema to include the fields property aligns with the PR objective and maintains consistency with the existing field schema. The selection of specific properties from ZFieldSchema ensures a focused representation of the fields, while the optional fieldMeta property allows for flexibility in including additional metadata.


426-426: Looks good!

Omitting the fields property from the ZSuccessfulSigningResponseSchema helps to maintain a focused and streamlined response structure for signing operations. This change aligns with the separation of concerns between document retrieval and signing functionalities.

packages/api/v1/implementation.ts (1)

95-98: Fetching fields using getFieldsForDocument looks good.

The fields are correctly fetched using the getFieldsForDocument function with the documentId and userId.

@Etrenak
Copy link
Contributor Author

Etrenak commented Nov 9, 2024

Hey, I see this has been approved. Any idea when it might get merged ? Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants