Commit 628e5bf2 authored by abergavenny's avatar abergavenny
Browse files

Version 1.0.0

parent b463e8cc
import express from 'express'
import { checkSchema } from 'express-validator'
import { createBuilding, getApartments, getBuilding, getBuildings, getSimulation, getUsers, updateBuilding, updateBuildingDataCharacteristics, updateBuildingDataBasement, updateBuildingDataFacade, updateBuildingDataHeating, updateBuildingDataRoof } from '../../controllers/buildings.js'
import asyncHandler from '../../middleware/asyncHandler.js'
import permissionHandler from '../../middleware/permissionHandler.js'
import { basementSchema, characteristicsSchema, createBuildingSchema, facadeSchema, heatingSchema, roofSchema, simulationSchema } from '../../schemas/buildings.js'
import { idSchema } from '../../schemas/index.js'
const router = express.Router()
router.get('/', permissionHandler, asyncHandler(getBuildings))
router.get('/:id', checkSchema(idSchema), asyncHandler(getBuilding))
router.get('/:id/apartments', checkSchema(idSchema), asyncHandler(getApartments))
router.get('/:id/simulations/run', checkSchema(simulationSchema), asyncHandler(getSimulation))
router.get('/:id/users', checkSchema(idSchema), asyncHandler(getUsers))
router.post('/', permissionHandler, checkSchema(createBuildingSchema), asyncHandler(createBuilding))
router.put('/:id', permissionHandler, asyncHandler(updateBuilding))
router.put('/:id/basement', permissionHandler, checkSchema(basementSchema), asyncHandler(updateBuildingDataBasement))
router.put('/:id/characteristics', permissionHandler, checkSchema(characteristicsSchema), asyncHandler(updateBuildingDataCharacteristics))
router.put('/:id/facade', permissionHandler, checkSchema(facadeSchema), asyncHandler(updateBuildingDataFacade))
router.put('/:id/heating', permissionHandler, checkSchema(heatingSchema), asyncHandler(updateBuildingDataHeating))
router.put('/:id/roof', permissionHandler, checkSchema(roofSchema), asyncHandler(updateBuildingDataRoof))
export default router
import express from 'express'
import { getSelf } from '../../controllers/self.js'
import asyncHandler from '../../middleware/asyncHandler.js'
const router = express.Router()
router.get('/', asyncHandler(getSelf))
export default router
import express from 'express'
import { checkSchema } from 'express-validator'
import { getSimulation, getSimulations } from '../../controllers/simulations.js'
import asyncHandler from '../../middleware/asyncHandler.js'
import { idSchema } from '../../schemas/index.js'
const router = express.Router()
router.get('/', asyncHandler(getSimulations))
router.get('/:id', checkSchema(idSchema), asyncHandler(getSimulation))
export default router
import express from 'express'
import { checkSchema } from 'express-validator'
import { allowOrRevokeSharing, getUser, getUsers, updatePassword, updateUser } from '../../controllers/users.js'
import asyncHandler from '../../middleware/asyncHandler.js'
import permissionHandler from '../../middleware/permissionHandler.js'
import { idSchema } from '../../schemas/index.js'
import { allowOrRevokeSharingSchema, updatePasswordSchema, updateUserSchema } from '../../schemas/users.js'
const router = express.Router()
router.get('/', permissionHandler, asyncHandler(getUsers))
router.get('/:id', permissionHandler, checkSchema(idSchema), asyncHandler(getUser))
router.put('/:id', checkSchema(updateUserSchema), asyncHandler(updateUser))
router.put('/:id/change-password', permissionHandler, checkSchema(updatePasswordSchema), asyncHandler(updatePassword))
router.put('/:id/sharing', checkSchema(allowOrRevokeSharingSchema), asyncHandler(allowOrRevokeSharing))
export default router
import express from 'express'
import { checkSchema } from 'express-validator'
import rateLimit from 'express-rate-limit'
import { confirm, login, register, resetPassword, updatePassword } from '../../controllers/authentication.js'
import asyncHandler from '../../middleware/asyncHandler.js'
import { confirmSchema, loginSchema, registerSchema, updatePasswordSchema } from '../../schemas/authentication.js'
import { emailSchema } from '../../schemas/index.js'
import { warning } from '../../helpers/index.js'
const router = express.Router()
const authLimiter = rateLimit({
windowMs: 15 * 60 * 1000,
max: 25,
standardHeaders: true,
legacyHeaders: false,
message: async (req, res) => {
return warning(res, { code: 'TOO_MANY_REQUESTS' })
}
})
router.use(authLimiter)
router.get('/confirm/:token', checkSchema(confirmSchema), asyncHandler(confirm))
router.post('/login', checkSchema(loginSchema), asyncHandler(login))
router.post('/register', checkSchema(registerSchema), asyncHandler(register))
router.post('/reset-password', checkSchema(emailSchema), asyncHandler(resetPassword))
router.post('/update-password/:token', checkSchema(updatePasswordSchema), asyncHandler(updatePassword))
export default router
export { default as authRouter } from './auth/index.js'
export { default as apartmentRouter } from './api/apartments.js'
export { default as buildingRouter } from './api/buildings.js'
export { default as selfRouter } from './api/self.js'
export { default as simulationRouter } from './api/simulations.js'
export { default as userRouter } from './api/users.js'
export const createApartmentSchema = {
apartmentName: {
in: ['body'],
exists: true,
errorMessage: 'Missing field'
},
buildingId: {
in: ['body'],
exists: true,
errorMessage: 'Missing field'
},
password: {
in: ['body'],
exists: true,
errorMessage: 'Missing field'
},
username: {
in: ['body'],
exists: true,
errorMessage: 'Missing field'
}
}
export const heatingSchema = {
id: {
in: ['params'],
exists: true,
isString: true
},
apartmentComment: {
in: ['body'],
errorMessage: 'Missing field',
isString: true,
optional: {
options: {
nullable: true
}
}
},
area: {
in: ['body'],
errorMessage: 'Missing field',
isInt: true,
toInt: true,
optional: {
options: {
nullable: true
}
}
},
ceilingHeight: {
in: ['body'],
errorMessage: 'Missing field',
isNumeric: true,
optional: {
options: {
nullable: true
}
}
},
centralHeating: {
in: ['body'],
errorMessage: 'Missing field',
isBoolean: true,
toBoolean: true,
optional: {
options: {
nullable: true
}
}
},
heatDemand: {
in: ['body'],
errorMessage: 'Missing field',
isInt: true,
toInt: true,
optional: {
options: {
nullable: true
}
}
},
heatingType: {
in: ['body'],
errorMessage: 'Missing field',
isString: true,
optional: {
options: {
nullable: true
}
}
},
largeHeatingElements: {
in: ['body'],
errorMessage: 'Missing field',
isInt: true,
toInt: true,
optional: {
options: {
nullable: true
}
}
},
mediumHeatingElements: {
in: ['body'],
errorMessage: 'Missing field',
isInt: true,
toInt: true,
optional: {
options: {
nullable: true
}
}
},
smallHeatingElements: {
in: ['body'],
errorMessage: 'Missing field',
isInt: true,
toInt: true,
optional: {
options: {
nullable: true
}
}
},
panelHeating: {
in: ['body'],
errorMessage: 'Missing field',
isInt: true,
toInt: true,
optional: {
options: {
nullable: true
}
}
},
powerDemand: {
in: ['body'],
errorMessage: 'Missing field',
isInt: true,
toInt: true,
optional: {
options: {
nullable: true
}
}
},
workingHeatingInstallations: {
in: ['body'],
errorMessage: 'Missing field',
isInt: true,
toInt: true,
optional: {
options: {
nullable: true
}
}
}
}
export const windowSchema = {
id: {
in: ['params'],
exists: true,
isString: true
},
averageWindowAge: {
in: ['body'],
errorMessage: 'Missing field',
isInt: true,
toInt: true,
optional: {
options: {
nullable: true
}
}
},
averageWindowCondition: {
in: ['body'],
errorMessage: 'Missing field',
isString: true,
optional: {
options: {
nullable: true
}
}
},
largeWindows: {
in: ['body'],
errorMessage: 'Missing field',
isInt: true,
toInt: true,
optional: {
options: {
nullable: true
}
}
},
mediumWindows: {
in: ['body'],
errorMessage: 'Missing field',
isInt: true,
toInt: true,
optional: {
options: {
nullable: true
}
}
},
roofWindows: {
in: ['body'],
errorMessage: 'Missing field',
isInt: true,
toInt: true,
optional: {
options: {
nullable: true
}
}
},
smallWindows: {
in: ['body'],
errorMessage: 'Missing field',
isInt: true,
toInt: true,
optional: {
options: {
nullable: true
}
}
},
windowComment: {
in: ['body'],
errorMessage: 'Missing field',
isString: true,
optional: {
options: {
nullable: true
}
}
},
windowFrame: {
in: ['body'],
errorMessage: 'Missing field',
isString: true,
optional: {
options: {
nullable: true
}
}
},
windowGlazing: {
in: ['body'],
errorMessage: 'Missing field',
isString: true,
optional: {
options: {
nullable: true
}
}
}
}
export const confirmSchema = {
token: {
in: ['params'],
exists: true,
errorMessage: 'Missing field'
}
}
export const loginSchema = {
emailOrUsername: {
in: ['body'],
exists: true,
errorMessage: 'Missing field'
},
password: {
in: ['body'],
exists: true,
errorMessage: 'Missing field'
}
}
export const registerSchema = {
email: {
in: ['body'],
exists: true,
errorMessage: 'Missing field'
},
password: {
in: ['body'],
exists: true,
errorMessage: 'Missing field'
},
gmlid: {
in: ['body'],
errorMessage: 'Missing field'
}
}
export const updatePasswordSchema = {
token: {
in: ['params'],
exists: true,
errorMessage: 'Missing field'
},
password: {
in: ['body'],
exists: true,
errorMessage: 'Missing field'
}
}
export const createBuildingSchema = {
buildingPrefix: {
in: ['body'],
exists: true,
errorMessage: 'Missing field'
},
buildingName: {
in: ['body'],
exists: true,
errorMessage: 'Missing field'
},
buildingAddress: {
in: ['body'],
exists: true,
errorMessage: 'Missing field'
},
buildingGmlId: {
in: ['body'],
errorMessage: 'Missing field'
}
}
export const basementSchema = {
id: {
in: ['params'],
exists: true,
isString: true
},
basementInsulatingMaterial: {
in: ['body'],
errorMessage: 'Missing field',
isString: true,
optional: {
options: {
nullable: true
}
}
},
basementInsulatingMaterialThickness: {
in: ['body'],
errorMessage: 'Missing field',
isInt: true,
toInt: true,
optional: {
options: {
nullable: true
}
}
},
basementRefurbishmentComment: {
in: ['body'],
errorMessage: 'Missing field',
isString: true,
optional: {
options: {
nullable: true
}
}
},
heatedBasement: {
in: ['body'],
errorMessage: 'Missing field',
isBoolean: true,
toBoolean: true,
optional: {
options: {
nullable: true
}
}
},
insulatedBasementCeiling: {
in: ['body'],
errorMessage: 'Missing field',
isBoolean: true,
toBoolean: true,
optional: {
options: {
nullable: true
}
}
},
insulatedBasementFloor: {
in: ['body'],
errorMessage: 'Missing field',
isBoolean: true,
toBoolean: true,
optional: {
options: {
nullable: true
}
}
}
}
export const characteristicsSchema = {
id: {
in: ['params'],
exists: true,
isString: true
},
characteristicsComment: {
in: ['body'],
errorMessage: 'Missing field',
isString: true,
optional: {
options: {
nullable: true
}
}
},
energyPerformanceCertificate: {
in: ['body'],
errorMessage: 'Missing field',
isInt: true,
toInt: true,
optional: {
options: {
nullable: true
}
}
},
listedBuilding: {
in: ['body'],
errorMessage: 'Missing field',
isBoolean: true,
toBoolean: true,
optional: {
options: {
nullable: true
}
}
},
livingSpace: {
in: ['body'],
errorMessage: 'Missing field',
isInt: true,
toInt: true,
optional: {
options: {
nullable: true
}
}
},
numberOfFloors: {
in: ['body'],
errorMessage: 'Missing field',
isInt: true,
toInt: true,
optional: {
options: {
nullable: true
}
}
},
yearOfConstruction: {
in: ['body'],
errorMessage: 'Missing field',
isInt: true,
toInt: true,
optional: {
options: {
nullable: true
}
}
}
}
export const facadeSchema = {
id: {
in: ['params'],
exists: true,
isString: true
},
buildingStructure: {
in: ['body'],
errorMessage: 'Missing field',
isString: true,
optional: {
options: {
nullable: true
}
}
},
buildingStructureThickness: {
in: ['body'],
errorMessage: 'Missing field',
isInt: true,
toInt: true,
optional: {
options: {
nullable: true
}
}
},
facadeEast: {
in: ['body'],
errorMessage: 'Missing field',
isBoolean: true,
toBoolean: true,
optional: {
options: {
nullable: true
}
}
},
facadeInsulatingMaterial: {
in: ['body'],
errorMessage: 'Missing field',
isString: true,
optional: {
options: {
nullable: true
}
}
},
facadeInsulatingMaterialThickness: {
in: ['body'],
errorMessage: 'Missing field',
isInt: true,
toInt: true,
optional: {
options: {
nullable: true
}
}
},
facadeNorth: {
in: ['body'],
errorMessage: 'Missing field',
isBoolean: true,
toBoolean: true,
optional: {
options: {
nullable: true
}
}
},
facadeRefurbishmentComment: {
in: ['body'],
errorMessage: 'Missing field',
isString: true,
optional: {
options: {
nullable: true
}
}
},
facadeSouth: {
in: ['body'],
errorMessage: 'Missing field',
isBoolean: true,
toBoolean: true,
optional: {
options: {
nullable: true
}
}
},
facadeWest: {
in: ['body'],
errorMessage: 'Missing field',
isBoolean: true,
toBoolean: true,
optional: {
options: {
nullable: true
}
}
}
}
export const heatingSchema = {
id: {
in: ['params'],
exists: true,
isString: true
},
heatingConsumption: {
in: ['body'],
errorMessage: 'Missing field',
isInt: true,
toInt: true,
optional: {
options: {
nullable: true
}
}
},
heatingInstallation: {
in: ['body'],
errorMessage: 'Missing field',
isString: true,
optional: {
options: {
nullable: true
}
}
},
heatingInstallationComment: {
in: ['body'],
errorMessage: 'Missing field',
isString: true,
optional: {
options: {
nullable: true
}
}
},
photovoltaic: {
in: ['body'],
errorMessage: 'Missing field',
isBoolean: true,
toBoolean: true,
optional: {
options: {
nullable: true
}
}
},
photovoltaicArea: {
in: ['body'],
errorMessage: 'Missing field',
isInt: true,
toInt: true,
optional: {
options: {
nullable: true
}
}
},
photovoltaicYield: {
in: ['body'],
errorMessage: 'Missing field',
isInt: true,
toInt: true,
optional: {
options: {
nullable: true
}
}
},
pipeSystem: {
in: ['body'],
errorMessage: 'Missing field',
isBoolean: true,
toBoolean: true,
optional: {
options: {
nullable: true
}
}
},
selfContainedCentralHeating: {
in: ['body'],
errorMessage: 'Missing field',
isBoolean: true,
toBoolean: true,
optional: {
options: {
nullable: true
}
}
},
solarHeat: {
in: ['body'],
errorMessage: 'Missing field',
isBoolean: true,
toBoolean: true,
optional: {
options: {
nullable: true
}
}
},
solarHeatArea: {
in: ['body'],
errorMessage: 'Missing field',
isInt: true,
toInt: true,
optional: {
options: {
nullable: true
}
}
}
}
export const roofSchema = {
id: {
in: ['params'],
exists: true,
isString: true
},
clouding: {
in: ['body'],
errorMessage: 'Missing field',
isInt: true,
toInt: true,
optional: {
options: {
nullable: true
}
}
},
flatRoof: {
in: ['body'],
errorMessage: 'Missing field',
isBoolean: true,
toBoolean: true,
optional: {
options: {
nullable: true
}
}
},
heatedAttic: {
in: ['body'],
errorMessage: 'Missing field',
isBoolean: true,
toBoolean: true,
optional: {
options: {
nullable: true
}
}
},
roofArea: {
in: ['body'],
errorMessage: 'Missing field',
isInt: true,
toInt: true,
optional: {
options: {
nullable: true
}
}
},
roofInsulatingMaterial: {
in: ['body'],
errorMessage: 'Missing field',
isString: true,
optional: {
options: {
nullable: true
}
}
},
roofInsulatingMaterialThickness: {
in: ['body'],
errorMessage: 'Missing field',
isInt: true,
toInt: true,
optional: {
options: {
nullable: true
}
}
},
roofRefurbishmentComment: {
in: ['body'],
errorMessage: 'Missing field',
isString: true,
optional: {
options: {
nullable: true
}
}
}
}
export const simulationSchema = {
gmlId: {
in: ['query'],
errorMessage: 'Missing field',
isAlphanumeric: true
}
}
export const idSchema = {
id: {
in: ['params'],
exists: true,
isAlphanumeric: true
}
}
export const emailSchema = {
email: {
in: ['body'],
isEmail: true
}
}
export const allowOrRevokeSharingSchema = {
sharingAllowed: {
in: ['body'],
errorMessage: 'Missing field',
exists: true,
isBoolean: true,
toBoolean: true
}
}
export const updatePasswordSchema = {
id: {
in: ['params'],
exists: true,
isString: true
},
newPassword: {
in: ['body'],
errorMessage: 'Missing field',
exists: true,
isString: true
}
}
export const updateUserSchema = {
id: {
in: ['params'],
exists: true,
isString: true
},
email: {
in: ['body'],
errorMessage: 'Missing field',
exists: true,
isString: true
}
}
import http from 'http'
import mongoose from 'mongoose'
import { app } from './app.js'
import config from './config/appConfig.js'
import { connectionOptions, getConnectionString, Management } from './db/index.js'
export default async function runServer (...args) {
try {
const uri = getConnectionString()
const db = await mongoose.connect(uri, connectionOptions)
await Management.initManagementSettings()
const server = http.createServer(app)
server.on('error', (error) => {
console.log('Error:', error.message)
server.close()
})
server.listen(config.port, () => {
console.log(`Server running on port ${config.port}`)
})
process.on('SIGTERM', () => {
server.close(() => {
console.log('HTTP server closed.')
db.connection.close(false, () => {
console.log('MongoDB server closed.')
})
})
})
} catch (err) {
if (err.name === 'MongooseServerSelectionError') {
console.log('ERROR: MongooseServerSelectionError')
} else {
console.log('ERROR:', err.name)
}
process.exit(1)
}
}
import axios from 'axios'
import appConfig from '../config/appConfig.js'
const URI = appConfig.simulationEndpoint
export const executeSimulation = async (data) => {
return axios.post(`${URI}/executeSimulation`, { ...data })
.then(response => response.data)
.catch(e => {
console.log('ERROR:', e.name)
return null
})
}
export const getInformation = async (gmlId) => {
return axios.get(`${URI}/getInformation?gmlId=${gmlId}`)
.then(response => response.data)
.catch(e => {
console.log('ERROR:', e.name)
return null
})
}
import { randomBytes, scryptSync } from 'crypto'
export const encryptString = (value, salt) => {
return scryptSync(value, salt, 32).toString('hex')
}
export const hashString = (value) => {
const salt = randomBytes(16).toString('hex')
return encryptString(value, salt) + salt
}
export const matchEncryptedString = (value, hash) => {
const storedHash = hash.slice(0, 64)
const testHash = encryptString(value, hash.slice(64))
return storedHash === testHash
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment