Skip to content

Commit

Permalink
Merge branch 'development' into 90-implement-logging-in-services
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Ebsen committed Sep 4, 2023
2 parents 1d5e61f + eea9be3 commit 99e6ba3
Show file tree
Hide file tree
Showing 37 changed files with 896 additions and 809 deletions.
12 changes: 11 additions & 1 deletion Server/src/models/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,20 @@ export type FullTrack = BareTrack & {
/** @see {isTrackList} ts-auto-guard:type-guard */
export type TrackList = BareTrack[]

/** @see {isCreatePOITypeIcon} ts-auto-guard:type-guard */
export enum POITypeIcon {
Generic = 0,
LevelCrossing = 1,
LesserLevelCrossing = 2,
Picnic = 3,
TrackEnd = 4,
TurningPoint = 5,
}

/** @see {isCreatePOIType} ts-auto-guard:type-guard */
export type CreatePOIType = {
name: string
icon: string
icon: POITypeIcon
description?: string
}

Expand Down
19 changes: 15 additions & 4 deletions Server/src/routes/init.route.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Request, Response, Router } from "express"
import { jsonParser } from "."
import { InitRequestApp, InitResponseApp, TrackListEntryApp } from "../models/api.app"
import { PointOfInterest, Position } from "../models/api"
import { PointOfInterest, POITypeIcon, Position } from "../models/api"
import { logger } from "../utils/logger"
import TrackService from "../services/track.service"
import { POI, Track } from "@prisma/client"
import { POI, POIType, Track } from "@prisma/client"
import POIService from "../services/poi.service"
import VehicleService from "../services/vehicle.service"
import { Feature, FeatureCollection, GeoJsonProperties, LineString, Point } from "geojson"
Expand Down Expand Up @@ -196,11 +196,22 @@ export class InitRoute {
private async getAppPoisFromDbPoi(pois: POI[]): Promise<PointOfInterest[]> {
const apiPois: PointOfInterest[] = []
for (const poi of pois) {
const type: number = poi.typeId
const type: POIType | null = await POIService.getPOITypeById(poi.typeId)
if (!type) {
logger.error(`Could not determine type of poi with id ${poi.uid}`)
continue
}
const poiIcon: number = Number.parseInt(type.icon)
if (!Number.isInteger(poiIcon)) {
logger.error(`Icon of type with id ${type.uid} is not an integer.`)
continue
}
// Check if the icon number is a member of the enum.
if (!(poiIcon in POITypeIcon)) {
logger.warn(`Icon of type with id ${type.uid} is ${poiIcon}, not one of the known icons.`)
}
// ensure that the app always gets an enum member.
const appType: POITypeIcon = poiIcon in POITypeIcon ? poiIcon : POITypeIcon.Generic

const geoJsonPos: Feature<Point, GeoJsonProperties> | null = GeoJSONUtils.parseGeoJSONFeaturePoint(poi.position)
if (!geoJsonPos) {
Expand All @@ -220,7 +231,7 @@ export class InitRoute {
apiPois.push({
id: poi.uid,
name: poi.name,
typeId: 0 <= type && type <= 4 ? type : 0,
typeId: appType,
pos: pos,
percentagePosition: percentagePosition,
isTurningPoint: poi.isTurningPoint,
Expand Down
8 changes: 4 additions & 4 deletions Server/src/routes/poi.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export class PoiRoute {
properties: null
}

const type: POIType | null = await POIService.getPOITypeById(userData.typeId)
const type: POIType | null = await database.pois.getTypeById(userData.typeId)
if (!type) {
logger.error(`Could not find poi type with id ${userData.typeId}`)
res.sendStatus(400)
Expand Down Expand Up @@ -179,7 +179,7 @@ export class PoiRoute {
return
}

const poiToUpdate: POI | null = await POIService.getPOIById(poiId)
const poiToUpdate: POI | null = await database.pois.getById(poiId)
if (!poiToUpdate) {
logger.error(`Could not find poi with id ${userData.id}`)
res.sendStatus(404)
Expand Down Expand Up @@ -233,13 +233,13 @@ export class PoiRoute {
}

// Look out for the POI
const poi: POI | null = await POIService.getPOIById(poiId)
const poi: POI | null = await database.pois.getById(poiId)
if (!poi) {
logger.error(`Could not find poi with id ${poiId}`)
res.sendStatus(500)
return
}
const success: boolean = await POIService.removePOI(poi)
const success: boolean = await database.pois.remove(poi.uid)
if (!success) {
logger.error(`Could not delete poi with id ${poiId}`)
res.sendStatus(500)
Expand Down
12 changes: 7 additions & 5 deletions Server/src/routes/poitype.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class PoiTypeRoute {
const type: APIPoiType = {
id: uid,
name,
icon,
icon: Number.parseInt(icon),
description: description ?? undefined
}
return type
Expand Down Expand Up @@ -74,17 +74,18 @@ export class PoiTypeRoute {
id: poiType.uid,
name: poiType.name,
description: poiType.description ?? undefined,
icon: poiType.icon
icon: Number.parseInt(poiType.icon)
}

res.json(apiPoiType)
return
}

private async createType(req: Request, res: Response): Promise<void> {
// TODO: ensure that the icon is a member of the enum (or check if the type guard checks that)
const { name, icon, description }: CreatePOIType = req.body

const poiType: POIType | null = await database.pois.saveType(name, icon, description)
const poiType: POIType | null = await database.pois.saveType(name, icon.toString(), description)
if (!poiType) {
logger.error("Could not create poi type")
res.sendStatus(500)
Expand All @@ -94,7 +95,7 @@ export class PoiTypeRoute {
const responseType: APIPoiType = {
id: poiType.uid,
name: poiType.name,
icon: poiType.icon,
icon: Number.parseInt(poiType.icon),
description: poiType.description ?? undefined
}
res.status(201).json(responseType)
Expand All @@ -104,6 +105,7 @@ export class PoiTypeRoute {
private async updateType(req: Request, res: Response): Promise<void> {
const typeId: number = parseInt(req.params.typeId)
const userData: APIPoiType = req.body
// TODO: ensure that the icon is a member of the enum (or check if the type guard checks that)
if (userData.id !== typeId) {
res.sendStatus(400)
return
Expand All @@ -116,7 +118,7 @@ export class PoiTypeRoute {
return
}

type = await database.pois.updateType(typeId, userData.name, userData.icon, userData.description)
type = await database.pois.updateType(typeId, userData.name, userData.icon.toString(), userData.description)
if (!type) {
logger.error(`Could not update poi type with id ${userData.id}`)
res.sendStatus(500)
Expand Down
11 changes: 5 additions & 6 deletions Server/src/routes/track.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import TrackService from "../services/track.service"
import { POI, Track, Vehicle } from "@prisma/client"
import please_dont_crash from "../utils/please_dont_crash"
import { logger } from "../utils/logger"
import { UpdateTrack, BareTrack, FullTrack, PointOfInterest, Position, Vehicle as APIVehicle } from "../models/api"
import { BareTrack, FullTrack, PointOfInterest, Position, UpdateTrack, Vehicle as APIVehicle } from "../models/api"
import VehicleService from "../services/vehicle.service"
import { Feature, GeoJsonProperties, LineString, Point } from "geojson"
import POIService from "../services/poi.service"
import GeoJSONUtils from "../utils/geojsonUtils"
import database from "../services/database.service"
import TrackerService from "../services/tracker.service"

/**
* The router class for the routing of the track uploads from the website.
Expand Down Expand Up @@ -221,9 +220,9 @@ export class TrackRoute {
// If we know that, convert it in the API format.
const pos: Position | undefined = geo_pos
? {
lat: GeoJSONUtils.getLatitude(geo_pos),
lng: GeoJSONUtils.getLongitude(geo_pos)
}
lat: GeoJSONUtils.getLatitude(geo_pos),
lng: GeoJSONUtils.getLongitude(geo_pos)
}
: undefined
// Also acquire the percentage position. It might happen that a percentage position is known, while the position is not.
// This might not make much sense.
Expand All @@ -234,7 +233,7 @@ export class TrackRoute {
track: vehicle.trackId,
name: vehicle.name ? vehicle.name : "Empty Name",
type: vehicle.typeId,
trackerIds: (await TrackerService.getTrackerByVehicle(vehicle.uid)).map(y => y.uid),
trackerIds: (await database.trackers.getByVehicleId(vehicle.uid)).map(y => y.uid),
pos,
percentagePosition,
heading
Expand Down
5 changes: 2 additions & 3 deletions Server/src/routes/tracker.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import TrackerService from "../services/tracker.service"
import { UplinkTracker } from "../models/api.tracker"
import please_dont_crash from "../utils/please_dont_crash"
import { Tracker, Vehicle } from "@prisma/client"
import VehicleService from "../services/vehicle.service"
import database from "../services/database.service"
import { Tracker as APITracker } from "../models/api"

Expand Down Expand Up @@ -152,7 +151,7 @@ export class TrackerRoute {
}
const trackerId = trackerData.end_device_ids.device_id
// load the tracker from the database
const tracker: Tracker | null = await TrackerService.getTrackerById(trackerId)
const tracker: Tracker | null = await database.trackers.getById(trackerId)
if (!tracker) {
logger.silly(`Tried to append log on unknown tracker with id ${trackerId}`)
res.sendStatus(401)
Expand All @@ -165,7 +164,7 @@ export class TrackerRoute {
}
// and get the vehicle the tracker is attached to. If it has no associated vehicle, do nothing.
const associatedVehicle: Vehicle | null = tracker.vehicleId
? await VehicleService.getVehicleById(tracker.vehicleId)
? await database.vehicles.getById(tracker.vehicleId)
: null
if (!associatedVehicle) {
logger.silly(`Got position from tracker ${trackerId} with no associated vehicle.`)
Expand Down
6 changes: 3 additions & 3 deletions Server/src/routes/vehicle.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ export class VehicleRoute {

/**
* Get a list of vehicles with all the required properties for CRUD operations
* @param req A request containing a track id in the parameters
* @param _req A request containing a track id in the parameters
* @param res A list of `VehicleListItemWebsite`.
* @returns Nothing
*/
private async getAllVehicles(req: Request, res: Response): Promise<void> {
private async getAllVehicles(_req: Request, res: Response): Promise<void> {
const vehicles = await database.vehicles.getAll()
const apiVehicles: APIVehicle[] = await Promise.all(
vehicles.map(async vehicle => {
Expand Down Expand Up @@ -320,7 +320,7 @@ export class VehicleRoute {
return
}

const userVehicle: Vehicle | null = await VehicleService.getVehicleById(userData.vehicleId)
const userVehicle: Vehicle | null = await database.vehicles.getById(userData.vehicleId)
if (!userVehicle) {
logger.error(`Could not find vehicle with id ${userData.vehicleId}`)
res.sendStatus(404)
Expand Down
10 changes: 9 additions & 1 deletion Server/src/services/crypto.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import * as argon from "argon2"

export default class CryptoService {

/**
* Wrapper method for verifying a plain password against hashed password using argon2.
*
* @param hashedPassword The argon2-hashed password from the database to verify against.
* @param plainPassword The plain password the user entered.
*/
public static async verify(hashedPassword: string, plainPassword: string): Promise<boolean> {
let isCorrectPassword: boolean
try {
Expand All @@ -10,10 +17,11 @@ export default class CryptoService {
}
return isCorrectPassword
}

/**
* Produces a hash using the argon hashing.
* @param input The password, that needs to be hashed
* @returns Undefined, if the hashing is unsuccessful, a hash of the password otherwise.
* @returns undefined, if the hashing is unsuccessful, a hash of the password otherwise.
*/
public static async produceHash(input: string): Promise<string | undefined> {
try {
Expand Down
1 change: 1 addition & 0 deletions Server/src/services/db/poi.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export default class POIController {
},
data: {
name: name,
icon: icon,
description: description
}
})
Expand Down
Loading

0 comments on commit 99e6ba3

Please sign in to comment.