Skip to content

Get User Documents (api/documents/getUserDocuments.ts)

Endpoint Overview

Retrieves all documents for a specific user and project, including document metadata and associated client information from summaries.

Request Handlers

POST

  • Body:
    {
      userID: string;    // User identifier
      projectID: string; // Project identifier
    }
    
  • Response:
    [
      {
        DocumentID: string;     // Document identifier
        userID: number;        // User ID
        DocumentName: string;   // Document name
        Status: string;        // Document status
        DateCreated: Date;     // Creation timestamp
        LastModified: Date;    // Last modification timestamp
        ClientName: string | null; // Client name from summary
      }
    ]
    
  • Status: 200 OK, 400 Bad Request, 405 Method Not Allowed, 500 Internal Server Error

Error Handling

if (!userID) {
  return res.status(400).json({ error: 'userID is required.' });
}

try {
  const userDocuments = await prisma.fileStore.findMany({...});
  const summaries = await prisma.summary.findMany({...});
  // Process and combine data
} catch (error) {
  res.status(500).json({ error: `Internal server error: ${error}` });
}

Common errors: - 400: Missing userID - 405: Non-POST request method - 500: Database query failure - 500: Data processing error

Usage Example

const response = await fetch("/api/documents/getUserDocuments", {
  method: "POST",
  headers: { 
    "Content-Type": "application/json"
  },
  body: JSON.stringify({
    userID: "123",
    projectID: "proj-456"
  })
});

const documents = await response.json();

Implementation Details

  • Uses Prisma for database queries
  • Joins FileStore and Summary tables
  • Filters by user and project
  • Maps client names to documents
  • Includes document metadata
  • Parses userID as integer
  • Handles missing summaries
  • Returns combined data
  • Selects specific fields

Pages/Components Referenced By

Notes

  • Requires valid user authentication
  • Filters by project ID
  • Includes client information
  • Maintains timestamps
  • Returns array of documents
  • Handles null client names
  • Optimizes data fetching
  • Uses database relations
  • Supports project organization