diff --git a/api.planx.uk/modules/send/s3/index.ts b/api.planx.uk/modules/send/s3/index.ts index 2a414d4e94..eed4ea0b0a 100644 --- a/api.planx.uk/modules/send/s3/index.ts +++ b/api.planx.uk/modules/send/s3/index.ts @@ -73,62 +73,58 @@ const sendToS3: SendIntegrationController = async (_req, res, next) => { payload: skipValidation ? "Discretionary" : "Validated ODP Schema", }, }; - const webhookResponse = await axios(webhookRequest) - .then(async (res) => { - // Mark session as submitted so that reminder and expiry emails are not triggered - markSessionAsSubmitted(sessionId); - // Create an audit entry - const applicationId = await $api.client.request( - gql` - mutation CreateS3Application( - $session_id: String! - $team_slug: String! - $webhook_request: jsonb! - $webhook_response: jsonb = {} - ) { - insertS3Application: insert_s3_applications_one( - object: { - session_id: $session_id - team_slug: $team_slug - webhook_request: $webhook_request - webhook_response: $webhook_response - } - ) { - id - } - } - `, - { - session_id: sessionId, - team_slug: localAuthority, - webhook_request: webhookRequest, - webhook_response: { - status: res.status, - statusText: res.statusText, - headers: res.headers, - config: res.config, - data: res.data, - }, - }, - ); + const webhookResponse = await axios(webhookRequest).catch((error) => { + throw new Error( + `Failed to send submission notification to ${localAuthority}'s Power Automate Webhook (${sessionId}): ${error}`, + ); + }); - return { - id: applicationId.insertS3Application?.id, - axiosResponse: res, - }; - }) - .catch((error) => { - throw new Error( - `Failed to send submission notification to ${localAuthority}'s Power Automate Webhook (${sessionId}): ${error}`, - ); - }); + // Mark session as submitted so that reminder and expiry emails are not triggered + markSessionAsSubmitted(sessionId); + + // Create an audit entry + const { + insertS3Application: { id: auditEntryId }, + } = await $api.client.request( + gql` + mutation CreateS3Application( + $session_id: String! + $team_slug: String! + $webhook_request: jsonb! + $webhook_response: jsonb = {} + ) { + insertS3Application: insert_s3_applications_one( + object: { + session_id: $session_id + team_slug: $team_slug + webhook_request: $webhook_request + webhook_response: $webhook_response + } + ) { + id + } + } + `, + { + session_id: sessionId, + team_slug: localAuthority, + webhook_request: webhookRequest, + webhook_response: { + status: webhookResponse.status, + statusText: webhookResponse.statusText, + headers: webhookResponse.headers, + config: webhookResponse.config, + data: webhookResponse.data, + }, + }, + ); res.status(200).send({ message: `Successfully uploaded submission to S3: ${fileUrl}`, payload: skipValidation ? "Discretionary" : "Validated ODP Schema", - webhookResponse: webhookResponse.axiosResponse.status, - auditEntryId: webhookResponse.id, + webhookResponse: webhookResponse.status, + auditEntryId, }); } catch (error) { return next({