From 7e1529180617f172520d25d255d285ea4eeac5fe Mon Sep 17 00:00:00 2001 From: garrettmills Date: Sat, 10 Apr 2021 04:57:10 -0500 Subject: [PATCH] Add API endpoint for testing transaction --- src/app/FirebaseResource.ts | 2 +- .../controllers/api/Blockchain.controller.ts | 29 +++++++++++++++++++ .../http/controllers/main/Home.controller.ts | 16 ---------- ...ValidateEncounterTransaction.middleware.ts | 27 +++++++++++++++++ src/app/http/routes/app.routes.ts | 5 +++- src/app/rtdb/TransactionResource.ts | 2 +- 6 files changed, 62 insertions(+), 19 deletions(-) create mode 100644 src/app/http/controllers/api/Blockchain.controller.ts delete mode 100644 src/app/http/controllers/main/Home.controller.ts create mode 100644 src/app/http/middlewares/api/ValidateEncounterTransaction.middleware.ts diff --git a/src/app/FirebaseResource.ts b/src/app/FirebaseResource.ts index 688067e..1755c95 100644 --- a/src/app/FirebaseResource.ts +++ b/src/app/FirebaseResource.ts @@ -2,7 +2,7 @@ import {Inject, Injectable} from "@extollo/di" import {Collection, Iterable} from "@extollo/util" import {FirebaseUnit, RTDBRef} from "./units/FirebaseUnit" import * as firebase from "firebase-admin" -import {Application} from "@extollo/lib"; +import {Application} from "@extollo/lib" /** * Base interface for an item in a Firebase RTDB collection. diff --git a/src/app/http/controllers/api/Blockchain.controller.ts b/src/app/http/controllers/api/Blockchain.controller.ts new file mode 100644 index 0000000..f0dd1a0 --- /dev/null +++ b/src/app/http/controllers/api/Blockchain.controller.ts @@ -0,0 +1,29 @@ +import {Controller} from "@extollo/lib" +import {Inject, Injectable} from "@extollo/di" +import {TransactionResource, TransactionResourceItem} from "../../../rtdb/TransactionResource" +import {one} from "@extollo/util" + +/** + * Blockchain Controller + * ------------------------------------ + * Route handlers for API endpoints. + */ +@Injectable() +export class Blockchain extends Controller { + + public async postTransaction() { + const item: TransactionResourceItem = { + firebaseID: '', + seqID: -1, + combinedHash: String(this.request.input('combinedHash')), + timestamp: parseInt(String(this.request.input('timestamp'))), + encodedGPSLocation: String(this.request.input('encodedGPSLocation')), + partnerPublicKey: String(this.request.input('partnerPublicKey')), + validationSignature: String(this.request.input('validationSignature')), + } + + await ( this.make(TransactionResource)).push(item) + return one(item) + } + +} diff --git a/src/app/http/controllers/main/Home.controller.ts b/src/app/http/controllers/main/Home.controller.ts deleted file mode 100644 index 9f562a9..0000000 --- a/src/app/http/controllers/main/Home.controller.ts +++ /dev/null @@ -1,16 +0,0 @@ -import {Controller, view, Session} from '@extollo/lib'; -import {Inject, Injectable} from "@extollo/di"; - -@Injectable() -export class Home extends Controller { - @Inject() - protected readonly session!: Session; - - public welcome() { - this.session.set('app_visits', this.session.get('app_visits', 0) + 1) - - return view('welcome', { - app_visits: this.session.get('app_visits'), - }) - } -} diff --git a/src/app/http/middlewares/api/ValidateEncounterTransaction.middleware.ts b/src/app/http/middlewares/api/ValidateEncounterTransaction.middleware.ts new file mode 100644 index 0000000..1433f1d --- /dev/null +++ b/src/app/http/middlewares/api/ValidateEncounterTransaction.middleware.ts @@ -0,0 +1,27 @@ +import {error, Middleware} from "@extollo/lib" +import {Injectable} from "@extollo/di" +import {HTTPStatus} from "@extollo/util"; + +/** + * ValidateEncounterTransaction Middleware + * -------------------------------------------- + * Put some description here. + */ +@Injectable() +export class ValidateEncounterTransaction extends Middleware { + public async apply() { + const required: string[] = [ + 'combinedHash', + 'encodedGPSLocation', + 'partnerPublicKey', + 'validationSignature', + 'timestamp', + ] + + for ( const field of required ) { + if ( !this.request.input(field) ) { + return error(`Missing required field: ${field}`, HTTPStatus.BAD_REQUEST, 'json') + } + } + } +} diff --git a/src/app/http/routes/app.routes.ts b/src/app/http/routes/app.routes.ts index 6e39b3c..76f96b0 100644 --- a/src/app/http/routes/app.routes.ts +++ b/src/app/http/routes/app.routes.ts @@ -1,3 +1,6 @@ import {Route} from "@extollo/lib" -Route.get('/', 'main:Home.welcome') +Route.group('/api/v1', () => { + Route.post('/encounter', 'api:Blockchain.postTransaction') + .pre('api:ValidateEncounterTransaction') +}) diff --git a/src/app/rtdb/TransactionResource.ts b/src/app/rtdb/TransactionResource.ts index 9420a04..c4609b6 100644 --- a/src/app/rtdb/TransactionResource.ts +++ b/src/app/rtdb/TransactionResource.ts @@ -1,7 +1,7 @@ import {FirebaseResource, FirebaseResourceItem} from "../FirebaseResource" import {Injectable} from "@extollo/di" import {RTDBRef} from "../units/FirebaseUnit" -import {AsyncCollection} from "@extollo/util"; +import {AsyncCollection} from "@extollo/util" /** * Interface representing a client-submitted encounter transaction.