cmc-sales/go/internal/cmc/db/quotes.sql.go

475 lines
12 KiB
Go

// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.29.0
// source: quotes.sql
package db
import (
"context"
"database/sql"
"time"
)
const getExpiringSoonQuotes = `-- name: GetExpiringSoonQuotes :many
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
)
SELECT
d.id AS document_id,
u.username,
e.id AS enquiry_id,
e.title as enquiry_ref,
uu.first_name as customer_name,
uu.email as customer_email,
q.date_issued,
q.valid_until,
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
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
JOIN users uu ON uu.id = e.contact_user_id
LEFT JOIN latest_quote_reminder lqr on d.id = lqr.quote_id
WHERE
q.valid_until >= CURRENT_DATE
AND q.valid_until <= DATE_ADD(CURRENT_DATE, INTERVAL ? DAY)
AND e.status_id = 5
ORDER BY q.valid_until
`
type GetExpiringSoonQuotesRow struct {
DocumentID int32 `json:"document_id"`
Username string `json:"username"`
EnquiryID int32 `json:"enquiry_id"`
EnquiryRef string `json:"enquiry_ref"`
CustomerName string `json:"customer_name"`
CustomerEmail string `json:"customer_email"`
DateIssued time.Time `json:"date_issued"`
ValidUntil time.Time `json:"valid_until"`
LatestReminderType int32 `json:"latest_reminder_type"`
LatestReminderSentTime time.Time `json:"latest_reminder_sent_time"`
}
func (q *Queries) GetExpiringSoonQuotes(ctx context.Context, dateADD interface{}) ([]GetExpiringSoonQuotesRow, error) {
rows, err := q.db.QueryContext(ctx, getExpiringSoonQuotes, dateADD)
if err != nil {
return nil, err
}
defer rows.Close()
items := []GetExpiringSoonQuotesRow{}
for rows.Next() {
var i GetExpiringSoonQuotesRow
if err := rows.Scan(
&i.DocumentID,
&i.Username,
&i.EnquiryID,
&i.EnquiryRef,
&i.CustomerName,
&i.CustomerEmail,
&i.DateIssued,
&i.ValidUntil,
&i.LatestReminderType,
&i.LatestReminderSentTime,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getExpiringSoonQuotesOnDay = `-- name: GetExpiringSoonQuotesOnDay :many
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
)
SELECT
d.id AS document_id,
u.username,
e.id AS enquiry_id,
e.title as enquiry_ref,
uu.first_name as customer_name,
uu.email as customer_email,
q.date_issued,
q.valid_until,
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
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
JOIN users uu ON uu.id = e.contact_user_id
LEFT JOIN latest_quote_reminder lqr on d.id = lqr.quote_id
WHERE
q.valid_until >= CURRENT_DATE
AND q.valid_until = DATE_ADD(CURRENT_DATE, INTERVAL ? DAY)
AND e.status_id = 5
ORDER BY q.valid_until
`
type GetExpiringSoonQuotesOnDayRow struct {
DocumentID int32 `json:"document_id"`
Username string `json:"username"`
EnquiryID int32 `json:"enquiry_id"`
EnquiryRef string `json:"enquiry_ref"`
CustomerName string `json:"customer_name"`
CustomerEmail string `json:"customer_email"`
DateIssued time.Time `json:"date_issued"`
ValidUntil time.Time `json:"valid_until"`
LatestReminderType int32 `json:"latest_reminder_type"`
LatestReminderSentTime time.Time `json:"latest_reminder_sent_time"`
}
func (q *Queries) GetExpiringSoonQuotesOnDay(ctx context.Context, dateADD interface{}) ([]GetExpiringSoonQuotesOnDayRow, error) {
rows, err := q.db.QueryContext(ctx, getExpiringSoonQuotesOnDay, dateADD)
if err != nil {
return nil, err
}
defer rows.Close()
items := []GetExpiringSoonQuotesOnDayRow{}
for rows.Next() {
var i GetExpiringSoonQuotesOnDayRow
if err := rows.Scan(
&i.DocumentID,
&i.Username,
&i.EnquiryID,
&i.EnquiryRef,
&i.CustomerName,
&i.CustomerEmail,
&i.DateIssued,
&i.ValidUntil,
&i.LatestReminderType,
&i.LatestReminderSentTime,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getQuoteRemindersByType = `-- 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
`
type GetQuoteRemindersByTypeParams struct {
QuoteID int32 `json:"quote_id"`
ReminderType int32 `json:"reminder_type"`
}
func (q *Queries) GetQuoteRemindersByType(ctx context.Context, arg GetQuoteRemindersByTypeParams) ([]QuoteReminder, error) {
rows, err := q.db.QueryContext(ctx, getQuoteRemindersByType, arg.QuoteID, arg.ReminderType)
if err != nil {
return nil, err
}
defer rows.Close()
items := []QuoteReminder{}
for rows.Next() {
var i QuoteReminder
if err := rows.Scan(
&i.ID,
&i.QuoteID,
&i.ReminderType,
&i.DateSent,
&i.Username,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getRecentlyExpiredQuotes = `-- name: GetRecentlyExpiredQuotes :many
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
)
SELECT
d.id AS document_id,
u.username,
e.id AS enquiry_id,
e.title as enquiry_ref,
uu.first_name as customer_name,
uu.email as customer_email,
q.date_issued,
q.valid_until,
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
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
JOIN users uu ON uu.id = e.contact_user_id
LEFT JOIN latest_quote_reminder lqr on d.id = lqr.quote_id
WHERE
q.valid_until < CURRENT_DATE
AND valid_until >= DATE_SUB(CURRENT_DATE, INTERVAL ? DAY)
AND e.status_id = 5
ORDER BY q.valid_until DESC
`
type GetRecentlyExpiredQuotesRow struct {
DocumentID int32 `json:"document_id"`
Username string `json:"username"`
EnquiryID int32 `json:"enquiry_id"`
EnquiryRef string `json:"enquiry_ref"`
CustomerName string `json:"customer_name"`
CustomerEmail string `json:"customer_email"`
DateIssued time.Time `json:"date_issued"`
ValidUntil time.Time `json:"valid_until"`
LatestReminderType int32 `json:"latest_reminder_type"`
LatestReminderSentTime time.Time `json:"latest_reminder_sent_time"`
}
func (q *Queries) GetRecentlyExpiredQuotes(ctx context.Context, dateSUB interface{}) ([]GetRecentlyExpiredQuotesRow, error) {
rows, err := q.db.QueryContext(ctx, getRecentlyExpiredQuotes, dateSUB)
if err != nil {
return nil, err
}
defer rows.Close()
items := []GetRecentlyExpiredQuotesRow{}
for rows.Next() {
var i GetRecentlyExpiredQuotesRow
if err := rows.Scan(
&i.DocumentID,
&i.Username,
&i.EnquiryID,
&i.EnquiryRef,
&i.CustomerName,
&i.CustomerEmail,
&i.DateIssued,
&i.ValidUntil,
&i.LatestReminderType,
&i.LatestReminderSentTime,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getRecentlyExpiredQuotesOnDay = `-- name: GetRecentlyExpiredQuotesOnDay :many
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
)
SELECT
d.id AS document_id,
u.username,
e.id AS enquiry_id,
e.title as enquiry_ref,
uu.first_name as customer_name,
uu.email as customer_email,
q.date_issued,
q.valid_until,
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
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
JOIN users uu ON uu.id = e.contact_user_id
LEFT JOIN latest_quote_reminder lqr on d.id = lqr.quote_id
WHERE
q.valid_until < CURRENT_DATE
AND valid_until = DATE_SUB(CURRENT_DATE, INTERVAL ? DAY)
AND e.status_id = 5
ORDER BY q.valid_until DESC
`
type GetRecentlyExpiredQuotesOnDayRow struct {
DocumentID int32 `json:"document_id"`
Username string `json:"username"`
EnquiryID int32 `json:"enquiry_id"`
EnquiryRef string `json:"enquiry_ref"`
CustomerName string `json:"customer_name"`
CustomerEmail string `json:"customer_email"`
DateIssued time.Time `json:"date_issued"`
ValidUntil time.Time `json:"valid_until"`
LatestReminderType int32 `json:"latest_reminder_type"`
LatestReminderSentTime time.Time `json:"latest_reminder_sent_time"`
}
func (q *Queries) GetRecentlyExpiredQuotesOnDay(ctx context.Context, dateSUB interface{}) ([]GetRecentlyExpiredQuotesOnDayRow, error) {
rows, err := q.db.QueryContext(ctx, getRecentlyExpiredQuotesOnDay, dateSUB)
if err != nil {
return nil, err
}
defer rows.Close()
items := []GetRecentlyExpiredQuotesOnDayRow{}
for rows.Next() {
var i GetRecentlyExpiredQuotesOnDayRow
if err := rows.Scan(
&i.DocumentID,
&i.Username,
&i.EnquiryID,
&i.EnquiryRef,
&i.CustomerName,
&i.CustomerEmail,
&i.DateIssued,
&i.ValidUntil,
&i.LatestReminderType,
&i.LatestReminderSentTime,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const insertQuoteReminder = `-- name: InsertQuoteReminder :execresult
INSERT INTO quote_reminders (quote_id, reminder_type, date_sent, username)
VALUES (?, ?, ?, ?)
`
type InsertQuoteReminderParams struct {
QuoteID int32 `json:"quote_id"`
ReminderType int32 `json:"reminder_type"`
DateSent time.Time `json:"date_sent"`
Username sql.NullString `json:"username"`
}
func (q *Queries) InsertQuoteReminder(ctx context.Context, arg InsertQuoteReminderParams) (sql.Result, error) {
return q.db.ExecContext(ctx, insertQuoteReminder,
arg.QuoteID,
arg.ReminderType,
arg.DateSent,
arg.Username,
)
}