Skip to content

Commit

Permalink
WIP - call out to Octopus Imaging API from database-bridge-lambda i…
Browse files Browse the repository at this point in the history
…n `createItem` mutation if the `type` is `imaging-request`
  • Loading branch information
twrichards committed Dec 19, 2022
1 parent ea59677 commit f701d2f
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 13 deletions.
6 changes: 4 additions & 2 deletions database-bridge-lambda/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
"devDependencies": {
"@types/aws-lambda": "^8.10.102",
"aws-sdk": "^2.840.0",
"ts-node-dev": "^1.0.0"
"ts-node-dev": "^1.0.0",
"@types/node-fetch": "2"
},
"dependencies": {
"postgres": "^3.2.4"
"postgres": "^3.2.4",
"node-fetch": "2"
}
}
33 changes: 24 additions & 9 deletions database-bridge-lambda/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { createDatabaseTunnel } from "../shared/database/local/databaseTunnel";
import prompts from "prompts";
import { DatabaseOperation } from "../shared/graphql/operations";
import { getYourEmail } from "../shared/local/yourEmail";
import { performImagingRequest } from "./src/imagingRequestCallout";

(async () => {
const baseInput = {
Expand All @@ -29,16 +30,30 @@ import { getYourEmail } from "../shared/local/yourEmail";
type: "select",
name: "inputPayload",
message: "Operation?",
choices: Object.entries(sampleInputs).map(([operation, sampleInput]) => ({
title: operation,
value: {
...baseInput,
arguments: sampleInput,
info: { fieldName: operation },
} as AppSyncResolverEvent<unknown, unknown>,
})),
choices: [
{ title: "SAMPLE IMAGING ORDER", value: "imaging-request" },
...Object.entries(sampleInputs).map(([operation, sampleInput]) => ({
title: operation,
value: {
...baseInput,
arguments: sampleInput,
info: { fieldName: operation },
} as AppSyncResolverEvent<unknown, unknown>,
})),
],
});

console.log(JSON.stringify(await handler(inputPayload), null, 2));
console.log(
JSON.stringify(
inputPayload === "imaging-request"
? await performImagingRequest({
item: {} as any,
existingPayload: {},
})
: await handler(inputPayload),
null,
2
)
);
}
})();
27 changes: 27 additions & 0 deletions database-bridge-lambda/src/imagingRequestCallout.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import fetch from "node-fetch";
import { Item } from "../../shared/graphql/graphql";
import { pinboardConfigPromiseGetter } from "../../shared/awsIntegration";

export const performImagingRequest = async ({
//TODO don't pass the whole item, just the fields we need
item,
existingPayload,
}: {
item: Item;
existingPayload: any;
}) => {
console.log("performImagingRequest", item, existingPayload);
const response = await fetch(
await pinboardConfigPromiseGetter("octopus/imaging/orderURL"),
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ bob: "hope", frank: " sinatra" }),
//TODO auth??
}
);
const responseJSON = (await response.json()) as { bob: string };
return responseJSON.bob;
};
30 changes: 28 additions & 2 deletions database-bridge-lambda/src/sql/Item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {
PinboardIdWithClaimCounts,
} from "../../../shared/graphql/graphql";
import { Sql } from "../../../shared/database/types";
import { performImagingRequest } from "../imagingRequestCallout";
import { IMAGING_REQUEST_ITEM_TYPE } from "../../../shared/octopusImaging";

const fragmentIndividualMentionsToMentionHandles = (
sql: Sql,
Expand Down Expand Up @@ -57,10 +59,34 @@ export const createItem = async (
args: { input: CreateItemInput },
userEmail: string
) =>
sql`
sql.begin(async (sql) => {
const insertResult = (await sql`
INSERT INTO "Item" ${sql({ userEmail, ...args.input })}
RETURNING ${fragmentItemFields(sql, userEmail)}
`.then((rows) => rows[0]);
`.then((rows) => rows[0])) as Item;
if (
insertResult.type === IMAGING_REQUEST_ITEM_TYPE &&
insertResult.payload
) {
const existingPayload = JSON.parse(insertResult.payload);
// if this throws, the SQL transaction should be rolled back
const octopusImagingOrderId = await performImagingRequest({
item: insertResult,
existingPayload,
});
return sql`
UPDATE "Item"
SET "payload" = ${{
...existingPayload,
octopusImagingOrderId,
}}}
WHERE "id" = ${insertResult.id}
RETURNING ${fragmentItemFields(sql, userEmail)}`.then(
(rows) => rows[0]
);
}
return insertResult;
});

export const listItems = (
sql: Sql,
Expand Down
1 change: 1 addition & 0 deletions shared/octopusImaging.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const IMAGING_REQUEST_ITEM_TYPE = "imaging-request";

0 comments on commit f701d2f

Please sign in to comment.