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 31, 2023
1 parent edbb5ff commit cd9d7e7
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 14 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: 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.

43 changes: 43 additions & 0 deletions database-bridge-lambda/src/imagingRequestCallout.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import fetch from "node-fetch";
import { getEnvironmentVariableOrThrow } from "shared/environmentVariables";
import { ItemWithParsedPayload } from "shared/types/ItemWithParsedPayload";

export const performImagingRequest = async (item: ItemWithParsedPayload) => {
const gridId = (item.payload?.embeddableUrl as string)?.split("/").pop();
if (!gridId) {
throw new Error(`Couldn't extract grid ID from payload: ${item.payload}`);
}
const imagingRequestBody = {
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,
// 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)
};
console.log("Performing imaging request", imagingRequestBody);

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(imagingRequestBody),
}
);
if (!response.ok) {
throw new Error(
`Imaging request failed: ${response.status} ${response.statusText}`
);
}
};
23 changes: 18 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,20 @@ 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
await performImagingRequest(insertResult);
}
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 cd9d7e7

Please sign in to comment.