152 lines
5.5 KiB
SQL
152 lines
5.5 KiB
SQL
-- name: InsertInboundEvent :one
|
|
INSERT INTO inbound_events (
|
|
account_id, swt_sid, seq_id, kind, swt_event_key,
|
|
event_subtype, op_name, text, swt_timestamp, raw_line,
|
|
normalized_payload, mapping_strategy, delivery_status
|
|
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
ON CONFLICT(swt_event_key) DO NOTHING
|
|
RETURNING *;
|
|
|
|
-- name: ClaimInboundEvent :one
|
|
UPDATE inbound_events SET
|
|
delivery_status = 'delivering',
|
|
attempts = attempts + 1,
|
|
updated_at = CURRENT_TIMESTAMP
|
|
WHERE id = (
|
|
SELECT candidate.id
|
|
FROM inbound_events AS candidate
|
|
WHERE candidate.delivery_status = 'pending'
|
|
AND (candidate.next_attempt_at IS NULL OR julianday(candidate.next_attempt_at) IS NULL OR julianday(candidate.next_attempt_at) <= julianday('now'))
|
|
AND NOT EXISTS (
|
|
SELECT 1 FROM inbound_events AS earlier
|
|
WHERE earlier.account_id = candidate.account_id
|
|
AND earlier.swt_sid = candidate.swt_sid
|
|
AND earlier.id < candidate.id
|
|
AND earlier.delivery_status IN ('pending', 'delivering')
|
|
AND NOT (earlier.kind IN (24, 52) AND candidate.kind NOT IN (24, 52))
|
|
)
|
|
ORDER BY candidate.id
|
|
LIMIT 1
|
|
)
|
|
AND delivery_status = 'pending'
|
|
RETURNING *;
|
|
|
|
-- name: GetInboundEventByKey :one
|
|
SELECT * FROM inbound_events
|
|
WHERE swt_event_key = ?
|
|
LIMIT 1;
|
|
|
|
-- name: CompleteInboundEvent :exec
|
|
UPDATE inbound_events SET
|
|
delivery_status = 'delivered',
|
|
gochat_message_id = ?,
|
|
last_error = NULL,
|
|
updated_at = CURRENT_TIMESTAMP
|
|
WHERE id = ? AND delivery_status = 'delivering';
|
|
|
|
-- name: CompleteInboundEventWithMapping :exec
|
|
UPDATE inbound_events SET
|
|
delivery_status = 'delivered',
|
|
gochat_message_id = sqlc.narg(gochat_message_id),
|
|
event_subtype = sqlc.narg(event_subtype),
|
|
normalized_payload = sqlc.narg(normalized_payload),
|
|
mapping_strategy = sqlc.arg(mapping_strategy),
|
|
last_error = NULL,
|
|
updated_at = CURRENT_TIMESTAMP
|
|
WHERE id = sqlc.arg(id) AND delivery_status = 'delivering';
|
|
|
|
-- name: RetryInboundEvent :exec
|
|
UPDATE inbound_events SET
|
|
delivery_status = 'pending',
|
|
next_attempt_at = ?,
|
|
last_error = ?,
|
|
updated_at = CURRENT_TIMESTAMP
|
|
WHERE id = ? AND delivery_status = 'delivering';
|
|
|
|
-- name: RecoverInboundDeliveries :execrows
|
|
UPDATE inbound_events SET
|
|
delivery_status = 'pending',
|
|
updated_at = CURRENT_TIMESTAMP
|
|
WHERE delivery_status = 'delivering';
|
|
|
|
-- name: FailInboundEvent :exec
|
|
UPDATE inbound_events SET
|
|
delivery_status = 'failed',
|
|
mapping_strategy = sqlc.narg(mapping_strategy),
|
|
last_error = sqlc.arg(last_error),
|
|
updated_at = CURRENT_TIMESTAMP
|
|
WHERE id = sqlc.arg(id) AND delivery_status = 'delivering';
|
|
|
|
-- name: GetConversationMap :one
|
|
SELECT * FROM conversation_maps
|
|
WHERE account_id = ? AND swt_sid = ?
|
|
LIMIT 1;
|
|
|
|
-- name: GetLatestConversationState :one
|
|
SELECT text FROM inbound_events
|
|
WHERE account_id = ? AND swt_sid = ? AND kind = 0
|
|
ORDER BY id DESC
|
|
LIMIT 1;
|
|
|
|
-- name: GetLatestXSTOperator :one
|
|
SELECT text, seq_id FROM inbound_events
|
|
WHERE account_id = ? AND swt_sid = ? AND kind = 6
|
|
ORDER BY id DESC
|
|
LIMIT 1;
|
|
|
|
-- name: UpsertConversationXSTRoute :one
|
|
INSERT INTO conversation_maps (
|
|
account_id, swt_sid, gochat_contact_source_id,
|
|
xst_required, xst_token, xst_cid, xst_kfname
|
|
) VALUES (?, ?, ?, ?, ?, ?, ?)
|
|
ON CONFLICT(account_id, swt_sid) DO UPDATE SET
|
|
xst_required = CASE WHEN excluded.xst_required = 1 THEN 1 ELSE conversation_maps.xst_required END,
|
|
xst_token = COALESCE(excluded.xst_token, conversation_maps.xst_token),
|
|
xst_cid = COALESCE(excluded.xst_cid, conversation_maps.xst_cid),
|
|
xst_kfname = COALESCE(excluded.xst_kfname, conversation_maps.xst_kfname),
|
|
updated_at = CURRENT_TIMESTAMP
|
|
RETURNING *;
|
|
|
|
-- name: MarkConversationXSTSynced :execrows
|
|
UPDATE conversation_maps SET
|
|
xst_sync_key = ?,
|
|
updated_at = CURRENT_TIMESTAMP
|
|
WHERE account_id = ? AND swt_sid = ? AND xst_token = ?;
|
|
|
|
-- name: UpsertConversationMap :one
|
|
INSERT INTO conversation_maps (
|
|
account_id, swt_sid, gochat_contact_source_id, gochat_contact_id,
|
|
gochat_conversation_id, gochat_display_id, swt_assignee_name
|
|
) VALUES (?, ?, ?, ?, ?, ?, ?)
|
|
ON CONFLICT(account_id, swt_sid) DO UPDATE SET
|
|
gochat_contact_source_id = excluded.gochat_contact_source_id,
|
|
gochat_contact_id = COALESCE(excluded.gochat_contact_id, conversation_maps.gochat_contact_id),
|
|
gochat_conversation_id = COALESCE(excluded.gochat_conversation_id, conversation_maps.gochat_conversation_id),
|
|
gochat_display_id = COALESCE(excluded.gochat_display_id, conversation_maps.gochat_display_id),
|
|
swt_assignee_name = COALESCE(excluded.swt_assignee_name, conversation_maps.swt_assignee_name),
|
|
updated_at = CURRENT_TIMESTAMP
|
|
RETURNING *;
|
|
|
|
-- name: InsertMessageMap :exec
|
|
INSERT INTO message_maps (
|
|
account_id, swt_sid, swt_message_id, swt_seq_id, kind, child_index,
|
|
direction, gochat_message_id, gochat_source_id, content_fingerprint
|
|
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
ON CONFLICT(account_id, swt_sid, kind, swt_seq_id, child_index) DO UPDATE SET
|
|
swt_message_id = COALESCE(excluded.swt_message_id, message_maps.swt_message_id),
|
|
gochat_message_id = excluded.gochat_message_id,
|
|
gochat_source_id = COALESCE(excluded.gochat_source_id, message_maps.gochat_source_id),
|
|
content_fingerprint = COALESCE(excluded.content_fingerprint, message_maps.content_fingerprint),
|
|
updated_at = CURRENT_TIMESTAMP;
|
|
|
|
-- name: GetMessageMapBySWTMessageID :one
|
|
SELECT * FROM message_maps
|
|
WHERE account_id = ? AND swt_sid = ? AND swt_message_id = ?
|
|
LIMIT 1;
|
|
|
|
-- name: MarkMessageMapRetracted :exec
|
|
UPDATE message_maps SET
|
|
retracted_at = CURRENT_TIMESTAMP,
|
|
updated_at = CURRENT_TIMESTAMP
|
|
WHERE account_id = ? AND swt_sid = ? AND swt_message_id = ?;
|