123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
- class CalendarSubscriptions::Tickets
- ALLOWED_METHODS = %w[all new_open pending escalation].freeze
- def initialize(user, preferences, time_zone)
- @user = user
- @preferences = preferences
- @time_zone = time_zone
- @translations = {}
- end
- def all
- return [] if @preferences.blank?
- new_open + pending + escalation
- end
- def alarm?
- !!@preferences[:alarm]
- end
- def owner_ids(method)
- output = []
- output << @user.id if @preferences.dig(method, :own)
- output << 1 if @preferences.dig(method, :not_assigned)
- output
- end
- def new_open
- owner_ids = owner_ids(:new_open)
- return [] if owner_ids.blank?
- fetch_tickets(owner_ids, state_names: %w[new open]).filter_map do |ticket|
- event_data_with_date(
- summary: base_summary(ticket),
- date: Time.zone.today,
- type: 'new_open',
- ticket: ticket,
- )
- end
- end
- def pending
- owner_ids = owner_ids(:pending)
- return [] if owner_ids.blank?
- fetch_tickets(owner_ids, state_names: ['pending reminder', 'pending action']).filter_map do |ticket|
- next if !ticket.pending_time
- pending_time = [ticket.pending_time, Time.zone.today].max
- event_data_with_time(
- summary: pending_summary(ticket),
- time: pending_time,
- type: 'pending',
- ticket: ticket
- )
- end
- end
- def escalation
- owner_ids = owner_ids(:escalation)
- return [] if owner_ids.blank?
- fetch_tickets(owner_ids, escalation_check: true).filter_map do |ticket|
- next if !ticket.escalation_at
- escalation_at = [ticket.escalation_at, Time.zone.today].max
- event_data_with_time(
- summary: escalated_summary(ticket),
- time: escalation_at,
- type: 'escalated',
- ticket: ticket
- )
- end
- end
- private
- def generate_conditions(owner_ids, state_names: nil, escalation_check: false)
- output = {
- 'ticket.owner_id' => {
- operator: 'is',
- value: owner_ids,
- },
- }
- if state_names
- output['ticket.state_id'] = {
- operator: 'is',
- value: Ticket::State.where(
- state_type_id: Ticket::StateType.where(
- name: state_names,
- ),
- ).map(&:id),
- }
- end
- if escalation_check
- output['ticket.escalation_at'] = {
- operator: 'is not',
- value: nil,
- }
- end
- output
- end
- def fetch_tickets(...)
- condition = generate_conditions(...)
- Ticket.search(
- current_user: @user,
- condition: condition,
- )
- end
- def translate_to_user(input)
- return @translations[input] if @translations[input]
- @translations[input] = Translation.translate(@user.locale, input)
- end
- def base_event_data(summary:, timestamp:, type:, ticket:)
- {
- dtstart: timestamp,
- dtend: timestamp,
- summary: summary,
- description: "T##{ticket.number}",
- type: type
- }
- end
- def event_data_with_date(date:, summary:, type:, ticket:)
- timestamp = Icalendar::Values::Date.new(date, 'tzid' => @time_zone)
- base_event_data(
- summary:,
- type:,
- ticket:,
- timestamp:,
- )
- end
- def event_data_with_time(time:, summary:, type:, ticket:)
- timestamp = Icalendar::Values::DateTime.new(time, 'tzid' => @time_zone)
- output = base_event_data(
- summary:,
- type:,
- ticket:,
- timestamp:,
- )
- if alarm?
- output[:alarm] = {
- summary: summary,
- trigger: '-PT1M',
- }
- end
- output
- end
- def base_summary(ticket)
- translated_state = translate_to_user(ticket.state.name)
- translated_ticket = translate_to_user('ticket')
- "#{translated_state} #{translated_ticket}: '#{ticket.title}'"
- end
- def pending_summary(ticket)
- translated_customer = translate_to_user('customer')
- "#{base_summary(ticket)} #{translated_customer}: #{ticket.customer.longname}"
- end
- def escalated_summary(ticket)
- translated_ticket_escalation = translate_to_user('ticket escalation')
- translated_customer = translate_to_user('customer')
- "#{translated_ticket_escalation}: '#{ticket.title}' #{translated_customer}: #{ticket.customer.longname}"
- end
- end
|