diff --git a/package.json b/package.json deleted file mode 100644 index f9a1335..0000000 --- a/package.json +++ /dev/null @@ -1,28 +0,0 @@ -{ - "name": "my-wallet-back", - "type": "module", - "version": "1.0.0", - "description": "", - "main": "index.js", - "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" - }, - "keywords": [], - "author": "", - "license": "ISC", - "dependencies": { - "bcrypt": "^5.0.1", - "cors": "^2.8.5", - "express": "^4.17.1", - "pg": "^8.6.0", - "uuid": "^8.3.2" - }, - "devDependencies": { - "@babel/plugin-transform-runtime": "^7.14.5", - "@babel/preset-env": "^7.14.7", - "babel-jest": "^27.0.5", - "jest": "^27.0.5", - "nodemon": "^2.0.7", - "supertest": "^6.1.3" - } -} diff --git a/src/app.js b/src/app.js deleted file mode 100644 index a673173..0000000 --- a/src/app.js +++ /dev/null @@ -1,125 +0,0 @@ -import express, { query } from 'express'; -import {v4 as uuid} from 'uuid'; -import bcrypt from 'bcrypt'; -import cors from 'cors'; -import connection from './database.js'; - -const app = express(); -app.use(express.json()); -app.use(cors()); - -app.post("/sign-up", async (req, res) => { - try{ - const { name, email, password } = req.body; - const result = await connection.query(`SELECT * FROM users WHERE email=$1`,[email]); - console.log(result); - if(password.length < 3) return res.status(400).send("A senha deve ter no mínimo 4 caractéres!"); - if(!result.rows.length === 0 ) return res.status(409).send("Esse email já está sendo utilizado"); - - const hashedPassword = bcrypt.hashSync(password, 12); - await connection.query(`INSERT INTO users (name, email, password) VALUES ($1,$2,$3)`, [name, email, hashedPassword]); - res.sendStatus(201); - }catch(e){ - console.log(e); - res.sendStatus(400); - } -}); - -app.post("/sign-in", async (req, res) => { - try{ - const {email, password } = req.body; - const result = await connection.query(`SELECT * FROM users WHERE email = $1`, [email]); - const user = result.rows[0]; - if(user && bcrypt.compareSync(password, user.password)){ - const token = uuid(); - - await connection.query(` - INSERT INTO sessions ("userId", token) VALUES ($1,$2) - `, [user.id, token]); - - res.send({user:user,token:token}); - } else { - res.status(401).send("Usuário ou senha não encontrados!"); - } - }catch(e){ - console.log(e); - res.sendStatus(400); - } -}); - -app.delete("/user", async (req, res) => { - try{ - const {id} = req.body; - await connection.query(`DELETE FROM sessions WHERE "userId" = $1`,[id]); - res.send(); - }catch(e){ - console.log(e); - res.sendStatus(400); - } -}); - -app.get("/user", async (req, res) => { - try{ - const authorization = req.headers['authorization']; - const user = await validateToken(authorization); - if(user){ - const data = await connection.query(`SELECT * FROM transactions WHERE "userId" = $1`,[user.id]); - res.send(data.rows); - } else { - res.sendStatus(401); - } - }catch(e){ - console.log(e); - } -}); - -app.post("/user", async (req, res) => { - try{ - const authorization = req.headers['authorization']; - const user = await validateToken(authorization); - if(user){ - const {value, description, type} = req.body; - const valueToCents = 100*(parseFloat(value).toFixed(2)); - if(type !== "income" && type !== "expense")return res.sendStatus(400).send("Error: invalid type transaction!"); - const date = await getDate(); - await connection.query(`INSERT INTO transactions ("userId", type, value, description, date) VALUES ($1,$2,$3,$4,$5)`, [user.userId, type, valueToCents, description, date]); - res.sendStatus(200); - }else{ - res.sendStatus(401); - } - }catch(e){ - console.log(e); - res.sendStatus(400); - } -}); - -app.get("/banana-test", async (req, res) => { - res.send(200); -}) - -function getDate(){ - const ts = Date.now(); - - const date_ob = new Date(ts); - const date = date_ob.getDate(); - const month = date_ob.getMonth() + 1; - const year = date_ob.getFullYear(); - - const formattedDate = (year + "-" + month + "-" + date); - return formattedDate; -} - -async function validateToken(authorization){ - const token = authorization?.replace('Bearer ', ''); - - if(!token) return res.sendStatus(401); - const result = await connection.query(` - SELECT * FROM sessions - JOIN users ON sessions."userId" = users.id - WHERE sessions.token = $1 - `, [token]); - console.log(result.rows); - return result.rows[0]; -} - -export default app; \ No newline at end of file diff --git a/src/database.js b/src/database.js deleted file mode 100644 index d1c6298..0000000 --- a/src/database.js +++ /dev/null @@ -1,13 +0,0 @@ -import pg from 'pg'; - -const databaseConfig = { - user: 'postgres', - password: '123456', - database: 'mywallet', - host: 'localhost', - port: 5432 -} - -const connection = new pg.Pool(databaseConfig); - -export default connection; \ No newline at end of file diff --git a/src/server.js b/src/server.js deleted file mode 100644 index 3687c8e..0000000 --- a/src/server.js +++ /dev/null @@ -1,5 +0,0 @@ -import app from './app.js'; - -app.listen(4000, () => { - console.log("Server running on port 4000"); -}); \ No newline at end of file diff --git a/tests/sign-up-in.test.js b/tests/sign-up-in.test.js deleted file mode 100644 index 216b3b0..0000000 --- a/tests/sign-up-in.test.js +++ /dev/null @@ -1,84 +0,0 @@ -import supertest from 'supertest'; -import app from '../src/app.js'; -import connection from '../src/database.js'; - -describe("POST /sign-up", () => { - it("should respond with status 201 when there is no user with given email", async () => { - const body = { - name: 'Fulano', - email: 'fulano@email.com', - password: '123456' - }; - - const response = await supertest(app).post("/sign-up").send(body); - - expect(response.status).toEqual(201); - }); - - it("should respond with status 409 when there already is an user with given email", async () => { - const body = { - name: 'Fulano', - email: 'fulano@email.com', - password: '123456' - }; - - await connection.query(`INSERT INTO users (name, email, password) VALUES ($1, $2, $3)`, [body.name, body.email, body.password]); - const response = await supertest(app).post("/sign-up").send(body); - - expect(response.status).toEqual(409); - }); - - it("should respond with status 400 when password length is minor than 4", async () => { - const body = { - name: 'Fulano', - email: 'fulano@email.com', - password: '123' - }; - - await connection.query(`INSERT INTO users (name, email, password) VALUES ($1, $2, $3)`, [body.name, body.email, body.password]); - const response = await supertest(app).post("/sign-up").send(body); - - expect(response.status).toEqual(400); - }); - -}); - -describe("POST /sign-in", () => { - it("should respond with status 200 when user exists and password is valid", async () => { - const body = { - name: "Fulano", - email: "fulano@email.com", - password: "123456" - }; - - await supertest(app).post("/sign-up").send(body); - const response = await supertest(app).post("/sign-in").send({ email: body.email, password: body.password }); - - expect(response.status).toEqual(200); - expect(response.body).toEqual( - expect.objectContaining({ - token: expect.any(String) - }) - ); - }); - - it("should respond with status 401 when user doesn't exist", async () => { - const body = { - name: "Fulano", - email: "fulano@email.com", - password: "123456" - }; - - await supertest(app).post("/sign-up").send(body); - - const response = await supertest(app).post("/sign-in").send({ email: "email_nao_cadastrado@email.com", password: "senha_incorreta" }); - - expect(response.status).toEqual(401); - }); - -}); - -afterAll( async () => { - await connection.query(`DELETE FROM users WHERE email = "fulano@email.com"`); - connection.end(); -}); \ No newline at end of file