2025-07-17 06:35:30 -07:00
|
|
|
-- name: GetExpiringSoonQuotes :many
|
2025-07-19 23:50:09 -07:00
|
|
|
WITH ranked_reminders AS (
|
|
|
|
|
SELECT
|
|
|
|
|
id,
|
|
|
|
|
quote_id,
|
|
|
|
|
reminder_type,
|
|
|
|
|
date_sent,
|
|
|
|
|
username,
|
|
|
|
|
ROW_NUMBER() OVER (
|
|
|
|
|
PARTITION BY quote_id
|
|
|
|
|
ORDER BY reminder_type DESC, date_sent DESC
|
|
|
|
|
) AS rn
|
|
|
|
|
FROM quote_reminders
|
|
|
|
|
),
|
|
|
|
|
latest_quote_reminder AS (
|
|
|
|
|
SELECT
|
|
|
|
|
id,
|
|
|
|
|
quote_id,
|
|
|
|
|
reminder_type,
|
|
|
|
|
date_sent,
|
|
|
|
|
username
|
|
|
|
|
FROM ranked_reminders
|
|
|
|
|
WHERE rn = 1
|
|
|
|
|
)
|
|
|
|
|
|
2025-07-17 06:35:30 -07:00
|
|
|
SELECT
|
|
|
|
|
d.id AS document_id,
|
|
|
|
|
u.username,
|
2025-12-03 04:59:57 -08:00
|
|
|
u.email as user_email,
|
2025-07-17 06:35:30 -07:00
|
|
|
e.id AS enquiry_id,
|
|
|
|
|
e.title as enquiry_ref,
|
2025-07-19 23:50:09 -07:00
|
|
|
uu.first_name as customer_name,
|
|
|
|
|
uu.email as customer_email,
|
2025-07-17 06:35:30 -07:00
|
|
|
q.date_issued,
|
|
|
|
|
q.valid_until,
|
2025-07-19 23:50:09 -07:00
|
|
|
COALESCE(lqr.reminder_type, 0) AS latest_reminder_type,
|
2025-12-06 21:53:59 -08:00
|
|
|
COALESCE(lqr.date_sent, CAST('1970-01-01 00:00:00' AS DATETIME)) AS latest_reminder_sent_time,
|
|
|
|
|
COALESCE(q.reminders_disabled, FALSE) AS reminders_disabled
|
2025-07-17 06:35:30 -07:00
|
|
|
|
|
|
|
|
FROM quotes q
|
|
|
|
|
JOIN documents d ON d.id = q.document_id
|
|
|
|
|
JOIN users u ON u.id = d.user_id
|
|
|
|
|
JOIN enquiries e ON e.id = q.enquiry_id
|
2025-07-19 23:50:09 -07:00
|
|
|
JOIN users uu ON uu.id = e.contact_user_id
|
2025-07-17 06:35:30 -07:00
|
|
|
|
2025-07-19 23:50:09 -07:00
|
|
|
LEFT JOIN latest_quote_reminder lqr on d.id = lqr.quote_id
|
2025-07-17 06:35:30 -07:00
|
|
|
|
|
|
|
|
WHERE
|
|
|
|
|
q.valid_until >= CURRENT_DATE
|
|
|
|
|
AND q.valid_until <= DATE_ADD(CURRENT_DATE, INTERVAL ? DAY)
|
|
|
|
|
AND e.status_id = 5
|
2025-12-06 17:44:30 -08:00
|
|
|
AND (q.reminders_disabled IS NULL OR q.reminders_disabled = FALSE)
|
2025-07-17 06:35:30 -07:00
|
|
|
|
|
|
|
|
ORDER BY q.valid_until;
|
|
|
|
|
|
|
|
|
|
-- name: GetExpiringSoonQuotesOnDay :many
|
2025-07-19 23:50:09 -07:00
|
|
|
WITH ranked_reminders AS (
|
|
|
|
|
SELECT
|
|
|
|
|
id,
|
|
|
|
|
quote_id,
|
|
|
|
|
reminder_type,
|
|
|
|
|
date_sent,
|
|
|
|
|
username,
|
|
|
|
|
ROW_NUMBER() OVER (
|
|
|
|
|
PARTITION BY quote_id
|
|
|
|
|
ORDER BY reminder_type DESC, date_sent DESC
|
|
|
|
|
) AS rn
|
|
|
|
|
FROM quote_reminders
|
|
|
|
|
),
|
|
|
|
|
latest_quote_reminder AS (
|
|
|
|
|
SELECT
|
|
|
|
|
id,
|
|
|
|
|
quote_id,
|
|
|
|
|
reminder_type,
|
|
|
|
|
date_sent,
|
|
|
|
|
username
|
|
|
|
|
FROM ranked_reminders
|
|
|
|
|
WHERE rn = 1
|
|
|
|
|
)
|
|
|
|
|
|
2025-07-17 06:35:30 -07:00
|
|
|
SELECT
|
|
|
|
|
d.id AS document_id,
|
|
|
|
|
u.username,
|
2025-12-03 04:59:57 -08:00
|
|
|
u.email as user_email,
|
2025-07-17 06:35:30 -07:00
|
|
|
e.id AS enquiry_id,
|
|
|
|
|
e.title as enquiry_ref,
|
2025-07-19 23:50:09 -07:00
|
|
|
uu.first_name as customer_name,
|
|
|
|
|
uu.email as customer_email,
|
2025-07-17 06:35:30 -07:00
|
|
|
q.date_issued,
|
|
|
|
|
q.valid_until,
|
2025-07-19 23:50:09 -07:00
|
|
|
COALESCE(lqr.reminder_type, 0) AS latest_reminder_type,
|
|
|
|
|
COALESCE(lqr.date_sent, CAST('1970-01-01 00:00:00' AS DATETIME)) AS latest_reminder_sent_time
|
2025-07-17 06:35:30 -07:00
|
|
|
|
|
|
|
|
FROM quotes q
|
|
|
|
|
JOIN documents d ON d.id = q.document_id
|
|
|
|
|
JOIN users u ON u.id = d.user_id
|
|
|
|
|
JOIN enquiries e ON e.id = q.enquiry_id
|
2025-07-19 23:50:09 -07:00
|
|
|
JOIN users uu ON uu.id = e.contact_user_id
|
2025-07-17 06:35:30 -07:00
|
|
|
|
2025-07-19 23:50:09 -07:00
|
|
|
LEFT JOIN latest_quote_reminder lqr on d.id = lqr.quote_id
|
2025-07-17 06:35:30 -07:00
|
|
|
|
|
|
|
|
WHERE
|
|
|
|
|
q.valid_until >= CURRENT_DATE
|
|
|
|
|
AND q.valid_until = DATE_ADD(CURRENT_DATE, INTERVAL ? DAY)
|
|
|
|
|
AND e.status_id = 5
|
2025-12-06 17:44:30 -08:00
|
|
|
AND (q.reminders_disabled IS NULL OR q.reminders_disabled = FALSE)
|
2025-07-17 06:35:30 -07:00
|
|
|
|
|
|
|
|
ORDER BY q.valid_until;
|
|
|
|
|
|
2025-07-14 06:26:26 -07:00
|
|
|
-- name: GetRecentlyExpiredQuotes :many
|
2025-07-19 23:50:09 -07:00
|
|
|
WITH ranked_reminders AS (
|
|
|
|
|
SELECT
|
|
|
|
|
id,
|
|
|
|
|
quote_id,
|
|
|
|
|
reminder_type,
|
|
|
|
|
date_sent,
|
|
|
|
|
username,
|
|
|
|
|
ROW_NUMBER() OVER (
|
|
|
|
|
PARTITION BY quote_id
|
|
|
|
|
ORDER BY reminder_type DESC, date_sent DESC
|
|
|
|
|
) AS rn
|
|
|
|
|
FROM quote_reminders
|
|
|
|
|
),
|
|
|
|
|
latest_quote_reminder AS (
|
|
|
|
|
SELECT
|
|
|
|
|
id,
|
|
|
|
|
quote_id,
|
|
|
|
|
reminder_type,
|
|
|
|
|
date_sent,
|
|
|
|
|
username
|
|
|
|
|
FROM ranked_reminders
|
|
|
|
|
WHERE rn = 1
|
|
|
|
|
)
|
|
|
|
|
|
2025-07-17 06:35:30 -07:00
|
|
|
SELECT
|
|
|
|
|
d.id AS document_id,
|
|
|
|
|
u.username,
|
2025-12-03 04:59:57 -08:00
|
|
|
u.email as user_email,
|
2025-07-17 06:35:30 -07:00
|
|
|
e.id AS enquiry_id,
|
|
|
|
|
e.title as enquiry_ref,
|
2025-07-19 23:50:09 -07:00
|
|
|
uu.first_name as customer_name,
|
|
|
|
|
uu.email as customer_email,
|
2025-07-17 06:35:30 -07:00
|
|
|
q.date_issued,
|
|
|
|
|
q.valid_until,
|
2025-07-19 23:50:09 -07:00
|
|
|
COALESCE(lqr.reminder_type, 0) AS latest_reminder_type,
|
|
|
|
|
COALESCE(lqr.date_sent, CAST('1970-01-01 00:00:00' AS DATETIME)) AS latest_reminder_sent_time
|
2025-07-17 06:35:30 -07:00
|
|
|
|
2025-07-14 06:26:26 -07:00
|
|
|
FROM quotes q
|
2025-07-17 06:35:30 -07:00
|
|
|
JOIN documents d ON d.id = q.document_id
|
|
|
|
|
JOIN users u ON u.id = d.user_id
|
|
|
|
|
JOIN enquiries e ON e.id = q.enquiry_id
|
2025-07-19 23:50:09 -07:00
|
|
|
JOIN users uu ON uu.id = e.contact_user_id
|
2025-07-17 06:35:30 -07:00
|
|
|
|
2025-07-19 23:50:09 -07:00
|
|
|
LEFT JOIN latest_quote_reminder lqr on d.id = lqr.quote_id
|
2025-07-17 06:35:30 -07:00
|
|
|
|
|
|
|
|
WHERE
|
|
|
|
|
q.valid_until < CURRENT_DATE
|
|
|
|
|
AND valid_until >= DATE_SUB(CURRENT_DATE, INTERVAL ? DAY)
|
|
|
|
|
AND e.status_id = 5
|
2025-12-06 17:44:30 -08:00
|
|
|
AND (q.reminders_disabled IS NULL OR q.reminders_disabled = FALSE)
|
2025-07-17 06:35:30 -07:00
|
|
|
|
|
|
|
|
ORDER BY q.valid_until DESC;
|
|
|
|
|
|
|
|
|
|
-- name: GetRecentlyExpiredQuotesOnDay :many
|
2025-07-19 23:50:09 -07:00
|
|
|
WITH ranked_reminders AS (
|
|
|
|
|
SELECT
|
|
|
|
|
id,
|
|
|
|
|
quote_id,
|
|
|
|
|
reminder_type,
|
|
|
|
|
date_sent,
|
|
|
|
|
username,
|
|
|
|
|
ROW_NUMBER() OVER (
|
|
|
|
|
PARTITION BY quote_id
|
|
|
|
|
ORDER BY reminder_type DESC, date_sent DESC
|
|
|
|
|
) AS rn
|
|
|
|
|
FROM quote_reminders
|
|
|
|
|
),
|
|
|
|
|
latest_quote_reminder AS (
|
|
|
|
|
SELECT
|
|
|
|
|
id,
|
|
|
|
|
quote_id,
|
|
|
|
|
reminder_type,
|
|
|
|
|
date_sent,
|
|
|
|
|
username
|
|
|
|
|
FROM ranked_reminders
|
|
|
|
|
WHERE rn = 1
|
|
|
|
|
)
|
|
|
|
|
|
2025-07-17 06:35:30 -07:00
|
|
|
SELECT
|
|
|
|
|
d.id AS document_id,
|
|
|
|
|
u.username,
|
2025-12-03 04:59:57 -08:00
|
|
|
u.email as user_email,
|
2025-07-17 06:35:30 -07:00
|
|
|
e.id AS enquiry_id,
|
|
|
|
|
e.title as enquiry_ref,
|
2025-07-19 23:50:09 -07:00
|
|
|
uu.first_name as customer_name,
|
|
|
|
|
uu.email as customer_email,
|
2025-07-17 06:35:30 -07:00
|
|
|
q.date_issued,
|
|
|
|
|
q.valid_until,
|
2025-07-19 23:50:09 -07:00
|
|
|
COALESCE(lqr.reminder_type, 0) AS latest_reminder_type,
|
|
|
|
|
COALESCE(lqr.date_sent, CAST('1970-01-01 00:00:00' AS DATETIME)) AS latest_reminder_sent_time
|
2025-07-14 06:26:26 -07:00
|
|
|
|
|
|
|
|
FROM quotes q
|
2025-07-17 06:35:30 -07:00
|
|
|
JOIN documents d ON d.id = q.document_id
|
|
|
|
|
JOIN users u ON u.id = d.user_id
|
|
|
|
|
JOIN enquiries e ON e.id = q.enquiry_id
|
2025-07-19 23:50:09 -07:00
|
|
|
JOIN users uu ON uu.id = e.contact_user_id
|
2025-07-17 06:35:30 -07:00
|
|
|
|
2025-07-19 23:50:09 -07:00
|
|
|
LEFT JOIN latest_quote_reminder lqr on d.id = lqr.quote_id
|
2025-07-17 06:35:30 -07:00
|
|
|
|
|
|
|
|
WHERE
|
|
|
|
|
q.valid_until < CURRENT_DATE
|
|
|
|
|
AND valid_until = DATE_SUB(CURRENT_DATE, INTERVAL ? DAY)
|
|
|
|
|
AND e.status_id = 5
|
2025-12-06 17:44:30 -08:00
|
|
|
AND (q.reminders_disabled IS NULL OR q.reminders_disabled = FALSE)
|
2025-07-17 06:35:30 -07:00
|
|
|
|
|
|
|
|
ORDER BY q.valid_until DESC;
|
|
|
|
|
|
|
|
|
|
-- name: GetQuoteRemindersByType :many
|
|
|
|
|
SELECT id, quote_id, reminder_type, date_sent, username
|
|
|
|
|
FROM quote_reminders
|
|
|
|
|
WHERE quote_id = ? AND reminder_type = ?
|
|
|
|
|
ORDER BY date_sent;
|
|
|
|
|
|
|
|
|
|
-- name: InsertQuoteReminder :execresult
|
|
|
|
|
INSERT INTO quote_reminders (quote_id, reminder_type, date_sent, username)
|
2025-12-06 17:44:30 -08:00
|
|
|
VALUES (?, ?, ?, ?);
|
|
|
|
|
|
|
|
|
|
-- name: DisableQuoteReminders :execresult
|
|
|
|
|
UPDATE quotes
|
|
|
|
|
SET reminders_disabled = TRUE,
|
|
|
|
|
reminders_disabled_at = NOW(),
|
|
|
|
|
reminders_disabled_by = ?
|
|
|
|
|
WHERE id = ?;
|
|
|
|
|
|
2025-12-06 21:53:59 -08:00
|
|
|
-- name: EnableQuoteReminders :execresult
|
|
|
|
|
UPDATE quotes
|
|
|
|
|
SET reminders_disabled = FALSE,
|
|
|
|
|
reminders_disabled_at = NULL,
|
|
|
|
|
reminders_disabled_by = NULL
|
|
|
|
|
WHERE id = ?;
|
|
|
|
|
|
2025-12-06 17:44:30 -08:00
|
|
|
-- name: GetQuoteRemindersDisabled :one
|
|
|
|
|
SELECT reminders_disabled, reminders_disabled_at, reminders_disabled_by
|
|
|
|
|
FROM quotes
|
|
|
|
|
WHERE id = ?;
|