# Customers

Customers are the people who create orders. A customer object must have an email and channel. All necessary information about customers is kept in customer objects. This includes information about communication permissions accepted by customers for example: email\_allowed, sms\_allowed etc.

## Customer Creation

In order to create a customer record on the Omnitron product, any order from the marketplace must be fetched to Omnitron. When the relevant command runs in marketplace integrations, the order is created in Omnitron. At this stage, if the customer record has not been created before, customer information is created; if the customer record has been created before, the customer information is updated (works with the Upsert logic). Service methods are used in these creation and update processes.

In the integration document, the creation of the order and, accordingly, the creation of the customer will be discussed.

**Potential Responses**

* 201 Created
* 400 Bad Request
* 404 Not Found
* 405 Method Not Allowed
* 406 Not Acceptable

### `GET` Customers

Returns all objects in the Customers table paged.

Path: `/api/v1/customers/`

**Filters**

The available filters are as follows.

```json
"filters": {
   "created_date": {
       "type": "DateFilter",
       "lookup_types": [
           "exact",
           "gt",
           "gte",
           "lt",
           "lte",
           "date"
       ]
   },
   "modified_date": {
       "type": "DateFilter",
       "lookup_types": [
           "exact",
           "gt",
           "gte",
           "lt",
           "lte",
           "date"
       ]
   },
   "attributes": {
       "type": "JsonMethodFilter",
       "lookup_types": [
           "exact"
       ]
   },
   "channel": {
       "type": "ModelMultipleChoiceFilter",
       "lookup_types": [
           "exact"
       ]
   },
   "email": {
       "type": "CharFilter",
       "lookup_types": [
           "exact",
           "startswith"
       ]
   },
   "first_name": {
       "type": "CharFilterWithNull",
       "lookup_types": [
           "exact"
       ]
   },
   "last_name": {
       "type": "CharFilterWithNull",
       "lookup_types": [
           "exact"
       ]
   },
   "phone_number": {
       "type": "CharFilterWithNull",
       "lookup_types": [
           "exact"
       ]
   },
   "is_active": {
       "type": "BooleanFilter",
       "lookup_types": [
           "exact"
       ]
   },
   "channel_code": {
       "type": "CharFilterWithNull",
       "lookup_types": [
           "exact"
       ]
   },
   "erp_code": {
       "type": "CharFilterWithNull",
       "lookup_types": [
           "exact"
       ]
   },
   "extra_field": {
       "type": "JsonMethodFilter",
       "lookup_types": [
           "exact"
       ]
   },
   "email_allowed": {
       "type": "BooleanFilter",
       "lookup_types": [
           "exact"
       ]
   },
   "sms_allowed": {
       "type": "BooleanFilter",
       "lookup_types": [
           "exact"
       ]
   },
   "call_allowed": {
       "type": "BooleanFilter",
       "lookup_types": [
           "exact"
       ]
   },
   "date_joined": {
       "type": "IsoDateTimeFilter",
       "lookup_types": [
           "exact"
       ]
   },
   "gender": {
       "type": "ChoiceFilter",
       "lookup_types": [
           "exact"
       ]
   },
   "user_type": {
       "type": "ChoiceFilter",
       "lookup_types": [
           "exact"
       ]
   },
   "date_of_birth": {
       "type": "DateFilter",
       "lookup_types": [
           "exact"
       ]
   },
   "id": {
       "type": "NumberFilter",
       "lookup_types": [
           "exact",
           "gt",
           "gte",
           "lt",
           "lte"
       ]
   },
   "pk": {
       "type": "NumberFilter",
       "lookup_types": [
           "exact",
           "gt",
           "gte",
           "lt",
           "lte"
       ]
   },
   "mapping": {
       "type": "RelatedFilter",
       "lookup_types": "See available filters for Integration Mappings"
   }
}
```

**Request**

```python
import requests
url = "{omnitron}/api/v1/customers/"
payload={}
headers = {
...
}
response = requests.request("GET", url, headers=headers, data=payload)
```

**Response**

```json
{
    "count": 2,
    "next": "http://{customer_omnitron_url}/api/v1/customers/?page=2",
    "previous": null,
    "results": [
        {
            "pk": 1103,
            "channel": 1,
            "email": "test@test.com",
            "first_name": "TEST",
            "last_name": "denemeler",
            "phone_number": null,
            "is_active": true,
            "channel_code": "7",
            "erp_code": "760906",
            "extra_field": {},
            "modified_date": "2022-12-08T10:49:45.794533Z",
            "created_date": "2017-02-02T09:40:28.855012Z",
            "date_joined": "2017-01-30T08:21:20.657225Z",
            "email_allowed": false,
            "sms_allowed": true,
            "call_allowed": true,
            "gender": null,
            "attributes": {},
            "user_type": null,
            "date_of_birth": null,
            "attributes_kwargs": {},
            "localized_attributes": {},
            "localized_attributes_kwargs": {}
        },
        {
            "pk": 1134,
            "channel": 1,
            "email": "test@test.com",
            "first_name": "",
            "last_name": "",
            "phone_number": null,
            "is_active": false,
            "channel_code": "Shop",
            "erp_code": null,
            "extra_field": {},
            "modified_date": "2023-01-17T09:01:27.544053Z",
            "created_date": "2023-01-17T09:01:27.544026Z",
            "date_joined": null,
            "email_allowed": false,
            "sms_allowed": false,
            "call_allowed": false,
            "gender": null,
            "attributes":  {
                "logged_ip": "127.0.0.1"
            },
            "user_type": null,
            "date_of_birth": null,
            "attributes_kwargs": {},
            "localized_attributes": {},
            "localized_attributes_kwargs": {}
            "user_type": "registered"
        },
    ]
}
```

### `POST` Create a Customer

Records new objects in the Customer table. The CustomerSerializer class defined at `omnitron.customers.resources.serializers` is used to validate the data.

Path: `/api/v1/customers/`

**Body**

```json
{
   "email": "test@test.com",
   "channel": 1,
   "channel_code": "1231",
   "gender": "male",
   "user_type": "registered",
   "is_active": false,
   "erp_code": "er_code",
   "email_allowed": true,
   "sms_allowed": true,
   "call_allowed": true,
   "phone_number": "05555555555",
   "attributes": {},
   "extra_field": {},
   "date_of_birth": "2001-01-01"
}

```

**Response**

```json
{
    "pk": 1117,
    "channel": 1,
    "email": "test@test.com",
    "first_name": "",
    "last_name": "",
    "phone_number": "05555555555",
    "is_active": false,
    "channel_code": "1231",
    "erp_code": "er_code",
    "extra_field": {},
    "modified_date": "2021-05-26T13:53:58.585932Z",
    "created_date": "2021-05-26T13:53:58.585910Z",
    "date_joined": null,
    "email_allowed": true,
    "sms_allowed": true,
    "call_allowed": true,
    "gender": "male",
    "attributes": {},
    "user_type": "registered",
    "date_of_birth": "2001-01-01",
    "attributes_kwargs": {},
    "localized_attributes": {},
    "localized_attributes_kwargs": {}
}
```

**Response HTTP 400**

```json
{
    "non_field_errors": [
        "The fields channel, channel_code must make a unique set."
    ]
}

-- OR

{
    "email": [
        "This field is required."
    ],
    "channel": [
        "This field is required."
    ],
    "channel_code": [
        "This field is required."
    ]
}

-- OR

{
    "email": [
        "Enter a valid email address."
    ]
}

```

### `GET` Customer Detail

Returns the object specified with the primary key in the Customers table by serializing it with the CustomerSerializer class defined at `omnitron.customers.resources.serializers`.

Path: `/api/v1/customers/{pk}/`

**Request**

```json
url = "{customer_omnitron_url}/api/v1/customers/1135/"
payload={}
headers = {
 'Authorization':...'
}

response = requests.request("GET", url, headers=headers, data=payload)
```

**Successful Response**

```json
{
    "pk": 1117,
    "channel": 1,
    "email": "test@test.com",
    "first_name": "",
    "last_name": "",
    "phone_number": "05555555555",
    "is_active": false,
    "channel_code": "1231",
    "erp_code": "er_code",
    "extra_field": {},
    "modified_date": "2021-05-26T13:53:58.585932Z",
    "created_date": "2021-05-26T13:53:58.585910Z",
    "date_joined": null,
    "email_allowed": true,
    "sms_allowed": true,
    "call_allowed": true,
    "gender": "male",
    "attributes": {},
    "user_type": "registered",
    "date_of_birth": "2001-01-01",
    "attributes_kwargs": {},
    "localized_attributes": {},
    "localized_attributes_kwargs": {}

}

```

**Failed Response**

```json
{
    "detail": "Not found."
}
```

### `PATCH` Update Customer

Updates the object of the relevant primary key in the Customer table with the provided parameters.

Path: `/api/v1/customers/{pk}/`

```json
{
   "is_active": true,
   "email_allowed": false,
   "sms_allowed": false,
   "user_type": "registered"
}
```

**Response**

```json
{
    "pk": 1117,
    "channel": 1,
    "email": "test@test.com",
    "first_name": "",
    "last_name": "",
    "phone_number": "05555555555",
    "is_active": true,
    "channel_code": "1231",
    "erp_code": "er_code",
    "extra_field": {},
    "modified_date": "2021-05-26T14:24:11.776744Z",
    "created_date": "2021-05-26T13:53:58.585910Z",
    "date_joined": null,
    "email_allowed": false,
    "sms_allowed": false,
    "call_allowed": true,
    "gender": "female",
    "attributes": {},
    "user_type": "registered",
    "date_of_birth": "2001-01-01",
    "attributes_kwargs": {},
    "localized_attributes": {},
    "localized_attributes_kwargs": {}
}
```

```
HTTP 406  
```

```json
{
    "non_field_errors": "channel:channel_name These field cannot be updated",
    "error_code": "customer_100_3"
} 
```

### `PUT` Update Customer

Allows to change the information of the relevant primary key in the Customer table. Mandatory parameters must be sent.

Path: `/api/v1/customers/{pk}/`

```json
{
    "email": "test2@test.com",
    "channel": 1,
    "channel_code": "1231",
    "is_active": true,
    "email_allowed": false,
    "sms_allowed": false,
    "user_type": "registered",
    "extra_field": {}
}
```

**Successful Response**

```json
{
    "pk": 1117,
    "channel": 1,
    "email": "test2@test.com",
    "first_name": "",
    "last_name": "",
    "phone_number": "05555555555",
    "is_active": true,
    "channel_code": "1231",
    "erp_code": "er_code",
    "extra_field": {},
    "modified_date": "2021-05-26T14:26:52.809180Z",
    "created_date": "2021-05-26T13:53:58.585910Z",
    "date_joined": null,
    "email_allowed": false,
    "sms_allowed": false,
    "call_allowed": true,
    "gender": "female",
    "attributes": {},
    "user_type": "registered",
    "date_of_birth": "2001-01-01",
    "attributes_kwargs": {},
    "localized_attributes": {},
    "localized_attributes_kwargs": {}
}
```

**Failed Response**

If we remove the email data from the body example, the following response will be returned.

```
HTTP 400  
```

```json
{
    "extra_field": [
        "This field is required."
    ],
    "channel": [
        "This field is required."
    ],
    "channel_code": [
        "This field is required."
    ]
}
```

**Note:** When we change the channel information and send a PUT or PATCH request in the body example, the following response will be returned.

```
HTTP 406  
```

```json
{
    "non_field_errors": "channel:channel_name These field cannot be updated",
    "error_code": "customer_100_3"
} 
```
