Skip to content

Commit

Permalink
#13 - Added support for modified the database structure from the code
Browse files Browse the repository at this point in the history
- Added driver table
- Added driver_car table
- Added driver_ride table
- Created initial prototype for sending sms
- Moved some mysql configurations
  • Loading branch information
douglascvas committed Aug 4, 2018
1 parent 568396b commit 1e29661
Show file tree
Hide file tree
Showing 15 changed files with 168 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CREATE TABLE IF NOT EXISTS location (
id INT(11),
location GEOMETRY NOT NULL,
suburb TEXT,
placeName TEXT,
postCode VARCHAR(10)
);
Original file line number Diff line number Diff line change
@@ -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)
);

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CREATE TABLE IF NOT EXISTS driver (
id INT(11) PRIMARY KEY,
name VARCHAR(255),
phone VARCHAR(20),
facebookUrl VARCHAR(255)
);

Original file line number Diff line number Diff line change
@@ -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)
);
Original file line number Diff line number Diff line change
@@ -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)
);
40 changes: 40 additions & 0 deletions infrastructure/services/database/db-utils.js
Original file line number Diff line number Diff line change
@@ -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;
3 changes: 3 additions & 0 deletions infrastructure/services/database/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
let reloadDb = require("./reload-db");
reloadDb.executeAll()
.catch(e => console.log(e));
24 changes: 24 additions & 0 deletions infrastructure/services/database/reload-db.js
Original file line number Diff line number Diff line change
@@ -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();
37 changes: 37 additions & 0 deletions infrastructure/services/notification/notify.js
Original file line number Diff line number Diff line change
@@ -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": ""
});
});
}
2 changes: 1 addition & 1 deletion infrastructure/services/rides/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion infrastructure/services/rides/findone.js
Original file line number Diff line number Diff line change
@@ -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) => {
Expand Down
2 changes: 1 addition & 1 deletion infrastructure/services/rides/list.js
Original file line number Diff line number Diff line change
@@ -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) => {
Expand Down
2 changes: 1 addition & 1 deletion infrastructure/services/rides/update.js
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
7 changes: 7 additions & 0 deletions infrastructure/services/serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
11 changes: 0 additions & 11 deletions infrastructure/services/utils/db.js

This file was deleted.

0 comments on commit 1e29661

Please sign in to comment.