<commit_before>import { isEqual, difference, values } from 'lodash'
import setupNetworkAttrs from './setupNetworkAttrs'

export default function updateNetwork (userId, id, params) {
  if (!userId) throw new Error('updateNetwork called with no userID')
  if (!id) throw new Error('updateNetwork called with no ID')
  return setupNetworkAttrs(userId, params).then(attrs =>
    bookshelf.transaction(transacting =>
      // NOTE: EnsureLoad not built to work with belongsToMany relations
      Network.find(id, {withRelated: 'communities'}).then(network => {
        return network.save(attrs, {patch: true, transacting})
        .tap(updatedNetwork => afterUpdatingNetwork(updatedNetwork, {params, userId, transacting}))
      })
    )
  )
}

export function afterUpdatingNetwork (network, opts) {
  const {
    params: { community_ids },
    transacting
  } = opts
  return Promise.all([
    updateCommunities(network, values(community_ids), transacting)
  ])
}

export function updateCommunities (network, newCommunityIds, transacting) {
  const oldCommunityIds = network.relations.communities.pluck('id')
  if (!isEqual(newCommunityIds, oldCommunityIds)) {
    const opts = { transacting }
    const communitiesToAdd = difference(newCommunityIds, oldCommunityIds)
    const communitesToRemove = difference(newCommunityIds, oldCommunityIds)
    return Promise.all([
      // communitiesToAdd.forEach(communityId =>
      //   Community.find(communityId, opts).then(community =>
      //     community && community.save({network_id: network.id}, opts)
      //   )
      // ),
      // communitesToRemove.forEach(communityId =>
      //   Community.find(communityId, opts).then(community =>
      //     community && community.save({network_id: null}, opts)
      //   )
      // )
    ])
  }
}<commit_msg>updateNetwork with communities<commit_after>import { isEqual, difference, values, some } from 'lodash'
import setupNetworkAttrs from './setupNetworkAttrs'

export default function updateNetwork (userId, id, params) {
  if (!userId) throw new Error('updateNetwork called with no userID')
  if (!id) throw new Error('updateNetwork called with no ID')
  return setupNetworkAttrs(userId, params).then(attrs =>
    bookshelf.transaction(transacting =>
      // NOTE: EnsureLoad not built to work with belongsToMany relations
      Network.find(id, {withRelated: 'communities'}).then(network => {
        return network.save(attrs, {patch: true, transacting})
        .tap(updatedNetwork => afterUpdatingNetwork(updatedNetwork, {params, userId, transacting}))
      })
    )
  )
}

export function afterUpdatingNetwork (network, opts) {
  const {
    params: { community_ids },
    transacting
  } = opts
  return Promise.all([
    updateCommunities(network, community_ids && values(community_ids), transacting) // eslint-disable-line camelcase
  ])
}

export function updateCommunities (network, newCommunityIds, transacting) {
  if (!newCommunityIds) return
  const currentCommunityIds = network.relations.communities.pluck('id')
  if (!isEqual(newCommunityIds, currentCommunityIds)) {
    const communitiesToAdd = difference(newCommunityIds, currentCommunityIds)
    const communitesToRemove = difference(currentCommunityIds, newCommunityIds)
    return Promise.all([
      // Add communities
      some(communitiesToAdd) && Community.query().where('id', 'in', communitiesToAdd)
      .update('network_id', network.id).transacting(transacting),
      // Remove communities
      some(communitesToRemove) && Community.query().where('id', 'in', communitesToRemove)
      .update('network_id', null).transacting(transacting)
    ])
  }
}
{'fraction_non_alphanumeric': 0.11633663366336634, 'fraction_numerical': 0.0, 'mean_word_length': 4.137005649717514, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6182801', 'n_tokens_mistral': 1044, 'n_tokens_neox': 995, 'n_words': 238}