* H-337: restore widget reply delivery * H-337: harden widget conversation ownership --------- Co-authored-by: Rogee <rogee@ipao.vip>
25 lines
948 B
SQL
25 lines
948 B
SQL
CREATE TABLE widget_conversation_contact_inbox_backfills (
|
|
conversation_id BIGINT PRIMARY KEY,
|
|
contact_inbox_id BIGINT NOT NULL
|
|
);
|
|
|
|
INSERT INTO widget_conversation_contact_inbox_backfills (conversation_id, contact_inbox_id)
|
|
SELECT conversations.id, MIN(contact_inboxes.id)
|
|
FROM conversations
|
|
JOIN inboxes ON inboxes.id = conversations.inbox_id
|
|
JOIN contact_inboxes
|
|
ON contact_inboxes.contact_id = conversations.contact_id
|
|
AND contact_inboxes.inbox_id = conversations.inbox_id
|
|
WHERE conversations.contact_inbox_id IS NULL
|
|
AND inboxes.channel_type IN ('web_widget', 'Channel::WebWidget')
|
|
GROUP BY conversations.id
|
|
HAVING COUNT(contact_inboxes.id) = 1;
|
|
|
|
UPDATE conversations
|
|
SET contact_inbox_id = (
|
|
SELECT backfills.contact_inbox_id
|
|
FROM widget_conversation_contact_inbox_backfills AS backfills
|
|
WHERE backfills.conversation_id = conversations.id
|
|
)
|
|
WHERE id IN (SELECT conversation_id FROM widget_conversation_contact_inbox_backfills);
|