A Rest API built with Node 8, Restify, Docker Compose and MountBank mockserver. Recieves a product list with a customer id associated, deal with discounts and return a cart object with all information about the products.
- Getting Started
- Prerequisites
- Unit test
- Functional test
- Built With
- Examples
- Action-Domain-Responder Pattern
docker-compose up
will start 2 cointainers:
- nodejs-checkout-service
- pricing-mock-server
nodejs-checkout-service can be accessed on localhost:8080
- Docker (https://www.docker.com/get-docker)
- Docker Compose (https://docs.docker.com/compose/gettingstarted/)
docker-compose -f docker-compose-unit.yml up
docker-compose -f docker-compose-functional.yml up
- Docker - Container
- Docker Compose - Container Orchestrator
- Node8 - Backend programmimg language
- Restify - Framework
- MountBank - Mock server
POST to localhost:8080/checkout
with request header application/json
and body:
{
"customerId": "default",
"products": [{
"id": "premium",
"quantity": 1
},
{
"id": "classic",
"quantity": 1
},
{
"id": "standout",
"quantity": 1
}
]
}
should respond:
{
"products": [
{
"id": "premium",
"value": 394.99,
"discount": 0,
"total": 394.99
},
{
"id": "classic",
"value": 269.99,
"discount": 0,
"total": 269.99
},
{
"id": "standout",
"value": 322.99,
"discount": 0,
"total": 322.99
}
],
"total": 987.97
}
Where:
- customerId
can be (unilever, nike, apple, ford, or default)
- product.id
can be (classic, standout or premium)
Status codes:
- 200 OK
- 412 Assert pre-condition failed
(invalid payload)
- 500 Internal server error
(ADR) is a software architectural pattern that was proposed by Paul M. Jones as a refinement of Model–view–controller (MVC) that is better suited for web applications. Read more...
The action takes HTTP requests (URLs and their methods) and uses that input to interact with the domain, after which it passes the domain's output to one and only one responder.
nodejs-checkout-service action deals with request and response, injecting validated/filtered data on the Domain layer
The validation is based on Json draft schema, there is a awesome tool to generate it Json schema generator
Responsible for transform a json payload into Domain objects
Fetchs information from external services/databases. Learn more_
The domain can modify state, interacting with storage and/or manipulating data as needed. It contains the business logic.
Top layer of domain, the CheckoutAction interacts with this service and then send the output to responder
Product representation
class Product {
constructor(id) {
this.id = id;
this.value = 0;
this.discount = 0;
this.total = 0;
}
}
Cart representation
class Cart {
constructor(){
this.products = [];
}
}
There are 3 types of discounts:
Applies directly on value, for example, if a product value is $100.00, and we want to apply a $10.00 Value discount, the product value will be $90.00.
Similar to value discount, but just applied when a minimum quantity of products is reached.
Based on quantity, eg.: every 5 products purchased you pay only 4.
Responsible for constructing the discount strategy, based on pricing rules.
The responder builds the entire HTTP response from the domain's output which is given to it by the action.
Extends abstract responder and knows how to build a responde from Cart
object