-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jack Dalrymple Hamilton
committed
Apr 23, 2018
0 parents
commit ee4dbbc
Showing
20 changed files
with
5,020 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"extends": "google", | ||
"parserOptions": { | ||
"ecmaVersion": 6 | ||
}, | ||
"rules": { | ||
"max-len": 0, | ||
"linebreak-style": 0, | ||
"no-trailing-spaces": 0 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# Logs | ||
logs | ||
*.log | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
|
||
# Runtime data | ||
pids | ||
*.pid | ||
*.seed | ||
*.pid.lock | ||
|
||
# Directory for instrumented libs generated by jscoverage/JSCover | ||
lib-cov | ||
|
||
# Coverage directory used by tools like istanbul | ||
coverage | ||
|
||
# nyc test coverage | ||
.nyc_output | ||
|
||
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) | ||
.grunt | ||
|
||
# Bower dependency directory (https://bower.io/) | ||
bower_components | ||
|
||
# node-waf configuration | ||
.lock-wscript | ||
|
||
# Compiled binary addons (http://nodejs.org/api/addons.html) | ||
build/Release | ||
|
||
# Dependency directories | ||
node_modules/ | ||
jspm_packages/ | ||
|
||
# Typescript v1 declaration files | ||
typings/ | ||
|
||
# Optional npm cache directory | ||
.npm | ||
|
||
# Optional eslint cache | ||
.eslintcache | ||
|
||
# Optional REPL history | ||
.node_repl_history | ||
|
||
# Output of 'npm pack' | ||
*.tgz | ||
|
||
# Yarn Integrity file | ||
.yarn-integrity | ||
|
||
# dotenv environment variables file | ||
.env | ||
|
||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
language: node_js | ||
node_js: | ||
- "8" | ||
services: mongodb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
FROM node:carbon | ||
|
||
WORKDIR /usr/src/app | ||
|
||
COPY package.json ./ | ||
|
||
RUN yarn install | ||
|
||
COPY . . | ||
|
||
EXPOSE 3000 | ||
|
||
ENV MONGO_URL=mongo | ||
|
||
CMD [ "yarn", "start" ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2018 Jack | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
const mongoose = require('mongoose'); | ||
const Schema = mongoose.Schema; | ||
|
||
const readValidator = [ | ||
{ | ||
validator: (array) => array.length === 2, | ||
msg: 'Please provide two reads', | ||
}, | ||
{ | ||
validator: (array) => { | ||
for (let read of array) if (typeof (read) !== 'object') return false; | ||
return true; | ||
}, | ||
msg: 'Please provide reads in the format {type: String, registerId: Number, value: Number}', | ||
}, | ||
]; | ||
|
||
const ReadSchema = new Schema({ | ||
'type': { | ||
type: String, | ||
required: [true, 'Please provide a value for {PATH}'], | ||
validate: { | ||
validator: (v) => { | ||
return v === 'ANYTIME' || v === 'NIGHT'; | ||
}, | ||
message: '{VALUE} is not a valid option. Select either (ANYTIME/NIGHT)', | ||
}, | ||
}, | ||
'registerId': { | ||
type: Number, | ||
required: [true, 'Please provide a value for {PATH}']}, | ||
'value': { | ||
type: Number, | ||
required: [true, 'Please provide a value for {PATH}'], | ||
}, | ||
}); | ||
|
||
const MeterReadingSchema = new Schema({ | ||
'customerId': {type: String, required: [true, 'Please define a customerId.']}, | ||
'serialNumber': {type: Number, required: [true, 'Please define a serial number.']}, | ||
'mpxn': {type: Number, required: [true, 'Please define a mpxn.']}, | ||
'read': { | ||
type: [ReadSchema], | ||
validate: readValidator, | ||
required: true, | ||
}, | ||
'readDate': {type: Date, default: Date.now()}, | ||
}); | ||
|
||
module.exports = mongoose.model('MeterReadings', MeterReadingSchema); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
const express = require('express'); | ||
const router = new express.Router(); | ||
const MeterReadings = require('../models/MeterReading'); | ||
const uuidv1 = require('uuid/v1'); | ||
const debug = require('debug')('readings.accept.js'); | ||
|
||
|
||
/** | ||
* Given a {reading} or [{reading}] it will insert them into the database. | ||
* Should any of the readings not be valid they will not be inserted. | ||
* | ||
* @name AcceptReadings | ||
* @Param {Object=} {Reading} - A single reading object | ||
* @Param [Object=] [{Reading}] - A list of single reading objects | ||
* @Return {Status Code} - 200 if successfully inserted, 400 if there was a formatting error | ||
*/ | ||
router.post('/meter-read', (req, res) => { | ||
MeterReadings.create(req.body).then( () => { | ||
res.sendStatus(200); | ||
}).catch((error) => { | ||
debug('Error saving, Details: %O', error); | ||
res.status(400).json(error.errors); | ||
}); | ||
}); | ||
|
||
module.exports = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
const express = require('express'); | ||
const debug = require('debug')('readings.present.js'); | ||
|
||
const MeterReadings = require('../models/MeterReading'); | ||
|
||
const router = new express.Router(); | ||
const LIMIT = 50; | ||
const ORDER = '-'; | ||
|
||
/** | ||
* Prepared API which will allow the user to safely personalise what is returned. | ||
* | ||
* @param {number=} [limit=50] - The number of items to return | ||
* @param {number=} [order=descending] - The order of items returned by date. | ||
* | ||
* @param {string=} {customerId} - The Id of the customer | ||
* @param {number=} {mpxn} - The mpxn of the premise. | ||
* @param {number=} {serialNumber} - The serial number of the meter. | ||
* @param {number=} {registerId} - Serial number of the register. | ||
* @param {string=} {registerIdType} - Can only be used in conjunction within registerId to define it's type. | ||
*/ | ||
router.get('/meter-read', (req, res) => { | ||
const limit = req.query.limit ? +req.query.limit : LIMIT; | ||
const order = (req.query.order && req.query.order === 'ascending') ? '' : ORDER; | ||
|
||
const query = {}; | ||
|
||
if (req.query.customerId) query['customerId'] = req.query.customerId; | ||
if (req.query.mpxn) query['mpxn'] = req.query.mpxn; | ||
if (req.query.serialNumber) query['serialNumber'] = req.query.serialNumber; | ||
if (req.query.registerId) { | ||
query['read'] = {$elemMatch: {registerId: req.query.registerId}}; | ||
if (req.query.registerIdType) query['read']['$elemMatch']['type'] = req.query.registerIdType; | ||
} | ||
|
||
MeterReadings.find(query).sort(`${order}readDate`).limit(limit).then( (model) => { | ||
res.send(model); | ||
}).catch( (error) => { | ||
debug('Failed finding a user: %O', error); | ||
res.sendStatus(500); | ||
}); | ||
}); | ||
|
||
|
||
module.exports = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
const express = require('express'); | ||
const debug = require('debug')('readings.present.js'); | ||
|
||
const MeterReadings = require('../models/MeterReading'); | ||
|
||
const router = new express.Router(); | ||
const LIMIT = 50; | ||
const ORDER = '-'; | ||
|
||
/** | ||
* Powerful API which allows the user to define their own queries. Although it could pose a security risk by returning information which we do not want them to see. This can be offset by limiting it to a single collection as well as with authentication. | ||
* | ||
* @param {number=} [limit=50] - The number of items to return | ||
* @param {number=} [order=descending] - The order of items returned by date. | ||
* | ||
* @param {object} [req.body] - The query passed into the method via the request's body. | ||
* @return {statusCode} | ||
*/ | ||
router.post('/meter-read/', (req, res) => { | ||
const limit = req.query.limit ? +req.query.limit : LIMIT; | ||
const order = (req.query.order && req.query.order === 'ascending') ? '' : ORDER; | ||
MeterReadings.find(req.body).sort(`${order}readDate`).limit(limit).then((model) => { | ||
if (model) { | ||
res.json(model); | ||
} else { | ||
res.sendStatus(404); | ||
} | ||
}).catch((err) => { | ||
res.status(400).send(err.message); | ||
}); | ||
}); | ||
|
||
module.exports = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
const express = require('express'); | ||
const router = new express.Router(); | ||
const MeterReadings = require('../models/MeterReading'); | ||
const uuidv1 = require('uuid/v1'); | ||
const debug = require('debug')('readings.accept.js'); | ||
|
||
/** | ||
* Customer has moved into a new house. | ||
* A Serial Number for the meter already exists. Lets create a new User and add it to them | ||
*/ | ||
router.get('/generateUniqueCustomerID', (req, res) => { | ||
generateCustomerID().then( (customerId) => { | ||
res.send(customerId); | ||
}).catch((err) => { | ||
debug(err); | ||
res.sendStatus(500); | ||
}); | ||
}); | ||
|
||
/** | ||
* Generates a truly unique customer id. | ||
* @return {Promise<any>} | ||
*/ | ||
function generateCustomerID() { | ||
return new Promise((resolve, reject) => { | ||
let id = uuidv1(); | ||
MeterReadings.findOne({'customerId': id}).then((model) => { | ||
if (model) { | ||
resolve(generateCustomerID()); | ||
} else { | ||
resolve(id); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
module.exports = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
version: "2" | ||
services: | ||
app: | ||
container_name: app | ||
restart: always | ||
build: . | ||
ports: | ||
- "3000:3000" | ||
links: | ||
- mongo | ||
mongo: | ||
container_name: mongo | ||
image: mongo | ||
volumes: | ||
- ./data:/data/db | ||
ports: | ||
- "27017:27017" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{ | ||
"name": "GreenEnergy", | ||
"version": "1.0.0", | ||
"author": "Jack Dalrymple Hamilton", | ||
"description": "Green Energy is an established utility company, and they have approached us to create a service for accepting meter reads. The main goal here is to *accept*, *store* and *present* meter readings based on a customer's unique identifier.", | ||
"main": "index.js", | ||
"license": "MIT", | ||
"dependencies": { | ||
"body-parser": "^1.18.2", | ||
"debug": "^3.1.0", | ||
"express": "^4.16.3", | ||
"mongoose": "^5.0.15", | ||
"morgan": "^1.9.0", | ||
"nodemon": "^1.17.3", | ||
"uuid": "^3.2.1" | ||
}, | ||
"devDependencies": { | ||
"chai": "^4.1.2", | ||
"chai-datetime": "^1.5.0", | ||
"chai-http": "^4.0.0", | ||
"eslint": "^4.19.1", | ||
"eslint-config-google": "^0.9.1", | ||
"mocha": "^5.1.1" | ||
}, | ||
"scripts": { | ||
"start": "set NODE_ENV=dev && node server.js", | ||
"test": "set NODE_ENV=test && mocha --exit" | ||
} | ||
} |
Oops, something went wrong.