Add API endpoint for testing transaction

working-state
Garrett Mills 3 years ago
parent 5ae20e945f
commit 7e15291806
Signed by: garrettmills
GPG Key ID: D2BF5FBA8298F246

@ -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.

@ -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 (<TransactionResource> this.make(TransactionResource)).push(item)
return one(item)
}
}

@ -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'),
})
}
}

@ -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')
}
}
}
}

@ -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')
})

@ -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.

Loading…
Cancel
Save