import { decodeEvent } from 'resolve-storage-dynamo'

const handleGetAppConfigEvent = async (lambdaEvent, resolve) => {
  return {
    applicationName: resolve.applicationName,
    readModels: resolve.readModels.map(
      ({ name, projection, resolvers, invariantHash }) => ({
        name,
        eventTypes: Object.keys(projection || {}).filter(
          eventType => eventType !== 'Init'
        ),
        resolverNames: Object.keys(resolvers || {}),
        invariantHash
      })
    ),
    viewModels: resolve.viewModels.map(
      ({ name, projection, invariantHash }) => ({
        name,
        eventTypes: Object.keys(projection || {}).filter(
          eventType => eventType !== 'Init'
        ),
        invariantHash
      })
    ),
    aggregates: resolve.aggregates.map(({ name, commands, invariantHash }) => ({
      name,
      commandTypes: Object.keys(commands || {}),
      invariantHash
    }))
  }
}

const handleApplyEvents = async (lambdaEvent, resolve) => {
  const { events, listenerId } = lambdaEvent
  await resolve.executeQuery.updateByEvents(listenerId, events.map(decodeEvent))
  return true
}

const handleEventBusEvent = async (lambdaEvent, resolve) => {
  switch (lambdaEvent['detail-type']) {
    case 'GET_APP_CONFIG': {
      return await handleGetAppConfigEvent(lambdaEvent, resolve)
    }
    case 'APPLY_EVENTS_FROM_EVENT_BUS': {
      return await handleApplyEvents(lambdaEvent, resolve)
    }
    default: {
      return null
    }
  }
}

export default handleEventBusEvent

import { decodeEvent } from 'resolve-storage-dynamo'

const handleApplyEvents = async (lambdaEvent, resolve) => {
  const { events, listenerId } = lambdaEvent
  const result = await resolve.executeQuery.updateByEvents(
    listenerId,
    events.map(decodeEvent)
  )
  return result
}

const handleEventBusEvent = async (lambdaEvent, resolve) => {
  switch (lambdaEvent['detail-type']) {
    case 'APPLY_EVENTS_FROM_EVENT_BUS': {
      return await handleApplyEvents(lambdaEvent, resolve)
    }
    default: {
      return null
    }
  }
}

export default handleEventBusEvent

Improve bus event handler (#1053)