Login with ReadCloud
```sequence
SuperAdmin -> NinjaWeb: Go to ReadyCloud setting page
SuperAdmin -> NinjaWeb: Click sync with ReadyCloud button
NinjaWeb -> ReadyCloud: Redirect to authorize page with scope have all permission we need
SuperAdmin -> ReadyCloud: Login with ReadyCloud account
SuperAdmin -> ReadyCloud: Allow Ninja to access ReadyCloud account resource
ReadyCloud -> NinjaWeb: Redirect to sync with ReadyCloud result page
NinjaWeb -> NinjaWeb: Read access_token and expires_in in the parameter
NinjaWeb -> ReadyCloud: Read list organizations
NinjaWeb -> SuperAdmin: Show list ReadyCloud organizations
SuperAdmin -> NinjaWeb: Choose organizations to sync
NinjaWeb -> NinjaAPI: Request sync ReadyCloud organization data
NinjaAPI -> Database: Save ReadyCloud organizations info and access_token
NinjaAPI -> RabbitMQ: Trigger message to register webhook
NinjaAPI --> NinjaWeb: Response success
NinjaWeb -> SuperAdmin: Response success
```
Register webhook
```sequence
RabbitMQ -> NinjaAPI: Trigger event
NinjaAPI -> ReadyCloud: Register webhooks (for all orders, boxs...)
ReadyCloud --> NinjaAPI: Response webhook info and webhook token
NinjaAPI -> Database: Save webhook info and webhook token
```
Refresh ReadyCloud token
```sequence
SuperAdmin -> NinjaWeb: Go to ReadyCloud setting page
SuperAdmin -> NinjaWeb: Click refresh ReadyCloud token button
NinjaWeb -> ReadyCloud: Redirect to authorize page with scope have all permission we need
SuperAdmin -> ReadyCloud: Login with ReadyCloud account
SuperAdmin -> ReadyCloud: Allow Ninja to access ReadyCloud account resource
ReadyCloud -> NinjaWeb: Redirect to sync with ReadyCloud result page
NinjaWeb -> NinjaWeb: Read access_token and expires_in in the parameter
NinjaWeb -> NinjaAPI: Request refresh ReadyCloud token
NinjaAPI -> Database: Get ReadyCloud organization info
NinjaAPI -> ReadyCloud: Check organization
NinjaAPI -> Database: If valid organization, save new access_token and expires_in
NinjaAPI -> RabbitMQ: If the old ReadyCloud token expired, trigger message request handle failure webhook message for ReadyCloud organization
NinjaAPI --> NinjaWeb: Response
NinjaWeb -> SuperAdmin: Response success
```
Receive data from webhook
```sequence
ReadyCloud -> Webhook Function: Request API to trigger event (create, update, delete)
Webhook Function -> RabbitMQ: Trigger message save to datalake
RabbitMQ -> NinjaAPI: Received message save to datalake
NinjaAPI -> Database: Get webhook data (can use cache)
NinjaAPI -> NinjaAPI: Compare webhook token with token from ReadyCloud request
NinjaAPI -> Datalake: Save data
```
Read ReadyCloud data from Datalake (every 5 minutes)
```sequence
NinjaAPI -> Datalake: Get records which weren't handled
NinjaAPI -> Datalake: Update status of records to InProgress and save last update time
NinjaAPI -> RabbitMQ: Trigger message handle ReadyCloud data for each Datalake record
```
Handle webhook message
```sequence
RabbitMQ -> NinjaAPI: Trigger event
NinjaAPI -> NinjaAPI: Transform message data
NinjaAPI -> Database: Get relation data (like box in order)
Note over NinjaAPI: If there is no relation data
NinjaAPI -> ReadyCloud: Get relation data
Note over NinjaAPI: If ReadyCloud access token expired
NinjaAPI -> Datalake: Update status of Datalake records to token expired
NinjaAPI -> SuperAdmin: Send notification (via email,...) if didn't send yet
NinjaAPI -> NinjaAPI: End
Note over NinjaAPI: If ReadyCloud response relation data
NinjaAPI -> Database: Create record for relation data
NinjaAPI -> Database: Create or update record for object from webhook
NinjaAPI -> Datalake: Delete processed Datalake records
NinjaAPI -> RabbitMQ: Trigger message get rate and verify address
```
Automatic get rate and verify address
```sequence
RabbitMQ -> NinjaAPI: Trigger event
NinjaAPI -> ReadyCloud: Get rate
NinjaAPI -> ReadyCloud: Verify address
NinjaAPI -> Database: Save rate and verify address result
```
Handle failure webhook message
```sequence
RabbitMQ -> NinjaAPI: Trigger event handle failure webhook message with ReadyCloud organization ID
NinjaAPI -> Datalake: Get all token expired Datalake records of ReadyCloud organization
NinjaAPI -> Datalake: Update status of records to InProgress and save last update time
NinjaAPI -> RabbitMQ: Trigger message handle ReadyCloud data for each Datalake record
```