Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
} from "io-functions-commons/dist/src/models/sender_service";
import { wrapCustomTelemetryClient } from "io-functions-commons/dist/src/utils/application_insights";
import { ulidGenerator } from "io-functions-commons/dist/src/utils/strings";
// Whether we're in a production environment
const isProduction = process.env.NODE_ENV === "production";
const getCustomTelemetryClient = wrapCustomTelemetryClient(
isProduction,
new TelemetryClient()
);
// Setup DocumentDB
const cosmosDbUri = getRequiredStringEnv("CUSTOMCONNSTR_COSMOSDB_URI");
const cosmosDbKey = getRequiredStringEnv("CUSTOMCONNSTR_COSMOSDB_KEY");
const cosmosDbName = getRequiredStringEnv("COSMOSDB_NAME");
const documentDbDatabaseUrl = documentDbUtils.getDatabaseUri(cosmosDbName);
const profilesCollectionUrl = documentDbUtils.getCollectionUri(
documentDbDatabaseUrl,
PROFILE_COLLECTION_NAME
);
const messagesCollectionUrl = documentDbUtils.getCollectionUri(
documentDbDatabaseUrl,
MESSAGE_COLLECTION_NAME
);
const notificationsCollectionUrl = documentDbUtils.getCollectionUri(
documentDbDatabaseUrl,
NOTIFICATION_COLLECTION_NAME
);
isProduction,
new TelemetryClient()
);
// Setup Express
const app = express();
secureExpressApp(app);
// Set up CORS (free access to the API from browser clients)
app.use(cors());
// Setup DocumentDB
const cosmosDbUri = getRequiredStringEnv("CUSTOMCONNSTR_COSMOSDB_URI");
const cosmosDbKey = getRequiredStringEnv("CUSTOMCONNSTR_COSMOSDB_KEY");
const cosmosDbName = getRequiredStringEnv("COSMOSDB_NAME");
const messageContainerName = getRequiredStringEnv("MESSAGE_CONTAINER_NAME");
const documentDbDatabaseUrl = documentDbUtils.getDatabaseUri(cosmosDbName);
const messagesCollectionUrl = documentDbUtils.getCollectionUri(
documentDbDatabaseUrl,
MESSAGE_COLLECTION_NAME
);
const messageStatusCollectionUrl = documentDbUtils.getCollectionUri(
documentDbDatabaseUrl,
MESSAGE_STATUS_COLLECTION_NAME
);
const profilesCollectionUrl = documentDbUtils.getCollectionUri(
documentDbDatabaseUrl,
PROFILE_COLLECTION_NAME
);
const servicesCollectionUrl = documentDbUtils.getCollectionUri(
const getCustomTelemetryClient = wrapCustomTelemetryClient(
isProduction,
new TelemetryClient()
);
// Setup Express
const app = express();
secureExpressApp(app);
// Set up CORS (free access to the API from browser clients)
app.use(cors());
// Setup DocumentDB
const cosmosDbUri = getRequiredStringEnv("CUSTOMCONNSTR_COSMOSDB_URI");
const cosmosDbKey = getRequiredStringEnv("CUSTOMCONNSTR_COSMOSDB_KEY");
const cosmosDbName = getRequiredStringEnv("COSMOSDB_NAME");
const messageContainerName = getRequiredStringEnv("MESSAGE_CONTAINER_NAME");
const documentDbDatabaseUrl = documentDbUtils.getDatabaseUri(cosmosDbName);
const messagesCollectionUrl = documentDbUtils.getCollectionUri(
documentDbDatabaseUrl,
MESSAGE_COLLECTION_NAME
);
const messageStatusCollectionUrl = documentDbUtils.getCollectionUri(
documentDbDatabaseUrl,
MESSAGE_STATUS_COLLECTION_NAME
);
const profilesCollectionUrl = documentDbUtils.getCollectionUri(
documentDbDatabaseUrl,
PROFILE_COLLECTION_NAME
const sendgridApiKey = useSendgridTransport
? getRequiredStringEnv("SENDGRID_API_KEY")
: undefined;
//
// options used when converting an HTML message to pure text
// see https://www.npmjs.com/package/html-to-text#options
//
const HTML_TO_TEXT_OPTIONS: HtmlToTextOptions = {
ignoreImage: true, // ignore all document images
tables: true
};
// default sender for email
const MAIL_FROM = getRequiredStringEnv("MAIL_FROM_DEFAULT");
export interface INotificationDefaults {
readonly HTML_TO_TEXT_OPTIONS: HtmlToTextOptions;
readonly MAIL_FROM: NonEmptyString;
}
//
// Main function
//
/**
* Input and output bindings for this function
* see EmailNotificationsQueueHandler/function.json
*/
const ContextWithBindings = t.interface({
bindings: t.partial({
const cosmosDbName = getRequiredStringEnv("COSMOSDB_NAME");
const documentDbDatabaseUrl = documentDbUtils.getDatabaseUri(cosmosDbName);
const notificationsCollectionUrl = documentDbUtils.getCollectionUri(
documentDbDatabaseUrl,
NOTIFICATION_COLLECTION_NAME
);
const notificationStatusCollectionUrl = documentDbUtils.getCollectionUri(
documentDbDatabaseUrl,
NOTIFICATION_STATUS_COLLECTION_NAME
);
export const WEBHOOK_NOTIFICATION_QUEUE_NAME = "webhooknotifications";
const queueConnectionString = getRequiredStringEnv("QueueStorageConnection");
// We create the db client, services and models here
// as if any error occurs during the construction of these objects
// that would be unrecoverable anyway and we neither may trig a retry
const documentClient = new DocumentDBClient(cosmosDbUri, {
masterKey: cosmosDbKey
});
const notificationStatusModel = new NotificationStatusModel(
documentClient,
notificationStatusCollectionUrl
);
const notificationModel = new NotificationModel(
documentClient,
notificationsCollectionUrl
documentDbDatabaseUrl,
SENDER_SERVICE_COLLECTION_NAME
);
const defaultWebhookUrl = HttpsUrl.decode(
getRequiredStringEnv("WEBHOOK_CHANNEL_URL")
).getOrElseL(_ => {
throw new Error(
`Check that the environment variable WEBHOOK_CHANNEL_URL is set to a valid URL`
);
});
// must be equal to the queue name in function.json
export const MESSAGE_QUEUE_NAME = "createdmessages";
const messageContainerName = getRequiredStringEnv("MESSAGE_CONTAINER_NAME");
const storageConnectionString = getRequiredStringEnv("QueueStorageConnection");
const queueConnectionString = getRequiredStringEnv("QueueStorageConnection");
// We create the db client, services and models here
// as if any error occurs during the construction of these objects
// that would be unrecoverable anyway and we neither may trig a retry
const documentClient = new DocumentDBClient(cosmosDbUri, {
masterKey: cosmosDbKey
});
const messageStatusModel = new MessageStatusModel(
documentClient,
messageStatusCollectionUrl
);
// As we cannot use Functions bindings to do retries,
import * as winston from "winston";
import { Context } from "@azure/functions";
import { createQueueService } from "azure-storage";
import { TelemetryClient } from "io-functions-commons/dist/src/utils/application_insights";
import { getRequiredStringEnv } from "io-functions-commons/dist/src/utils/env";
import { configureAzureContextTransport } from "io-functions-commons/dist/src/utils/logging";
import { MESSAGE_QUEUE_NAME } from "./created_message_queue_handler";
import { EMAIL_NOTIFICATION_QUEUE_NAME } from "./emailnotifications_queue_handler";
import { getQueueMetadata } from "./utils/azure_queues";
import { WEBHOOK_NOTIFICATION_QUEUE_NAME } from "./webhook_queue_handler";
const queueConnectionString = getRequiredStringEnv("QueueStorageConnection");
const queueService = createQueueService(queueConnectionString);
// Whether we're in a production environment
const isProduction = process.env.NODE_ENV === "production";
const appInsightsClient = new TelemetryClient();
// needed otherwise AI will wait for the batching loop to end
// see https://github.com/Microsoft/ApplicationInsights-node.js/issues/390
// tslint:disable-next-line:no-object-mutation
appInsightsClient.config.maxBatchSize = 1;
/**
* A function to store the length of Azure Storage Queues
* into Application Insights Metrics.
*
const notificationModel = new NotificationModel(
documentClient,
notificationsCollectionUrl
);
// As we cannot use Functions bindings to do retries,
// we resort to update the message visibility timeout
// using the queue service (client for Azure queue storage)
const queueService = createQueueService(queueConnectionString);
//
// setup NodeMailer
//
const mailupUsername = getRequiredStringEnv("MAILUP_USERNAME");
const mailupSecret = getRequiredStringEnv("MAILUP_SECRET");
//
// setup SendGrid
//
const useSendgridTransport = process.env.USE_SENDGRID_TRANSPORT;
const sendgridApiKey = useSendgridTransport
? getRequiredStringEnv("SENDGRID_API_KEY")
: undefined;
//
// options used when converting an HTML message to pure text
// see https://www.npmjs.com/package/html-to-text#options
//
const HTML_TO_TEXT_OPTIONS: HtmlToTextOptions = {
ignoreImage: true, // ignore all document images
const cosmosDbName = getRequiredStringEnv("COSMOSDB_NAME");
const documentDbDatabaseUrl = documentDbUtils.getDatabaseUri(cosmosDbName);
const notificationsCollectionUrl = documentDbUtils.getCollectionUri(
documentDbDatabaseUrl,
NOTIFICATION_COLLECTION_NAME
);
const notificationStatusCollectionUrl = documentDbUtils.getCollectionUri(
documentDbDatabaseUrl,
NOTIFICATION_STATUS_COLLECTION_NAME
);
export const EMAIL_NOTIFICATION_QUEUE_NAME = "emailnotifications";
const queueConnectionString = getRequiredStringEnv("QueueStorageConnection");
// We create the db client, services and models here
// as if any error occurs during the construction of these objects
// that would be unrecoverable anyway and we neither may trig a retry
const documentClient = new DocumentDBClient(cosmosDbUri, {
masterKey: cosmosDbKey
});
const notificationStatusModel = new NotificationStatusModel(
documentClient,
notificationStatusCollectionUrl
);
const notificationModel = new NotificationModel(
documentClient,
notificationsCollectionUrl
);
const notificationModel = new NotificationModel(
documentClient,
notificationsCollectionUrl
);
// As we cannot use Functions bindings to do retries,
// we resort to update the message visibility timeout
// using the queue service (client for Azure queue storage)
const queueService = createQueueService(queueConnectionString);
//
// setup NodeMailer
//
const mailupUsername = getRequiredStringEnv("MAILUP_USERNAME");
const mailupSecret = getRequiredStringEnv("MAILUP_SECRET");
//
// setup SendGrid
//
const useSendgridTransport = process.env.USE_SENDGRID_TRANSPORT;
const sendgridApiKey = useSendgridTransport
? getRequiredStringEnv("SENDGRID_API_KEY")
: undefined;
//
// options used when converting an HTML message to pure text
// see https://www.npmjs.com/package/html-to-text#options
//
const HTML_TO_TEXT_OPTIONS: HtmlToTextOptions = {