Metadata-Version: 2.5
Name: trytond_ticket_hub_zammad
Version: 8.0.0.3
Summary: Ticket Hub Zammad
Author: Clemens Hupka
Maintainer-email: Clemens Hupka <clemens.hupka@backend.at>
License-Expression: GPL-3.0-or-later
License-File: COPYRIGHT
License-File: LICENSE
Keywords: helpdesk,ticket,tryton,zammad
Classifier: Development Status :: 2 - Pre-Alpha
Classifier: Environment :: Plugins
Classifier: Framework :: Tryton
Classifier: Topic :: Office/Business
Classifier: Topic :: Office/Business :: Groupware
Requires-Python: >=3.10
Requires-Dist: requests
Requires-Dist: trytond-company<8.1,>=8.0
Requires-Dist: trytond-party<8.1,>=8.0
Requires-Dist: trytond-ticket-hub<8.1,>=8.0
Requires-Dist: trytond<8.1,>=8.0
Provides-Extra: test
Description-Content-Type: text/x-rst

Ticket Hub Zammad
=================

The ``ticket_hub_zammad`` module is the Zammad leg of the Ticket Hub
flywheel: it connects the platform-neutral ``ticket_hub`` base to a
Zammad instance.

It provides the Zammad REST API client — ``ZammadClient``, an
authenticated ``requests`` session with token authentication, timeouts,
pagination and failures typed for the retry framework of the base
module — and the initial ticket import of the connection.

The import is offered by the *Import Tickets* button of a Zammad
connection: it walks the Zammad tickets (and their articles) page by
page, creates or updates the matching ``ticket_hub.ticket`` and appends
the articles to the ``ticket_hub.message`` journal.  The state is
translated through the state mappings (with the configurable fall-back
of the connection) and the tags through the tag mappings; the Zammad
customer is matched to a ``party.party`` by email, and an unmatched
customer is resolved by the ``requester_policy`` of the connection
(create a party, reuse a shared generic party, or leave the requester
empty).

Every run records its outcome in the ``ticket_hub.sync.log`` and stores
the identifier of the last imported ticket on the connection, so an
interrupted or repeated import resumes where it left off.

Bidirectional synchronization
-----------------------------

Beyond the initial import the module keeps a Zammad ticket and its
Tryton ticket aligned in both directions.  A local state transition of
a synchronized ticket is pushed back to Zammad as it happens: the state
is translated through the ``sync_back`` mappings of the connection, and
a divergence the platform reports on the next import is journaled on
the ticket timeline under a documented conflict policy (the newer
platform revision wins, a local-only change keeps its pending
write-back).  A comment marked *publish externally* becomes a new
Zammad article, and the identifier Zammad assigns is stamped on the
message so it is never published twice; a failed write-back is recorded
in the sync log without rolling the local change back.

The *Synchronize* button of a Zammad connection runs the
synchronization on demand through the sync adapter of the base module:
an incremental inbound leg reads the tickets modified since the stored
cursor and an outbound leg publishes the pending comments.  The wizard
reports the counters of both legs, or the error that prevented the run.

Webhook receiver
----------------

Zammad can push its ticket events to Tryton instead of waiting for the
periodic synchronization.  The receiver registers a Zammad handler with
the webhook framework of the base module: a delivery is parsed, the
ticket it concerns is imported through the same engine as the initial
import, and a repeated delivery never duplicates a ticket or a journal
message.  A delivery that identifies no ticket at all is queued: the
connection is marked due and the periodic incremental synchronization
imports the changed tickets on its next run.

A delivery that fails while the ticket is fetched or imported is
recorded in the sync log as a retryable failure and retried by the
cron with the exponential backoff of the framework, so a temporary
Zammad outage does not lose the event.  Every raw payload is stored on
its log entry, and the *Replay* button of a webhook entry hands it to
the handler again, so a delivery can be replayed after the cause of a
failure was fixed.

Zammad-side setup
~~~~~~~~~~~~~~~~~

1. In Zammad, open *Admin > Manage > Webhook* (or the *Webhook*
   trigger of an older version) and create a trigger.
2. Set the endpoint URL to the webhook URL of the connection::

       https://<tryton-host>/<database_name>/ticket_hub/webhook/<token>

   The ``<token>`` is the *Webhook Token* of a Zammad
   ``ticket_hub.connection``, visible to the Ticket Hub administration
   on the connection form.  The ``<database_name>`` is the name of the
   Tryton database the connection lives in.
3. Add the authentication header the receiver validates the delivery
   with::

       X-Webhook-Token: <token>

   The alternative ``X-Zammad-Token`` header is accepted as well.  An
   unknown token, an inactive connection and a wrong header all answer
   the same bare ``404``, so the endpoint never reveals whether a token
   exists.
4. Select the events to subscribe: *ticket created*, *ticket updated*
   and *article created*.  The receiver understands the ``event`` value
   of the payload (``ticket.created``, ``ticket.updated`` and
   ``article.created``); it is not required for processing, but it feeds
   the idempotency key of a delivery.
5. Set the payload template so that it carries the ticket identifier,
   for example::

       {
         "event": "ticket.updated",
         "ticket": {"id": "#{ticket.id}"}
       }

   The receiver reads the identifier from the embedded ``ticket``
   object or from a flat ``ticket_id`` field.  When the template already
   carries the full ticket record it is imported directly; when it only
   carries the identifier the full record is fetched from Zammad before
   the import, so the minimal template above is enough.

The webhook URL contains a secret: rotating the token with the
*Regenerate Webhook Token* button of the connection revokes the previous
URL.  Rate limiting is delegated to the reverse proxy in front of the
Tryton server, like for the base webhook endpoint.

Dependencies
------------

- Tryton modules: ``ir``, ``res``, ``company``, ``party``, ``ticket_hub``.
- Python packages: ``requests``.
