api: store notifications as records before delivering them
Notifications need to exist independently of the push that carries them, so the table and its kind enum land first, ahead of any producer or reader. The migration creates only the type and the table, and nothing is backfilled
Pedro Lucas Porcellis porcellis@eletrotupi.com · 11 days ago
f1edb1555ea0c01ec072ceb15489d8f5fa0df5fb Browse filesParents
6b62f4dChanged files
2
Additions
+54
Deletions
-2
/dev/null → api/prisma/migrations/20261017030503_add_notifications/migration.sql
1 +
-- CreateEnum
2 +
CREATE TYPE "NotificationKind" AS ENUM ('DAILY_REMINDER', 'MEDICINE_REMINDER', 'INSIGHT_ALERT');
3 +
4 +
-- CreateTable
5 +
CREATE TABLE "notifications" (
6 +
"id" SERIAL NOT NULL,
7 +
"kind" "NotificationKind" NOT NULL,
8 +
"title" TEXT NOT NULL,
9 +
"body" TEXT NOT NULL,
10 +
"scheduled_for" TIMESTAMP(3) NOT NULL,
11 +
"expires_at" TIMESTAMP(3),
12 +
"pushed_at" TIMESTAMP(3),
13 +
"regimen_id" INTEGER,
14 +
"user_id" INTEGER NOT NULL,
15 +
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
16 +
17 +
CONSTRAINT "notifications_pkey" PRIMARY KEY ("id")
18 +
);
19 +
20 +
-- AddForeignKey
21 +
ALTER TABLE "notifications" ADD CONSTRAINT "notifications_regimen_id_fkey" FOREIGN KEY ("regimen_id") REFERENCES "medicine_regimens"("id") ON DELETE SET NULL ON UPDATE CASCADE;
22 +
23 +
-- AddForeignKey
24 +
ALTER TABLE "notifications" ADD CONSTRAINT "notifications_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
api/prisma/schema.prisma
1 1
generator client {
2 2
provider = "prisma-client-js"
3 3
//output = "../src/generated/prisma"
4 4
}
5 5
37 37
triggers Trigger[]
38 38
sleepRecords SleepRecord[]
39 39
insights Insight[]
40 40
medicineRegimens MedicineRegimen[]
41 41
careActions CareAction[]
42 +
notifications Notification[]
42 43
43 44
@@map("users")
44 45
}
45 46
46 47
model Mood {
111 112
periodicity MedicinePeriodicity
112 113
scheduledAt String[] @default([]) @map("scheduled_at")
113 114
active Boolean @default(true)
114 115
userId Int @map("user_id")
115 116
116 -
user User @relation(fields: [userId], references: [id])
117 -
medicineLogs MedicineLog[]
117 +
user User @relation(fields: [userId], references: [id])
118 +
medicineLogs MedicineLog[]
119 +
notifications Notification[]
118 120
119 121
createdAt DateTime @default(now()) @map("created_at")
120 122
updatedAt DateTime @updatedAt @map("updated_at")
121 123
122 124
@@map("medicine_regimens")
210 212
periodStart DateTime @map("period_start")
211 213
periodEnd DateTime @map("period_end")
212 214
213 215
@@unique([userId, type, periodStart])
214 216
@@map("insights")
217 +
}
218 +
219 +
model Notification {
220 +
id Int @id @default(autoincrement())
221 +
kind NotificationKind
222 +
title String
223 +
body String @db.Text
224 +
scheduledFor DateTime @map("scheduled_for")
225 +
expiresAt DateTime? @map("expires_at")
226 +
pushedAt DateTime? @map("pushed_at")
227 +
228 +
regimenId Int? @map("regimen_id")
229 +
userId Int @map("user_id")
230 +
231 +
regimen MedicineRegimen? @relation(fields: [regimenId], references: [id])
232 +
user User @relation(fields: [userId], references: [id])
233 +
234 +
createdAt DateTime @default(now()) @map("created_at")
235 +
236 +
@@map("notifications")
237 +
}
238 +
239 +
enum NotificationKind {
240 +
DAILY_REMINDER
241 +
MEDICINE_REMINDER
242 +
INSIGHT_ALERT
215 243
}
216 244
217 245
enum InsightType {
218 246
MOOD_TREND
219 247
ENERGY_SLEEP_CORRELATION
306 334
THREE_TIMES_DAILY
307 335
WEEKLY
308 336
BIWEEKLY
309 337
MONTHLY
310 338
}