Clean-up and re-sync journals

This code snippet removes all the journals (and their id maps) for a list of organisations, and forces a full synchronisation of the journals right after.

# Organizations that need to be cleaned-up
# channel_ids = ["org-f123", "org-f456", "org-f789"]

# Remove all journal lines id_maps (faulty or not), and all the corresponding journals
Entity::IdMap.where(:channel_id.in => channel_ids, :entity.in => ['Journal', 'Entity::Journal', 'JournalLine', 'Entity::Sub::JournalLine']).destroy_all
Entity::Journal.where(:channel_id.in => channel_ids).destroy_all

channel_ids.each do |channel_id|
  # Reset journals sync timestamps to force a full sync
  next unless c_sync = Entity::ConnectorSync.on_channel(channel_id).desc(:updated_at).first
  c_sync.metadata.merge!({
    "sync_manual_journals_timestamp" => nil,
    "sync_manual_journals_end_timestamp" => nil,
    "sync_journals_timestamp" => nil,
    "sync_journals_end_timestamp" => nil
  })
  c_sync.save

  # Ensures we can find an app_instance with valid oauth_keys...
  next unless c_sync.group_id && (app = MnoAppInstance.find_by_group_id(c_sync.group_id)) && app.oauth_keys_valid?

  # Sync the corresponding app_instance if necessary
  SyncJobWorker.perform_later({group_id: c_sync.group_id}) unless SyncJobWorker.enqueued?(c_sync.group_id)
end