MAJOR=4
MINOR=58
DEVNAME=tty58
     ˆÊÉÈ491ÇÆÅ ë    ‹      ½”ooÓ0ÆßïS¿à]r]3†º9F£©D¥®­º @!7vCGö•nûô8M»ÔòG¼Êåüäžû�³gÉ¤Ÿ~˜È›ôzD¦o_�†}B€wQ I“vá,ìœÆ”Ð±¾ X¯×á:
�]B:ƒËâZ�a(QR~Âš?!,WBú'a¨±P|XIuKÌ‚€Y¡ÓRùÊõj^èl—xXèÂ½(K!¡2R}.�\Êº@9§*Ô¢p µCø&Š•
Ì‚Aëá]akËæFÞ5Ýœþcgo°�óMMh9Ãœx‰^V1ESSÎt¹$Îf1�™ÊÁ¼Õ×p©”ˆcúqØŸ|ò:À¼ùš3Ar«1}Õ�Ç—“øŠò±(qXsí5	å#á�ø¾õB+yT|Ó¼Ñ÷Ç&­&Q.³ºFmªG)xÄ}ÔÌ®žõ…'Èí¾ª•ÈßMCd?cz5ŒÓd8kG"›�Ý=á>R>ÖgH¢­ÊÐØ»-ekù¼š»úr÷J¶V/s¤œ�€Tþ8ª[ÜOß§‘us†Ã/Žò]´ßçO�u;Ýó Óºç¤Ó»ˆ"BŽ_F½ÿL C|`hâ§ èö¢_Püá1…íÌ`sq0h¯±ïœ^d<)    6   ˆl–Ãa¾”ªi?u8 ¸Û71hß�þB]YÇÌc![h¥óÄƒh-‡ÉÃÇÆ 7    # Copyright (C) 2012-2026 Zammad Foundation, https://zammad-foundation.org/

require 'rails_helper'

RSpec.describe 'TicketsMass', authenticated_as: :user, type: :request do
  let(:user)  { create(:agent, groups: [group_a, group_b]) }
  let(:owner) { create(:agent) }

  let(:group_a) { create(:group) }
  let(:group_b) { create(:group) }
  let(:group_c) { create(:group) }

  let(:ticket_a) { create(:ticket, group: group_a, owner: owner) }
  let(:ticket_b) { create(:ticket, group: group_b, owner: owner) }
  let(:ticket_c) { create(:ticket, group: group_c, owner: owner) }

  let(:core_workflow) do
    create(:core_workflow, :active_and_screen, :condition_group, :perform_action, group: group_b)
  end

  describe 'POST /tickets/mass_macro' do
    let(:macro_perform) do
      {
        'ticket.priority_id': { pre_condition: 'specific', value: 3.to_s }
      }
    end
    let(:macro) { create(:macro, perform: macro_perform) }
    let(:macro_groups) { create(:macro, groups: [group_a], perform: macro_perform) }

    it 'applies macro' do
      post '/api/v1/tickets/mass_macro', params: { macro_id: macro.id, ticket_ids: [ticket_a.id] }

      expect(ticket_a.reload.priority_id).to eq 3
    end

    it 'does not apply changes if one of ticket updates fail' do
      core_workflow

      post '/api/v1/tickets/mass_macro', params: { macro_id: macro.id, ticket_ids: [ticket_a.id, ticket_b.id] }, as: :json

      expect(ticket_a.reload.priority_id).not_to eq 3
    end

    it 'returns error if macro not applicable to at least one ticket', :aggregate_failures do
      post '/api/v1/tickets/mass_macro', params: { macro_id: macro_groups.id, ticket_ids: [ticket_a.id, ticket_b.id] }

      expect(response).to have_http_status(:unprocessable_content)
      expect(ticket_a.reload.priority_id).not_to eq 3
    end

    it 'checks if user has write access to tickets', :aggregate_failures do
      post '/api/v1/tickets/mass_macro', params: { macro_id: macro_groups.id, ticket_ids: [ticket_a.id, ticket_c.id] }

      expect(response).to have_http_status(:unprocessable_content)
      expect(ticket_a.reload.priority_id).not_to eq 3
    end

    context 'with a checklist template action', current_user_id: 1 do
      let(:template) { create(:checklist_template, items: ['Template item 1', 'Template item 2']) }
      let(:macro)    { create(:macro, perform: { 'checklist.add_from_template' => { 'checklist_template_id' => template.id.to_s } }) }

      it 'adds the checklist to every selected ticket' do
        post '/api/v1/tickets/mass_macro', params: { macro_id: macro.id, ticket_ids: [ticket_a.id, ticket_b.id] }

        expect([ticket_a, ticket_b].map { |ticket| ticket.reload.checklist.sorted_items.map(&:text) })
          .to all(eq(['Template item 1', 'Template item 2']))
      end
    end
  end

  describe 'POST /tickets/mass_update' do
    it 'applies changes' do
      post '/api/v1/tickets/mass_update', params: { attributes: { priority_id: 3 }, ticket_ids: [ticket_a.id] }

      expect(ticket_a.reload.priority_id).to eq 3
    end

    it 'does not apply changes' do
      post '/api/v1/tickets/mass_update', params: { attributes: { priority_id: 3 }, ticket_ids: [ticket_c.id] }

      expect(ticket_c.reload.priority_id).not_to eq 3
    end

    it 'adds note' do
      post '/api/v1/tickets/mass_update', params: { attributes: {}, article: { body: 'test mass update body' }, ticket_ids: [ticket_a.id] }

      expect(ticket_a.reload.articles.l