leviroiz.dev Back to tracking
PT

Side A · System 02 · In productionPython, Django, TOTVS and J&T Express

Tracking and notifications

I integrated J&T events into Kallifon's tracking system and built two email workflows: delivery notification and electronic invoice delivery.

See email examples ↓
A road at dusk and a lit window representing delivery and the email notification.

Email examples

Synthetic data

Subject

Your order is out for delivery

Hello! The carrier marked your order as out for delivery: DEMO-1042.

Latest stageOut for delivery

Subject

Electronic invoice for order DEMO-1042

Hello! Your authorized electronic invoice is attached.

Sample attachmentnota-fiscal-exemplo.pdf

393 emails recorded as sent

276 delivery notifications · 117 electronic invoice emails

Checked on

Read the electronic invoicing case study ↗

What if the email fails?

Tracking continues. The queue holds the notification for another attempt.

Order DEMO-1042Simulation · synthetic data

  1. Event received
  2. Send attempt
  3. Send failed
  4. Retry
  5. Email sent
Email sent

The notification was sent successfully on the second attempt.

The story

1 record per order and stagethe queue skips duplicate records when a sync repeats an event

The team already had a tracking system. After the carrier integration, a stage could arrive more than once. Each stage had to generate one notification, and tracking had to stay available if the mail server went down.

Each stage change adds a row to the queue, at most one per order and stage, enforced by the database. A separate process consumes the queue and records each send attempt.

Test the queue manually

Tracking continues even when email fails.

Sample order. Advance one stage to see the notification enter the queue.

  1. Linked
  2. Preparing
  3. In transit
  4. Picking
  5. Out for delivery
  6. Delivered

Email queue · order DEMO-4821

StageStatusAttempts

Log

    Under the hoodconstraints, steps and details

    What could go wrong

    • A repeated sync sends a second email for the same stage.
    • A mail server outage stops or delays tracking.
    • Orders already in transit receive a flood of historical emails.

    How the queue works

    1. Sync: tracking runs on its own schedule.
    2. Detect stage changes: only an actual change counts.
    3. Queue one row per order and stage.
    4. Process the queue in batches.
    5. Send using the email address from TOTVS.
    6. Record the outcome and the number of attempts.

    The TOTVS API provides the default email address or, if none is marked, the first populated address. For older orders, a command prepares the recipient without queuing historical emails. Each message gets up to five attempts. Further retries require an administrator. Sending starts disabled and is enabled only after the mail server has been validated.

    Next trackPrice audit