diff --git a/README.md b/README.md index 7e08617..f214f11 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,7 @@ TOKEN_SECRET_TEST=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjp7ImlkIjoxLCJma - Install packages dependencies: `npm install` - Run tests: `npm run test` - Run the database: `npm run up` -- Launch the application: `npm run start` +- Launch the application: `npm run start`, you can acces the application with this link `127.0.0.1:3000` Note : If needed you can reset the tables by closing the app and running the following commands: @@ -74,6 +74,44 @@ Note : If needed you can reset the tables by closing the app and running the fol | Post | `/orders/:id/products` | Create ( Token ) | | Delete | `/orders/:id/products` | Delete ( Token ) | +# Data shapes + +- Products : + + | Column | Type | + |:-------:|:-----------------------| + | id | SERIAL PRIMARY KEY | + | name | VARCHAR(250) NOT NULL | + | price | INTEGER NOT NULL | + +- Users : + + | Column | Type | + |:------------:|:-----------------------| + | id | SERIAL PRIMARY KEY | + | firstName | VARCHAR(250) NOT NULL | + | lastName | VARCHAR(250) NOT NULL | + | username | VARCHAR(250) NOT NULL | + | password | VARCHAR(250) NOT NULL | + +- Orders : + + | Column | Type | + |:------------:|:---------------------------------------| + | id | SERIAL PRIMARY KEY | + | status | VARCHAR(15) | + | user_id | INTEGER NOT NULL REFERENCES users(id) | + +- Orders Products : + + | Column | Type | + |:-------------:|:------------------------------------------| + | id | SERIAL PRIMARY KEY | + | quantity | INTEGER NOT NULL, | + | order_id | INTEGER NOT NULL REFERENCES orders(id) | + | product_id | INTEGER NOT NULL REFERENCES products(id) | + + # Built with diff --git a/migrations/20220525184648-products.js b/migrations/20220525184648-products.js new file mode 100644 index 0000000..454e702 --- /dev/null +++ b/migrations/20220525184648-products.js @@ -0,0 +1,53 @@ +'use strict'; + +var dbm; +var type; +var seed; +var fs = require('fs'); +var path = require('path'); +var Promise; + +/** + * We receive the dbmigrate dependency from dbmigrate initially. + * This enables us to not have to rely on NODE_PATH. + */ +exports.setup = function(options, seedLink) { + dbm = options.dbmigrate; + type = dbm.dataType; + seed = seedLink; + Promise = options.Promise; +}; + +exports.up = function(db) { + var filePath = path.join(__dirname, 'sqls', '20220525184648-products-up.sql'); + return new Promise( function( resolve, reject ) { + fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){ + if (err) return reject(err); + console.log('received data: ' + data); + + resolve(data); + }); + }) + .then(function(data) { + return db.runSql(data); + }); +}; + +exports.down = function(db) { + var filePath = path.join(__dirname, 'sqls', '20220525184648-products-down.sql'); + return new Promise( function( resolve, reject ) { + fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){ + if (err) return reject(err); + console.log('received data: ' + data); + + resolve(data); + }); + }) + .then(function(data) { + return db.runSql(data); + }); +}; + +exports._meta = { + "version": 1 +}; diff --git a/migrations/20220520025053-shelf.js b/migrations/20220525184653-users.js similarity index 87% rename from migrations/20220520025053-shelf.js rename to migrations/20220525184653-users.js index d7d22d5..7a0effd 100644 --- a/migrations/20220520025053-shelf.js +++ b/migrations/20220525184653-users.js @@ -19,7 +19,7 @@ exports.setup = function(options, seedLink) { }; exports.up = function(db) { - var filePath = path.join(__dirname, 'sqls', '20220520025053-shelf-up.sql'); + var filePath = path.join(__dirname, 'sqls', '20220525184653-users-up.sql'); return new Promise( function( resolve, reject ) { fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){ if (err) return reject(err); @@ -34,7 +34,7 @@ exports.up = function(db) { }; exports.down = function(db) { - var filePath = path.join(__dirname, 'sqls', '20220520025053-shelf-down.sql'); + var filePath = path.join(__dirname, 'sqls', '20220525184653-users-down.sql'); return new Promise( function( resolve, reject ) { fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){ if (err) return reject(err); diff --git a/migrations/20220525184702-orders.js b/migrations/20220525184702-orders.js new file mode 100644 index 0000000..fa205d1 --- /dev/null +++ b/migrations/20220525184702-orders.js @@ -0,0 +1,53 @@ +'use strict'; + +var dbm; +var type; +var seed; +var fs = require('fs'); +var path = require('path'); +var Promise; + +/** + * We receive the dbmigrate dependency from dbmigrate initially. + * This enables us to not have to rely on NODE_PATH. + */ +exports.setup = function(options, seedLink) { + dbm = options.dbmigrate; + type = dbm.dataType; + seed = seedLink; + Promise = options.Promise; +}; + +exports.up = function(db) { + var filePath = path.join(__dirname, 'sqls', '20220525184702-orders-up.sql'); + return new Promise( function( resolve, reject ) { + fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){ + if (err) return reject(err); + console.log('received data: ' + data); + + resolve(data); + }); + }) + .then(function(data) { + return db.runSql(data); + }); +}; + +exports.down = function(db) { + var filePath = path.join(__dirname, 'sqls', '20220525184702-orders-down.sql'); + return new Promise( function( resolve, reject ) { + fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){ + if (err) return reject(err); + console.log('received data: ' + data); + + resolve(data); + }); + }) + .then(function(data) { + return db.runSql(data); + }); +}; + +exports._meta = { + "version": 1 +}; diff --git a/migrations/20220525184708-order-products.js b/migrations/20220525184708-order-products.js new file mode 100644 index 0000000..bba7033 --- /dev/null +++ b/migrations/20220525184708-order-products.js @@ -0,0 +1,53 @@ +'use strict'; + +var dbm; +var type; +var seed; +var fs = require('fs'); +var path = require('path'); +var Promise; + +/** + * We receive the dbmigrate dependency from dbmigrate initially. + * This enables us to not have to rely on NODE_PATH. + */ +exports.setup = function(options, seedLink) { + dbm = options.dbmigrate; + type = dbm.dataType; + seed = seedLink; + Promise = options.Promise; +}; + +exports.up = function(db) { + var filePath = path.join(__dirname, 'sqls', '20220525184708-order-products-up.sql'); + return new Promise( function( resolve, reject ) { + fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){ + if (err) return reject(err); + console.log('received data: ' + data); + + resolve(data); + }); + }) + .then(function(data) { + return db.runSql(data); + }); +}; + +exports.down = function(db) { + var filePath = path.join(__dirname, 'sqls', '20220525184708-order-products-down.sql'); + return new Promise( function( resolve, reject ) { + fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){ + if (err) return reject(err); + console.log('received data: ' + data); + + resolve(data); + }); + }) + .then(function(data) { + return db.runSql(data); + }); +}; + +exports._meta = { + "version": 1 +}; diff --git a/migrations/sqls/20220520025053-shelf-down.sql b/migrations/sqls/20220520025053-shelf-down.sql deleted file mode 100644 index 04ba033..0000000 --- a/migrations/sqls/20220520025053-shelf-down.sql +++ /dev/null @@ -1,4 +0,0 @@ -DROP TABLE order_products; -DROP TABLE orders; -DROP TABLE users; -DROP TABLE products; \ No newline at end of file diff --git a/migrations/sqls/20220520025053-shelf-up.sql b/migrations/sqls/20220520025053-shelf-up.sql deleted file mode 100644 index 5e0dcc9..0000000 --- a/migrations/sqls/20220520025053-shelf-up.sql +++ /dev/null @@ -1,29 +0,0 @@ -CREATE TABLE products ( - id SERIAL PRIMARY KEY, - name VARCHAR(250) NOT NULL, - price INTEGER NOT NULL -); - - -CREATE TABLE users ( - id SERIAL PRIMARY KEY, - firstName VARCHAR(250) NOT NULL, - lastName VARCHAR(250) NOT NULL, - username VARCHAR(250) NOT NULL, - password VARCHAR(250) NOT NULL -); - -CREATE TABLE orders ( - id SERIAL PRIMARY KEY, - status VARCHAR(15), - user_id INTEGER NOT NULL REFERENCES users(id) -); - -CREATE TABLE order_products ( - id SERIAL PRIMARY KEY, - quantity INTEGER NOT NULL, - order_id INTEGER NOT NULL REFERENCES orders(id), - product_id INTEGER NOT NULL REFERENCES products(id) -) - - diff --git a/migrations/sqls/20220525184648-products-down.sql b/migrations/sqls/20220525184648-products-down.sql new file mode 100644 index 0000000..3f27afc --- /dev/null +++ b/migrations/sqls/20220525184648-products-down.sql @@ -0,0 +1 @@ +DROP TABLE products; \ No newline at end of file diff --git a/migrations/sqls/20220525184648-products-up.sql b/migrations/sqls/20220525184648-products-up.sql new file mode 100644 index 0000000..0f9c242 --- /dev/null +++ b/migrations/sqls/20220525184648-products-up.sql @@ -0,0 +1,5 @@ +CREATE TABLE products ( + id SERIAL PRIMARY KEY, + name VARCHAR(250) NOT NULL, + price INTEGER NOT NULL +); \ No newline at end of file diff --git a/migrations/sqls/20220525184653-users-down.sql b/migrations/sqls/20220525184653-users-down.sql new file mode 100644 index 0000000..441087a --- /dev/null +++ b/migrations/sqls/20220525184653-users-down.sql @@ -0,0 +1 @@ +DROP TABLE users; \ No newline at end of file diff --git a/migrations/sqls/20220525184653-users-up.sql b/migrations/sqls/20220525184653-users-up.sql new file mode 100644 index 0000000..96dd4b4 --- /dev/null +++ b/migrations/sqls/20220525184653-users-up.sql @@ -0,0 +1,7 @@ +CREATE TABLE users ( + id SERIAL PRIMARY KEY, + firstName VARCHAR(250) NOT NULL, + lastName VARCHAR(250) NOT NULL, + username VARCHAR(250) NOT NULL, + password VARCHAR(250) NOT NULL +); \ No newline at end of file diff --git a/migrations/sqls/20220525184702-orders-down.sql b/migrations/sqls/20220525184702-orders-down.sql new file mode 100644 index 0000000..247916e --- /dev/null +++ b/migrations/sqls/20220525184702-orders-down.sql @@ -0,0 +1 @@ +DROP TABLE orders; \ No newline at end of file diff --git a/migrations/sqls/20220525184702-orders-up.sql b/migrations/sqls/20220525184702-orders-up.sql new file mode 100644 index 0000000..92fcfb9 --- /dev/null +++ b/migrations/sqls/20220525184702-orders-up.sql @@ -0,0 +1,5 @@ +CREATE TABLE orders ( + id SERIAL PRIMARY KEY, + status VARCHAR(15), + user_id INTEGER NOT NULL REFERENCES users(id) +); \ No newline at end of file diff --git a/migrations/sqls/20220525184708-order-products-down.sql b/migrations/sqls/20220525184708-order-products-down.sql new file mode 100644 index 0000000..0bcd8f3 --- /dev/null +++ b/migrations/sqls/20220525184708-order-products-down.sql @@ -0,0 +1 @@ +DROP TABLE order_products; \ No newline at end of file diff --git a/migrations/sqls/20220525184708-order-products-up.sql b/migrations/sqls/20220525184708-order-products-up.sql new file mode 100644 index 0000000..eaccb10 --- /dev/null +++ b/migrations/sqls/20220525184708-order-products-up.sql @@ -0,0 +1,6 @@ +CREATE TABLE order_products ( + id SERIAL PRIMARY KEY, + quantity INTEGER NOT NULL, + order_id INTEGER NOT NULL REFERENCES orders(id), + product_id INTEGER NOT NULL REFERENCES products(id) +) \ No newline at end of file diff --git a/package.json b/package.json index 897bab3..84272f1 100644 --- a/package.json +++ b/package.json @@ -7,10 +7,10 @@ "start": "node dist/server.js", "dev": "nodemon src/server.ts", "watch": "tsc-watch --esModuleInterop src/server.ts --outDir ./dist --onSuccess \"node ./dist/server.js\"", - "test": "db-migrate --env test up && ENV=test jasmine-ts --config jasmine.json && db-migrate --env test down", + "test": "db-migrate --env test up && ENV=test jasmine-ts --config jasmine.json && db-migrate reset --env test", "build": "npx tsc", "up": "db-migrate up", - "down": "db-migrate down", + "down": "db-migrate reset", "lint": "eslint --ext .ts", "prettier": "prettier \"src/**/*.ts\" --write", "tsc": "tsc" diff --git a/src/handlers/orders.ts b/src/handlers/orders.ts index 44ef7a5..d6447ad 100644 --- a/src/handlers/orders.ts +++ b/src/handlers/orders.ts @@ -3,8 +3,8 @@ import { Order, OrderProduct, OrderStore } from "../models/order"; import { verifyAuthToken } from "./utils"; const orderRoutes = (app: express.Application) => { - app.get("/orders", index); - app.get("/orders/:id", read); + app.get("/orders", verifyAuthToken, index); + app.get("/orders/:id", verifyAuthToken, read); app.post("/orders", verifyAuthToken, create); app.post("/orders/:id/products", verifyAuthToken, addProduct); app.delete("/orders/:id/products", verifyAuthToken, deleteProduct); diff --git a/src/tests/handlers/orders_spec.ts b/src/tests/handlers/orders_spec.ts index 30f6e4c..414a7f7 100644 --- a/src/tests/handlers/orders_spec.ts +++ b/src/tests/handlers/orders_spec.ts @@ -56,13 +56,17 @@ describe("Order handler", () => { }); it("Should index orders", async () => { - const response = await request.get("/orders"); + const response = await request + .get("/orders") + .auth(token, { type: "bearer" }); expect(response.status).toBe(200); }); it("Should get order by id", async () => { - const response = await request.get("/orders/2"); + const response = await request + .get("/orders/2") + .auth(token, { type: "bearer" }); expect(response.status).toBe(200); });