Filters consolidated sales data by territory, matches reports to assigned representatives, and sends personalized HTML report emails via SMTP — with full audit logging of every delivery attempt. Use when the user needs to send or schedule sales reports to reps, distribute territory-based reports, automate report delivery to a sales team, email territory data to assigned representatives, or review report distribution history. Covers daily/weekly scheduled sends, manual on-demand distribution, and querying the distribution audit trail.
93
92%
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Passed
No known issues
Query active representatives and their territory assignments:
SELECT r.id, r.email, r.name, t.territory_code, t.territory_name
FROM representatives r
JOIN territory_assignments ta ON ta.rep_id = r.id
JOIN territories t ON t.id = ta.territory_id
WHERE r.active = true
AND ta.effective_date <= CURRENT_DATE
AND (ta.end_date IS NULL OR ta.end_date >= CURRENT_DATE);For manager/admin roll-ups:
SELECT u.id, u.email, u.name, 'ALL' AS territory_code
FROM users u
WHERE u.role IN ('admin', 'manager') AND u.active = true;Validation checkpoint: If the recipient list is empty, abort and log NO_RECIPIENTS — do not proceed to report generation.
Call the Data Consolidation Agent for each territory:
GET /api/consolidation/territory/{territory_code}?period={daily|weekly}&date={YYYY-MM-DD}For manager roll-ups:
GET /api/consolidation/company-summary?period={daily|weekly}&date={YYYY-MM-DD}Validation checkpoint: Verify each report payload is non-empty and contains at least one row. Log EMPTY_REPORT and skip the send for that territory if the payload is empty.
Render the territory report using the full template defined in TEMPLATES.md. All layout details — headers, column definitions, brand colours, and row styling — are specified there.
Validation checkpoint: Render the HTML and confirm it is well-formed (no unclosed tags, no empty <tbody>) before sending.
SMTP configuration (environment variables):
SMTP_HOST=smtp.stgcrm.internal
SMTP_PORT=587
SMTP_USER=$SMTP_USER
SMTP_PASS=$SMTP_PASS
SMTP_FROM=reports@stgcrm.com
SMTP_STARTTLS=trueSend each email:
TO: {rep.email}
FROM: reports@stgcrm.com
SUBJECT: [STGCRM] {territory_name} {period_label} Sales Report — {date}
BODY: {rendered_html}Validation checkpoint: Verify SMTP connection health (EHLO handshake) before starting the batch. On 5xx error, mark recipient as FAILED and continue to next. Do not retry transiently failed sends inline — schedule a retry job instead.
Insert one row per send attempt:
INSERT INTO distribution_log
(recipient_id, territory_code, report_period, status, error_message, sent_at)
VALUES
(:recipient_id, :territory_code, :report_period,
:status, -- 'sent' | 'failed' | 'skipped'
:error_message, -- NULL on success
NOW());Surface any failed rows to the admin dashboard within 5 minutes via:
GET /api/distribution-log?status=failed&since={5_minutes_ago}Register cron jobs (server-local time, weekdays):
# Daily territory reports — Mon–Fri at 08:00
0 8 * * 1-5 app/jobs/distribute_reports.sh --type=daily
# Weekly company summary — Monday at 07:00
0 7 * * 1 app/jobs/distribute_reports.sh --type=weeklyManual on-demand trigger via admin dashboard:
POST /api/distribution/trigger
Body: { "type": "daily|weekly", "date": "YYYY-MM-DD", "territory": "ALL|{code}" }The distribution_log table is queryable for compliance reporting. Common query patterns (all sends for a date, failed sends within a time window, per-rep delivery history) are documented with examples in AUDIT.md.
010799b
If you maintain this skill, you can claim it as your own. Once claimed, you can manage eval scenarios, bundle related skills, attach documentation or rules, and ensure cross-agent compatibility.