Skip to content

fonsecamar/cosmos-inventory-demo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Cosmos DB NoSQL API - Inventory

Introduction

This repository provides a code sample in .NET on how you might use a combination of Azure Functions and Cosmos DB to implement an inventory management process.

This demo contains 2 alternative ways to implement inventory management.

Async Pattern Components:

  • AsyncInventory Function (Http Trigger - Rest API)
  • InventoryProcessor Function (CosmosDB Trigger)
  • inventoryLedger container holds inventory events
  • inventorySnapshot container holds inventory snapshots (current inventory position)
  • leases container used by CosmosDB Trigger

Async Architecture

Sync Pattern:

  • SyncInventory Function (Http Trigger - Rest API)
  • syncInventory container holds inventory events and shapshots as separate documents

Sync Architecture

Requirements to deploy

Setup shell was tested on WSL2 (Ubuntu 22.04.2 LTS)

Setup environment

The setup will provision and configure all the resources required.

  • Sign in with Azure CLI

    az login
  • Clone the repo

    git clone https://github.com/fonsecamar/cosmos-inventory-demo.git
    cd cosmos-inventory-demo/deploy/
  • Run setup.sh with the appropriete parameters. Keep the API's URIs prompted when completed.

Provide a non-existent resource group name. Setup will provision.

#SAMPLE
#./setup.sh 00000000-0000-0000-0000-000000000000 rg-my-demo SouthCentralUS

./setup.sh <subscription id> <resource group> <location>

Setup has some pause stages. Hit enter to continue when prompted.

It takes around 3min to provision and configure resoures.

Resources created:

  • Resource group
  • Azure Blob Storage
  • Azure Cosmos DB account (1 database with 1000 RUs autoscale shared with 4 containers)
  • Azure Functions Basic Plan
  • Azure Log Analytics Workspace
  • Azure Application Insights

Microsoft Fabric is not created or configured by this automation.

For performance/load testing, use containers with dedicated throughput and Elastic Premium Functions.

Running the sample

You can call Function APIs from Azure Portal or your favorite tool.

Async and Sync calls are similar. Just change the api path: /CreateAsyncInventoryEvent or /CreateSyncInventoryEvent, /GetAsyncSnapshot or /GetSyncSnapshot.

  1. Creates initial inventory of a product

    InventoryUpdated event will create or patch InventorySnapshot increasing onHand and availbleToSell quantities.

    curl --request POST "https://api-funcinv<suffix>.azurewebsites.net/api/CreateAsyncInventoryEvent" \
    --header 'Content-Type: application/json' \
    --data-raw '{
        "pk": "1-1000",
        "eventType": "InventoryUpdated",
        "eventDetails": {
            "productId": "1000",
            "nodeId": "1",
            "onHandQuantity": 1000
        }
    }'
  2. Notifies items reserved

    ItemReserved event will patch InventorySnapshot increasing activeCustomerReservations and decreasing availbleToSell quantities. Reservations can only occur if reservedQuantity < availableToSell.

    curl --request POST "https://api-funcinv<suffix>.azurewebsites.net/api/CreateAsyncInventoryEvent" \
    --header 'Content-Type: application/json' \
    --data-raw '{
        "pk": "1-1000",
        "eventType": "ItemReserved",
        "eventDetails": {
            "productId": "1000",
            "nodeId": "1",
            "reservedQuantity": 10
        }
    }'
  3. Notifies order shipped

    OrderShipped event will patch InventorySnapshot decreasing activeCustomerReservations and onHand quantities. Shippments can only occur if shippedQuantity <= activeCustomerReservations.

    curl --request POST "https://api-funcinv<suffix>.azurewebsites.net/api/CreateAsyncInventoryEvent" \
    --header 'Content-Type: application/json' \
    --data-raw '{
        "pk": "1-1000",
        "eventType": "OrderShipped",
        "eventDetails": {
            "productId": "1000",
            "nodeId": "1",
            "shippedQuantity": 10
        }
    }'
  4. Notifies order cancelled

    OrderCancelled event will patch InventorySnapshot decreasing activeCustomerReservations and increasing availableToSell quantities. Cancellations can only occur if cancelledQuantity <= activeCustomerReservations.

    curl --request POST "https://api-funcinv<suffix>.azurewebsites.net/api/CreateAsyncInventoryEvent" \
    --header 'Content-Type: application/json' \
    --data-raw '{
        "pk": "1-1000",
        "eventType": "OrderCancelled",
        "eventDetails": {
            "productId": "1000",
            "nodeId": "1",
            "cancelledQuantity": 10
        }
    }'
  5. Notifies order returned

    OrderReturned event will patch InventorySnapshot increasing returned and onHand quantities.

    curl --request POST "https://api-funcinv<suffix>.azurewebsites.net/api/CreateAsyncInventoryEvent" \
    --header 'Content-Type: application/json' \
    --data-raw '{
        "pk": "1-1000",
        "eventType": "OrderReturned",
        "eventDetails": {
            "productId": "1000",
            "nodeId": "1",
            "returnedQuantity": 10
        }
    }'
  6. Get snapshot

    curl --request GET "https://api-funcinv<suffix>.azurewebsites.net/api/GetAsyncSnapshot/1-1000"

Clean Up

  1. Delete the Resource Group to destroy all resources

How to Contribute

If you find any errors or have suggestions for changes, please be part of this project!

  1. Create your branch: git checkout -b my-new-feature
  2. Add your changes: git add .
  3. Commit your changes: git commit -m '<message>'
  4. Push your branch to Github: git push origin my-new-feature
  5. Create a new Pull Request 😄

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors