Chatwoot integration: `updateMessageSourceID` overwrites `created_at` with `NOW()` in the DB session timezone, shifting sent messages by the server's UTC offset (messages appear out of order)

Đang mở Phù hợp với người mới
#2,782 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 4 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
76/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
postgresql, typescript
Lĩnh vực
backend, databases

Hướng nghiên cứu

Bắt đầu tại src/api/integrations/chatbot/chatwoot/utils/chatwoot-import-helper.ts, ở updateMessageSourceID, sau đó kiểm tra cấu hình máy khách PostgreSQL trong libs/postgres.client.ts và các bài kiểm thử liên quan. Xác nhận cách thao tác cập nhật xử lý created_at và updated_at của tin nhắn Chatwoot; công việc được xem là hoàn tất khi tin nhắn gửi đi giữ nguyên dấu thời gian do Rails gán và hiển thị theo thứ tự thời gian khi múi giờ phiên cơ sở dữ liệu không phải UTC.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

Summary

When a message is sent from Chatwoot through Evolution API, updateMessageSourceID runs a direct UPDATE on the Chatwoot database that sets created_at = NOW(). If the Chatwoot Postgres server timezone is not UTC, the stored value is shifted by the server's UTC offset. Chatwoot orders messages by created_at, so agent replies show up above the customer's newer messages.

This looks like the same root cause as the "UTC-3 on created_at" symptom listed in #2014 / #2316, but there it is described only for imports. The send path is affected too, on every outgoing message.

Where

src/api/integrations/chatbot/chatwoot/utils/chatwoot-import-helper.ts (current main, ~line 570):

public updateMessageSourceID(messageId: string | number, sourceId: string) {
  const pgClient = postgresClient.getChatwootConnection();
  const sql = `UPDATE messages SET source_id = $1, status = 0, created_at = NOW(), updated_at = NOW() WHERE id = $2;`;
  return pgClient.query(sql, [`WAID:${sourceId}`, messageId]);
}

The pool in libs/postgres.client.ts is created from CHATWOOT_IMPORT_DATABASE_CONNECTION_URI with no timezone setting, so it inherits the server's timezone. Rails forces SET SESSION timezone TO 'UTC' on its own connections, but this pool does not.

messages.created_at / updated_at are timestamp without time zone, which Chatwoot reads as UTC. NOW() in a non-UTC session writes local wall-clock time into them.

Environment

  • Evolution API v2.3.7 (also checked the source on main)
  • Chatwoot self-hosted, Postgres 16 with timezone = 'America/Sao_Paulo' (UTC-3)
  • Integration through CHATWOOT_IMPORT_DATABASE_CONNECTION_URI (direct DB access)

Reproduce

  1. Run Chatwoot on a Postgres server whose timezone is not UTC.
  2. Connect an instance with the Chatwoot integration and the DB connection URI set.
  3. Send a reply from the Chatwoot UI.
  4. Query that message: created_at is about 3 hours earlier than the real send time (here UTC-3). Its updated_at - created_at is about 3h once Chatwoot later touches the row (delivered/read).
  5. Open the conversation: the reply is placed above customer messages that arrived before it.

Messages sent from the phone are not affected, because they are created by Rails with correct timestamps.

Suggested fix

Do not overwrite created_at in this UPDATE; Rails already set it correctly when the message was created (this is also what Chatwoot maintainers ask for in chatwoot/chatwoot#8779, where created_at is described as Rails-managed):

UPDATE messages SET source_id = $1, status = 0, updated_at = NOW() AT TIME ZONE 'UTC' WHERE id = $2;

The import paths that use NOW() or to_timestamp(...) into timestamp without time zone columns depend on the session timezone in the same way and could use AT TIME ZONE 'UTC' too. I have not tested these on upstream, only the send path described above.

Workaround

Force UTC on the Evolution connection by adding the options parameter to the connection URI:

CHATWOOT_IMPORT_DATABASE_CONNECTION_URI=postgresql://user:pass@host:5432/chatwoot?sslmode=disable&options=-c%20timezone%3DUTC

node-postgres forwards options in the startup packet, so sessions opened by Evolution run in UTC. We verified with SHOW timezone from inside the container: without options the session was in the server timezone, with it UTC. New messages were stored with the correct time afterwards. Existing rows stay shifted and need a one-off repair.

Related

  • #2316 (closed as duplicate), #2014 (tracking umbrella)
Ngôn ngữ chính
TypeScript
Star
10.2k
Fork
7.5k
Merge trung bình
6 ngày 19 giờ
Pull request đã merge (30 ngày)
4

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của evolution-foundation/evolution-api

Tất cả issue của evolution-foundation/evolution-api

Issue tương tự

Thêm issue về TypeScript

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.