# TwentyCRM Custom Object & Field Setup (DRE)

## Deployment
- Installed at `/root/docker/twenty/` via Docker Compose
- Access: `https://crm.debtrecoveryexperts.com`
- API key: `TWENTY_CRM_API_KEY` in `~/.hermes/.env`
- Metadata API endpoint: `POST https://crm.debtrecoveryexperts.com/metadata`
- GraphQL endpoint: `POST https://crm.debtrecoveryexperts.com/graphql`
- REST endpoint: `GET/POST/DELETE https://crm.debtrecoveryexperts.com/rest/{object-name}`

## Critical Config
- `SERVER_URL` in `.env` must be set to the public domain, not `localhost`
- `REACT_APP_SERVER_BASE_URL` must be passed to the `server` container (copy from `SERVER_URL`)
- Port mapped: `3001:3000` in docker-compose (not 3000 directly)

## Custom Objects Created for DRE

### Claims
ID: `0c6808cf-b5e8-4f39-8c4f-6dc6b7d029f3`
Fields: claimNumber (TEXT), claimAmount (NUMBER), debtType (TEXT), currentTier (TEXT), status (TEXT), state (TEXT), dateFiled (DATE), aiScore (NUMBER), description (TEXT)

### Debtors
ID: `bac887ba-22a9-46fe-b988-e1985ee19b29`
Fields: businessName (TEXT), contactName (TEXT), email (TEXT), phone (TEXT), address (TEXT), businessType (TEXT), notes (TEXT)

### Payments
ID: `bde74e7b-b1c2-48eb-be5b-f3ea2207873c`
Fields: paymentAmount (NUMBER), paymentDate (DATE), paymentMethod (TEXT), dreFee (NUMBER), costsDeducted (NUMBER), netToClient (NUMBER)

### Case Notes
ID: `176736a7-6d3b-4129-ac03-67cea102d618`
Fields: content (TEXT), timestamp (DATE)

## Field Creation (GraphQL Metadata API)
```graphql
mutation {
  createOneField(input: {
    field: {
      name: "fieldName", label: "Field Label", type: TEXT,
      objectMetadataId: "uuid-of-object"
    }
  }) { id name }
}
```

## RELATION Field Creation
```graphql
mutation {
  createOneField(input: {
    field: {
      name: "debtorRelation", label: "Debtor", type: RELATION,
      objectMetadataId: "source-object-uuid",
      relationCreationPayload: {
        type: "MANY_TO_ONE",
        targetObjectMetadataId: "target-object-uuid",
        targetFieldLabel: "Related Field Label",
        targetFieldIcon: "IconBriefcase"
      }
    }
  }) { id name }
}
```

## Relationship Creation Status
- Claim → Debtor: MANY_TO_ONE ✓ (confirmed working)
- Payment → Claim: via UI (API needs different payload format)
- Case Note → Claim: via UI

## Deleting Default Data
- Objects via REST: `DELETE https://crm.debtrecoveryexperts.com/rest/{object-name}/{id}`
- Workflows: `DELETE https://crm.debtrecoveryexperts.com/rest/workflows/{id}`
- Two default workflows deleted: "Create company when adding a new person" and "Quick Lead"

## Common Pitfalls
- RELATION type fields need `relationCreationPayload` at the field level
- Field types use UPPERCASE enum names without quotes in GraphQL
- Use `objectMetadataId` NOT `objectId`
- REST endpoints are plural: `/rest/workflows`, `/rest/companies`
- API key is workspace-scoped — one key per workspace
- Portal/UI login and API login are separate sessions
