Skip to content
This repository was archived by the owner on Aug 31, 2023. It is now read-only.

Locate Unique Patron #15

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Configure asynchoronous flow
jasonycin committed Mar 12, 2023
commit e85e5f4ff52b417928864ee19b0fd97877f0939e
3 changes: 0 additions & 3 deletions server/.gitignore
Original file line number Diff line number Diff line change
@@ -31,9 +31,6 @@ node_modules
.npmrc
*.log

# Typings
typings/

# Typescript
src/**/*.js
src/**/*.js.map
2 changes: 1 addition & 1 deletion server/src/controllers/api/FlowController.ts
Original file line number Diff line number Diff line change
@@ -10,6 +10,6 @@ export class FlowController {

@Post('/test')
async upload(@BodyParams() body: object) {
await this.flow.send(body);
return await this.flow.send(body);
}
}
2 changes: 1 addition & 1 deletion server/src/controllers/api/WebhookController.ts
Original file line number Diff line number Diff line change
@@ -17,7 +17,7 @@ export class WebhookController {
@Example('Success!')
async test() {
await this.python.spawn(
"/Users/jlap/Documents/code/gdc/Project-Dispense/scripts/test.py",
"../scripts/test.py",
["test", "test2"]
);

13 changes: 8 additions & 5 deletions server/src/providers/FlowProvider.ts
Original file line number Diff line number Diff line change
@@ -42,7 +42,7 @@ export class FlowService {
* @param url
* @private
*/
private async poll(url: string): Promise<{ status: number, message: string | null}> {
private async poll(url: string): Promise<FlowResult> {
const res = await fetch(url);

switch (res.status) {
@@ -53,18 +53,21 @@ export class FlowService {
default:
return {
status: res.status,
message: res.headers.get('message')
message: res.headers.get('message'),
// If body is missing, this will throw an error. Ensure the flow returns a body, even if it is empty
body: await res.json()
}
}
}

private handleFlowResult(result: { status: number, message: string | null}) {
private handleFlowResult(result: FlowResult) {
switch (result.status) {
case 200:
this.logger.info('Flow completed with 200 status');
this.logger.info(result.body);
this.logger.info('Flow completed with status 200');
return;
default:
this.logger.error(`Flow failed with status ${result.status} and message ${result.message}`);
this.logger.error(`Flow failed with status ${result.status} and message "${result.message}"`);
throw new BadRequest(result.message || 'Unknown error while executing flow')
}
}
16 changes: 16 additions & 0 deletions server/src/typings/FlowTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
interface FlowResult {
status: number;
message: string | null;
body: UserRecord | null;
}

/**
* The body of the response from the flow endpoint
*/
type UserRecord = {
email: string;
firstName: string;
lastName: string;
availablePoints: number;
userType: string;
}