Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2,936 changes: 2,936 additions & 0 deletions logic-exercises/checaParenteses/package-lock.json

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions semana24/WireCard/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
package-lock.json
build
.env
17 changes: 17 additions & 0 deletions semana24/WireCard/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# WIRECARD

### Introdução
Esta API realiza compras através do modo ***BOLETO*** e modo ***CARTAO***, além de mostras todas as compras feitas.

### Overview
Things that the developers should know about
Ao fazer uma compra via boleto, é retornado o código para pagamento. E ao fazer via cartão, é retornada uma mensagem de aprovado.

Ambas url's de compras são iguais, porém é exemplificado duas possibilidades para representar o ***body*** a ser passado mediante a compra por boleto ou cartão.

***Para ambas as compras, deve ser passado um cpf em formato string com 11 caracteres.***

Para as compras com ***cartão*** os valores do ***card_number*** e do ***card_cvv***, devem ser passados em formato string com 16 caracteres e 3 caracteres, respectivamente.

### Link do Postman
https://documenter.getpostman.com/view/17588210/UVeGs6KF
33 changes: 33 additions & 0 deletions semana24/WireCard/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "WireCard",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"dev": "ts-node-dev ./src/index.ts",
"start": "tsc && node ./build/index.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@types/cors": "^2.8.12",
"@types/express": "^4.17.13",
"ts-node-dev": "^1.1.8",
"typescript": "^4.5.5"
},
"dependencies": {
"@types/bcryptjs": "^2.4.2",
"@types/jsonwebtoken": "^8.5.8",
"@types/knex": "^0.16.1",
"@types/uuid": "^8.3.4",
"bcryptjs": "^2.4.3",
"cors": "^2.8.5",
"dotenv": "^15.0.0",
"express": "^4.17.2",
"jsonwebtoken": "^8.5.1",
"knex": "^1.0.1",
"mysql": "^2.18.1",
"uuid": "^8.3.2"
}
}
18 changes: 18 additions & 0 deletions semana24/WireCard/src/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import express, {Express} from 'express'
import cors from 'cors'
import { AddressInfo } from "net"

export const app: Express = express();

app.use(express.json());
app.use(cors());

const server = app.listen(process.env.PORT || 3003, () => {
if (server) {
const address = server.address() as AddressInfo;
console.log(`Server is running in http://localhost:${address.port}`);
} else {
console.error(`Failure upon starting server.`);
}
});

14 changes: 14 additions & 0 deletions semana24/WireCard/src/data/connection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import knex from "knex";
import dotenv from "dotenv";

dotenv.config()
export const connection = knex({
client: "mysql",
connection: {
host: process.env.DB_HOST,
port: 3306,
user: process.env.DB_USER,
password: process.env.DB_PASS,
database: process.env.DB_NAME
}
});
66 changes: 66 additions & 0 deletions semana24/WireCard/src/endpoint/createPayment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { Request, Response } from "express";
import { connection } from "../data/connection";
import { PaymentMethod } from "../entities/PaymentMethod";
import { Buyer, TYPE_PAYMENT } from "../entities/Buyer";
import { IdGenerator } from "../services/IdGenerator";

export const createPayment = async(req:Request, res:Response) => {
try {

const {id_client, name, cpf, email, amount, type, card_name,card_number, card_date,card_cvv} = req.body

if(!id_client || !name || !amount || !type){
res.statusCode = 422
throw new Error("Preencha os campos 'id_client:1-Padaria, 2-Mercado, 3-Papelaria','name', 'cpf', 'email', 'amount' e 'type'.")
}

if (!email || email.indexOf("@") === -1) {
throw new Error("Email inválido");
}

if (!cpf || cpf.length !== 11) {
throw new Error("CPF inválido");
}

const id = new IdGenerator().generateId()

const newPurchase: Buyer = {
id,
id_client,
name,
cpf,
email,
amount,
type
}

const paymentType = async() => {
if(type === TYPE_PAYMENT.BOLETO){
return new PaymentMethod().boletoPayment()
}


if(type === TYPE_PAYMENT.CARTAO){
const purchaseCard = {... newPurchase,
card_name,card_number,card_date,card_cvv}



if(!purchaseCard.card_name || !purchaseCard.card_number || purchaseCard.card_number.toString().length !==16 || !purchaseCard.card_date || !purchaseCard.card_cvv || purchaseCard.card_cvv.toString().length !==3){
res.statusCode = 422
throw new Error("Preencha os campos 'card_name','card_number', 'card_date', 'card_cvv'.")
}else{
return "Cartão aprovado"
}

}
}

await connection("wirecard_purchase").insert(newPurchase)

res.status(200).send({"Compra aprovada": await paymentType()})

} catch (error:any) {
res.status(400).send({message: error.sqlMessage || error.message})
}
}
23 changes: 23 additions & 0 deletions semana24/WireCard/src/endpoint/searchPurchases.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Request, Response } from "express";
import { connection } from "../data/connection";

export const searchPurchases = async(req:Request, res:Response) => {
try {

const [result] = await connection.raw(`
SELECT C.name, B.name, B.amount, B.type, B.status
FROM wirecard_client as C
JOIN wirecard_purchase as B ON C.id = B.id_client;
`)

if(result.length === 0){
throw new Error("Nenhuma compra registrada!")
}


res.status(200).send(result)

} catch (error:any) {
res.status(400).send({message: error.sqlMessage || error.message})
}
}
23 changes: 23 additions & 0 deletions semana24/WireCard/src/entities/Buyer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
export enum TYPE_PAYMENT {
BOLETO = "BOLETO",
CARTAO = "CARTAO"
}

export interface Credit_Card {
card_name: string
card_number: string
card_date: string
card_cvv: number
}

export interface Buyer{
id: string
id_client: string
name: string
cpf: string
email: string
amount: number
type: TYPE_PAYMENT
card ?: Credit_Card
}

15 changes: 15 additions & 0 deletions semana24/WireCard/src/entities/PaymentMethod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Credit_Card } from "./Buyer"


export class PaymentMethod {

public boletoPayment = ()=> {
let array = []
for(let i:number = 0; i < 48; i++){
const n = Math.floor(Math.random() * 10)
array.push(n)
}
return array.join('')
}

}
6 changes: 6 additions & 0 deletions semana24/WireCard/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { app } from "./app";
import { routerPurchase } from "./routes/routerPurchase";

app.use("/purchase", routerPurchase)


8 changes: 8 additions & 0 deletions semana24/WireCard/src/routes/routerPurchase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Router } from "express"
import { createPayment } from "../endpoint/createPayment"
import { searchPurchases } from "../endpoint/searchPurchases"

export const routerPurchase = Router()

routerPurchase.get("/", searchPurchases)
routerPurchase.post("/", createPayment)
8 changes: 8 additions & 0 deletions semana24/WireCard/src/services/IdGenerator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { v4 } from "uuid";

export class IdGenerator {

public generateId(){
return v4()
}
}
17 changes: 17 additions & 0 deletions semana24/WireCard/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"sourceMap": true,
"outDir": "./build",
"rootDir": "./src",
"removeComments": true,
"strict": true,
"noImplicitAny": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"resolveJsonModule": true

}
}