in source/appsync-lambda-resolver/index.ts [23:115]
export async function handler(event: IAppSyncResolverRequest): Promise<any> {
logger.log(LogLevel.INFO, 'Received event', JSON.stringify(event, null, 2));
// Ensure this handler supports the incoming handler event input
validateHandlerInput(event);
if (event.info.parentTypeName === SupportedParentTypeNames.MUTATION) {
// The ID for the AVA Event
const eventId = event.prev.result.id;
switch (event.info.fieldName) {
case SupportedMutationFieldNames.CREATE_EVENT:
case SupportedMutationFieldNames.UPDATE_EVENT:
let subscriptionsForEvent: IAvaTopicSubscription[] = [];
if (event.prev.result.email) {
// Add an array of email addresses that need to be subscribed to notifications for this event
subscriptionsForEvent.push(
...(event.prev.result.email as string).split(',').map(email => {
return { protocol: SubscriptionProtocols.EMAIL, endpoint: email.trim() };
})
);
}
if (event.prev.result.sms) {
// Add an array of phone numbers that need to be subscribed to notifications for this event
subscriptionsForEvent.push(
...(event.prev.result.sms as string).split(',').map(phoneNumber => {
return { protocol: SubscriptionProtocols.SMS, endpoint: phoneNumber.trim() };
})
);
}
logger.log(LogLevel.INFO, `Total number of subscriptions for event: ${subscriptionsForEvent.length}`);
for (let i = 0; i < subscriptionsForEvent.length; i++) {
logger.log(LogLevel.INFO, `Handling subscription #${i + 1} of ${subscriptionsForEvent.length} total subscriptions`);
logger.log(LogLevel.VERBOSE, 'Handling subscription', JSON.stringify(subscriptionsForEvent, null, 2));
let subscription: IAvaTopicSubscription = await getSubscriptionFromDataHierarchyTable(subscriptionsForEvent[i]);
let subscriptionUpdated = false;
if (!subscription) {
logger.log(LogLevel.INFO, 'Subscription did not exist. It will need to be created');
// Set the base attributes of the subscription.
// `placeholder-event-id` is included just in case this endpoint will be
// removed from all events. You cannot set a filter policy with an empty array
subscription = {
protocol: subscriptionsForEvent[i].protocol,
endpoint: subscriptionsForEvent[i].endpoint,
filterPolicy: { eventId: ['placeholder-event-id', eventId] }
}
// Create the new subscription
const subscriptionArn = await subscribeToIssueNotificationTopic(subscription);
// Update the subscription object with the newly created SubscriptionArn
subscription.subscriptionArn = subscriptionArn;
subscriptionUpdated = true;
} else {
logger.log(LogLevel.INFO, 'Subscription already existed');
if (subscription.filterPolicy.eventId.includes(eventId)) {
logger.log(LogLevel.INFO, `Filter policy already included event ID (${eventId}). No need to update`);
} else {
logger.log(LogLevel.INFO, `Filter policy will be updated to include event ID (${eventId})`);
subscription.filterPolicy.eventId.push(eventId);
await updateSubscriptionFilterPolicy(subscription);
subscriptionUpdated = true;
}
}
if (subscriptionUpdated && subscription.subscriptionArn && subscription.subscriptionArn.trim() !== '') {
logger.log(LogLevel.INFO, 'Persisting subscription details in the Data Hierarchy table');
await persistSubscriptionInDataHierarchyTable(subscription);
}
}
await cleanupPreviousSubscriptions(subscriptionsForEvent, event.arguments.previousSms, event.arguments.previousEmail, eventId);
break;
case SupportedMutationFieldNames.DELETE_EVENT:
await cleanupPreviousSubscriptions([], event.prev.result.sms, event.prev.result.email, eventId);
break;
}
// return the result from previous AppSync pipeline functions
return event.prev.result;
} else {
return getPrevDayIssuesStats();
}
}