# Create Customer API (TSHIRTORDER-1653)

Creates a customer that belongs to a channel.

| | |
|---|---|
| **Method / URL** | `POST /api/v1/customers/` |
| **Auth** | `Authorization: Bearer <access token>` (32 chars, `[0-9a-z]`) |
| **Content-Type** | `application/json` (form-data / x-www-form-urlencoded also accepted) |
| **Controller** | `CustomerController::add()` → `CustomerService::add()` |

> Request data is the merge of JSON body, form body and query string
> (`ApiService::getRequestData()`).

## Authentication

- Missing / malformed / unknown / expired token → `401 {"message": "Invalid token authorization"}`.
- A token flagged `lockForOnlyIntegration` is **rejected** by this endpoint (it is not an integration endpoint).

## Request body

### Required

| Field | Type | Notes |
|---|---|---|
| `name` | string | NotNull |
| `email` | string | NotNull, must be a valid email |
| `customerNr` | string | NotNull **after** defaulting — see below. Unique per (`customerNr`, `channelId`) |

`customerNr` may be omitted: the server generates `customerCount` from the `customer` auto-counter and
uses it as `customerNr`.

### Optional

| Field | Type | Notes |
|---|---|---|
| `channelId` | int | Channel the customer belongs to. Fills `channelName` / `channelShortName` automatically. An unknown id is **silently ignored** (customer is created without a channel) |
| `customerCount` | string | Normally omit; auto-generated |
| `typeId` | int | Must be a `StatusList` of type `customer_type`; otherwise ignored. `type` (name) is filled automatically |
| `statusId` | int | Must be a `StatusList` of type `customer_status`; otherwise ignored. `status` (name) is filled automatically |
| `active` | bool | Defaults to `false` when omitted |
| `address`, `addressOther`, `postCode`, `city`, `country` | string | |
| `mobile`, `orgNrPersonNr` | string | |
| `invoiceEmail`, `kickbackInvoiceEmail` | string | |
| `bankKonto`, `bankGiro`, `informationText` | string | |
| `defaultPaymentTypeId` | int | |
| `customerUniqueId` | string | External unique id |
| `contacts` | array | See below |
| `deliveryAddresses` | array | See below |

Any other key that matches a setter on the `Customer` entity is also written, so do not forward
untrusted client data blindly.

#### `contacts[]`

All three keys are required for every item (a missing key causes a server error).

| Field | Type | Notes |
|---|---|---|
| `name` | string | |
| `email` | string | Lower-cased; unique per customer |
| `mobile` | string | |

#### `deliveryAddresses[]`

| Field | Type |
|---|---|
| `id` | int (omit on create) |
| `name` | string |
| `deliveryAddressOther` | string |
| `contactName` | string |
| `contactEmail` | string |
| `mobileNr` | string |
| `deliveryAddress` | string |
| `levAddress` | string |
| `deliveryPostNr` | string |
| `deliveryCity` | string |
| `deliveryCountry` | string |

## Example

```http
POST /api/v1/customers/
Authorization: Bearer 0123456789abcdef0123456789abcdef
Content-Type: application/json

{
  "channelId": 12,
  "name": "ACME AB",
  "email": "info@acme.se",
  "typeId": 5,
  "statusId": 2,
  "active": true,
  "address": "Storgatan 1",
  "postCode": "111 22",
  "city": "Stockholm",
  "country": "SE",
  "contacts": [
    { "name": "Anna", "email": "anna@acme.se", "mobile": "0701234567" }
  ],
  "deliveryAddresses": [
    {
      "name": "Warehouse",
      "deliveryAddress": "Lagervägen 5",
      "deliveryPostNr": "123 45",
      "deliveryCity": "Stockholm",
      "deliveryCountry": "SE"
    }
  ]
}
```

## Response

### 200 OK

```json
{
  "id": 1001,
  "channelId": 12,
  "channelName": "ACME Shop",
  "channelShortName": "ACME",
  "invoiceEmail": null,
  "customerNr": "1001",
  "countNr": "1001",
  "name": "ACME AB",
  "email": "info@acme.se",
  "address": "Storgatan 1",
  "addressOther": null,
  "city": "Stockholm",
  "country": "SE",
  "mobile": null,
  "orgNrPersonNr": null,
  "postCode": "111 22",
  "status": "Active",
  "statusId": 2,
  "type": "B2B",
  "typeId": 5,
  "active": true,
  "defaultPaymentTypeId": null,
  "informationText": null,
  "bankKonto": null,
  "bankGiro": null,
  "kickbackInvoiceEmail": null,
  "contacts": [
    { "id": 77, "name": "Anna", "mobile": "0701234567", "email": "anna@acme.se" }
  ],
  "deliveryAddresses": [ { "id": 31, "name": "Warehouse", "...": "..." } ]
}
```

### Errors

| Status | Body | Cause |
|---|---|---|
| 400 | `{"<field>": "<message>"}` | Validation failed (`name`/`email`/`customerNr` null, invalid email, duplicate `customerNr` in the channel) |
| 401 | `{"message": "Invalid token authorization"}` | Bad token |
| 500 | `{"message": "<exception message>"}` | Unexpected error (e.g. malformed `contacts`) |

## Related: public create (no token)

`POST /api/v1/customers/public-add` — **no authentication on purpose**; used by the frontend to let a new
customer register without logging in. Only works for the channel named `PODDY`.

- Required: `email` (trimmed + lower-cased; must not already exist in the PODDY channel → 400 `Email already exists`).
- Server forces: `channelId` = PODDY channel, `active = true`, `statusId` = the `Inactive` customer status.
- Server **discards** these client-sent fields: `channelId`, `customerNr`, `typeId`, `type`, `statusId`,
  `status`, `active`, `informationText`, `defaultPaymentTypeId`, `bankKonto`, `bankGiro`.
- Then goes through the same logic as the authenticated create (so `name` is still required, `customerNr` is auto-generated).
- `500` if the PODDY channel cannot be found.

## Gotchas

- `channelId`, `typeId` and `statusId` that do not resolve are dropped silently — always verify the response.
- Uniqueness of `customerNr` is per channel; omitting `channelId` makes it unique among customers without a channel.
- `email` is **not** unique per channel on this endpoint (that check is commented out in the entity).
- `contacts` / `deliveryAddresses` are only touched when the key is present in the payload.

## Related files

| File | Purpose |
|---|---|
| `src/Application/ApiBundle/Resources/config/route/customer.yaml` | Route definitions |
| `src/Application/ApiBundle/Controller/CustomerController.php` | `add()`, `publicAdd()` |
| `src/Service/CustomerService.php` | `add()`, `addPublicForPoddy()`, `generateUpdate()`, `validateData()` |
| `src/Entity/Customer.php` | Fields + validation constraints |
