Skip to content

Commit 3af427f

Browse files
committed
call out to Octopus Imaging API from database-bridge-lambda in createItem mutation if the type is imaging-request
1 parent edbb5ff commit 3af427f

File tree

9 files changed

+108
-24
lines changed

9 files changed

+108
-24
lines changed

client/src/app.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {
2222
gqlSetWebPushSubscriptionForUser,
2323
} from "../gql";
2424
import { Item, MyUser, User } from "shared/graphql/graphql";
25-
import { ItemWithParsedPayload } from "./types/ItemWithParsedPayload";
25+
import { ItemWithParsedPayload } from "shared/types/ItemWithParsedPayload";
2626
import { HiddenIFrameForServiceWorker } from "./pushNotificationPreferences";
2727
import { GlobalStateProvider } from "./globalState";
2828
import { Floaty } from "./floaty";

client/src/grid/octopusImagingOrderDisplay.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ export const OctopusImagingOrderDisplay = ({
7878
payload.requestType
7979
)}
8080
</div>
81+
{payload.octopusImagingOrderId && (
82+
<div>
83+
<strong>Order ID: </strong>
84+
{payload.octopusImagingOrderId}
85+
</div>
86+
)}
8187
</div>
8288
);
8389
};

client/src/push-notifications/serviceWorker.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { ItemWithParsedPayload } from "../types/ItemWithParsedPayload";
1+
import { ItemWithParsedPayload } from "shared/types/ItemWithParsedPayload";
22
import {
33
EXPAND_PINBOARD_QUERY_PARAM,
44
OPEN_PINBOARD_QUERY_PARAM,
55
PINBOARD_ITEM_ID_QUERY_PARAM,
6-
} from "../../../shared/constants";
7-
import { extractNameFromEmail } from "../../../shared/util";
6+
} from "shared/constants";
7+
import { extractNameFromEmail } from "shared/util";
88

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

client/src/types/ItemWithParsedPayload.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.

client/src/types/PayloadAndType.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ export type MamVideoPayload = {
7878

7979
export type ImagingOrderPayload = {
8080
type: typeof IMAGING_REQUEST_ITEM_TYPE;
81-
payload: PayloadWithRequestType;
81+
payload: PayloadWithRequestType & {
82+
octopusImagingOrderId?: string; // populated once ordered
83+
};
8284
};
8385

8486
export type PayloadAndType =

database-bridge-lambda/run.ts

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import prompts from "prompts";
55
import { DatabaseOperation } from "../shared/graphql/operations";
66
import { getYourEmail } from "../shared/local/yourEmail";
77
import { CreateItemInput, EditItemInput } from "../shared/graphql/graphql";
8+
import { performImagingRequest } from "./src/imagingRequestCallout";
9+
import { ItemWithParsedPayload } from "shared/types/ItemWithParsedPayload";
810

911
(async () => {
1012
const baseInput = {
@@ -48,16 +50,29 @@ import { CreateItemInput, EditItemInput } from "../shared/graphql/graphql";
4850
type: "select",
4951
name: "inputPayload",
5052
message: "Operation?",
51-
choices: Object.entries(sampleInputs).map(([operation, sampleInput]) => ({
52-
title: operation,
53-
value: {
54-
...baseInput,
55-
arguments: sampleInput,
56-
info: { fieldName: operation },
57-
} as AppSyncResolverEvent<unknown, unknown>,
58-
})),
53+
choices: [
54+
{ title: "SAMPLE IMAGING ORDER", value: "imaging-request" },
55+
...Object.entries(sampleInputs).map(([operation, sampleInput]) => ({
56+
title: operation,
57+
value: {
58+
...baseInput,
59+
arguments: sampleInput,
60+
info: { fieldName: operation },
61+
} as AppSyncResolverEvent<unknown, unknown>,
62+
})),
63+
],
5964
});
6065

61-
console.log(JSON.stringify(await handler(inputPayload), null, 2));
66+
console.log(
67+
JSON.stringify(
68+
inputPayload === "imaging-request"
69+
? await performImagingRequest({
70+
item: {} as ItemWithParsedPayload,
71+
})
72+
: await handler(inputPayload),
73+
null,
74+
2
75+
)
76+
);
6277
}
6378
})();
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import fetch from "node-fetch";
2+
import { getEnvironmentVariableOrThrow } from "shared/environmentVariables";
3+
import { ItemWithParsedPayload } from "shared/types/ItemWithParsedPayload";
4+
5+
export const performImagingRequest = async ({
6+
item,
7+
}: {
8+
item: ItemWithParsedPayload;
9+
}) => {
10+
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0"; // self-signed cert on imaging server, which fails SSL check, so ignore
11+
const response = await fetch(
12+
`https://${getEnvironmentVariableOrThrow(
13+
"octopusImagingApiVpcEndpoint"
14+
)}/v1/rgbimageorder`,
15+
{
16+
// note this travels via vpc endpoint, via VPN to specific machine(s) on office network, no need to auth at this point
17+
method: "POST",
18+
headers: {
19+
"Content-Type": "application/json",
20+
},
21+
body: JSON.stringify({
22+
workflowId: item.pinboardId,
23+
pinboardItemId: item.id,
24+
lastUser: item.userEmail,
25+
notes: item.message, //TODO check for 256 max (probably limit in UI too)
26+
requestType: item.payload?.requestType, // TODO tighten this up
27+
// gridId: TODO extract from url in payload probably
28+
// composerId: TODO lookup somehow
29+
// pubDate TODO scheduled launch vs some date field in workflow - what's worse wrong date or no date?
30+
// section TODO lookup somehow
31+
// story group name TODO (synced from InCopy most likely, if available)
32+
}),
33+
}
34+
);
35+
return (await response.json()).octopusImagingOrderId;
36+
};

database-bridge-lambda/src/sql/Item.ts

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@ import {
33
EditItemInput,
44
Item,
55
PinboardIdWithClaimCounts,
6-
} from "../../../shared/graphql/graphql";
7-
import { Sql } from "../../../shared/database/types";
8-
import { Range } from "../../../shared/types/grafanaType";
6+
} from "shared/graphql/graphql";
7+
import { Sql } from "shared/database/types";
8+
import { Range } from "shared/types/grafanaType";
9+
import { performImagingRequest } from "../imagingRequestCallout";
10+
import { IMAGING_REQUEST_ITEM_TYPE } from "shared/octopusImaging";
11+
import { ItemWithParsedPayload } from "shared/types/ItemWithParsedPayload";
912

1013
const fragmentIndividualMentionsToMentionHandles = (
1114
sql: Sql,
@@ -59,10 +62,32 @@ export const createItem = async (
5962
args: { input: CreateItemInput },
6063
userEmail: string
6164
) =>
62-
sql`
65+
sql.begin(async (sql) => {
66+
const insertResult = (await sql`
6367
INSERT INTO "Item" ${sql({ userEmail, ...args.input })}
6468
RETURNING ${fragmentItemFields(sql, userEmail)}
65-
`.then((rows) => rows[0]);
69+
`.then((rows) => rows[0])) as ItemWithParsedPayload;
70+
if (
71+
insertResult.type === IMAGING_REQUEST_ITEM_TYPE &&
72+
insertResult.payload
73+
) {
74+
// if this throws, the SQL transaction should be rolled back TODO test/confirm this
75+
const octopusImagingOrderId = await performImagingRequest({
76+
item: insertResult,
77+
});
78+
return sql`
79+
UPDATE "Item"
80+
SET "payload" = ${{
81+
...insertResult.payload,
82+
octopusImagingOrderId,
83+
}}
84+
WHERE "id" = ${insertResult.id}
85+
RETURNING ${fragmentItemFields(sql, userEmail)}`.then(
86+
(rows) => rows[0]
87+
);
88+
}
89+
return insertResult;
90+
});
6691

6792
export const editItem = async (
6893
sql: Sql,
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { Item } from "../graphql/graphql";
2+
3+
export type ItemWithParsedPayload = Omit<Item, "payload"> & {
4+
payload: Record<string, unknown> | null | undefined;
5+
};

0 commit comments

Comments
 (0)