diff --git a/infrastructure/services/database/changes/2018-08-04-1-create-location-table.sql b/infrastructure/services/database/changes/2018-08-04-1-create-location-table.sql new file mode 100644 index 00000000..5adbfdda --- /dev/null +++ b/infrastructure/services/database/changes/2018-08-04-1-create-location-table.sql @@ -0,0 +1,7 @@ +CREATE TABLE IF NOT EXISTS location ( + id INT(11), + location GEOMETRY NOT NULL, + suburb TEXT, + placeName TEXT, + postCode VARCHAR(10) +); \ No newline at end of file diff --git a/infrastructure/services/database/changes/2018-08-04-2-create-rides-table.sql b/infrastructure/services/database/changes/2018-08-04-2-create-rides-table.sql new file mode 100644 index 00000000..f6917744 --- /dev/null +++ b/infrastructure/services/database/changes/2018-08-04-2-create-rides-table.sql @@ -0,0 +1,21 @@ +CREATE TABLE IF NOT EXISTS rides ( + id INT(11) PRIMARY KEY, + client VARCHAR(255), + facilitatorEmail VARCHAR(255), + pickupTimeAndDateInUTC DATETIME, + locationFrom POINT, + locationTo POINT, + fbLink VARCHAR(255), + driverGender VARCHAR(10), + carType VARCHAR(255), + status ENUM('OPEN','CONFIRMED','ENDED','CANCELLED') DEFAULT 'OPEN', + deleted TINYINT(4), + suburbFrom VARCHAR(255), + placeNameFrom VARCHAR(255), + postCodeFrom VARCHAR(10), + suburbTo VARCHAR(255), + placeNameTo VARCHAR(255), + postCodeTo VARCHAR(10), + description VARCHAR(1024) +); + diff --git a/infrastructure/services/database/changes/2018-08-04-3-create-driver-table.sql b/infrastructure/services/database/changes/2018-08-04-3-create-driver-table.sql new file mode 100644 index 00000000..9f8fe8f0 --- /dev/null +++ b/infrastructure/services/database/changes/2018-08-04-3-create-driver-table.sql @@ -0,0 +1,7 @@ +CREATE TABLE IF NOT EXISTS driver ( + id INT(11) PRIMARY KEY, + name VARCHAR(255), + phone VARCHAR(20), + facebookUrl VARCHAR(255) +); + diff --git a/infrastructure/services/database/changes/2018-08-04-4-create-driver_car-table.sql b/infrastructure/services/database/changes/2018-08-04-4-create-driver_car-table.sql new file mode 100644 index 00000000..025dc814 --- /dev/null +++ b/infrastructure/services/database/changes/2018-08-04-4-create-driver_car-table.sql @@ -0,0 +1,8 @@ +CREATE TABLE IF NOT EXISTS driver_car ( + id INT(11) PRIMARY KEY, + driver_id INT(11), + carModel VARCHAR(255), + color VARCHAR(255), + licensePlateNumber VARCHAR(255), + FOREIGN KEY (driver_id) REFERENCES driver(id) +); \ No newline at end of file diff --git a/infrastructure/services/database/changes/2018-08-04-5-create-driver_ride-table.sql b/infrastructure/services/database/changes/2018-08-04-5-create-driver_ride-table.sql new file mode 100644 index 00000000..bbac59f7 --- /dev/null +++ b/infrastructure/services/database/changes/2018-08-04-5-create-driver_ride-table.sql @@ -0,0 +1,10 @@ +CREATE TABLE IF NOT EXISTS driver_ride ( + id INT(11) PRIMARY KEY, + driver_id INT(11), + ride_id INT(11), + confirmed TINYINT(1), + notified24h TINYINT(1), + notified5m TINYINT(1), + FOREIGN KEY (driver_id) REFERENCES driver(id), + FOREIGN KEY (ride_id) REFERENCES rides(id) +); \ No newline at end of file diff --git a/infrastructure/services/database/db-utils.js b/infrastructure/services/database/db-utils.js new file mode 100644 index 00000000..3f956826 --- /dev/null +++ b/infrastructure/services/database/db-utils.js @@ -0,0 +1,40 @@ +const connection = () => mysql.createConnection({ + host: process.env.MYSQL_HOST, + port: process.env.MYSQL_PORT, + user: process.env.MYSQL_USER, + password: process.env.MYSQL_PW, + database: 'carpal', + multipleStatements: true +}); + +class DbUtils { + + static getConnection(){ + return connection; + } + + static query(connection, queryString) { + if (typeof connection === 'string') { + const db = require('./db').connection; + queryString = connection; + connection = db(); + } + + return new Promise((resolve, reject) => { + connection.query(queryString, (error, results, fields) => { + if (error) { + console.log("Error executing", queryString, error); + } + connection.end(function (err) { + if (error) { + return reject(error); + } + resolve(results); + }); + }); + }); + } + +} + +module.exports = DbUtils; \ No newline at end of file diff --git a/infrastructure/services/database/index.js b/infrastructure/services/database/index.js new file mode 100644 index 00000000..1171977f --- /dev/null +++ b/infrastructure/services/database/index.js @@ -0,0 +1,3 @@ +let reloadDb = require("./reload-db"); +reloadDb.executeAll() + .catch(e => console.log(e)); \ No newline at end of file diff --git a/infrastructure/services/database/reload-db.js b/infrastructure/services/database/reload-db.js new file mode 100644 index 00000000..48447f3f --- /dev/null +++ b/infrastructure/services/database/reload-db.js @@ -0,0 +1,24 @@ +const DbUtils = require('./db-utils'); +const fs = require('fs'); +const path = require('path'); + +const changeSet = [ + '2018-08-04-1-create-location-table.sql', + '2018-08-04-2-create-rides-table.sql', + '2018-08-04-3-create-driver-table.sql', + '2018-08-04-4-create-driver_car-table.sql', + '2018-08-04-5-create-driver_ride-table.sql' +]; + +class ReloadDB { + executeAll() { + return Promise.all(changeSet.map(file => this.execute(file))); + } + + execute(fileName) { + let sql = fs.readFileSync(path.resolve(__dirname, './changes/' + fileName)).toString().trim(); + return DbUtils.query(sql); + } +} + +module.exports = new ReloadDB(); \ No newline at end of file diff --git a/infrastructure/services/notification/notify.js b/infrastructure/services/notification/notify.js new file mode 100644 index 00000000..d67c878f --- /dev/null +++ b/infrastructure/services/notification/notify.js @@ -0,0 +1,37 @@ +exports.handler = function (event, context, callback) { + + let AWS = require('aws-sdk'); + + // Create chron job + // search rides with time below 24 hours + // send sms to each one + // update entry to say that driver was notified + + // create amazon account for hillscarpal + + sendSMS(); + + +}; + +function sendSMS() { + let sns = new AWS.SNS(); + sns.publish({ + Message: 'Hello World', + PhoneNumber: '+610123456789', + MessageStructure: 'text' + }, function (err, data) { + console.log("test2") + if (err) { + console.log(err.stack); + } + + console.log('push sent'); + + callback(null, { + "statusCode": 200, + "headers": {"Date": new Date()}, + "body": "" + }); + }); +} \ No newline at end of file diff --git a/infrastructure/services/rides/create.js b/infrastructure/services/rides/create.js index 7e201824..a7bd60a1 100644 --- a/infrastructure/services/rides/create.js +++ b/infrastructure/services/rides/create.js @@ -2,7 +2,7 @@ const validate = require('jsonschema').validate; // eslint-disable-line import/no-extraneous-dependencies const rideSchema = require('../schema/ride.json'); // eslint-disable-line import/no-extraneous-dependencies -const db = require('../utils/db').connection; +const db = require('../database/db-utils').getConnection(); const rideStatus = require('./ride-status'); const decodeJwt = require('../utils/jwt').decodeJwt; diff --git a/infrastructure/services/rides/findone.js b/infrastructure/services/rides/findone.js index 4ddfa330..14e40a45 100644 --- a/infrastructure/services/rides/findone.js +++ b/infrastructure/services/rides/findone.js @@ -1,5 +1,5 @@ 'use strict'; -const db = require('../utils/db').connection; +const db = require('../database/db-utils').getConnection(); const mapToDto = require('./rides-mapper').mapToDto; module.exports.findone = (event, context, callback) => { diff --git a/infrastructure/services/rides/list.js b/infrastructure/services/rides/list.js index 9d77ff7f..3aaff32a 100644 --- a/infrastructure/services/rides/list.js +++ b/infrastructure/services/rides/list.js @@ -1,6 +1,6 @@ 'use strict'; const decodeJwt = require('../utils/jwt').decodeJwt; -const db = require('../utils/db').connection; +const db = require('../database/db-utils').getConnection(); const mapToDto = require('./rides-mapper').mapToDto; module.exports.list = (event, context, callback) => { diff --git a/infrastructure/services/rides/update.js b/infrastructure/services/rides/update.js index 1248f338..5b035ac2 100644 --- a/infrastructure/services/rides/update.js +++ b/infrastructure/services/rides/update.js @@ -1,6 +1,6 @@ 'use strict'; -const db = require('../utils/db').connection; +const db = require('../database/db-utils').getConnection() const rideStatus = require('./ride-status'); const decodeJwt = require('../utils/jwt').decodeJwt; diff --git a/infrastructure/services/serverless.yml b/infrastructure/services/serverless.yml index 3f6665c0..6930d561 100644 --- a/infrastructure/services/serverless.yml +++ b/infrastructure/services/serverless.yml @@ -9,8 +9,10 @@ provider: timeout: 30 environment: AUTH0_CLIENT_ID: ${file(./props.json):AUTH0_CLIENT_ID} + MYSQL_HOST: carpal.cttgjqpjknhf.ap-southeast-2.rds.amazonaws.com MYSQL_PW: ${file(./secrets.json):MYSQL_PW} MYSQL_PORT: ${file(./secrets.json):MYSQL_PORT} + MYSQL_USER: carpaladmin AUTH0_CLIENT_PUBLIC_KEY: ${file(./public_key)} vpc: @@ -86,6 +88,11 @@ functions: path: map method: get cors: true + notify: + handler: notify + events: + - schedule: rate(2 hours) + - schedule: cron(0 12 * * ? *) resources: Resources: GatewayResponse: diff --git a/infrastructure/services/utils/db.js b/infrastructure/services/utils/db.js deleted file mode 100644 index b194ea77..00000000 --- a/infrastructure/services/utils/db.js +++ /dev/null @@ -1,11 +0,0 @@ -var mysql = require('mysql'); -const MYSQL_PW = process.env.MYSQL_PW; -const MYSQL_PORT = process.env.MYSQL_PORT; - -module.exports.connection = () => mysql.createConnection({ - host: 'carpal.cttgjqpjknhf.ap-southeast-2.rds.amazonaws.com', - port: MYSQL_PORT, - user: 'carpaladmin', - password: MYSQL_PW, - database: 'carpal' -}); \ No newline at end of file