# User

B2B API provides user actions from the `/api/v1/users/` endpoint. It consists of several endpoints and includes endpoints for managing user accounts such as creating, updating profiles and accessing user-specific data.

Users can utilize the Omnitron token to access the B2B system as a superuser.

## Create User

Used to create a user within the system. When a user is generated on the B2B platform, the same user is subsequently created on the Commerce platform using the `/users/registration` endpoint.

Endpoint: `POST` `/api/v1/users/`

**Example Request**

In order to create a new user in the system, a request is sent to the endpoint given above using the header specified below and with the request body parameters:

<table><thead><tr><th width="146.6796875">Property</th><th width="104.3671875">Data Type</th><th width="141.85546875">Required</th><th>Descriptions</th></tr></thead><tbody><tr><td>first_name</td><td>string</td><td>True</td><td>The first name of the user.</td></tr><tr><td>last_name</td><td>string</td><td>True</td><td>The last name of the user.</td></tr><tr><td>email</td><td>string</td><td>True</td><td>The email of the user.</td></tr><tr><td>password</td><td>string</td><td>True</td><td>The password of the user.</td></tr><tr><td>phone_number</td><td>string</td><td>False</td><td>The phone number of the user.</td></tr><tr><td>division</td><td>integer</td><td>True</td><td>The division id of the user.</td></tr></tbody></table>

```
curl --location '{host}/api/v1/users/' \
--header 'Authorization: Token {TOKEN}' \
--header 'Content-Type: application/json' \
--data-raw '{
    "division": 1,
    "email": "firstname.lastname@akinon.com",
    "first_name": "firstname",
    "last_name": "lastname",
    "phone_number": "05XXXXXXXXX",
    "password": "123456"
}'
```

**Example Response (200 OK)**

```json
{
    "id": 1,
    "email": "firstname.lastname@akinon.com",
    "first_name": "firstname",
    "last_name": "lastname",
    "phone_number": "05XXXXXXXXX",
    "division": 1,
    "password": "#hashedpassword#"
}
```

{% hint style="warning" %}

* If the user exists and is a superuser, it updates the first\_name, last\_name, phone\_number and division information. If not a superuser, it raises a UserAlreadyExistsException error.
* If division.is\_active is not true, it gives the error "Deactivated divisions cannot be assigned to users."
* If the Commerce service returns an HTTP status error, it raises an APIConnectionException error.
  {% endhint %}

## Update User

Used to update a user in the system. During the update process, the system first checks if the user is active. If the user is inactive, then verifies whether the division is active. If the division is active, the user is updated as active. However, if the division is not active, a DivisionNotActiveException error is thrown.

Following the update on the B2B side, updates on the Commerce platform using the `/users/profile` endpoint.

**Path Parameter:**

| Property | Required | Descriptions             |
| -------- | -------- | ------------------------ |
| id       | True     | The user id of the user. |

**Body Parameters:**

<table><thead><tr><th width="152.703125">Property</th><th width="119.4140625">Data Type</th><th width="133.77734375">Required</th><th>Descriptions</th></tr></thead><tbody><tr><td>first_name</td><td>string</td><td>False</td><td>The first name of the user.</td></tr><tr><td>last_name</td><td>string</td><td>False</td><td>The last name of the user.</td></tr><tr><td>phone_number</td><td>string</td><td>False</td><td>The phone number of the user.</td></tr><tr><td>division</td><td>integer</td><td>False</td><td>The division id of the user.</td></tr></tbody></table>

**Example Request**

```
curl --location --request PUT '{host}/api/v1/users/1/' \
--header 'Authorization: Token {TOKEN}' \
--header 'Content-Type: application/json' \
--data '{
    "division": 1,
    "first_name": "firstname",
    "last_name": "lastname",
    "phone_number": "5554443322"
}'
```

**Example Response (200 OK)**

```javascript
{
    "id": 1,
    "first_name": "firstname",
    "last_name": "lastname",
    "phone_number": "5554443322",
    "division": 1
}
```

{% hint style="warning" %}
If `division.is_active` is not true, it gives the error "Deactivated divisions cannot be assigned to users."
{% endhint %}

## User List

This process is used to retrieve a list of all users in the system where the remote\_id is not null.

**Example Request**

```javascript
curl --location --request POST '{host}/api/v1/users/' \
--header 'Authorization: Token {TOKEN}' \
```

**Query Parameters:**

<table><thead><tr><th width="139.45703125">Property</th><th width="171.4375">Filter Type</th><th>Description</th></tr></thead><tbody><tr><td>id</td><td>Exact</td><td>Filters users by the exact user ID.</td></tr><tr><td>is_active</td><td>Boolean</td><td>Filters users based on their activation status (True/False).</td></tr><tr><td>email</td><td>Case-insensitive Contain</td><td>Filters users with email containing the specified substring.</td></tr><tr><td>division</td><td>Exact</td><td>Filters users by the exact division id.</td></tr><tr><td>division_type</td><td>Enum</td><td>Filters users using a main, sub</td></tr><tr><td>first_name</td><td>Case-insensitive Contain</td><td>Filters users with the first name containing the specified substring.</td></tr><tr><td>last_name</td><td>Case-insensitive Contain</td><td>Filters users with the last name containing the specified substring.</td></tr><tr><td>phone_number</td><td>Case-insensitive Contain</td><td>Filters users with the phone number containing the specified substring.</td></tr></tbody></table>

**Example Response (200 OK)**

```javascript
{
    "count": 1,
    "next": "{host}/api/v1/users/?limit=20&page=2",
    "previous": null,
    "results": [
        {
            "id": 1,
            "email": "firstname.lastname@test.com",
            "first_name": "firstname",
            "last_name": "lastname",
            "phone_number": "5554443322",
            "division": {
                "id": 1,
                "name": "Akinon Main Division",
                "division_type": "main"
            },
            "is_active": true,
            "shop_token": "########",
            "remote_id": 1,
            "date_joined": "2023-12-14T11:17:22.670111Z",
            "last_login": null
        }
    ]
}
```

## User Detail

This operation is used to retrieve details of a specific user in the system.

**Path Parameter:**

| Property | Required | Descriptions             |
| -------- | -------- | ------------------------ |
| id       | True     | The user id of the user. |

**Example Request**

```
curl --location --request GET '{host}/api/v1/users/1/' \
--header 'Authorization: Token {TOKEN}' \
```

**Example Response (200 OK)**

```javascript
{
    "id": 1,
    "email": "firstname.lastname@akinon.com",
    "first_name": "firstname",
    "last_name": "lastname",
    "phone_number": "5554443322",
    "division": {
        "id": 1,
        "name": "Akinon Main Division",
        "division_type": "main"
    },
    "is_active": true,
    "shop_token": "########",
    "remote_id": 1,
    "date_joined": "2023-12-14T11:17:22.670111Z",
    "last_login": null
}
```

## Activate User

This process is used to activate users. First, it checks whether the division to which the user is affiliated is active. If the division is not active, a DivisionNotActiveException error is raised.

If the division is active, the user is activated. Subsequently, a request is sent to the `/users/{user_id}/` endpoint on the Commerce side to notify that the user has been activated. The request body for this operation is {"is\_active": true}.

**Path Parameters:**

<table><thead><tr><th width="115.71875">Property</th><th width="146.24609375">Required</th><th>Descriptions</th></tr></thead><tbody><tr><td>id</td><td>True</td><td>The user id of the user.</td></tr></tbody></table>

**Example Request**

```
curl --location --request POST '{host}/api/v1/users/1/activate/' \
--header 'Authorization: Token {TOKEN}' \
```

**Example Response (200 OK)**

```json
{}
```

{% hint style="warning" %}
If the Commerce service returns an HTTP status error, it raises an APIConnectionException error.
{% endhint %}

## Deactivate User

This process is used to deactivate users. After deactivating the user, a request is sent to the `/users/{user_id}/` endpoint on the Commerce side to notify that the user has been deactivated. The request body for this operation is {"is\_active": false}

**Path Parameters:**

<table><thead><tr><th width="164.82421875">Property</th><th width="200.40625">Required</th><th>Descriptions</th></tr></thead><tbody><tr><td>id</td><td>True</td><td>The user id of the user.</td></tr></tbody></table>

**Example Request**

```
curl --location --request POST '{host}/api/v1/users/1/deactivate/' \
--header 'Authorization: Token {TOKEN}' \
```

**Example Response (200 OK)**

```json
{}
```

{% hint style="warning" %}
If the Commerce service returns an HTTP status error, it raises an APIConnectionException error.
{% endhint %}

## Update Remote ID

This process is used to update user remote id users. A Celery task is initiated to send a request to the `/users/?email={email}` endpoint on the Commerce side. Subsequently, the task ensures the user's remote\_id information is updated with the received id.

**Path Parameters:**

<table><thead><tr><th width="161.796875">Property</th><th width="161.48046875">Required</th><th>Descriptions</th></tr></thead><tbody><tr><td>id</td><td>True</td><td>The user id of the user.</td></tr></tbody></table>

**Example Request**

```
curl --location --request POST '{host}/api/v1/users/1/update_remote_id/' \
--header 'Authorization: Token {TOKEN}' \
```

**Example Response (200 OK)**

```json
{}
```

{% hint style="warning" %}
If the Commerce service returns an HTTP status error, it raises an APIConnectionException error.
{% endhint %}
