Skip to content

Commit

Permalink
call out to Octopus Imaging API from database-bridge-lambda in `cre…
Browse files Browse the repository at this point in the history
…ateItem` mutation if the `type` is `imaging-request`
  • Loading branch information
twrichards committed Aug 30, 2023
1 parent edbb5ff commit 3af427f
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 24 deletions.
2 changes: 1 addition & 1 deletion client/src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
gqlSetWebPushSubscriptionForUser,
} from "../gql";
import { Item, MyUser, User } from "shared/graphql/graphql";
import { ItemWithParsedPayload } from "./types/ItemWithParsedPayload";
import { ItemWithParsedPayload } from "shared/types/ItemWithParsedPayload";
import { HiddenIFrameForServiceWorker } from "./pushNotificationPreferences";
import { GlobalStateProvider } from "./globalState";
import { Floaty } from "./floaty";
Expand Down
6 changes: 6 additions & 0 deletions client/src/grid/octopusImagingOrderDisplay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ export const OctopusImagingOrderDisplay = ({
payload.requestType
)}
</div>
{payload.octopusImagingOrderId && (
<div>
<strong>Order ID: </strong>
{payload.octopusImagingOrderId}
</div>
)}
</div>
);
};
6 changes: 3 additions & 3 deletions client/src/push-notifications/serviceWorker.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { ItemWithParsedPayload } from "../types/ItemWithParsedPayload";
import { ItemWithParsedPayload } from "shared/types/ItemWithParsedPayload";
import {
EXPAND_PINBOARD_QUERY_PARAM,
OPEN_PINBOARD_QUERY_PARAM,
PINBOARD_ITEM_ID_QUERY_PARAM,
} from "../../../shared/constants";
import { extractNameFromEmail } from "../../../shared/util";
} from "shared/constants";
import { extractNameFromEmail } from "shared/util";

const toolsDomain = self.location.hostname.replace("pinboard.", "");

Expand Down
5 changes: 0 additions & 5 deletions client/src/types/ItemWithParsedPayload.ts

This file was deleted.

4 changes: 3 additions & 1 deletion client/src/types/PayloadAndType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ export type MamVideoPayload = {

export type ImagingOrderPayload = {
type: typeof IMAGING_REQUEST_ITEM_TYPE;
payload: PayloadWithRequestType;
payload: PayloadWithRequestType & {
octopusImagingOrderId?: string; // populated once ordered
};
};

export type PayloadAndType =
Expand Down
33 changes: 24 additions & 9 deletions database-bridge-lambda/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import prompts from "prompts";
import { DatabaseOperation } from "../shared/graphql/operations";
import { getYourEmail } from "../shared/local/yourEmail";
import { CreateItemInput, EditItemInput } from "../shared/graphql/graphql";
import { performImagingRequest } from "./src/imagingRequestCallout";
import { ItemWithParsedPayload } from "shared/types/ItemWithParsedPayload";

(async () => {
const baseInput = {
Expand Down Expand Up @@ -48,16 +50,29 @@ import { CreateItemInput, EditItemInput } from "../shared/graphql/graphql";
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 ItemWithParsedPayload,
})
: await handler(inputPayload),
null,
2
)
);
}
})();
36 changes: 36 additions & 0 deletions database-bridge-lambda/src/imagingRequestCallout.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import fetch from "node-fetch";
import { getEnvironmentVariableOrThrow } from "shared/environmentVariables";
import { ItemWithParsedPayload } from "shared/types/ItemWithParsedPayload";

export const performImagingRequest = async ({
item,
}: {
item: ItemWithParsedPayload;
}) => {
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0"; // self-signed cert on imaging server, which fails SSL check, so ignore
const response = await fetch(
`https://${getEnvironmentVariableOrThrow(
"octopusImagingApiVpcEndpoint"
)}/v1/rgbimageorder`,
{
// note this travels via vpc endpoint, via VPN to specific machine(s) on office network, no need to auth at this point
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
workflowId: item.pinboardId,
pinboardItemId: item.id,
lastUser: item.userEmail,
notes: item.message, //TODO check for 256 max (probably limit in UI too)
requestType: item.payload?.requestType, // TODO tighten this up
// gridId: TODO extract from url in payload probably
// composerId: TODO lookup somehow
// pubDate TODO scheduled launch vs some date field in workflow - what's worse wrong date or no date?
// section TODO lookup somehow
// story group name TODO (synced from InCopy most likely, if available)
}),
}
);
return (await response.json()).octopusImagingOrderId;
};
35 changes: 30 additions & 5 deletions database-bridge-lambda/src/sql/Item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ import {
EditItemInput,
Item,
PinboardIdWithClaimCounts,
} from "../../../shared/graphql/graphql";
import { Sql } from "../../../shared/database/types";
import { Range } from "../../../shared/types/grafanaType";
} from "shared/graphql/graphql";
import { Sql } from "shared/database/types";
import { Range } from "shared/types/grafanaType";
import { performImagingRequest } from "../imagingRequestCallout";
import { IMAGING_REQUEST_ITEM_TYPE } from "shared/octopusImaging";
import { ItemWithParsedPayload } from "shared/types/ItemWithParsedPayload";

const fragmentIndividualMentionsToMentionHandles = (
sql: Sql,
Expand Down Expand Up @@ -59,10 +62,32 @@ 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 ItemWithParsedPayload;
if (
insertResult.type === IMAGING_REQUEST_ITEM_TYPE &&
insertResult.payload
) {
// if this throws, the SQL transaction should be rolled back TODO test/confirm this
const octopusImagingOrderId = await performImagingRequest({
item: insertResult,
});
return sql`
UPDATE "Item"
SET "payload" = ${{
...insertResult.payload,
octopusImagingOrderId,
}}
WHERE "id" = ${insertResult.id}
RETURNING ${fragmentItemFields(sql, userEmail)}`.then(
(rows) => rows[0]
);
}
return insertResult;
});

export const editItem = async (
sql: Sql,
Expand Down
5 changes: 5 additions & 0 deletions shared/types/ItemWithParsedPayload.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Item } from "../graphql/graphql";

export type ItemWithParsedPayload = Omit<Item, "payload"> & {
payload: Record<string, unknown> | null | undefined;
};

0 comments on commit 3af427f

Please sign in to comment.