Skip to content

Commit

Permalink
refactor: Unnest async calls
Browse files Browse the repository at this point in the history
  • Loading branch information
DafyddLlyr committed Dec 7, 2024
1 parent c15fc9f commit 61ae6a4
Showing 1 changed file with 46 additions and 50 deletions.
96 changes: 46 additions & 50 deletions api.planx.uk/modules/send/s3/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<CreateS3Application>(
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<CreateS3Application>(
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({
Expand Down

0 comments on commit 61ae6a4

Please sign in to comment.