@ordinatio/scheduling 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ordinatio/scheduling",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "license": "MIT",
5
5
  "description": "Scheduling engine for appointment-based businesses. API-first, no iframes.",
6
6
  "main": "./dist/index.js",
@@ -0,0 +1,75 @@
1
+ -- CreateTable
2
+ CREATE TABLE "WebhookEndpoint" (
3
+ "id" TEXT NOT NULL,
4
+ "organizationId" TEXT NOT NULL,
5
+ "url" TEXT NOT NULL,
6
+ "description" TEXT,
7
+ "secret" TEXT NOT NULL,
8
+ "eventTypes" TEXT[],
9
+ "isActive" BOOLEAN NOT NULL DEFAULT true,
10
+ "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
11
+
12
+ CONSTRAINT "WebhookEndpoint_pkey" PRIMARY KEY ("id")
13
+ );
14
+
15
+ -- CreateTable
16
+ CREATE TABLE "WebhookDelivery" (
17
+ "id" TEXT NOT NULL,
18
+ "organizationId" TEXT NOT NULL,
19
+ "endpointId" TEXT NOT NULL,
20
+ "eventId" TEXT NOT NULL,
21
+ "eventType" TEXT NOT NULL,
22
+ "body" TEXT NOT NULL,
23
+ "status" TEXT NOT NULL DEFAULT 'PENDING',
24
+ "attempts" INTEGER NOT NULL DEFAULT 0,
25
+ "nextAttemptAt" TIMESTAMP(3) NOT NULL,
26
+ "lastStatusCode" INTEGER,
27
+ "lastError" TEXT,
28
+ "deliveredAt" TIMESTAMP(3),
29
+ "processingId" TEXT,
30
+ "claimedAt" TIMESTAMP(3),
31
+ "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
32
+
33
+ CONSTRAINT "WebhookDelivery_pkey" PRIMARY KEY ("id")
34
+ );
35
+
36
+ -- CreateTable
37
+ CREATE TABLE "WebhookAttempt" (
38
+ "id" TEXT NOT NULL,
39
+ "deliveryId" TEXT NOT NULL,
40
+ "attemptNumber" INTEGER NOT NULL,
41
+ "statusCode" INTEGER,
42
+ "error" TEXT,
43
+ "durationMs" INTEGER NOT NULL,
44
+ "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
45
+
46
+ CONSTRAINT "WebhookAttempt_pkey" PRIMARY KEY ("id")
47
+ );
48
+
49
+ -- CreateIndex
50
+ CREATE INDEX "WebhookEndpoint_organizationId_isActive_idx" ON "WebhookEndpoint"("organizationId", "isActive");
51
+
52
+ -- CreateIndex
53
+ CREATE INDEX "WebhookDelivery_status_nextAttemptAt_idx" ON "WebhookDelivery"("status", "nextAttemptAt");
54
+
55
+ -- CreateIndex
56
+ CREATE INDEX "WebhookDelivery_organizationId_createdAt_idx" ON "WebhookDelivery"("organizationId", "createdAt");
57
+
58
+ -- CreateIndex
59
+ CREATE INDEX "WebhookDelivery_endpointId_createdAt_idx" ON "WebhookDelivery"("endpointId", "createdAt");
60
+
61
+ -- CreateIndex
62
+ CREATE UNIQUE INDEX "WebhookDelivery_endpointId_eventId_key" ON "WebhookDelivery"("endpointId", "eventId");
63
+
64
+ -- CreateIndex
65
+ CREATE INDEX "WebhookAttempt_deliveryId_idx" ON "WebhookAttempt"("deliveryId");
66
+
67
+ -- AddForeignKey
68
+ ALTER TABLE "WebhookEndpoint" ADD CONSTRAINT "WebhookEndpoint_organizationId_fkey" FOREIGN KEY ("organizationId") REFERENCES "Organization"("id") ON DELETE CASCADE ON UPDATE CASCADE;
69
+
70
+ -- AddForeignKey
71
+ ALTER TABLE "WebhookDelivery" ADD CONSTRAINT "WebhookDelivery_endpointId_fkey" FOREIGN KEY ("endpointId") REFERENCES "WebhookEndpoint"("id") ON DELETE CASCADE ON UPDATE CASCADE;
72
+
73
+ -- AddForeignKey
74
+ ALTER TABLE "WebhookAttempt" ADD CONSTRAINT "WebhookAttempt_deliveryId_fkey" FOREIGN KEY ("deliveryId") REFERENCES "WebhookDelivery"("id") ON DELETE CASCADE ON UPDATE CASCADE;
75
+
@@ -27,6 +27,7 @@ model Organization {
27
27
  couponCodes CouponCode[]
28
28
  recurringSeries RecurringSeries[]
29
29
  notificationTemplates NotificationTemplate[]
30
+ webhookEndpoints WebhookEndpoint[]
30
31
  }
31
32
 
32
33
  // ===== BEGIN SHARED SCHEDULING MODELS (copied verbatim into the host schema by packages/database/scripts/sync-scheduling-schema.ts) =====
@@ -381,6 +382,67 @@ model NotificationTemplate {
381
382
  @@index([organizationId])
382
383
  }
383
384
 
385
+ // A web address that is told about booking changes. Every delivery is signed with `secret`, so the secret
386
+ // must stay readable here; it is shown once when the endpoint is created and never returned again.
387
+ model WebhookEndpoint {
388
+ id String @id @default(cuid())
389
+ organizationId String
390
+ organization Organization @relation(fields: [organizationId], references: [id], onDelete: Cascade)
391
+ url String
392
+ description String?
393
+ secret String
394
+ eventTypes String[] // event names such as appointment.booked, or * for all of them
395
+ isActive Boolean @default(true)
396
+ createdAt DateTime @default(now())
397
+
398
+ deliveries WebhookDelivery[]
399
+
400
+ @@index([organizationId, isActive])
401
+ }
402
+
403
+ // One event waiting to be sent to one endpoint. `body` is the exact text that is signed and sent, so a retry
404
+ // sends the same bytes.
405
+ model WebhookDelivery {
406
+ id String @id @default(cuid())
407
+ organizationId String
408
+ endpointId String
409
+ endpoint WebhookEndpoint @relation(fields: [endpointId], references: [id], onDelete: Cascade)
410
+ eventId String
411
+ eventType String
412
+ body String @db.Text
413
+ status String @default("PENDING") // PENDING | PROCESSING | SUCCEEDED | DEAD_LETTER
414
+ attempts Int @default(0)
415
+ nextAttemptAt DateTime
416
+ lastStatusCode Int?
417
+ lastError String?
418
+ deliveredAt DateTime?
419
+ processingId String? // worker holding the claim; null = claimable
420
+ claimedAt DateTime? // rows claimed more than 5 minutes ago are reclaimable (crash recovery)
421
+ createdAt DateTime @default(now())
422
+
423
+ attemptLog WebhookAttempt[]
424
+
425
+ // The same event is queued once per endpoint, however many times the caller reports it.
426
+ @@unique([endpointId, eventId])
427
+ @@index([status, nextAttemptAt])
428
+ @@index([organizationId, createdAt])
429
+ @@index([endpointId, createdAt])
430
+ }
431
+
432
+ // The log: one row per try.
433
+ model WebhookAttempt {
434
+ id String @id @default(cuid())
435
+ deliveryId String
436
+ delivery WebhookDelivery @relation(fields: [deliveryId], references: [id], onDelete: Cascade)
437
+ attemptNumber Int
438
+ statusCode Int?
439
+ error String?
440
+ durationMs Int
441
+ createdAt DateTime @default(now())
442
+
443
+ @@index([deliveryId])
444
+ }
445
+
384
446
  model ServicePackage {
385
447
  id String @id @default(cuid())
386
448
  name String