From e6adbb6bdb3bce51e61d1e425d56ce4b88b77a1d Mon Sep 17 00:00:00 2001 From: Jake Date: Tue, 1 Feb 2022 11:44:20 +0000 Subject: [PATCH 1/2] Add bill, journey and location models --- server/database/index.js | 5 ++++- server/models/bill.js | 30 ++++++++++++++++++++++++++++++ server/models/journey.js | 29 +++++++++++++++++++++++++++++ server/models/location.js | 22 ++++++++++++++++++++++ server/package.json | 1 + 5 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 server/models/bill.js create mode 100644 server/models/journey.js create mode 100644 server/models/location.js diff --git a/server/database/index.js b/server/database/index.js index 8ffff98..9867b72 100644 --- a/server/database/index.js +++ b/server/database/index.js @@ -3,9 +3,12 @@ const environment = process.env.NODE_ENV; const mongoose = require("mongoose"); mongoose.Promise = global.Promise; const dbConfig = require("../config/db.config.js"); +const journey = require("../models/journey")(mongoose) +const bill = require("../models/bill")(mongoose) +const location = require("../models/location")(mongoose) // Create mongoose and read in config -const db = {}; +const db = {journey: journey, bill: bill, location: location}; db.mongoose = mongoose; db.url = dbConfig.url; diff --git a/server/models/bill.js b/server/models/bill.js new file mode 100644 index 0000000..474f5ed --- /dev/null +++ b/server/models/bill.js @@ -0,0 +1,30 @@ +module.exports = mongoose => { + const billSchema = mongoose.Schema( + { + journey: { + type: mongoose.Schema.Types.ObjectId, + ref: "journey", + required: [true, "A journey must be attached to a bill."], + unique: true + }, + driver: { + type: String, + required: [true, "A driver must be assigned to a bill."] + }, + cost: { + type: Number, + required: [true, "A cost for the bill is required."] + } + }, + { + toJSON: {virtuals: true}, + toObject: {virtuals: true} + } + ) + + billSchema.virtual('billReferenceNumber', { + billReferenceNumber: this._id + }) + + return mongoose.model("bill", billSchema) +} \ No newline at end of file diff --git a/server/models/journey.js b/server/models/journey.js new file mode 100644 index 0000000..dc3feea --- /dev/null +++ b/server/models/journey.js @@ -0,0 +1,29 @@ +const haversine = require('haversine-distance') + +module.exports = mongoose => { + const journeySchema = mongoose.Schema( + { + regNumber: { + type: "String", + required: [true, "A car registration number is required"] + }, + entryLocation: { + type: mongoose.Schema.Types.ObjectId, + ref: "location" + }, + exitLocation: { + type: mongoose.Schema.Types.ObjectId, + ref: "location" + }, + journeyDateTime: { + type: Date + } + } + ) + + journeySchema.methods.getJourneyDistance = function() { + return haversine(this.entryLocation.coordinates, this.exitLocation.coordinates) + } + + return mongoose.model("journey", journeySchema) +} \ No newline at end of file diff --git a/server/models/location.js b/server/models/location.js new file mode 100644 index 0000000..aa25a29 --- /dev/null +++ b/server/models/location.js @@ -0,0 +1,22 @@ +module.exports = mongoose => { + const locationSchema = mongoose.Schema( + { + name: { + type: "String", + required: [true, "A location name is required"] + }, + coordinates: { + longitude: { + type: "Number", + required: [true, "A longitude is required"] + }, + latitude: { + type: "Number", + required: [true, "A latitude is required"] + } + } + } + ) + + return mongoose.model("location", locationSchema) +} \ No newline at end of file diff --git a/server/package.json b/server/package.json index 29e0bde..f73f5af 100644 --- a/server/package.json +++ b/server/package.json @@ -8,6 +8,7 @@ "dotenv": "^15.0.0", "eslint": "^8.8.0", "express": "^4.17.2", + "haversine-distance": "^1.2.1", "jshint": "^2.13.4", "mocha": "^9.2.0", "mongodb": "^4.3.1", From bda31d2395439c24e78f9d547ac7ce2d4dca7722 Mon Sep 17 00:00:00 2001 From: Jake Date: Tue, 1 Feb 2022 11:50:53 +0000 Subject: [PATCH 2/2] Make exit and entry location required based on code review --- server/models/journey.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/server/models/journey.js b/server/models/journey.js index dc3feea..481b3f1 100644 --- a/server/models/journey.js +++ b/server/models/journey.js @@ -9,11 +9,13 @@ module.exports = mongoose => { }, entryLocation: { type: mongoose.Schema.Types.ObjectId, - ref: "location" + ref: "location", + required: [true, "A entry location is required."] }, exitLocation: { type: mongoose.Schema.Types.ObjectId, - ref: "location" + ref: "location", + required: [true, "A exit location is required."] }, journeyDateTime: { type: Date