
# TypeScript / ER connection to the PHP database

Do **not** connect ER/TypeScript directly to the cPanel MySQL username/password from the browser or frontend.
Use the PHP REST API in `/api/v1/tickets.php`.

## Why API instead of direct DB connection?

- Your database password stays hidden on the PHP server.
- You can control what ER is allowed to read, create, update, and delete.
- Every change can be logged.
- You can prevent accidental deletion or editing of fields owned by PHP/CX chat/calls.

## ER/TypeScript flow

```txt
ER/TypeScript server
→ HTTPS request to PHP API
→ PHP validates Bearer token
→ PHP reads/updates MySQL
→ PHP returns JSON
```

## Required header

```txt
Authorization: Bearer YOUR_API_TOKEN
Content-Type: application/json
```

## Endpoints

### List tickets

```ts
const res = await fetch('https://yourdomain.com/ushs/api/v1/tickets.php?action=list&source_system=all', {
  headers: { Authorization: `Bearer ${process.env.USHS_API_TOKEN}` }
});
const data = await res.json();
```

### Create/import an ER ticket into PHP view table

```ts
await fetch('https://yourdomain.com/ushs/api/v1/tickets.php?action=create', {
  method: 'POST',
  headers: {
    Authorization: `Bearer ${process.env.USHS_API_TOKEN}`,
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    er_ticket_id: 'ER-10001',
    request_number: 'ER-10001',
    full_name: 'Customer Name',
    phone_number: '5551234567',
    customer_email: 'customer@email.com',
    service_address: '123 Main St',
    zip_code: '30303',
    issue_description: 'Washer not draining',
    status_id: 1,
    source_system: 'typescript_er',
    origin_type: 'ER Ticket'
  })
});
```

### Update a ticket status/details from ER

```ts
await fetch('https://yourdomain.com/ushs/api/v1/tickets.php?action=update&id=123', {
  method: 'PATCH',
  headers: {
    Authorization: `Bearer ${process.env.USHS_API_TOKEN}`,
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    job_status_id: 3,
    er_ticket_id: 'ER-10001',
    issue_description: 'Updated diagnosis from ER'
  })
});
```

### Delete / archive a ticket from PHP view table

```ts
await fetch('https://yourdomain.com/ushs/api/v1/tickets.php?action=delete&id=123', {
  method: 'DELETE',
  headers: { Authorization: `Bearer ${process.env.USHS_API_TOKEN}` }
});
```

Current implementation performs a real delete because you requested delete capability. For production, consider "archive" instead of hard delete.
