# POS sync inventory

## Sync inventory

<mark style="color:green;">`POST`</mark> `https://integrations.mipos.shop/api/v1/inventory/pos/store/{store_id}/system/sync`

Send inventory content from the POS to the inventory system.&#x20;

#### Path Parameters

| Name      | Type   | Description                                     |
| --------- | ------ | ----------------------------------------------- |
| store\_id | string | Id of the store wich you want to sync inventory |

#### Headers

| Name          | Type   | Description            |
| ------------- | ------ | ---------------------- |
| Authorization | string | Bearer {access\_token} |
| Content-type  | string | application/json       |

#### Request Body

| Name                  | Type   | Description                                                  |
| --------------------- | ------ | ------------------------------------------------------------ |
| user\_id\_tps         | string | ID of the user who owns the inventory system                 |
| items                 | array  | Items from your POS inventory                                |
| inventory\_system\_id | string | ID of the inventory system with wich you want to synchronize |
| units                 | array  | Units of measurement of your POS system                      |
| suppliers             | array  | Suppliers that you manage in your POS system                 |

{% tabs %}
{% tab title="200 " %}

```javascript
{
    "message": "Synchronization data was sent to the inventory system.",
    "data": null
}
```

{% endtab %}

{% tab title="409 " %}

```javascript
{
    "message": [
        "You don't have this store available.",
        "The third_party_id does not have an Inventory System role or does not exist.",
        "No integration record was found between this user store and the third party system.",
        "External id not found for this store in the inventory system.",
        "The item must have a name.",
        "The item must have an external id.",
        "The item must have an purchase unit external id.",
        "The item must have an consumption unit external id.",
        "The item must have a supplier external id.",
        "The item must have a cost.",
        "The item must have a stock.",
        "The unit must have a name.",
        "The unit must have a short name.",
        "The unit must have an external id.",
        "The supplier must have a name.",
        "The supplier must have an external id.",
        "Item purchase unit external id (____) not found in units array",
        "The item purchase unit external id must be a string.",
        "The item consumption unit external id must be a string.",
        "Item supplier external id (____) not found in suppliers array",
        "The item cost must be a string.",
        "The item stock must be a string."
    ],
    "data": null
}
```

{% endtab %}

{% tab title="422 " %}

```javascript
{
    "items": [
        "The items field is required."
    ],
    "inventory_system_id": [
        "The inventory system id field is required.",
        "The inventory system id must be a string."
    ],
    "units": [
        "The units field is required."
    ],
    "suppliers": [
        "The suppliers field is required."
    ],
    "user_id_tps": [
        "The user id tps field is required."
    ]
}
```

{% endtab %}
{% endtabs %}

### Body example

```javascript
{
	"items": [
		{
			"name": "Testing item 1",
			"external_id": "item_abc123",
			"purchase_unit_external_id": "pos_u1",
			"consumption_unit_external_id": "pos_u2",
			"supplier_external_id": "pos_sup_1",
			"cost": "1250",
			"stock": "11.22"
		},
		{
			"name": "Testing item 2",
			"external_id": "item_abc456",
			"purchase_unit_external_id": "pos_u2",
			"consumption_unit_external_id": "pos_u2",
			"supplier_external_id": "pos_sup_1",
			"cost": "100",
			"stock": "5.50"
		}
	],
	"user_id_tps": "30",
	"inventory_system_id": "3d40a24e-d570-4598-ae68-e20687edd1f9",
	"units": [
		{
			"name": "Unit 1",
			"short_name": "u1",
			"external_id": "pos_u1"
		},
		{
			"name": "Unit 2",
			"short_name": "u2",
			"external_id": "pos_u2"
		}
	],
	"suppliers": [
		{
			"name": "supplier 1",
			"external_id": "pos_sup_1"
		}	
	]
}
```

## Send update of stock of an item

<mark style="color:green;">`POST`</mark> `https://integrations.mipos.shop/api/v1/inventory/pos/store/{store_id}/system/stock/update`

#### Path Parameters

| Name      | Type   | Description                                     |
| --------- | ------ | ----------------------------------------------- |
| store\_id | string | Id of the store wich you want to sync inventory |

#### Headers

| Name          | Type   | Description            |
| ------------- | ------ | ---------------------- |
| Authorization | string | Bearer {access\_token} |
| Content-type  | string | application/json       |

#### Request Body

| Name                  | Type    | Description                                                 |
| --------------------- | ------- | ----------------------------------------------------------- |
| user\_id\_tps         | string  | Id of the user who owns the inventory system                |
| inventory\_system\_id | string  | Id of the inventory system with wich you want to syncronize |
| current\_stock        | string  | Current stock of the item in your POS system.               |
| quantity              | string  | Stock movement quantity.                                    |
| date                  | string  | Movement date.                                              |
| movement\_type        | integer | Movement type(1: add, 2: remove)                            |
| external\_id          | string  | ID of the item in your POS system                           |

{% tabs %}
{% tab title="204 " %}

```javascript
```

{% endtab %}

{% tab title="409 " %}

```javascript
{
    "message": [
        "This user_id_tps does not own this inventory system.",
        "No integration record was found between this user store and the third party system.",
        "External id not found for this store in the inventory system.",
        "This type of webhook is not available.",
        "This inventory system does not have this type of webhook available."

    ],
    "data": null
}
```

{% endtab %}

{% tab title="422 " %}

```javascript
{
    "user_id_tps": [
        "The user id tps field is required."
    ],
    "inventory_system_id": [
        "The inventory system id field is required."
    ],
    "current_stock": [
        "The current stock field is required."
    ],
    "quantity": [
        "The quantity field is required."
    ],
    "date": [
        "The date field is required."
    ],
    "movement_type": [
        "The movement type field is required.",
        "The selected movement type is invalid."
    ]
}
```

{% endtab %}
{% endtabs %}

{% hint style="warning" %}
Any other error response you receive from this endpoint comes directly from the inventory system.
{% endhint %}

### Body example

```javascript
{
	"user_id_tps": "30",
	"inventory_system_id": "4344be8c-c6b9-4c51-88a6-d620144ab406",
	"current_stock": "12.44",
	"quantity": "1.25",
	"date": "2020-05-30 12:45:03",
	"movement_type": 2
}
```

## Webhooks

The inventory system can send different types of data about its  items, the POS system that you have registered can receive this data by registering the urls of the webhooks where it wants to receive each of these types.\
\
Types of information that the POS sends:

* Type 3: Receive the stock update of an inventory item

## Manage Webhook

<mark style="color:green;">`POST`</mark> `https://integrations.mipos.shop/api/v1/inventory/pos/webhook/manage`

Create or update inventory system webhooks.

#### Headers

| Name          | Type   | Description            |
| ------------- | ------ | ---------------------- |
| Authorization | string | Bearer {access\_token} |
| Content-type  | string | application/json       |

#### Request Body

| Name | Type   | Description      |
| ---- | ------ | ---------------- |
| type | string | Webhook type (3) |
| url  | string | Webhook URL      |

{% tabs %}
{% tab title="200 " %}

```javascript
{
    "message": "Webhook successfully created.",
    "data": null
}
```

{% endtab %}

{% tab title="409 " %}

```javascript
{
    "message": [
        "Invalid type.",
    ],
    "data": null
}
```

{% endtab %}

{% tab title="422 " %}

```javascript
{
    "type": [
        "The type field is required.",
        "The selected type is invalid."
    ],
    "url": [
        "The url field is required."
    ]
}
```

{% endtab %}
{% endtabs %}

{% hint style="warning" %}
These webhook requests will send a header called **X-Signature-Request**, as a security measure to confirm that the request came from our system.
{% endhint %}

### Webhook(Type 3 - Receive stock update item)

{% hint style="info" %}
The value of this field is a hexadecimal HMAC signature of the body request payload, using the client secret as a key(generated on account creation) and SHA256 as the hash function.

Example

Using the body example

client\_secret:

9468da7ccaed13055abcf183fb05c73e8cfc48c5

Signature: 7c38c67ad234c4cdbe71425b5c961805ac3955d87de40d1d63e09f1f76d1ff61
{% endhint %}

#### Content Body

| Key                   | Value                                             |
| --------------------- | ------------------------------------------------- |
| pos\_id               | ID of your POS                                    |
| inventory\_system\_id | ID of the inventory system that sending the event |
| quantity              | Stock movement quantity                           |
| date                  | Inventory movement date                           |
| movement\_type        | Movement type(1: add, 2: remove)                  |
| external\_id          | ID of the item in your POS system                 |
| now                   | Date sent to generate the signature               |
| store\_id             | Id of the store in your POS system                |
| current\_stock        | Current stock of the item in the inventory system |

#### JSON received example

```javascript
{
    "pos_id": "1",
    "inventory_system_id": "4344be8c-c6b9-4c51-88a6-d620144ab406",
    "current_stock": "12.44",
    "quantity": "1.25",
    "date": "2020-05-30 12:45:03",
    "movement_type": 2,
    "external_id": "avcc123",
    "now": "2020-06-01 02:55:18",
    "store_id": 1
}
```

#### Expected response

Your service should POST a `204` response status code with an empty response body to acknowledge receipt of the webhook event. If an error occurs in your POS system, the response must be returned with the respective status code and the body must include a field called message, this will be taken to show it to the Inventory System.
