2020-11-08 - Notification Manager - Functions

From Izara Wiki
Jump to navigation Jump to search

Service - Notification Manager

Lambda Functions

RcvActivityMsg

/**
 * Receives one activity message from Activity Switchboard, find matching notification, stores for consolidation or sends immediately
 * @param {Object} activityMsg
 * @param {Object} activityMsg.messageAttributes
 * @param {Object} activityMsg.message
 * @param {String} uniqueId - In NotificationMgr, uniqueId is notificationId
 * @param {number} time 
 *
 */

HdrSqs

Subscribes to Service - Activity Switchboard OutTriggerGroupPassed topic, (? filter policy limit) receiverTag = {this service's izServiceName}

logic

  1. get notification node by getNodeAndRelationship
    • nodeLabel: "notification"
    • nodeProperties.notificationId
    • relationshipType: "has_notification"
    • relationshipDirection: "incoming"
    • toNodeVersionedDataLabels: ["notificationGroupSettings"]
  2. if not found notification or notificationGroup node
    • throw new NoRetryError
  3. If have setting and consolidated = true
    1. putItem to ConsolidationPending table
      • notificationGroupId
      • time: {timestamp activity handled in Activity Switchboard}_{small random UUID}
      • activityMsg
  4. else


DisableNotificationGroups

/**
 * Disables a set of matching notification groups
 * @param {string} userId
 * @param {string} userContactId
 *
 */

HdrSqs

Subscribes to InDisableNotificationGroups topic

logic

  1. get notificationGroup node by GraphHandler getNodeAndRelationships
    • nodeLabel: "userContact"
    • nodeProperties.userContactId
    • relationshipType: "has_notificationGroup"
    • relationshipDirection: "outgoing"
  2. if not found notificationGroup or notification node
    • throw new NoRetryError
  3. else
    1. for each notificationGroup

UpdateNotificationGroup

/**
 * Update a notification group
 * @param {string} userId
 * @param {string} notificationGroupId
 * @param {string} notificationGroupName
 * @param {boolean} consolidated 
 * @param {string} consolidatedType
 * @param {..?} consolidatedFrequency
 * @param {number} consolidatedNextSendDue
 * @param {boolean} consolidatedSendIfEmpty
 * @param {boolean} enabled
 *
 */

HdrSqs

Subscribes to InUpdateNotificationGroup topic

logic

  1. get notificationGroup node by GraphHandler getNodeAndRelationships
    • nodeLabel: "notificationGroup"
    • nodeProperties.notificationGroupId
    • relationshipType: "has_notification" or "disabled_notification" (depend on enable)
    • relationshipDirection: "outgoing"
  2. if not found notificationGroup or notification node
    • throw new NoRetryError
  3. else update notificationGroup node by send message to Service - Graph Handler InCreateVersionedData
    • nodeStructure
      • nodeLabel: "notificationGroup"
      • nodeProperties.notificationGroupId
    • versionDataStructure
      • nodeLabel: "notificationGroupSetting"
      • nodeProperties: {notificationGroupName, consolidated, consolidatedType, consolidatedFrequency, consolidatedNextSendDue, consolidatedSendIfEmpty}
  4. if enabled = false
    1. use function #disableNotificationGroup
  5. if enabled = true
    1. #enableNotificationGroup

DisableNotification

/**
 * @param {string} userId 
 * @param {string} notificationId
 *
 */

HdrSqs

Subscribes to InDisableNotification topic

logic

  1. get notification node by GraphHandler getNodeAndRelationships
    • nodeLabel: "notification"
    • nodeProperties.notificationId
    • relationshipType: "has_notification"
    • relationshipDirection: "incoming"
  2. if not found notification node
    • throw new NoRetryError
  3. change relationshipType to "disabled_notification" by send message to Service - Graph Handler InChangeRelationshipType
  4. send message to Service - Activity Switchboard InRemoveTriggerGroup
    • receiverTag: process.env.iz_serviceName
    • notificationId
  5. send message to OutNotificationDisabled topic
    • notificationId

CreateNotificationGroup

/**
 * Creates one notification group
 * @param {string} userId
 * @param {string} notificationGroupName
 * @param {boolean} consolidated
 * @param {string} consolidatedType
 * @param {..?} consolidatedFrequency
 * @param {number} consolidatedNextSendDue 
 * @param {boolean} consolidatedSendIfEmpty
 * @param {string} recieverTag
 *
 */

HdrSqs

Subscribes to InCreateNotificationGroup topic

logic

  1. create notificationGroupId by random UUID
  2. if !consolidate set consolidate is false
  3. create versionedDataProperties object
    • consolidate
  4. if consolidated = true, validate:
    1. consolidatedType must equal "overview" or "detailed"
    2. consolidatedSendIfEmpty set default is "false"
    3. consolidatedFrequency ..? (not sure, maybe use some sort of standard like cron)
    4. if have notificationGroupName save it to versionedDataProperties
    5. versionedDataProperties: {notificationGroupName, consolidated, consolidatedType, consolidatedFrequency, consolidatedNextSendDue, consolidatedSendIfEmpty}
  5. create notificationGroup by send message to Service - Graph Handler InCreateNodeWithVersionedData
    • nodeStructure
      • nodeLabel: "notificationGroup"
      • nodeProperties: {notificationGroupId, recieverTag}
    • versionedDatas
      • nodeLabel: "notificationGroupSettings"
      • nodeProperties: versionedDataProperties
  6. send message to OutNotificationGroupCreated topic, can include full notificationGroup object

CreateNotification

/**
 * Creates notification
 * @param {string} userId
 * @param {string} notificationGroupId
 * @param {string} notificationName
 *
 */

HdrSqs

Subscribes to InCreateNotification topic

logic

  1. create notificationId by random UUID
  2. if notification have notificationName
  3. else

CreateNotificationTrigger

/**
 * Creates notificationTrigger
 * @param {string} userId
 * @param {string} notificationId
 * @param {string} propertyName
 * @param {string} valueType - property|attribute|serviceName|topicName
 * @param {string} [valueString] - either value or values will be set
 * @param {number} [valueNumber] - either value or values will be set
 * @param {string[]} [values] - either value or values will be set
 *
 */

HdrSqs

Subscribes to InCreateNotificationTrigger topic

logic

  1. create notificationTriggerId by random UUID
  2. #notificationTriggerValidator
    1. if validateResult is "error"
      1. send message to OutCreateNotificationTriggerFailed
        • notificationTriggerId
        • errorsFound: {errorsFound}
  3. send message to Service - Graph Handler InCreateChildNodeAndRelationship

NotificationGroup/List

/**
 * Lists all notification groups for one primary key (notificationGroupId/uniqueId)
 * @param {string} receiverTag
 * @param {string} groupingId
 * @param {string} [uniqueIdPrefix=null]
 *
 * @returns {Object[]} array of objects, each object is one notification group
 */

HdrApi

Validator:

  • receiverTag
  • groupingId
  • uniqueIdPrefix with default value

handler logic

(standard)

logic

Query NotificationGroups table for all records that match notificationGroupId and if uniqueIdPrefix not null uniqueId starts_with uniqueIdPrefix, and return.

Functions

sendNotificationToReceiver

/**
 * Send message to receiver endpoint
 * @param {Object} notification - object from the Notifications table
 * @param {Object} notificationGroup - object from the NotificationGroups table
 * @param {string} notificationBody - string sent to receiver
 */

logic

  1. get notificationGroup node by getNodeAndRelationships
    • nodeLabel: "notificationGroup"
    • nodeProperties.notificationGroupId
    • relationshipType: "has_notificationGroup"
    • relationshipDirection: "incoming"
    return; notificationGroup and userContact node (in future; will have another service eg. SMS, site notification)
  2. send message to OutNewNotification topic, adding below properties:
    • uniqueId
    • notificationBody
    • (? filter policy limit) receiverTag gets added to message attributes so can be filtered for by receiving service
Service - User Contact Manager and Service - Contact Method Email will change logic or structure

sharedCreateTriggerGroup

/**
 * Send message to receiver endpoint
 * @param {Object} notificationId
 * @param {Array} notificationTriggers
 */

logic

  1. send message to ActySwichboardTestInCreateTriggerGroup topic, adding below properties:
    • receiverTag: {process.env.iz_serviceName}
    • uniqueId: {notificationId}
    • requestedTriggers: notificationTriggers

notificationTriggerValidator

/**
 * Validate notificationTrigger
 * @param {Object} notificationTrigger
 * @param {string} notificationTrigger.propertyName - optional
 * @param {string} notificationTrigger.valueType - require
 * @param {string} notificationTrigger.valueString - optional
 * @param {number} notificationTrigger.valueNumber - optional
 * @param {Array} notificationTrigger.values - optional
 *
 * @returns {Array}
 */

logic

  1. vatidate notificationTrigger (see. Service - Notification Manager) collect error and return status
    • notificationTrigger must have only one of those ["valueString", "valueNumber", "values"]
    • "serviceName" and "topicName" valueTypes do not have propertyName
    • validate type of properties ["valueString", "valueNumber", "values"]
    - typeof "valueString" is string
    - typeof "valueNumber" is number
    - typeof "values" is array
  2. return
    • validateStatus: {"complete" or "error"}
    • errorsFound[ ]

reformNotificationTrigger

/**
 * Reform notificationTriggers for send to InCreateTriggerGroup
 * @param {Object[]} notificationTriggers
 * 
 * returns {Object[]}
 */

logic

  1. for each notificationTrigger of notificationTriggers
    1. notificationTrigger must have nodeLabels and nodeLabels is "notificationTrigger"
    2. notificationTrigger must have nodeProperties
    3. use nodeProperties for #notificationTriggerValidator
    4. if error; throw new NoRetryError
    5. push nodeProperties to notificationTriggerReformed[ ]
  2. return notificationTriggerReformed[ ]

disableNotificationGroup

/**
 * @param {Object} notificationGroup
 * @param {string} userId
 * @param {string} userContactId  - default = null
 *
 */

logic

  1. for each notificationId
    1. change relationshipType to "disabled_notification" by send message to Service - Graph Handler InChangeRelationshipType
    2. send message to Service - Activity Switchboard InRemoveTriggerGroup
      • receiverTag
      • notificationId
    3. send message to OutNotificationDisabled topic
      • notificationId
  2. if !userContactId
    1. get notificationGroup node by GraphHandler getNodeAndRelationships for find userContact node
      • nodeLabel: "notificationGroup"
      • nodeProperties.notificationGroupId
      • relationshipType: "has_notificationGroup"
      • relationshipDirection: "incoming"
  3. change relationshipType to "disabled_notificationGroup" by send message to Service - Graph Handler InChangeRelationshipType
  4. send message to OutNotificationGroupDisabled topic
    • notificationGroupId

enableNotificationGroup

/**
 * @param {Object} notificationGroup
 * @param {string} userId
 *
 */

logic

  1. for each notificationId
    1. get notification node by GraphHandler getNodeAndRelationships
      • nodeLabel: "notification"
      • nodeProperties.notificationId
      • relationshipType: "has_notificationTrigger"
      • relationshipDirection: "outgoing"
    2. change relationshipType to "has_notification" by send message to Service - Graph Handler InChangeRelationshipType
    3. use child notificationTriggers for #reformNotificationTrigger
    4. use function sharedCreateTriggerGroup.sharedCreateTriggerGroup
      1. send message to Service - Activity Switchboard InCreateTriggerGroup
        • uniqueId: notificationId
        • requestedTriggers: [{notificationTriggers}]
    5. send message to OutNotificationEnabled topic
      • notificationId
  2. change relationshipType to "has_notificationGroup" by send message to Service - Graph Handler InChangeRelationshipType
  3. send message to OutNotificationGroupEnabled topic
    • notificationGroupId

Sample notification object sent to receiver service

{
	notificationBody: [
		{
			messageAttributes: {
				// xxx: yyy
			},
			message: {
				// full body of message received
			}
		},
	],
	uniqueId: {emailId}
}