Add API endpoint for testing transaction

This commit is contained in:
Garrett Mills 2021-04-10 04:57:10 -05:00
parent 5ae20e945f
commit 7e15291806
Signed by: garrettmills
GPG Key ID: D2BF5FBA8298F246
6 changed files with 62 additions and 19 deletions

View File

@ -2,7 +2,7 @@ import {Inject, Injectable} from "@extollo/di"
import {Collection, Iterable} from "@extollo/util" import {Collection, Iterable} from "@extollo/util"
import {FirebaseUnit, RTDBRef} from "./units/FirebaseUnit" import {FirebaseUnit, RTDBRef} from "./units/FirebaseUnit"
import * as firebase from "firebase-admin" 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. * Base interface for an item in a Firebase RTDB collection.

View File

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

View File

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

View File

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

View File

@ -1,3 +1,6 @@
import {Route} from "@extollo/lib" import {Route} from "@extollo/lib"
Route.get('/', 'main:Home.welcome') Route.group('/api/v1', () => {
Route.post('/encounter', 'api:Blockchain.postTransaction')
.pre('api:ValidateEncounterTransaction')
})

View File

@ -1,7 +1,7 @@
import {FirebaseResource, FirebaseResourceItem} from "../FirebaseResource" import {FirebaseResource, FirebaseResourceItem} from "../FirebaseResource"
import {Injectable} from "@extollo/di" import {Injectable} from "@extollo/di"
import {RTDBRef} from "../units/FirebaseUnit" import {RTDBRef} from "../units/FirebaseUnit"
import {AsyncCollection} from "@extollo/util"; import {AsyncCollection} from "@extollo/util"
/** /**
* Interface representing a client-submitted encounter transaction. * Interface representing a client-submitted encounter transaction.