Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
addressSource: emailNotification.addressSource,
transport: lMailerTransporter.transporter.name
}
};
if (isLeft(sendResult)) {
const error = sendResult.value;
// track the event of failed delivery
lAppInsightsClient.trackDependency({
...eventContent,
data: error.message,
resultCode: error.name,
success: false
});
return left(
TransientError(
`Error while sending email|notification=${notificationId}|message=${
message.id
}|error=${error.message}`
)
);
}
// track the event of successful delivery
lAppInsightsClient.trackDependency({
...eventContent,
data: "OK",
resultCode: 200,
success: true
});
// TODO: handling bounces and delivery updates
callbackError =>
handleQueueProcessingFailure(
queueService,
queueMessageBindings,
queueName,
onTransientError,
onPermanentError,
TransientError(callbackError.message)
),
// exits (stop processing) in case no error
`Message expired|notification=${notificationId}|message=${message.id}`
)
);
}
// fetch the notification
const errorOrMaybeNotification = await lNotificationModel.find(
notificationId,
message.id
);
if (isLeft(errorOrMaybeNotification)) {
const error = errorOrMaybeNotification.value;
// we got an error while fetching the notification
return left(
TransientError(
`Error while fetching the notification|notification=${notificationId}|message=${
message.id
}|error=${error.code}`
)
);
}
const maybeWebhookNotification = errorOrMaybeNotification.value;
if (isNone(maybeWebhookNotification)) {
// it may happen that the object is not yet visible to this function due to latency
// as the notification object is retrieved from database (?)
return left(
TransientError(
`Notification not found|notification=${notificationId}|message=${
message.id
async error => {
if (isExpiredError(error)) {
// message is expired. try to save the notification status into the database
const errorOrUpdateNotificationStatus = await notificationStatusUpdater(
NotificationChannelStatusValueEnum.EXPIRED
);
if (isLeft(errorOrUpdateNotificationStatus)) {
// retry the whole handler in case we cannot save
// the notification status into the database
throw TransientError(
errorOrUpdateNotificationStatus.value.message
);
}
// if the message is expired we're done, stop here
return;
}
// for every other kind of error
// delegate to the catch handler
throw error;
},
async _ => {
RecipientError("Sender is blocked by recipient.")
);
}
// Save the content of the message to the blob storage.
// In case of a retry this operation will overwrite the message content with itself
// (this is fine as we don't know if the operation succeeded at first)
const errorOrAttachment = await lMessageModel.attachStoredContent(
lBlobService,
newMessageWithoutContent.id,
newMessageWithoutContent.fiscalCode,
createdMessageEvent.content
);
if (isLeft(errorOrAttachment)) {
return left(
TransientError("Cannot store message content")
);
}
// Now that the message content has been stored, we can make the message
// visible to getMessages by changing the pending flag to false
const updatedMessageOrError = await lMessageModel.createOrUpdate(
{
...newMessageWithoutContent,
isPending: false
},
createdMessageEvent.message.fiscalCode
);
if (isLeft(updatedMessageOrError)) {
return left(
TransientError("Cannot update message pending status")
);
return left(
TransientError(
`Error while fetching the notification|notification=${notificationId}|message=${
message.id
}|error=${error.code}`
)
);
}
const maybeWebhookNotification = errorOrMaybeNotification.value;
if (isNone(maybeWebhookNotification)) {
// it may happen that the object is not yet visible to this function due to latency
// as the notification object is retrieved from database (?)
return left(
TransientError(
`Notification not found|notification=${notificationId}|message=${
message.id
}`
)
);
}
const errorOrWebhookNotification = WebhookNotification.decode(
maybeWebhookNotification.value
);
if (isLeft(errorOrWebhookNotification)) {
const error = readableReport(errorOrWebhookNotification.value);
return left(
PermanentError(
`Wrong format for webhook notification|notification=${notificationId}|message=${
response => {
if (response.error) {
return left(
// in case of server HTTP 5xx errors we trigger a retry
response.serverError
? TransientError(
`Transient HTTP error calling API Proxy: ${response.text}`
)
: PermanentError(
`Permanent HTTP error calling API Proxy: ${response.text}`
)
);
}
return right(response);
},
err => {
async function createNotification(
lNotificationModel: NotificationModel,
senderMetadata: CreatedMessageEventSenderMetadata,
newMessageWithoutContent: NewMessageWithoutContent,
newMessageContent: MessageContent,
newNotification: NewNotification
): Promise> {
const errorOrNotification = await lNotificationModel.create(
newNotification,
newNotification.messageId
);
if (isLeft(errorOrNotification)) {
return left(
TransientError("Cannot save notification to database")
);
}
const notification = errorOrNotification.value;
const notificationEvent: NotificationEvent = {
content: newMessageContent,
message: newMessageWithoutContent,
notificationId: notification.id,
senderMetadata
};
return right(notificationEvent);
}