notification_template.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. """Pydantic schemas for notification templates."""
  2. from datetime import datetime
  3. from pydantic import BaseModel, Field
  4. from backend.app.core.compat import StrEnum
  5. from backend.app.schemas.not_null import IgnoresNullForRequiredColumns
  6. class EventType(StrEnum):
  7. """Supported notification event types."""
  8. PRINT_START = "print_start"
  9. PRINT_COMPLETE = "print_complete"
  10. PRINT_FAILED = "print_failed"
  11. PRINT_STOPPED = "print_stopped"
  12. PRINT_PROGRESS = "print_progress"
  13. PRINT_MISSING_SPOOL_ASSIGNMENT = "print_missing_spool_assignment"
  14. BILLING_CHARGE_FAILED = "billing_charge_failed"
  15. PRINTER_OFFLINE = "printer_offline"
  16. PRINTER_ERROR = "printer_error"
  17. AI_FAILURE_DETECTION = "ai_failure_detection"
  18. FILAMENT_LOW = "filament_low"
  19. MAINTENANCE_DUE = "maintenance_due"
  20. AMS_HUMIDITY_HIGH = "ams_humidity_high"
  21. AMS_TEMPERATURE_HIGH = "ams_temperature_high"
  22. AMS_DRYING_SUSPENDED = "ams_drying_suspended"
  23. AMS_DRYING_NOT_HEATING = "ams_drying_not_heating"
  24. BED_COOLED = "bed_cooled"
  25. HA_SENSOR_ALERT = "ha_sensor_alert"
  26. TEST = "test"
  27. # Available variables for each event type
  28. EVENT_VARIABLES: dict[str, list[str]] = {
  29. # Post-print outcome request (#1898): good/reject are the one-tap links,
  30. # confirm_url opens the archive's confirmation dialog.
  31. "print_confirm_request": [
  32. "printer",
  33. "filename",
  34. "confirm_url",
  35. "good_url",
  36. "reject_url",
  37. "archive_id",
  38. "finish_photo_url",
  39. "timestamp",
  40. "app_name",
  41. ],
  42. "print_start": ["printer", "filename", "estimated_time", "eta", "timestamp", "app_name"],
  43. "print_complete": [
  44. "printer",
  45. "filename",
  46. "duration",
  47. "filament_grams",
  48. "filament_details",
  49. "archive_id",
  50. "run_id",
  51. "finish_photo_url",
  52. "timestamp",
  53. "app_name",
  54. ],
  55. "print_failed": [
  56. "printer",
  57. "filename",
  58. "duration",
  59. "filament_grams",
  60. "filament_details",
  61. "progress",
  62. "reason",
  63. "archive_id",
  64. "run_id",
  65. "finish_photo_url",
  66. "timestamp",
  67. "app_name",
  68. ],
  69. "print_stopped": [
  70. "printer",
  71. "filename",
  72. "duration",
  73. "filament_grams",
  74. "filament_details",
  75. "progress",
  76. "archive_id",
  77. "run_id",
  78. "finish_photo_url",
  79. "timestamp",
  80. "app_name",
  81. ],
  82. "print_progress": ["printer", "filename", "progress", "remaining_time", "eta", "timestamp", "app_name"],
  83. "print_missing_spool_assignment": [
  84. "printer",
  85. "missing_slots",
  86. "missing_slot_details",
  87. "timestamp",
  88. "app_name",
  89. ],
  90. "billing_charge_failed": ["printer", "filename", "archive_id", "error", "timestamp", "app_name"],
  91. "printer_offline": ["printer", "timestamp", "app_name"],
  92. "printer_error": ["printer", "error_type", "error_detail", "timestamp", "app_name"],
  93. "ai_failure_detection": [
  94. "printer",
  95. "task_name",
  96. "provider",
  97. "confidence",
  98. "print_quality",
  99. "action",
  100. "timestamp",
  101. "app_name",
  102. ],
  103. "plate_not_empty": ["printer", "difference_percent", "timestamp", "app_name"],
  104. "plate_clear_required": ["printer", "timestamp", "app_name"],
  105. "filament_low": ["printer", "slot", "remaining_percent", "color", "timestamp", "app_name"],
  106. "maintenance_due": ["printer", "items", "timestamp", "app_name"],
  107. "ams_humidity_high": ["printer", "ams_label", "humidity", "threshold", "timestamp", "app_name"],
  108. "ams_temperature_high": ["printer", "ams_label", "temperature", "threshold", "timestamp", "app_name"],
  109. "ams_drying_suspended": [
  110. "printer",
  111. "ams_label",
  112. "humidity",
  113. "threshold",
  114. "cycles",
  115. "timestamp",
  116. "app_name",
  117. ],
  118. "ams_drying_not_heating": [
  119. "printer",
  120. "ams_label",
  121. "humidity",
  122. "threshold",
  123. "cycles",
  124. "timestamp",
  125. "app_name",
  126. ],
  127. "bed_cooled": ["printer", "bed_temp", "threshold", "filename", "timestamp", "app_name"],
  128. "ha_sensor_alert": ["printer", "sensor", "state", "timestamp", "app_name"],
  129. "location_ha_sensor_alert": ["location", "sensor", "state", "timestamp", "app_name"],
  130. "first_layer_complete": ["printer", "filename", "total_layers", "timestamp", "app_name"],
  131. "print_pause_upcoming": [
  132. "printer",
  133. "filename",
  134. "time_to_pause",
  135. "eta",
  136. "layer",
  137. "total_layers",
  138. "timestamp",
  139. "app_name",
  140. ],
  141. "test": ["app_name", "timestamp"],
  142. # Queue notifications
  143. "queue_job_added": ["job_name", "target", "timestamp", "app_name"],
  144. "queue_job_assigned": ["job_name", "printer", "target_model", "timestamp", "app_name"],
  145. "queue_job_started": ["printer", "job_name", "estimated_time", "eta", "timestamp", "app_name"],
  146. "queue_job_waiting": ["job_name", "target_model", "waiting_reason", "timestamp", "app_name"],
  147. "queue_job_skipped": ["printer", "job_name", "reason", "timestamp", "app_name"],
  148. "queue_job_failed": ["printer", "job_name", "reason", "timestamp", "app_name"],
  149. "queue_completed": ["completed_count", "timestamp", "app_name"],
  150. "stock_reorder_alert": [
  151. "material",
  152. "subtype",
  153. "brand",
  154. "color",
  155. "stock_g",
  156. "rate_g_day",
  157. "days_left",
  158. "timestamp",
  159. "app_name",
  160. ],
  161. "stock_break_alert": [
  162. "material",
  163. "subtype",
  164. "brand",
  165. "color",
  166. "stock_g",
  167. "rate_g_day",
  168. "days_left",
  169. "lead_time_days",
  170. "timestamp",
  171. "app_name",
  172. ],
  173. # User management notifications
  174. "user_created": ["username", "login_url", "app_name", "timestamp"],
  175. "password_reset": ["username", "app_name", "timestamp"],
  176. # User email print notifications
  177. "user_print_start": ["username", "printer", "filename", "timestamp", "app_name"],
  178. "user_print_complete": ["username", "printer", "filename", "finish_photo_url", "timestamp", "app_name"],
  179. "user_print_failed": ["username", "printer", "filename", "finish_photo_url", "timestamp", "app_name"],
  180. "user_print_stopped": ["username", "printer", "filename", "finish_photo_url", "timestamp", "app_name"],
  181. }
  182. # Events whose real notification can carry a camera snapshot — mirrors which
  183. # NotificationService.on_* methods accept image_data.
  184. PHOTO_CAPABLE_EVENTS: set[str] = {
  185. "print_confirm_request",
  186. "print_start",
  187. "print_complete",
  188. "print_failed",
  189. "print_stopped",
  190. "print_progress",
  191. "printer_error",
  192. "ai_failure_detection",
  193. "plate_not_empty",
  194. "first_layer_complete",
  195. "user_print_complete",
  196. "user_print_failed",
  197. "user_print_stopped",
  198. "test",
  199. }
  200. # Sample data for previewing templates
  201. SAMPLE_DATA: dict[str, dict[str, str]] = {
  202. "print_start": {
  203. "printer": "Bambu X1C",
  204. "filename": "Benchy.3mf",
  205. "estimated_time": "1h 23m",
  206. "eta": "15:53",
  207. "timestamp": "2024-01-15 14:30",
  208. "app_name": "Bambuddy",
  209. },
  210. "print_complete": {
  211. "printer": "Bambu X1C",
  212. "filename": "Benchy.3mf",
  213. "duration": "1h 18m",
  214. "filament_grams": "15.2",
  215. "filament_details": "AMS-A T1 PLA: 12.4g | AMS-A T3 PETG: 2.8g",
  216. "finish_photo_url": "/api/v1/archives/123/photos/finish_20240115_154800_abc12345.jpg",
  217. "archive_id": "123",
  218. "run_id": "456",
  219. "timestamp": "2024-01-15 15:48",
  220. "app_name": "Bambuddy",
  221. },
  222. "print_failed": {
  223. "printer": "Bambu X1C",
  224. "filename": "Benchy.3mf",
  225. "duration": "0h 45m",
  226. "filament_grams": "7.6",
  227. "filament_details": "AMS-A T1 PLA: 7.6g",
  228. "progress": "50",
  229. "reason": "Filament runout",
  230. "finish_photo_url": "/api/v1/archives/123/photos/finish_20240115_151500_def67890.jpg",
  231. "archive_id": "123",
  232. "run_id": "456",
  233. "timestamp": "2024-01-15 15:15",
  234. "app_name": "Bambuddy",
  235. },
  236. "print_stopped": {
  237. "printer": "Bambu X1C",
  238. "filename": "Benchy.3mf",
  239. "duration": "0h 30m",
  240. "filament_grams": "4.6",
  241. "filament_details": "AMS-A T2 PLA: 4.6g",
  242. "progress": "30",
  243. "finish_photo_url": "/api/v1/archives/123/photos/finish_20240115_150000_ghi11223.jpg",
  244. "archive_id": "123",
  245. "run_id": "456",
  246. "timestamp": "2024-01-15 15:00",
  247. "app_name": "Bambuddy",
  248. },
  249. "print_progress": {
  250. "printer": "Bambu X1C",
  251. "filename": "Benchy.3mf",
  252. "progress": "50",
  253. "remaining_time": "0h 41m",
  254. "eta": "15:41",
  255. "timestamp": "2024-01-15 15:00",
  256. "app_name": "Bambuddy",
  257. },
  258. "print_missing_spool_assignment": {
  259. "printer": "Bambu X1C",
  260. "missing_slots": "A1, A3",
  261. "missing_slot_details": "- A1: PLA Basic\n- A3: PETG HF",
  262. "timestamp": "2024-01-15 14:30",
  263. "app_name": "Bambuddy",
  264. },
  265. "billing_charge_failed": {
  266. "printer": "Bambu X1C",
  267. "filename": "Benchy.3mf",
  268. "archive_id": "123",
  269. "error": "The transaction could not be persisted",
  270. "timestamp": "2024-01-15 15:48",
  271. "app_name": "Bambuddy",
  272. },
  273. "printer_offline": {
  274. "printer": "Bambu X1C",
  275. "timestamp": "2024-01-15 14:30",
  276. "app_name": "Bambuddy",
  277. },
  278. "printer_error": {
  279. "printer": "Bambu X1C",
  280. "error_type": "AMS Error",
  281. "error_detail": "Filament slot 1 jammed",
  282. "timestamp": "2024-01-15 14:30",
  283. "app_name": "Bambuddy",
  284. },
  285. "ai_failure_detection": {
  286. "printer": "Bambu X1C",
  287. "task_name": "Benchy.3mf",
  288. "provider": "OctoEverywhere",
  289. "confidence": "N/A",
  290. "print_quality": "2/10",
  291. "action": "pause",
  292. "timestamp": "2024-01-15 14:30",
  293. "app_name": "Bambuddy",
  294. },
  295. "print_confirm_request": {
  296. "printer": "Bambu X1C",
  297. "filename": "Benchy",
  298. "confirm_url": "http://bambuddy.local:8000/archives?confirm=42",
  299. "good_url": "http://bambuddy.local:8000/api/v1/archives/confirm/sample-token/good",
  300. "reject_url": "http://bambuddy.local:8000/api/v1/archives/confirm/sample-token/reject",
  301. "archive_id": "42",
  302. "finish_photo_url": "/api/v1/archives/42/photos/finish_20240115_143500_abc12345.jpg",
  303. "timestamp": "2024-01-15 14:35",
  304. "app_name": "Bambuddy",
  305. },
  306. "plate_not_empty": {
  307. "printer": "Bambu X1C",
  308. "difference_percent": "12.4",
  309. "timestamp": "2024-01-15 09:00",
  310. "app_name": "Bambuddy",
  311. },
  312. "plate_clear_required": {
  313. "printer": "Bambu X1C",
  314. "timestamp": "2024-01-15 15:48",
  315. "app_name": "Bambuddy",
  316. },
  317. "filament_low": {
  318. "printer": "Bambu X1C",
  319. "slot": "1",
  320. "remaining_percent": "15",
  321. "color": "Black PLA",
  322. "timestamp": "2024-01-15 14:30",
  323. "app_name": "Bambuddy",
  324. },
  325. "maintenance_due": {
  326. "printer": "Bambu X1C",
  327. "items": "• Nozzle cleaning (OVERDUE)\n• Carbon rod lubrication (Soon)",
  328. "timestamp": "2024-01-15 14:30",
  329. "app_name": "Bambuddy",
  330. },
  331. "ams_humidity_high": {
  332. "printer": "Bambu X1C",
  333. "ams_label": "AMS-A",
  334. "humidity": "75",
  335. "threshold": "60",
  336. "timestamp": "2024-01-15 14:30",
  337. "app_name": "Bambuddy",
  338. },
  339. "ams_temperature_high": {
  340. "printer": "Bambu X1C",
  341. "ams_label": "AMS-A",
  342. "temperature": "42",
  343. "threshold": "35",
  344. "timestamp": "2024-01-15 14:30",
  345. "app_name": "Bambuddy",
  346. },
  347. "ams_drying_suspended": {
  348. "printer": "Bambu X1C",
  349. "ams_label": "AMS-A",
  350. "humidity": "16",
  351. "threshold": "14",
  352. "cycles": "2",
  353. "timestamp": "2024-01-15 14:30",
  354. "app_name": "Bambuddy",
  355. },
  356. "ams_drying_not_heating": {
  357. "printer": "Bambu P2S",
  358. "ams_label": "HT-A",
  359. "humidity": "75",
  360. "threshold": "60",
  361. "cycles": "2",
  362. "timestamp": "2024-01-15 14:30",
  363. "app_name": "Bambuddy",
  364. },
  365. "bed_cooled": {
  366. "printer": "Bambu X1C",
  367. "bed_temp": "34",
  368. "threshold": "35",
  369. "filename": "Benchy",
  370. "timestamp": "2024-01-15 14:30",
  371. "app_name": "Bambuddy",
  372. },
  373. "ha_sensor_alert": {
  374. "printer": "Bambu X1C",
  375. "sensor": "Enclosure Door",
  376. "state": "open",
  377. "timestamp": "2024-01-15 14:30",
  378. "app_name": "Bambuddy",
  379. },
  380. "location_ha_sensor_alert": {
  381. "location": "Drybox 1",
  382. "sensor": "Drybox 1 Humidity",
  383. "state": "68.00 %",
  384. "timestamp": "2024-01-15 14:30",
  385. "app_name": "Bambuddy",
  386. },
  387. "first_layer_complete": {
  388. "printer": "Bambu X1C",
  389. "filename": "Benchy.3mf",
  390. "total_layers": "184",
  391. "timestamp": "2024-01-15 14:35",
  392. "app_name": "Bambuddy",
  393. },
  394. "print_pause_upcoming": {
  395. "printer": "Bambu X1C",
  396. "filename": "Wifi_QR_NFC.3mf",
  397. "time_to_pause": "5m",
  398. "eta": "16:05",
  399. "layer": "42",
  400. "total_layers": "120",
  401. "timestamp": "2024-01-15 16:00",
  402. "app_name": "Bambuddy",
  403. },
  404. "test": {
  405. "app_name": "Bambuddy",
  406. "timestamp": "2024-01-15 14:30",
  407. },
  408. # Queue notifications
  409. "queue_job_added": {
  410. "job_name": "Benchy.3mf",
  411. "target": "Bambu X1C",
  412. "timestamp": "2024-01-15 14:30",
  413. "app_name": "Bambuddy",
  414. },
  415. "queue_job_assigned": {
  416. "job_name": "Benchy.3mf",
  417. "printer": "Bambu X1C #1",
  418. "target_model": "X1C",
  419. "timestamp": "2024-01-15 14:30",
  420. "app_name": "Bambuddy",
  421. },
  422. "queue_job_started": {
  423. "printer": "Bambu X1C",
  424. "job_name": "Benchy.3mf",
  425. "estimated_time": "1h 23m",
  426. "eta": "15:53",
  427. "timestamp": "2024-01-15 14:30",
  428. "app_name": "Bambuddy",
  429. },
  430. "queue_job_waiting": {
  431. "job_name": "Benchy.3mf",
  432. "target_model": "X1C",
  433. "waiting_reason": "Printer1 (needs PLA)",
  434. "timestamp": "2024-01-15 14:30",
  435. "app_name": "Bambuddy",
  436. },
  437. "queue_job_skipped": {
  438. "printer": "Bambu X1C",
  439. "job_name": "Benchy.3mf",
  440. "reason": "Previous print failed",
  441. "timestamp": "2024-01-15 14:30",
  442. "app_name": "Bambuddy",
  443. },
  444. "queue_job_failed": {
  445. "printer": "Bambu X1C",
  446. "job_name": "Benchy.3mf",
  447. "reason": "Upload failed: connection timeout",
  448. "timestamp": "2024-01-15 14:30",
  449. "app_name": "Bambuddy",
  450. },
  451. "queue_completed": {
  452. "completed_count": "5",
  453. "timestamp": "2024-01-15 18:30",
  454. "app_name": "Bambuddy",
  455. },
  456. "stock_reorder_alert": {
  457. "material": "PLA",
  458. "subtype": "Matte",
  459. "brand": "Bambu Lab",
  460. "color": "Charcoal",
  461. "stock_g": "420",
  462. "rate_g_day": "18.5",
  463. "days_left": "22",
  464. "timestamp": "2024-01-15 14:30",
  465. "app_name": "Bambuddy",
  466. },
  467. "stock_break_alert": {
  468. "material": "PLA",
  469. "subtype": "Matte",
  470. "brand": "Bambu Lab",
  471. "color": "Charcoal",
  472. "stock_g": "160",
  473. "rate_g_day": "18.5",
  474. "days_left": "8",
  475. "lead_time_days": "14",
  476. "timestamp": "2024-01-15 14:30",
  477. "app_name": "Bambuddy",
  478. },
  479. # User management notifications
  480. "user_created": {
  481. "username": "john_doe",
  482. "login_url": "https://bambuddy.example.com/login",
  483. "app_name": "Bambuddy",
  484. "timestamp": "2024-01-15 14:30",
  485. },
  486. "password_reset": {
  487. "username": "john_doe",
  488. "app_name": "Bambuddy",
  489. "timestamp": "2024-01-15 14:30",
  490. },
  491. # User email print notifications
  492. "user_print_start": {
  493. "username": "john_doe",
  494. "printer": "Bambu X1C",
  495. "filename": "Benchy.3mf",
  496. "timestamp": "2024-01-15 14:30",
  497. "app_name": "Bambuddy",
  498. },
  499. "user_print_complete": {
  500. "username": "john_doe",
  501. "printer": "Bambu X1C",
  502. "filename": "Benchy.3mf",
  503. "finish_photo_url": "/api/v1/archives/123/photos/finish_20240115_154800_abc12345.jpg",
  504. "timestamp": "2024-01-15 15:48",
  505. "app_name": "Bambuddy",
  506. },
  507. "user_print_failed": {
  508. "username": "john_doe",
  509. "printer": "Bambu X1C",
  510. "filename": "Benchy.3mf",
  511. "finish_photo_url": "/api/v1/archives/123/photos/finish_20240115_151500_def67890.jpg",
  512. "timestamp": "2024-01-15 15:15",
  513. "app_name": "Bambuddy",
  514. },
  515. "user_print_stopped": {
  516. "username": "john_doe",
  517. "printer": "Bambu X1C",
  518. "filename": "Benchy.3mf",
  519. "finish_photo_url": "/api/v1/archives/123/photos/finish_20240115_150000_ghi11223.jpg",
  520. "timestamp": "2024-01-15 15:15",
  521. "app_name": "Bambuddy",
  522. },
  523. }
  524. class NotificationTemplateBase(BaseModel):
  525. """Base schema for notification templates."""
  526. title_template: str = Field(..., min_length=1, max_length=200)
  527. body_template: str = Field(..., min_length=1, max_length=2000)
  528. class NotificationTemplateUpdate(IgnoresNullForRequiredColumns):
  529. """Schema for updating a notification template."""
  530. __orm_table__ = "notification_templates"
  531. title_template: str | None = Field(default=None, min_length=1, max_length=200)
  532. body_template: str | None = Field(default=None, min_length=1, max_length=2000)
  533. class NotificationTemplateResponse(NotificationTemplateBase):
  534. """Schema for notification template API responses."""
  535. id: int
  536. event_type: str
  537. name: str
  538. is_default: bool
  539. created_at: datetime
  540. updated_at: datetime
  541. class Config:
  542. from_attributes = True
  543. class EventVariablesResponse(BaseModel):
  544. """Response for available variables per event type."""
  545. event_type: str
  546. event_name: str
  547. variables: list[str]
  548. supports_photo: bool = False
  549. class TemplatePreviewRequest(BaseModel):
  550. """Request to preview a template with sample data."""
  551. event_type: str
  552. title_template: str
  553. body_template: str
  554. class TemplatePreviewResponse(BaseModel):
  555. """Response with rendered template preview."""
  556. title: str
  557. body: str