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

@@ -59,33 +59,27 @@
 t handle
-GetAppConfig
+Apply
 Event
+s
  = async
@@ -113,920 +113,67 @@
 %7B%0A  
-return %7B%0A    applicationName: resolve.applicationName,%0A    readModels: resolve.readModels.map(%0A      (%7B name, projection, resolvers, invariantHash %7D) =%3E (%7B%0A        name,%0A        eventTypes: Object.keys(projection %7C%7C %7B%7D).filter(%0A          eventType =%3E eventType !== 'Init'%0A        ),%0A        resolverNames: Object.keys(resolvers %7C%7C %7B%7D),%0A        invariantHash%0A      %7D)%0A    ),%0A    viewModels: resolve.viewModels.map(%0A      (%7B name, projection, invariantHash %7D) =%3E (%7B%0A        name,%0A        eventTypes: Object.keys(projection %7C%7C %7B%7D).filter(%0A          eventType =%3E eventType !== 'Init'%0A        ),%0A        invariantHash%0A      %7D)%0A    ),%0A    aggregates: resolve.aggregates.map((%7B name, commands, invariantHash
+const %7B events, listenerId
  %7D
-)
  =
-%3E (%7B%0A      name,%0A      commandTypes: Object.keys(commands %7C%7C %7B%7D),%0A      invariantHash%0A    %7D))%0A  %7D%0A%7D%0A%0Aconst handleApplyEvents = async (lambdaEvent, resolve) =%3E %7B%0A  const %7B events, listenerId %7D = lambdaEvent%0A 
+ lambdaEvent%0A  const result =
  awa
@@ -211,16 +211,21 @@
 yEvents(
+%0A    
 listener
@@ -227,16 +227,20 @@
 tenerId,
+%0A   
  events.
@@ -255,16 +255,19 @@
 deEvent)
+%0A  
 )%0A  retu
@@ -273,12 +273,14 @@
 urn 
-true
+result
 %0A%7D%0A%0A
@@ -385,108 +385,8 @@
 ) %7B%0A
-    case 'GET_APP_CONFIG': %7B%0A      return await handleGetAppConfigEvent(lambdaEvent, resolve)%0A    %7D%0A
     

Improve bus event handler (#1053)