> ## Documentation Index
> Fetch the complete documentation index at: https://docs.xenia.team/llms.txt
> Use this file to discover all available pages before exploring further.

# Corrective Actions

> Track and resolve issues flagged during inspections

## Overview

Corrective actions are tasks created to address issues identified during inspections (submissions). When inspectors flag non-compliant items, those flags can be converted into actionable tasks for resolution.

**Workflow:**

1. Inspector completes a submission and flags non-compliant items
2. System captures flagged items with notes and photos
3. Manager reviews flagged items report
4. Tasks or work orders are created from flagged items
5. Issues are resolved and verified

***

## Get Flagged Items

Retrieve all flagged (non-compliant) items from submissions.

<CodeGroup>
  ```bash cURL theme={null}
  curl -X POST "https://api.xenia.team/api/v1/ops/workspaces/{workspaceId}/submissions/flagged-items" \
    -H "x-client-key: YOUR_CLIENT_KEY" \
    -H "x-client-secret: YOUR_CLIENT_SECRET" \
    -H "workspace-id: {workspaceId}" \
    -H "Content-Type: application/json" \
    -d '{
      "checklistId": "template-uuid",
      "dateFrom": "2024-12-01",
      "dateTo": "2024-12-31",
      "locationIds": ["location-uuid"]
    }'
  ```
</CodeGroup>

**Request Body:**

| Field             | Type      | Required | Description               |
| ----------------- | --------- | -------- | ------------------------- |
| `checklistId`     | string    | No       | Filter by template        |
| `dateFrom`        | string    | No       | Start date (YYYY-MM-DD)   |
| `dateTo`          | string    | No       | End date (YYYY-MM-DD)     |
| `locationIds`     | string\[] | No       | Filter by locations       |
| `flagCategoryIds` | string\[] | No       | Filter by flag categories |
| `assigneeIds`     | string\[] | No       | Filter by submitter       |

**Response:**

```json theme={null}
{
  "status": "success",
  "code": 200,
  "data": [
    {
      "submissionId": "submission-uuid-1",
      "submissionDate": "2024-12-15T10:30:00Z",
      "submissionNumber": "SUB-2024-001234",
      "template": {
        "id": "template-uuid",
        "title": "Daily Safety Inspection"
      },
      "location": {
        "id": "location-uuid",
        "name": "Building A - Floor 2"
      },
      "submitter": {
        "id": "user-uuid",
        "fullName": "John Smith"
      },
      "flaggedItem": {
        "id": "item-uuid",
        "title": "Fire extinguisher inspection tag current?",
        "sectionTitle": "Fire Safety",
        "answer": "no",
        "flagNote": "Tag expired 2 months ago. Needs immediate replacement.",
        "flagCategory": {
          "id": "category-uuid",
          "name": "Safety Violation"
        },
        "attachments": [
          "https://storage.example.com/photos/expired-tag-001.jpg"
        ]
      }
    },
    {
      "submissionId": "submission-uuid-2",
      "submissionDate": "2024-12-16T14:00:00Z",
      "submissionNumber": "SUB-2024-001248",
      "template": {
        "id": "template-uuid",
        "title": "Daily Safety Inspection"
      },
      "location": {
        "id": "location-uuid",
        "name": "Building A - Floor 2"
      },
      "submitter": {
        "id": "user-uuid-2",
        "fullName": "Jane Doe"
      },
      "flaggedItem": {
        "id": "item-uuid-2",
        "title": "Emergency exit clear and unobstructed?",
        "sectionTitle": "Fire Safety",
        "answer": "no",
        "flagNote": "Boxes blocking exit. Moved to storeroom.",
        "flagCategory": {
          "id": "category-uuid-2",
          "name": "Housekeeping"
        },
        "attachments": []
      }
    }
  ]
}
```

***

## Flag Categories

Flag categories help classify issues for routing and reporting.

### List Flag Categories

<CodeGroup>
  ```bash cURL theme={null}
  curl -X GET "https://api.xenia.team/api/v1/mgt/workspaces/{workspaceId}/flag-categories" \
    -H "x-client-key: YOUR_CLIENT_KEY" \
    -H "x-client-secret: YOUR_CLIENT_SECRET" \
    -H "workspace-id: {workspaceId}"
  ```
</CodeGroup>

**Response:**

```json theme={null}
{
  "status": "success",
  "code": 200,
  "data": [
    {
      "id": "category-uuid-1",
      "name": "Safety Violation",
      "description": "Issues affecting personnel safety",
      "color": "#FF0000"
    },
    {
      "id": "category-uuid-2",
      "name": "Maintenance Required",
      "description": "Equipment needing repair or service",
      "color": "#FFA500"
    },
    {
      "id": "category-uuid-3",
      "name": "Housekeeping",
      "description": "Cleanliness and organization issues",
      "color": "#0000FF"
    },
    {
      "id": "category-uuid-4",
      "name": "Documentation",
      "description": "Missing or expired documentation",
      "color": "#808080"
    }
  ]
}
```

### Create Flag Category

<CodeGroup>
  ```bash cURL theme={null}
  curl -X POST "https://api.xenia.team/api/v1/mgt/workspaces/{workspaceId}/flag-category" \
    -H "x-client-key: YOUR_CLIENT_KEY" \
    -H "x-client-secret: YOUR_CLIENT_SECRET" \
    -H "workspace-id: {workspaceId}" \
    -H "Content-Type: application/json" \
    -d '{
      "name": "Regulatory Compliance",
      "description": "Issues requiring regulatory attention",
      "color": "#9932CC"
    }'
  ```
</CodeGroup>

**Request Body:**

| Field         | Type   | Required | Description                              |
| ------------- | ------ | -------- | ---------------------------------------- |
| `name`        | string | Yes      | Category name                            |
| `description` | string | No       | Category description                     |
| `color`       | string | No       | Hex color code for visual identification |

**Response:**

```json theme={null}
{
  "status": "success",
  "code": 200,
  "data": {
    "id": "new-category-uuid",
    "name": "Regulatory Compliance",
    "description": "Issues requiring regulatory attention",
    "color": "#9932CC",
    "createdAt": "2024-12-20T10:00:00Z"
  }
}
```

### Update Flag Category

<CodeGroup>
  ```bash cURL theme={null}
  curl -X POST "https://api.xenia.team/api/v1/mgt/workspaces/{workspaceId}/flag-category" \
    -H "x-client-key: YOUR_CLIENT_KEY" \
    -H "x-client-secret: YOUR_CLIENT_SECRET" \
    -H "workspace-id: {workspaceId}" \
    -H "Content-Type: application/json" \
    -d '{
      "id": "category-uuid",
      "name": "Updated Category Name",
      "description": "Updated description",
      "color": "#FF6600"
    }'
  ```
</CodeGroup>

<Note>
  Include the `id` field to update an existing category. Without `id`, a new category is created.
</Note>

### Delete Flag Category

<CodeGroup>
  ```bash cURL theme={null}
  curl -X DELETE "https://api.xenia.team/api/v1/mgt/workspaces/{workspaceId}/flag-category/{categoryId}" \
    -H "x-client-key: YOUR_CLIENT_KEY" \
    -H "x-client-secret: YOUR_CLIENT_SECRET" \
    -H "workspace-id: {workspaceId}"
  ```
</CodeGroup>

***

## Create Corrective Action Tasks

Convert flagged items into tasks for resolution.

### Create Task from Flagged Item

<CodeGroup>
  ```bash cURL theme={null}
  curl -X POST "https://api.xenia.team/api/v1/ops/workspaces/{workspaceId}/tasks" \
    -H "x-client-key: YOUR_CLIENT_KEY" \
    -H "x-client-secret: YOUR_CLIENT_SECRET" \
    -H "workspace-id: {workspaceId}" \
    -H "Content-Type: application/json" \
    -d '{
      "title": "Replace expired fire extinguisher tag - Building A Floor 2",
      "description": "Found during Daily Safety Inspection (SUB-2024-001234).\n\nIssue: Tag expired 2 months ago. Needs immediate replacement.",
      "startDate": "2024-12-20T09:00:00Z",
      "dueDate": "2024-12-20T17:00:00Z",
      "priority": "High",
      "assignees": ["maintenance-user-uuid"],
      "locationIds": ["location-uuid"],
      "AssetId": "fire-extinguisher-asset-uuid",
      "attachment": ["https://storage.example.com/photos/expired-tag-001.jpg"]
    }'
  ```
</CodeGroup>

### Create Work Order from Flagged Item

For issues requiring facility maintenance.

<CodeGroup>
  ```bash cURL theme={null}
  curl -X POST "https://api.xenia.team/api/v1/ops/workspaces/{workspaceId}/tasks/work-orders" \
    -H "x-client-key: YOUR_CLIENT_KEY" \
    -H "x-client-secret: YOUR_CLIENT_SECRET" \
    -H "workspace-id: {workspaceId}" \
    -H "Content-Type: application/json" \
    -d '{
      "title": "Repair emergency exit door - Building A Floor 2",
      "description": "Found during Daily Safety Inspection.\n\nIssue: Door not closing properly. Safety hazard.",
      "startDate": "2024-12-20T09:00:00Z",
      "dueDate": "2024-12-21T17:00:00Z",
      "priority": "High",
      "locationIds": ["location-uuid"],
      "ServiceTypeId": "maintenance-category-uuid"
    }'
  ```
</CodeGroup>

***

## Common Workflows

### Automated Corrective Action Processing

```mermaid theme={null}
sequenceDiagram
    participant Cron as Scheduled Job
    participant API as Xenia API
    participant Manager as Manager

    Cron->>API: 1. POST /submissions/flagged-items
    Note right of Cron: Get flagged items from last 24h
    API-->>Cron: List of flagged items

    loop For each flagged item
        Cron->>API: 2. POST /tasks or /tasks/work-orders
        Note right of Cron: Create task with details
        API-->>Cron: Task created
    end

    Manager->>API: 3. GET /tasks/list
    Note right of Manager: Review corrective action tasks
```

### Daily Corrective Action Report

```bash theme={null}
#!/bin/bash
# Generate daily corrective action report

WORKSPACE_ID="your-workspace-uuid"
TODAY=$(date +%Y-%m-%d)
YESTERDAY=$(date -d "yesterday" +%Y-%m-%d)

# Get flagged items from yesterday
FLAGGED=$(curl -s -X POST "https://api.xenia.team/api/v1/ops/workspaces/${WORKSPACE_ID}/submissions/flagged-items" \
  -H "x-client-key: YOUR_KEY" \
  -H "x-client-secret: YOUR_SECRET" \
  -H "workspace-id: ${WORKSPACE_ID}" \
  -H "Content-Type: application/json" \
  -d "{
    \"dateFrom\": \"${YESTERDAY}\",
    \"dateTo\": \"${TODAY}\"
  }")

# Count by category
echo "Flagged Items by Category:"
echo "$FLAGGED" | jq -r '.data | group_by(.flaggedItem.flagCategory.name) | .[] | "\(.[0].flaggedItem.flagCategory.name): \(length)"'

# Count by location
echo ""
echo "Flagged Items by Location:"
echo "$FLAGGED" | jq -r '.data | group_by(.location.name) | .[] | "\(.[0].location.name): \(length)"'

# List high-priority items (Safety Violations)
echo ""
echo "Safety Violations requiring immediate attention:"
echo "$FLAGGED" | jq -r '.data[] | select(.flaggedItem.flagCategory.name == "Safety Violation") | "\(.location.name): \(.flaggedItem.title)"'
```

### Bulk Create Tasks from Flagged Items

```bash theme={null}
#!/bin/bash
# Create corrective action tasks for all flagged items

WORKSPACE_ID="your-workspace-uuid"

# Get unprocessed flagged items
FLAGGED=$(curl -s -X POST "https://api.xenia.team/api/v1/ops/workspaces/${WORKSPACE_ID}/submissions/flagged-items" \
  -H "x-client-key: YOUR_KEY" \
  -H "x-client-secret: YOUR_SECRET" \
  -H "workspace-id: ${WORKSPACE_ID}" \
  -H "Content-Type: application/json" \
  -d '{
    "dateFrom": "2024-12-19",
    "dateTo": "2024-12-20"
  }')

# Process each flagged item
echo "$FLAGGED" | jq -c '.data[]' | while read item; do
  TITLE=$(echo "$item" | jq -r '.flaggedItem.title')
  LOCATION_ID=$(echo "$item" | jq -r '.location.id')
  LOCATION_NAME=$(echo "$item" | jq -r '.location.name')
  NOTE=$(echo "$item" | jq -r '.flaggedItem.flagNote')
  SUBMISSION_NUM=$(echo "$item" | jq -r '.submissionNumber')
  CATEGORY=$(echo "$item" | jq -r '.flaggedItem.flagCategory.name')

  # Set priority based on category
  PRIORITY="Medium"
  if [ "$CATEGORY" == "Safety Violation" ]; then
    PRIORITY="High"
  fi

  # Create task
  curl -s -X POST "https://api.xenia.team/api/v1/ops/workspaces/${WORKSPACE_ID}/tasks" \
    -H "x-client-key: YOUR_KEY" \
    -H "x-client-secret: YOUR_SECRET" \
    -H "workspace-id: ${WORKSPACE_ID}" \
    -H "Content-Type: application/json" \
    -d "{
      \"title\": \"Corrective Action: ${TITLE}\",
      \"description\": \"Source: ${SUBMISSION_NUM}\\nLocation: ${LOCATION_NAME}\\nCategory: ${CATEGORY}\\n\\nIssue: ${NOTE}\",
      \"startDate\": \"$(date -u +%Y-%m-%dT%H:%M:%SZ)\",
      \"dueDate\": \"$(date -u -d '+3 days' +%Y-%m-%dT%H:%M:%SZ)\",
      \"priority\": \"${PRIORITY}\",
      \"locationIds\": [\"${LOCATION_ID}\"]
    }"

  echo "Created corrective action for: ${TITLE}"
done
```

***

## Track Corrective Action Resolution

### Monitor Open Corrective Actions

<CodeGroup>
  ```bash cURL theme={null}
  curl -X POST "https://api.xenia.team/api/v1/ops/workspaces/{workspaceId}/tasks/list" \
    -H "x-client-key: YOUR_CLIENT_KEY" \
    -H "x-client-secret: YOUR_CLIENT_SECRET" \
    -H "workspace-id: {workspaceId}" \
    -H "Content-Type: application/json" \
    -d '{
      "page": 1,
      "pageSize": 50,
      "sort": {
        "field": "dueDate",
        "direction": "ASC"
      },
      "searchText": "Corrective Action",
      "filters": {
        "taskStatus": ["Open", "In Progress", "On Hold"]
      }
    }'
  ```
</CodeGroup>

### Verify Resolution with Follow-up Inspection

Link a follow-up inspection to verify the corrective action was completed.

<CodeGroup>
  ```bash cURL theme={null}
  curl -X POST "https://api.xenia.team/api/v1/ops/workspaces/{workspaceId}/tasks/{taskId}/edit" \
    -H "x-client-key: YOUR_CLIENT_KEY" \
    -H "x-client-secret: YOUR_CLIENT_SECRET" \
    -H "workspace-id: {workspaceId}" \
    -H "Content-Type: application/json" \
    -d '{
      "ChecklistId": "verification-checklist-uuid",
      "isChecklistRequired": true
    }'
  ```
</CodeGroup>

***

## Best Practices

<AccordionGroup>
  <Accordion title="Define clear flag categories">
    Create categories that match your operational needs (Safety, Maintenance, Compliance, etc.). Clear categories enable better routing and reporting.
  </Accordion>

  <Accordion title="Include context in corrective actions">
    When creating tasks from flagged items, include the source submission number, original inspector notes, and any photos. This context helps assignees understand and resolve issues.
  </Accordion>

  <Accordion title="Set appropriate priorities">
    Use flag categories to drive priority. Safety violations should typically be high priority; housekeeping issues may be lower priority.
  </Accordion>

  <Accordion title="Track resolution time">
    Monitor how long corrective actions take to resolve. Chronic delays may indicate systemic issues or resource constraints.
  </Accordion>

  <Accordion title="Verify fixes">
    For critical issues, attach a verification checklist to the corrective action task. This ensures the fix is confirmed before the task is closed.
  </Accordion>

  <Accordion title="Automate where possible">
    Use scheduled jobs to automatically generate corrective action tasks from flagged items. This ensures nothing falls through the cracks.
  </Accordion>
</AccordionGroup>

***

## Reporting Integration

### Export Flagged Items for BI

```bash theme={null}
# Extract flagged items for business intelligence
curl -s -X POST "https://api.xenia.team/api/v1/ops/workspaces/${WORKSPACE_ID}/submissions/flagged-items" \
  -H "x-client-key: YOUR_KEY" \
  -H "x-client-secret: YOUR_SECRET" \
  -H "workspace-id: ${WORKSPACE_ID}" \
  -H "Content-Type: application/json" \
  -d '{
    "dateFrom": "2024-01-01",
    "dateTo": "2024-12-31"
  }' | jq '[.data[] | {
    date: .submissionDate,
    location: .location.name,
    template: .template.title,
    category: .flaggedItem.flagCategory.name,
    item: .flaggedItem.title,
    note: .flaggedItem.flagNote
  }]' > flagged_items_2024.json
```

***

## Related Guides

* [Submission Workflow](/guides/workflows/submission-workflow) - Complete inspections that generate flags
* [Task Management](/guides/workflows/task-management) - Create and manage corrective action tasks
* [Work Orders](/guides/workflows/work-orders) - Facility maintenance requests
* [Submission Exports](/guides/workflows/submission-exports) - Export flagged items reports
