Wpkenpachi / hash-challenge

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Hash Challenge

Challenge Description

Giving permission to run file

$ chmod +x run

Runnig Up Docker-Compose

$ sh run up

This command will setup Postgres database, gRPC client (Node Api) and gRPC server (Python Api)

Stopping Docker-Compose and removing images and containers

$ sh run down

This command will down Postgres database, gRPC client (Node Api) and gRPC server (Python Api)

Running tests

$ sh run tests

This command will run python gRPC server tests and node gRPC client tests (Obs: docker-compose need to be running sh run up)

Database Schema

database-schema

How Discount is Structured

All Discount types will be recorded on database with an id, title (the name and type of discount) and a metadata. The metadata will have all required data to apply product discounts. The required metadata properties is the percentage or/and value_in_cents and type (to set if discount is by percentage or value_in_cents), all aditional properties is specific data for a specific discount type.

How Discount Works

For every each discount record, there's a method on gRPC Python server to handle it. For each discount to be created needs one handler method with the same name as title field. e.g: grpc-service-discount-methods

Endpoints

Base Url: http://localhost:8080

if you want test on insomnia by importing workspace file:
Insomnia Workspace import file

Route Header Query Body Response
/product
                [
                    {
                        "id": 1,
                        "price_in_cents": 7972,
                        "title": "Port - 74 Brights",
                        "description": "description"
                    },
                    ...
                ]
                
/product X-USER-ID (int)
                [
                    {
                        "id": 1,
                        "price_in_cents": 7972,
                        "title": "Port - 74 Brights",
                        "description": "description",
                        "discount": {
                            "percentage": 5,
                            "value_in_cents": 398
                        }
                    },
                    ...
                ]
                

Service Handler Scripts

For docker-compose handling, we have run file.

$ sh run OPTION

up             run docker-compose up -d --build
down           down docker-compose and remove containers, networks, images and volumes
tests          run gRPC Python Server tests and gRPC Node Client tests
downserver     down gRPC Python Server

Obs: When running this commands, the services can take some seconds to be available
and configured, even after images builded.

Restarting gRPC Server Container

$ docker-compose start server

Obs: gRPC Server can take some seconds to be restarted, and available

Test Helper Scripts

For test help, we have get file. Will be accessed by docker commands

$ docker exec -it python_server sh -c "python3 get ARG"

Valid arguments:
buser       Will return a valid birthday user from database.
nuser       Will return a valid normal user (not in birthday) from database.
bfriday     Will set black friday for today on database.
reset       Will reset black friday day on database.

Folder and File Tree ( only important files )

📦desafio_hash
 ┣ 📂grpc-client-api-microservice           # GRPC NODE CLIENT ( Typescript + Node + Typeorm )
 ┃ ┣ 📂database
 ┃ ┃ ┣ 📂migrations
 ┃ ┃ ┗ 📂seeds
 ┃ ┃ ┃ ┣ 📜DiscountSeeds.ts
 ┃ ┃ ┃ ┣ 📜ProductSeeds.ts
 ┃ ┃ ┃ ┗ 📜UserSeeds.ts
 ┃ ┣ 📂src
 ┃ ┃ ┣ 📂business
 ┃ ┃ ┃ ┗ 📜GetDiscount.ts
 ┃ ┃ ┣ 📂db
 ┃ ┃ ┃ ┗ 📜Connection.ts                    # Database Connection
 ┃ ┃ ┣ 📂models
 ┃ ┃ ┃ ┣ 📜Discount.ts
 ┃ ┃ ┃ ┣ 📜Product.ts
 ┃ ┃ ┃ ┗ 📜User.ts
 ┃ ┃ ┣ 📂proto                              # gRPC Proto and Generated Files
 ┃ ┃ ┃ ┣ 📜model.proto
 ┃ ┃ ┃ ┣ 📜model_grpc_pb.d.ts
 ┃ ┃ ┃ ┣ 📜model_grpc_pb.js
 ┃ ┃ ┃ ┣ 📜model_pb.d.ts
 ┃ ┃ ┃ ┣ 📜model_pb.js
 ┃ ┃ ┃ ┣ 📜model_pb2.py
 ┃ ┃ ┃ ┗ 📜model_pb2_grpc.py
 ┃ ┃ ┣ 📂request
 ┃ ┃ ┃ ┣ 📂interfaces
 ┃ ┃ ┃ ┃ ┗ 📜Product.ts
 ┃ ┃ ┃ ┗ 📂middlewares
 ┃ ┃ ┃ ┃ ┗ 📜validator.ts
 ┃ ┃ ┣ 📂routes
 ┃ ┃ ┃ ┗ 📜index.ts                         # API Expose Routes
 ┃ ┃ ┣ 📂services
 ┃ ┃ ┃ ┗ 📜GrpcClient.ts                    # gRPC Client Service
 ┃ ┃ ┣ 📂utils
 ┃ ┃ ┣ 📜api.ts
 ┃ ┃ ┗ 📜server.ts                          # Server Listening Entrypoint
 ┃ ┣ 📂tests                                # API tests
 ┃ ┃ ┗ 📜GetDiscount.test.ts
 ┃ ┣ 📜Dockerfile
 ┃ ┣ 📜setup                                # Bash script to create and populate database
 ┣ 📂grpc-server-microservice               # GRPC PYTHON SERVER ( Python + peewee orm )
 ┃ ┣ 📂app
 ┃ ┃ ┣ 📂db
 ┃ ┃ ┃ ┗ 📜database.py                      # Database Connection
 ┃ ┃ ┣ 📂models
 ┃ ┃ ┃ ┣ 📜discount.py
 ┃ ┃ ┃ ┣ 📜model.py
 ┃ ┃ ┃ ┣ 📜product.py
 ┃ ┃ ┃ ┗ 📜user.py
 ┃ ┃ ┣ 📂services
 ┃ ┃ ┃ ┣ 📜check_discount_rule_service.py
 ┃ ┃ ┃ ┗ 📜discount_service.py
 ┃ ┃ ┣ 📜interfaces.py
 ┃ ┃ ┗ 📜utils.py
 ┃ ┣ 📂proto                                # gRPC Proto and Generated Files
 ┃ ┃ ┣ 📜model.proto
 ┃ ┃ ┣ 📜model_pb2.py
 ┃ ┃ ┗ 📜model_pb2_grpc.py
 ┃ ┣ 📂tests                                # gRPC Tests
 ┃ ┃ ┣ 📜test_grpc_server.py
 ┃ ┃ ┗ 📜test_utils.py
 ┃ ┣ 📜Dockerfile
 ┃ ┣ 📜get                                  # Test helper script
 ┃ ┣ 📜protobuild                           # Bash file that will generate gRPC proto bundles
 ┃ ┣ 📜requirements.txt
 ┃ ┗ 📜server.py
 ┣ 📜docker-compose.yml
 ┗ 📜run                                    # Bash script to deploy docker-compose

About


Languages

Language:TypeScript 51.5%Language:Python 40.7%Language:Shell 4.9%Language:Dockerfile 2.7%Language:JavaScript 0.2%