|
| 1 | +import { S3 } from "@aws-sdk/client-s3"; |
| 2 | +import { generateAppSyncConfig } from "./generateAppSyncConfig"; |
| 3 | +import { generatePreSignedGridUploadUrl } from "shared/grid"; |
| 4 | +import { CreateItemInput } from "shared/graphql/graphql"; |
| 5 | +import { appSyncCreateItem } from "./appSyncRequest"; |
| 6 | +import { |
| 7 | + IMAGING_COMPLETED_ITEM_TYPE, |
| 8 | + IMAGING_PICKED_UP_ITEM_TYPE, |
| 9 | +} from "shared/octopusImaging"; |
| 10 | + |
| 11 | +interface CommonArgs { |
| 12 | + /* the email of the user who did the imaging work */ |
| 13 | + userEmail: string; //TODO probably receive the desktop auth token instead (to verify on pinboard side) |
| 14 | + /* the id of the pinboard itself */ |
| 15 | + workflowId: string; |
| 16 | + /* the itemId of the original request item in pinboard */ |
| 17 | + pinboardItemId: string; |
| 18 | +} |
| 19 | + |
| 20 | +type ImagingOrderPickedUp = CommonArgs; |
| 21 | +interface GeneratePreSignedGridUploadUrl extends CommonArgs { |
| 22 | + originalGridId: string; |
| 23 | + filename: string; |
| 24 | + /* SHA1 hash of the file content */ |
| 25 | + newGridId: string; |
| 26 | + /* e.g. cutout, composite, etc */ |
| 27 | + requestType: string; |
| 28 | +} |
| 29 | +interface ImagingOrderCompleted extends CommonArgs { |
| 30 | + /* SHA1 hash of the file content */ |
| 31 | + newGridId: string; |
| 32 | +} |
| 33 | + |
| 34 | +export type ImagingCallFromOctopus = |
| 35 | + | ImagingOrderPickedUp |
| 36 | + | GeneratePreSignedGridUploadUrl |
| 37 | + | ImagingOrderCompleted; |
| 38 | + |
| 39 | +export const isImagingCallFromOctopus = ( |
| 40 | + detail: any |
| 41 | +): detail is ImagingCallFromOctopus => !!detail && "pinboardItemId" in detail; |
| 42 | + |
| 43 | +export const handleImagingCallFromOctopus = async ( |
| 44 | + s3: S3, |
| 45 | + detail: ImagingCallFromOctopus |
| 46 | +): Promise<string | void> => { |
| 47 | + console.log("Handling imaging call from Octopus", detail); |
| 48 | + if ("originalGridId" in detail) { |
| 49 | + return await generatePreSignedGridUploadUrl(detail); |
| 50 | + } |
| 51 | + const appSyncConfig = await generateAppSyncConfig(detail.userEmail, s3); |
| 52 | + const appSyncCreateItemInput: CreateItemInput = { |
| 53 | + pinboardId: detail.workflowId, |
| 54 | + relatedItemId: detail.pinboardItemId, |
| 55 | + claimable: false, |
| 56 | + mentions: null, //TODO consider @ing the original requester for these updates |
| 57 | + message: null, |
| 58 | + groupMentions: null, |
| 59 | + ...("newGridId" in detail |
| 60 | + ? { |
| 61 | + type: IMAGING_COMPLETED_ITEM_TYPE, |
| 62 | + payload: null, //TODO make use of the newGridId (perform lookup to grid) |
| 63 | + } |
| 64 | + : { |
| 65 | + type: IMAGING_PICKED_UP_ITEM_TYPE, |
| 66 | + payload: null, |
| 67 | + }), |
| 68 | + }; |
| 69 | + return appSyncCreateItem(appSyncConfig, appSyncCreateItemInput).then( |
| 70 | + async (response) => { |
| 71 | + console.log(await response.text()); |
| 72 | + if (!response.ok) { |
| 73 | + throw new Error( |
| 74 | + `Failed to create item: ${response.status} ${response.statusText}` |
| 75 | + ); |
| 76 | + } |
| 77 | + } |
| 78 | + ); |
| 79 | +}; |
0 commit comments