Skip to content

Gustavohsdp/technical-api

Repository files navigation

Mypharma technical test

API in Node.js + TypeScript that applies SOLID concepts and has unit test.

🚀 Techs & Tools

Prisma

TypeScript

JWT

Fastify

Vitest

MongoDB

Setup

Create an .env file:

$ cp .env.example .env

Edit this file and set the values for the requested environment variables, example:

# Node
NODE_ENV="dev"

#Auth
JWT_SECRET="your-secret"

# Database
DATABASE_URL="mongodb://USERNAME:PASSWORD@HOST/DATABASE?ssl=true&connectTimeoutMS=5000&maxPoolSize=50"

Run app

$ npm install
$ npm run start:dev

Build app

$ npm run build

Run tests

Run unit tests

$ npm run test

Generate coverage

$ npm run test:coverage

Routes and Parameters

Customer

POST /customer

This route allows registering a new client in the application. The following attributes are required:

{
  "email": "john@example.com",
  "name": "John",
  "address": "123 Main Street",
  "phone": "31999999999"
}

PATCH /customer/:customerId

This route allows you to update the information of an existing customer in the application. It is necessary to inform the user ID to be updated in the URL. The following attributes are required:

{
  "email": "john@example.com",
  "name": "John",
  "address": "123 Main Street",
  "phone": "31999999999"
}

GET /customer/email

This route returns a customer registered in the application, which was searched for by email.

email (string): User email for the filter..

Example of use: '/customer/email'

{
  "email": "john@example.com"
}

Category

POST /category

This route allows registering a new category in the application. The following attributes are required:

{
  "name": "Camiseta"
}

PATCH /category/:categoryId

This route allows updating the information of an existing category in the application. It is necessary to inform the category ID to be updated in the URL. The following attributes are required:

{
  "name": "Camiseta"
}

GET /category/name

This route returns a category registered in the application, which was searched by name.

name (string): Name of the category for the filter.

Example of use: /category/name

{
  "name": "Camiseta"
}

GET /category

This route returns all categories registered in the application.

Example of use: /category

Product

POST /product

This route allows registering a new product in the application. The following attributes are required:

{
  "name": "Camiseta Preta",
  "description": "camiseta preta de algodão, com um foguete",
  "sku": "cpf",
  "unitaryValue": "60,89",
  "categoryId": "642af54667ff671e9c10e070",
  "active": true
}

PATCH /product/:productId

This route allows you to update the information of an existing product in the application. It is necessary to inform the ID of the product to be updated in the URL. The following attributes are required:

{
  "name": "Camiseta Preta",
  "description": "camiseta preta de algodão, com um foguete",
  "sku": "cpf",
  "unitaryValue": "60,89",
  "categoryId": "642af54667ff671e9c10e070",
  "active": true
}

PATCH /product/status/:productId

This route allows you to update product information, whether it is active or inactive.

Example of use: /product/status/642af7b54b7e9b5f71669ad5

GET /product/sku

This route returns a product registered in the application, which was searched by the sku.

sku (string): Sku from product to filter.

Example of use: /product/sku

{
  "sku": "cpf"
}

GET /product

This route returns all products registered in the application.

Example of use: /product

Order

POST /order

This route allows registering a new order in the application. The following attributes are required:

{
  "customerId": "642af9a7108366f242d2ca8c",
  "productId": "642af7b54b7e9b5f71669ad5"
}

PATCH /order/:orderId

This route allows updating the information of an existing order in the application. It is necessary to inform the ID of the order to be updated in the URL. The following attributes are required:

{
  "customerId": "642af9a7108366f242d2ca8c",
  "productId": "642af7b54b7e9b5f71669ad5"
}

PATCH /order/cancel/:orderId

This route allows you to cancel an existing order.

Example of use: /order/cancel/642af7b54b7e9b5f71669ad5

GET /order/:orderId

This route returns an order registered in the application, it is necessary to inform the order ID to be located in the URL

Example of use: '/order/642af7b54b7e9b5f71669ad5'

GET /order/all/:customerId

This route returns all orders registered in a customer's application, it is necessary to inform the customer ID to locate the orders

Example of use: '/order/all/642af7b54b7e9b5f71669ad5'

GET /order

This route returns all orders registered in the application.

Example of use: /order

Admin

POST /admin

This route allows registering a new admin in the application. The following attributes are required:

{
  "email": "xxxx@email.com",
  "name": "John Doe",
  "password": "xxxxxx"
}

PATCH /admin/:adminId

This route allows updating the information of an existing admin in the application. It is necessary to inform the ID of the admin to be updated in the URL. The following attributes are required:

{
  "email": "xxxx@email.com",
  "name": "John Doe",
  "password": "xxxxxx"
}

GET /admin

This route returns an admin registered in the application, it is necessary to inform the email in the query to be located

Authenticate

POST /sessions

This route returns a JWT token and authenticates you in the application:

{
  "email": "xxxx@email.com",
  "password": "xxxxxx"
}