import { prepareAnimalResponse } from '../controllers/base'

import {
  ActivityBelongsToAnimal,
  ActivityBelongsToEvent,
  ActivityBelongsToPlace,
  ActivityBelongsToResource,
  ActivityRequestBelongsToEvent,
} from '../database/associations'

import Activity from '../models/activity'

function prepareResponse(conversation) {
  const response = conversation.toJSON()

  if (response.animal) {
    response.animal = prepareAnimalResponse(response.animal)
  }

  let item

  if (response.resource) {
    item = response.resource
  } else if (response.event) {
    item = response.event
  } else if (response.place) {
    item = response.place
  }

  const object = item ? Object.assign({}, {
    id: item.id,
    slug: item.slug,
    title: item.title,
  }) : null

  const event = response.requestedEvent ? Object.assign({}, {
    id: response.requestedEvent.id,
    slug: response.requestedEvent.slug,
    title: response.requestedEvent.title,
  }) : null

  const { id, createdAt, type, animal, objectTitle, objectType } = response

  return Object.assign({}, {
    animal,
    createdAt,
    event,
    id,
    object,
    objectTitle,
    objectType,
    type,
  })
}

function prepareResponseAll(rows) {
  return rows.map(row => prepareResponse(row))
}

export function getMyActivities(limit, offset) {
  return new Promise((resolve, reject) => {
    Activity.findAndCountAll({
      include: [
        ActivityBelongsToAnimal,
        ActivityBelongsToEvent,
        ActivityBelongsToPlace,
        ActivityBelongsToResource,
        ActivityRequestBelongsToEvent,
      ],
      limit,
      offset,
      order: [
        ['createdAt', 'DESC'],
      ],
    })
      .then(result => {
        resolve({
          data: prepareResponseAll(result.rows),
          limit: parseInt(limit, 10),
          offset: parseInt(offset, 10),
          total: result.count,
        })
      })
      .catch(err => reject(err))
  })
}

@@ -368,98 +368,83 @@
 N()%0A
-%0A
   
-if (response.animal) %7B%0A    response.animal = prepareAnimalResponse(response.animal)%0A  %7D
+const %7B id, createdAt, type, animal, objectTitle, objectType %7D = response
 %0A%0A  
@@ -459,33 +459,40 @@
 %0A%0A  if (
-response.
+objectType === '
 resource
 ) %7B%0A    
@@ -483,16 +483,17 @@
 resource
+'
 ) %7B%0A    
@@ -530,30 +530,38 @@
 lse if (
-response.
+objectType === '
 event
+'
 ) %7B%0A    
@@ -595,30 +595,38 @@
 lse if (
-response.
+objectType === '
 place
+'
 ) %7B%0A    
@@ -969,81 +969,95 @@
 %0A%0A  
-const %7B id, createdAt, type, animal, objectTitle, objectType %7D = response
+if (response.animal) %7B%0A    response.animal = prepareAnimalResponse(response.animal)%0A  %7D
 %0A%0A  

Fix an issue with wrong object types in the activity stream