dana-medical-site/internal/storage/lead_notice.go
2026-10-14 13:33:13 +00:00

44 lines
1.6 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package storage
import (
"context"
"encoding/json"
"dana/internal/domain"
)
// LeadNotice собирает заявку для уведомления: одним запросом вместе с названиями
// формы, услуги, врача и филиала, чтобы администратору не пришлось искать их по id.
func (s *Storage) LeadNotice(ctx context.Context, id int64) (domain.LeadNotice, error) {
var (
n domain.LeadNotice
recipients []byte
payload []byte
)
err := s.pool.QueryRow(ctx, `
SELECT l.id,
COALESCE(f.code, ''), COALESCE(f.title, ''), COALESCE(f.recipients, '[]'::jsonb),
l.name, l.phone, l.email,
COALESCE(sv.name, ''),
concat_ws(' ', d.last_name, d.first_name, NULLIF(d.middle_name, '')),
COALESCE(b.address, ''),
l.message, l.payload, l.page_url, l.consent_at
FROM leads l
LEFT JOIN forms f ON f.id = l.form_id
LEFT JOIN services sv ON sv.id = l.service_id
LEFT JOIN doctors d ON d.id = l.doctor_id
LEFT JOIN branches b ON b.id = l.branch_id
WHERE l.id = $1`, id).
Scan(&n.ID, &n.FormCode, &n.FormTitle, &recipients,
&n.Name, &n.Phone, &n.Email,
&n.Service, &n.Doctor, &n.Branch,
&n.Message, &payload, &n.PageURL, &n.CreatedAt)
if err != nil {
return n, wrapNotFound(err)
}
// Битые JSON-поля не должны мешать уведомлению: заявка уже сохранена, отправим без них.
_ = json.Unmarshal(recipients, &n.Recipients)
_ = json.Unmarshal(payload, &n.Extra)
return n, nil
}