       0        0
  6   ·ˆl–Ðz¾”ªC]Šq@=q¦®41hß�þY�«8Ø�@	)\þß990ÞÇÍÜ Þ    ·# Copyright (C) 2012-2026 Zammad Foundation, https://zammad-foundation.org/

require 'rails_helper'

RSpec.describe Sequencer::Unit::Kayako::Connected, sequencer: :unit do

  context 'when checking the connection to Kayako' do

    let(:params) do
      {
        dry_run:    false,
        import_job: instance_double(ImportJob),
        field_map:  {},
        id_map:     {},
      }
    end

    let(:response_ok)           { Net::HTTPOK.new(1.0, '200', 'OK') }
    let(:response_unauthorized) { Net::HTTPUnauthorized.new(1.0, '401', 'Unauthorized') }

    it 'check for correct connection' do
      allow(described_class).to receive(:perform_request).with(any_args).and_return(response_ok)
      expect(process(params)).to eq({ connected: true })
    end

    it 'check for unauthorized connection' do
      allow(described_class).to receive(:perform_request).with(any_args).and_return(response_unauthorized)
      expect(process(params)).to eq({ connected: false })
    end
  end
end
  6   ¹ˆÈ�þ_}¥œp,Æ2%d­?ßƒM‹ÞÇa–ßi~” T¾R( 	ÅÍÆEq–ÔÅ£Ý 	”    ¹# Copyright (C) 2012-2026 Zammad Foundation, https://zammad-foundation.org/

# Create a list of grouped history records by given interval and issuer for a
# given object.
#
# An entry in the list contains the following information:
# - created_at: The timestamp when the group was created.
# - records:    The records that were grouped by the given interval.
#
# The created_at timestamp is the start of the interval rounded to seconds.
#
# A record contains the following information:
# - issuer: The issuer who created the record.
# - events: The events triggered by the issuer.
#
# For the events, see Service::History::List. (The issuer is removed in the
# event, to avoid redundancy.)
#
# Example:
# Service::History::Group.with_current_user(current_user).execute(object: ticket)
# # => [
# #      {
# #        created_at: ActiveRecord::DateTime,
# #        records:    [
# #          {
# #            issuer: User,
# #            events: [
# #              {
# #                created_at: ActiveRecord::DateTime,
# #                action:     'created',
# #                object:     Ticket,
# #                attribute:  nil,
# #                changes:    { from: nil, to: nil }
# #              },
# #            ]
# #          }
# #        ]
# #      }
# #    ]
class Service::History::Group < Service::Base
  requires_current_user!

  INTERVAL = 15.seconds

  attr_reader :object, :interval

  def initialize(object:, interval: INTERVAL)
    @object = object
    @interval = interval
  end

  def execute
    list = Service::History::List
      .with_current_user(current_user)
      .execute(object:)

    group_by_time_and_issuer(list, interval)
  end

  private

  # Group records by given interval and issuers
  def group_by_time_and_issuer(list, interval)
    start_time = list.first&.dig(:created_at).to_i

    list
      .group_by { |record| [(record[:created_at].to_i - start_time) / interval, record[:issuer]] }
      .map do |(_, issuer), records|
        {
          created_at: records.first[:created_at],
          issuer:     issuer,
          events:     records.map { |record| record.except(:issuer) }
        }
      end
      .group_by { |record| (record[:created_at].to_i - start_time) / interval }
      .map do |_, grouped_records|
        {
          created_at: grouped_records.first[:created_at],
          records:    grouped_records.map { |record| record.slice(:issuer, :events) }
        }
      end
  end
end
  8   »ˆl–Ãa¾”Je¶¥	@µq·.2ú˜´o�þE #Í”LˆŽ�æ] ?Ÿá…Í4ûŸà¿Þ ?÷     »ELF          >     œ      @       ðÈ         @ 8  @         @       @       @       Ø      Ø                                                                                        è�      è�                    �       �       �      �>     �>                   Ð      Ð      Ð     €Ð      €Ð                   P¬     P¼     P¼     ñ      X(                   à­     à½