What it does
Pulls all appointments where
No date stepping needed — this trigger always fires for today. Vehicle information is included (
Dedup via
date_trunc('day', start_time)::date = current_date for the target shop. Filters to customers with a rewards balance ≥ minBalance.No date stepping needed — this trigger always fires for today. Vehicle information is included (
VehicleYearModel, AppTime) so the message can reference the specific visit.Dedup via
row_number() partitioned by customer_id (SMS) or email (Email), ordered by start_time — so if a customer has two appointments today, the earlier one wins.
This is the simplest query in the set — no date walking, no interval math. It anchors directly on
current_date.CTEs
rewardsBalance
Current balance per customer
credits − debits from cash_ledgers, familyID scoped, deleted_at excluded
mainQuery
Candidate selection with dedup
Anchors on customer_appointments for today. Joins vehicles, customers, reward_cards, rewardsBalance. Balance threshold applied in where clause.
Output fields
companyID
"Name"
"FirstName"
last_name
"FullName"
candidateID
"RewardsBalance"
reward_card_number
"Email"
"lvehicleID"
"VehicleYearModel"
"ScheduleDate"
"AppDate"
"AppTime"