whatsapp.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { EnumChannelArea } from '#shared/graphql/types.ts'
  3. import { i18n } from '#shared/i18n.ts'
  4. import type { TicketChannelPlugin } from './types.ts'
  5. import type { TicketById } from '../../types.ts'
  6. const ticketChannelPlugin: TicketChannelPlugin = {
  7. area: EnumChannelArea.WhatsAppBusiness,
  8. channelAlert(ticket: TicketById) {
  9. const lastWhatsappTimestamp =
  10. ticket.preferences?.whatsapp?.timestamp_incoming
  11. // In case the customer service window is not open yet, or the ticket is closed, hide the alert.
  12. if (
  13. !lastWhatsappTimestamp ||
  14. /^(closed|merged|removed)$/.test(ticket.state.name)
  15. )
  16. return null
  17. // Determine the end of the customer service window and set the appropriate alert text and type.
  18. const timeWindowEnd = new Date(lastWhatsappTimestamp * 1000)
  19. timeWindowEnd.setHours(timeWindowEnd.getHours() + 24)
  20. // If time window is already closed, show an error alert.
  21. if (timeWindowEnd <= new Date()) {
  22. return {
  23. text: __(
  24. 'The 24 hour customer service window is now closed, no further WhatsApp messages can be sent.',
  25. ),
  26. variant: 'danger',
  27. }
  28. }
  29. // Otherwise, show a warning alert with a "humanized" end time of the window.
  30. return {
  31. text: __(
  32. 'You have a 24 hour window to send WhatsApp messages in this conversation. The customer service window closes %s.',
  33. ),
  34. textPlaceholder: i18n.relativeDateTime(timeWindowEnd.toString()),
  35. variant: 'warning',
  36. }
  37. },
  38. }
  39. export default ticketChannelPlugin