# Link files to a shell case (v2 - improved error handling)

Links sample files from S3 to an existing shell case.
**Version 2 Improvements:**
This endpoint provides enhanced error handling with clear, actionable error messages:
- **Sample validation**: If the sample doesn't exist, returns a clear "not found" error with the sample name
- **File type validation**: If an unrecognized file type is provided, lists the supported types
- **File existence validation**: If files aren't found in S3, lists the specific files that are missing and the configured buckets
- **File requirements validation**: If insufficient files are provided, explains what file types are needed
- **Duplicate files validation**: If the sample already has files, lists the existing file types

Once linked, the system will automatically:
- Transfer and process the files
- Trigger appropriate annotation pipelines (VCF annotation, SV annotation, etc.)
- Update the case status

**Supported File Types:**
- `VCF_SHORT` - Short variant VCF
- `VCF_SV` - Structural variant VCF
- `VCF_LOH` - Loss of heterozygosity VCF
- `VCF_RNA` - RNA VCF
- `VCF_REPEATS` - Repeat expansion VCF
- `FASTQ` - DNA FASTQ files
- `FASTQ_RNA` - RNA FASTQ files
- `BAM` - DNA BAM file
- `BAI` - DNA BAM index
- `CSV`, `TSV` - Delimited files
- `BW`, `BIGWIG` - BigWig files
- `ORA` - ORA compressed files
- `GFF3` - GFF3 annotation files
- `BED` - BED region files
- `JSON`, `OTHER` - Other file types

**Common Error Responses:**
- `"Sample 'SampleName' not found"` - The sample doesn't exist
- `"Cannot link sample files to shell case 'SampleName' since it already contains sample files (VCF, FASTQ)"` - Sample already has files
- `"Unrecognized file type(s): XYZ. Supported types: VCF_SHORT, VCF_SV, ..."` - Invalid file type specified
- `"Sample files not found in configured buckets (bucket1, bucket2): file1, file2"` - Files don't exist in S3
- `"Insufficient sample files: Expected at least 2 file(s) of type FASTQ but found 1"` - Not enough files for the assay requirements
- `"No AWS buckets configured for organization"` - Organization doesn't have upload buckets configured

Endpoint: POST /v2/shell_case/link
Version: 96
Security: bearerAuth

## Security:

  - `bearerAuth` (unknown)
    http bearer JWT

## Request fields (application/json):

  - `sample_name` (string, required)
    Sample name to link files to
    Example: Sample_001

  - `sample_files_aws_details` (array)
    Files to link
    Example: [{"key":"uploads/sample_001.vcf.gz","type":"VCF_SHORT"}]

  - `sample_files_aws_details.key` (string, required)
    S3 key (path) to the file
    Example: uploads/2024/sample_001/sample_001.vcf.gz

  - `sample_files_aws_details.type` (string, required)
    File type
    Enum: "VCF_SHORT", "VCF_SV", "VCF_LOH", "VCF_RNA", "VCF_REPEATS", "FASTQ", "FASTQ_RNA", "BAM", "BAI", "CSV", "TSV", "BW", "BIGWIG", "ORA", "GFF3", "BED", "JSON", "OTHER"

## Response 200:

  - `200` (unknown)
    Files linked successfully

## Response 200 fields (application/json):

  - `success` (boolean, required)
    Whether linking was successful
    Example: true

## Response 400:

  - `400` (unknown)
    Bad request - validation failed. Common causes:
- Sample not found
- Sample already has files
- Invalid file type
- Files not found in S3
- Insufficient files for assay requirements

## Response 400 fields (application/json):

  - `error` (string, required)
    Error type
    Example: Not Found

  - `message` (string, required)
    Human-readable error description
    Example: Analysis with ID 12345 was not found

## Response 401:

  - `401` (unknown)
    Authentication failed - invalid or missing token

## Response 401 fields (application/json):

  - `error` (string, required)
    Error type
    Example: Not Found

  - `message` (string, required)
    Human-readable error description
    Example: Analysis with ID 12345 was not found

## Response 404:

  - `404` (unknown)
    Resource not found

## Response 404 fields (application/json):

  - `error` (string, required)
    Error type
    Example: Not Found

  - `message` (string, required)
    Human-readable error description
    Example: Analysis with ID 12345 was not found

## Response 429:

  - `429` (unknown)
    Rate limit exceeded

## Response 429 fields (application/json):

  - `error` (string, required)
    Error type
    Example: Not Found

  - `message` (string, required)
    Human-readable error description
    Example: Analysis with ID 12345 was not found

## Response 429 headers (application/json):

  - `Retry-After` (integer)
    Number of seconds to wait before retrying

## Response 400 examples:

  - `Sample not found` (unknown)

  - `Sample already has files` (unknown)

  - `Invalid file type` (unknown)

  - `Files not found in S3` (unknown)

  - `Insufficient files` (unknown)

