text: // Code generated by generate-maintenance-handler. DO NOT EDIT.
  /* eslint-disable */
  import EventsHandler from 'api/eventsHandler'
  import { messageType } from 'aws/sns'
  import MaintenancesStore from 'db/maintenances'
  import MaintenanceUpdatesStore from 'db/maintenanceUpdates'
  import { Component } from 'model/components'
  import { Maintenance, MaintenanceUpdate } from 'model/maintenances'
  import { NotFoundError, ValidationError } from 'utils/errors'
  
  export async function handle (event, context, callback) {
    try {
      const maintenanceID = event.params.maintenanceid
      const eventsStore = new MaintenancesStore()
      const existingMaintenance = await eventsStore.get(maintenanceID)
      const newMaintenance = new Maintenance({...existingMaintenance.objectify(), ...event.body})
      const maintenanceUpdate = new MaintenanceUpdate({
        maintenanceID,
        maintenanceStatus: (event.body.status === undefined ? newMaintenance.status : event.body.status),
        message: event.body.message
      })
      const components = event.body.components === undefined ? [] : event.body.components.map(comp => new Component(comp))
  
      const handler = new EventsHandler(new MaintenancesStore(), new MaintenanceUpdatesStore())
      const msgType = messageType.maintenanceUpdated
      const [respMaintenance, respMaintenanceUpds] = await handler.updateEvent(newMaintenance, maintenanceUpdate, msgType, components)
  
      const resp = {
        ...respMaintenance.objectify(),
        maintenanceUpdates: respMaintenanceUpds.map(upd => upd.objectify())
      }
      if (event.body.components !== undefined) {
        resp.components = components
      }
  
      callback(null, resp)
    } catch (error) {
      console.log(error.message)
      console.log(error.stack)
      switch (error.name) {
        case ValidationError.name:
          callback('Error: ' + error.message)
          break
        case NotFoundError.name:
          callback('Error: an item not found')
          break
        default:
          callback('Error: failed to update the maintenance')
      }
    }
  }
  