Skip to content

Shopping cart with RESTFul API and Message Broker

License

Notifications You must be signed in to change notification settings

permaficus/lef4cart

Repository files navigation

LEF4CART

Overview

Lef4Cart is a micro shopping cart app that is FREE and open source. One of its standout features is the use of RabbitMQ as a message broker. Additionally, Lef4Cart uses the standard HTTP protocol for data transmission

See environment example

ENVIRONMENT EXAMPLE

# USING AT DOCKER CONTAINER
DATABASE_URL="mongodb://username:password@host.docker.internal:27017/shoppingCart?retryWrites=true&authSource=admin&directConnection=true"
# USING AT LOCALHOST
DATABASE_URL="mongodb://username:password@localhost:27017/shoppingCart?retryWrites=true&authSource=admin&directConnection=true"
## APP ENV
NODE_ENV="DEVELOPMENT"

## RABBITMQ ENV
RBMQ_URL="amqp://username:password@localhost:5672"
# CONFIG FOR CONSUMER
RBMQ_CART_EXCHANGE="lef4cart"
RBMQ_CART_QUEUE="sub.cartMessageQueue"
RBMQ_CART_ROUTING_KEY="cartMessageRoutingKey"
# CONFIG FOR PUBLISHER
RBMQ_PUB_QUEUE="pub.cartMessageQueue"
RBMQ_PUB_ROUTING_KEY="pub.MessageRouting"
## LOCAL SERVICE ENV
SERVICE_LOCAL_PORT="8081"

## DOCKER ENV
COMPOSE_PROJECT_NAME="shopping-cart"

API

Endpoint :

  • /v1/shopping-cart : method would be POST, PATCH, DELETE
  • /v1/shopping-cart/{userId} : method GET

POST Body JSON attribute
  {
    "task": "create",
    "payload": {
      "apps_id": "6606cc8ed9c25c6c5f00b48b",
      "merchant_id": "5798426b-8c7c-4064-b43b-d51e5ef6067b",
      "merchant_name": "My Favorite Store",
      "session_id": "a0a8ae1a-aa31-488c-9a6c-cfda44202446",
      "user_id": "deanknowles@valpreal.com",
      "product_id": "6606cc8e1b69fbabf8a3b534",
      "product_image": "https://images.mediaservice.io/example.jpeg",
      "product_name": "Apple Vision Pro",
      "price": 1000000,
      "quantity": 1
    }
  }
READ Body JSON attribute
  {
    "task": "read",
    "payload": {
      "user_id": "deanknowles@valpreal.com"
    }
  }

Note

Payload on READ Request Body only works when you're using RabbitMQ

PATCH Body JSON attribute
  {
    "task": "update",
    "payload": {
      "user_id": "deanknowles@valpreal.com",
      "product_id": "6606cc8e1b69fbabf8a3b534",
      "quantity": 2,
      "params": "params"
    }
  }

PARAMS: value must be increment or decrement

DELETE Body JSON attribute
  {
    "task": "delete",
    "payload": {
        "id": ["arrayOfIDs"]
    }
  }

arrayOfIDs: ID must be in array format consist of cart ID / ID's. eg: [1, 2, 3, 4]


Note

task consist of create, read, update and delete

📍 Task Description & Method

Task Method Minimum Payload
create POST user_id, product_id, product_image, product_name, price, quantity
read GET user_id
update PATCH user_id, product_id, quantity
remove DELETE [id] or [cartId]

RabbitMQ

Message Payload:
{
    "task": "create",
    "payload": {
      "apps_id": "6606cc8ed9c25c6c5f00b48b",
      "merchant_id": "5798426b-8c7c-4064-b43b-d51e5ef6067b",
      "merchat_name": "XYZ Company",
      "merchant_name": "My Favorite Store",
      "session_id": "a0a8ae1a-aa31-488c-9a6c-cfda44202446",
      "user_id": "deanknowles@valpreal.com",
      "product_id": "6606cc8e1b69fbabf8a3b534",
      "product_image": "https://images.mediaservice.io/example.jpeg",
      "product_name": "Apple Vision Pro",
      "price": 1000000,
      "quantity": 1
    },
    "origin": {
      "queue": "customerOrder",
      "routingKey": "customerOrderKey"
    }
}

Attribute descriptions

Element Description Required
task create, read, update, delete Required
payload Object: Item details Required
apps_id String: (eg: server-key or service id) Optional
merchant_id String Optional
merchant_name String Optional
session_id String Optional
user_id String: (eg: email or random token) Required
product_id String: (eg: Product SKU) Required
product_image String: value must be valid URL format Required
product_name String Required
price Number Required
quantity Number Required
custom_fields Object: see example Optional
product_links Object: see example Optional
origin Object: consist of two key [queue, routingKey] only required when using MQTT protocols Required
params String: value must be decrement or increment. Required when updating items quantity Required

Custom Fields on Payload

Custom fields allow you to add any extra info on user items, such as brand name, item size, color, and more.

Body JSON Attribute
{
  "task": "create",
  "payload": {
    "custom_fields": {
      "brand": "XYZ",
      "color": "Green",
      "size": "XL"
    }
  }
}

Product Links Attribute Example

In the product_links field, you can include attributes such as URL, shortened URL, catalog link, etc. To facilitate the Front-End team, you can add attributes like href, rel, and others.

Example
{
  "product_links": {
    "url": "https://resource.e-commerce.app/product/apple-vision-pro-x",
    "rel": "_blank",
    "brochure": {
      "url": "https://resource.e-commerce.app/product/apple-vision-pro-x/brochure.pdf",
      "method": "GET"
    }
  }
}

OR

{
  "product_links": {
    "video_preview": "https://preview.media.app/dasno238as3/demo.video",
    "video_link": "https://www.youtube.com/watch?v=abcdef"
  }
}