live
  $   ˆÇ’þDè–l²„,�7  ´ÞÍ_óÆÅÄÃ324ÂÉÈ D    ‹      mQANÃ0<Ç¯0äTjcÔCD= ÔSå®³)F©vmD…úwì4méÑ³ã�™8y4Ú‹’±/…üüÂFÄ’Ñ|Î>ƒAÈÅüÄbÔ³ï½8«•_µªRëâ§Ó¹¸_í¶k×ÿ©÷bÌ=ýiŸJ2I*Íô„¨vçó<rÞÜ³s({Îà�·ká‚‘.””üÝû–¥½U)2 UÞ8«šÂáæ OgS9+ä-�žÒ]0‚1¶uUh €ïÖ¡§(V«Ó–äòùrÄXfjž'éy3Ÿs±\@,¦f> åµjJ–íü+÷?ÐSLºè$_Ž¯cgÝ2Å]}ú­³lµ±PõNŽV†³æt¾kßÙëY}E1TÉö%û^xr    7   ˆl–äY>” ”¾R( 	ÅíÆCq—Å£‘äÇò 8àF03#e‘ÿ?Çƒh¹ÄÊÉ      auto
  S   ˆl–Ãa¾”ÊC]Še@
ãB¸ÐTÅ£“þ]ë7(Û#’4ø†Ð›Ù«þÈÇÆÅ763Äa–äY>” ”¾R( 	ÅíÆCq—TÅ£Ë û    ‹      �TmOÛ0þž_qëH!
ûL×MŒuÒ: MBÈu.­×(Žl‡­‚þ÷�_òBhÙò!‰ÏwÏ�Ÿ{|£Z#h£7£I].~!7i†¹(ñJÉ
•ÙÄø§’ÊèF¨ç2«%ð<²¢ÆS0ªÆh;žDÁÕ"°º00…±:ßðBðsYm&ÑÉÑQG`J,Wb>†¯ŒãBÊu%O�•£�å¹(3¨S8+
¸¶®Q£zÄ,% ‹u»´¬Gà2C %åÃRcu™¡³B˜_Ü6fÈems”vÃB|»8Ÿ}¿™åÃ`%¥�L(bDªÈœ¬]"£m'QÄe©ÌæW·?éÀ%þ†41±‘×%7B–bÇZ²²›šb,•‹‚ñu!´9õP	ÙÖGm0Òl*¢9g…&žà„…:$û�l=gU<;‘ClÝ©^—ÞM§p(]gáù9X§d-k"•,Ÿë<G•
í|}@¡©UéÃ&dÙR.�¯#]1½;&ì/‰¿?ˆ?SŠm(¯ûî†xI]Ï±å¯¡£þ„×ø›Ñv_ëB°góu´Ëëå@�ñÎÔ¥à–¾h'|‚pÝ¸BÒxVDVës™‡TÄÊ)<m'-|†š+Q‘2­ºÐËßeso¿t.Š‹÷=ÑØ£­Õ9°5ntÜK3Ns©fŒ¯bÚ�éÇÀ±mcs¾VÂpp ¯ŒN&ÛvèìæŽî'aÛ÷Ï¯¶î=<¼0»‚{· sH{ÂÎ;å²Ã~9ÏáøŠ‡I»ûûgp�wÊ!8o_(±9v/-1�‹e­Ø¢°õÙÉë4ëüƒàw�q�:nûœôY£È½z÷÷�Ù÷¿Ôî\ß»U{4îðÇ©ƒRµ·coK,—feÅwß‰ß[)Wßi íPsS˜£‡1)Å¾ŸÐçCÀ¢ÿããFMÌ�¸­ vºÎïëûw};˜>m›B¨A¯ÀPL©    $   ˆÊ’þDùl²„,�7  ´ÞÍ_óÉÈÇÆ342Å¾Ë V    ‹      •‘AOÃ0…ÏÍ¯0‚)ÝÄÚ2B”rC‰ ÜBëv‘Öd¸É6„ößIÓu[¹qµ?¿g?sÛ 4†dnxÊØJ<¢yR®¢™C„_V†¼B3•}ƒ�wôÙ‹P>irðñlÈG‡Þh?ð¦�Ò±vïË-ØqÏ¨*3ÿ‹uÕžz5¾‹…Å$=D-oÇ07fÙÜÆñìza^‹ö"$%ŒÔJ,"MU|%ñiƒùÔh©
Ü0VëÂ.0ÂÍR“iœKiUÞNøc®ZŸ1ü°@–šï%ê|²,nUJ…÷P@h,)HRlYÐ"»ë½œsØÇ±“N;åuI§fæ¤× p}ü��ûí¡¶��O„û’6…ƒaïÔ%:ÐîýN!�óNaüÏ@¨Âm>ƒÉ®.a
ýR»LüHÊ¶)û¹�Ùœ    E   ˆl–Ãa¾”ªi?u8 ¸Û71hß�þYj1,ã�f1�­¸QùÊ„->Ç_�uÐb&=LyZËcõÀÍ 4²    # Copyright (C) 2012-2026 Zammad Foundation, https://zammad-foundation.org/

require 'rails_helper'

RSpec.describe Service::Ticket::Create, current_user_id: -> { user.id } do
  subject(:service_result) { described_class.with_current_user(user).execute(ticket_data:) }

  let(:user)     { create(:agent, groups: [group]) }
  let(:group)    { create(:group) }
  let(:customer) { create(:customer) }

  describe '#execute' do
    let(:sample_title) { Faker::Lorem.sentence }

    let(:ticket_data) do
      {
        title:    sample_title,
        group:    group,
        customer: customer
      }
    end

    it 'creates a ticket with given metadata' do
      expect(service_result)
        .to have_attributes(
          title:    sample_title,
          group:    group,
          customer: customer
        )
    end

    it 'creates a ticket with customer email address' do
      test_email = Faker::Internet.unique.email
      ticket_data[:customer] = { email: test_email }

      expect(service_result.customer).to have_attributes(
        email:    test_email,
        role_ids: Role.signup_role_ids
      )
    end

    it 'takes an existing customer by email address' do
      ticket_data[:customer] = { email: customer.email }

      expect(service_result.customer).to eq(customer)
    end

    context 'when the customer is given as a phone number' do
      let(:phone_number) { '+49 30 609812345' }

      before { ticket_data[:customer] = { phone: phone_number } }

      it 'creates a customer with the number' do
        expect(service_result.customer).to have_attributes(
          phone:    phone_number,
          email:    '',
          role_ids: Role.signup_role_ids
        )
      end

      it 'indexes the number as the caller ID of the new customer, so the next call finds them' do
        expect(Cti::CallerId.where(user_id: service_result.customer.id, level: 'known').pluck(:caller_id))
          .to include('4930609812345')
      end

      it 'takes an existing customer whose phone is the number, however written' do
        existing_customer = create(:customer, phone: '030 / 609812345')

        expect(service_result.customer).to eq(existing_customer)
      end

      it 'takes an existing customer whose mobile is the number' do
        existing_cus