Skip to content

Commit cd9d7e7

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 cd9d7e7

File tree

6 files changed

+70
-14
lines changed

6 files changed

+70
-14
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/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.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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 (item: ItemWithParsedPayload) => {
6+
const gridId = (item.payload?.embeddableUrl as string)?.split("/").pop();
7+
if (!gridId) {
8+
throw new Error(`Couldn't extract grid ID from payload: ${item.payload}`);
9+
}
10+
const imagingRequestBody = {
11+
workflowId: item.pinboardId,
12+
pinboardItemId: item.id,
13+
lastUser: item.userEmail,
14+
notes: item.message, //TODO check for 256 max (probably limit in UI too)
15+
requestType: item.payload?.requestType, // TODO tighten this up
16+
gridId,
17+
// composerId: TODO lookup somehow
18+
// pubDate TODO scheduled launch vs some date field in workflow - what's worse wrong date or no date?
19+
// section TODO lookup somehow
20+
// story group name TODO (synced from InCopy most likely, if available)
21+
};
22+
console.log("Performing imaging request", imagingRequestBody);
23+
24+
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0"; // self-signed cert on imaging server, which fails SSL check, so ignore
25+
const response = await fetch(
26+
`https://${getEnvironmentVariableOrThrow(
27+
"octopusImagingApiVpcEndpoint"
28+
)}/v1/rgbimageorder`,
29+
{
30+
// note this travels via vpc endpoint, via VPN to specific machine(s) on office network, no need to auth at this point
31+
method: "POST",
32+
headers: {
33+
"Content-Type": "application/json",
34+
},
35+
body: JSON.stringify(imagingRequestBody),
36+
}
37+
);
38+
if (!response.ok) {
39+
throw new Error(
40+
`Imaging request failed: ${response.status} ${response.statusText}`
41+
);
42+
}
43+
};

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

Lines changed: 18 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,20 @@ 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
75+
await performImagingRequest(insertResult);
76+
}
77+
return insertResult;
78+
});
6679

6780
export const editItem = async (
6881
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)