Skip to content

Commit e1768bc

Browse files
committed
feat: finalize schema
1 parent 10fd852 commit e1768bc

File tree

3 files changed

+133
-18
lines changed

3 files changed

+133
-18
lines changed

backend/prisma/migrations/20250411160616_create_tables/migration.sql renamed to backend/prisma/migrations/20250417075435_create_schema/migration.sql

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ CREATE TABLE "teamlevelassignment" (
5555
"sequence" INTEGER NOT NULL,
5656
"current_score" INTEGER NOT NULL DEFAULT 0,
5757
"is_finished" BOOLEAN NOT NULL DEFAULT false,
58+
"updated_at" TIMESTAMP(3),
5859

5960
CONSTRAINT "teamlevelassignment_pkey" PRIMARY KEY ("team_id","level_id")
6061
);
@@ -113,6 +114,7 @@ CREATE TABLE "teamspellattempt" (
113114
"spell_id" UUID NOT NULL,
114115
"active" BOOLEAN NOT NULL DEFAULT true,
115116
"created_at" TIMESTAMP(3) NOT NULL DEFAULT now(),
117+
"updated_at" TIMESTAMP(3),
116118

117119
CONSTRAINT "teamspellattempt_pkey" PRIMARY KEY ("id")
118120
);
@@ -126,6 +128,34 @@ CREATE TABLE "teamlocationprogress" (
126128
CONSTRAINT "teamlocationprogress_pkey" PRIMARY KEY ("attempt_id","location_id")
127129
);
128130

131+
-- CreateTable
132+
CREATE TABLE "qte" (
133+
"id" VARCHAR(6) NOT NULL,
134+
"name" TEXT NOT NULL,
135+
"scanned" BOOLEAN NOT NULL DEFAULT false,
136+
137+
CONSTRAINT "qte_pkey" PRIMARY KEY ("id")
138+
);
139+
140+
-- CreateTable
141+
CREATE TABLE "qtescan" (
142+
"qteId" VARCHAR(6) NOT NULL,
143+
"teamId" VARCHAR(6) NOT NULL,
144+
"created_at" TIMESTAMP(3) NOT NULL DEFAULT now(),
145+
146+
CONSTRAINT "qtescan_pkey" PRIMARY KEY ("qteId","teamId")
147+
);
148+
149+
-- CreateTable
150+
CREATE TABLE "qrlog" (
151+
"id" UUID NOT NULL DEFAULT gen_random_uuid(),
152+
"user_id" UUID NOT NULL,
153+
"log_info" TEXT NOT NULL,
154+
"created_at" TIMESTAMP(3) NOT NULL DEFAULT now(),
155+
156+
CONSTRAINT "qrlog_pkey" PRIMARY KEY ("id")
157+
);
158+
129159
-- CreateIndex
130160
CREATE UNIQUE INDEX "userprofile_email_key" ON "userprofile"("email");
131161

@@ -141,6 +171,9 @@ CREATE UNIQUE INDEX "teamlevelassignment_team_id_sequence_key" ON "teamlevelassi
141171
-- CreateIndex
142172
CREATE UNIQUE INDEX "teamspellattempt_team_id_active_key" ON "teamspellattempt"("team_id", "active");
143173

174+
-- CreateIndex
175+
CREATE UNIQUE INDEX "qtescan_qteId_key" ON "qtescan"("qteId");
176+
144177
-- AddForeignKey
145178
ALTER TABLE "teammember" ADD CONSTRAINT "teammember_team_id_fkey" FOREIGN KEY ("team_id") REFERENCES "team"("id") ON DELETE CASCADE ON UPDATE CASCADE;
146179

@@ -179,3 +212,12 @@ ALTER TABLE "teamlocationprogress" ADD CONSTRAINT "teamlocationprogress_attempt_
179212

180213
-- AddForeignKey
181214
ALTER TABLE "teamlocationprogress" ADD CONSTRAINT "teamlocationprogress_location_id_fkey" FOREIGN KEY ("location_id") REFERENCES "location"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
215+
216+
-- AddForeignKey
217+
ALTER TABLE "qtescan" ADD CONSTRAINT "qtescan_qteId_fkey" FOREIGN KEY ("qteId") REFERENCES "qte"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
218+
219+
-- AddForeignKey
220+
ALTER TABLE "qtescan" ADD CONSTRAINT "qtescan_teamId_fkey" FOREIGN KEY ("teamId") REFERENCES "team"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
221+
222+
-- AddForeignKey
223+
ALTER TABLE "qrlog" ADD CONSTRAINT "qrlog_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "userprofile"("id") ON DELETE CASCADE ON UPDATE CASCADE;

backend/prisma/schema.prisma

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ model UserProfile {
2828
createdAt DateTime @default(dbgenerated("now()")) @map("created_at")
2929
//
3030
member TeamMember?
31+
qrLog QRLog[]
3132
3233
@@map("userprofile")
3334
}
@@ -40,6 +41,7 @@ model Team {
4041
levelAssignment TeamLevelAssignment[]
4142
spellAssignment TeamSpellAssignment[]
4243
spellAttempts TeamSpellAttempt[]
44+
qteScan QTEScan[]
4345
4446
@@map("team")
4547
}
@@ -69,14 +71,15 @@ model Level {
6971
}
7072

7173
model TeamLevelAssignment {
72-
teamId String @map("team_id") @db.VarChar(6)
73-
levelId Int @map("level_id")
74+
teamId String @map("team_id") @db.VarChar(6)
75+
levelId Int @map("level_id")
7476
sequence Int
75-
currentScore Int @default(0) @map("current_score")
76-
isFinished Boolean @default(false) @map("is_finished")
77+
currentScore Int @default(0) @map("current_score")
78+
isFinished Boolean @default(false) @map("is_finished")
79+
updatedAt DateTime? @map("updated_at")
7780
//
78-
team Team @relation(fields: [teamId], references: [id], onDelete: Cascade)
79-
level Level @relation(fields: [levelId], references: [id])
81+
team Team @relation(fields: [teamId], references: [id], onDelete: Cascade)
82+
level Level @relation(fields: [levelId], references: [id])
8083
8184
@@id([teamId, levelId])
8285
@@unique([teamId, sequence])
@@ -88,7 +91,7 @@ model Spell {
8891
rewardScore Int @map("reward_score")
8992
numVertex Int @map("num_vertex")
9093
cooldown Int
91-
imageUrl String? @map("image_url")
94+
imageUrl String? @map("image_url")
9295
//
9396
pattern Pattern[]
9497
teamAssignment TeamSpellAssignment[]
@@ -149,6 +152,7 @@ model TeamSpellAttempt {
149152
spellId String @map("spell_id") @db.Uuid
150153
active Boolean @default(true)
151154
createdAt DateTime @default(dbgenerated("now()")) @map("created_at")
155+
updatedAt DateTime? @map("updated_at")
152156
//
153157
team Team @relation(fields: [teamId], references: [id])
154158
spell Spell @relation(fields: [spellId], references: [id])
@@ -169,3 +173,36 @@ model TeamLocationProgress {
169173
@@id([attemptId, locationId])
170174
@@map("teamlocationprogress")
171175
}
176+
177+
model QTE {
178+
id String @id @db.VarChar(6)
179+
name String
180+
scanned Boolean @default(false)
181+
//
182+
scan QTEScan?
183+
184+
@@map("qte")
185+
}
186+
187+
model QTEScan {
188+
qteId String @unique @db.VarChar(6)
189+
teamId String @db.VarChar(6)
190+
createdAt DateTime @default(dbgenerated("now()")) @map("created_at")
191+
//
192+
qte QTE @relation(fields: [qteId], references: [id])
193+
team Team @relation(fields: [teamId], references: [id])
194+
195+
@@id([qteId, teamId])
196+
@@map("qtescan")
197+
}
198+
199+
model QRLog {
200+
id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
201+
userId String @map("user_id") @db.Uuid
202+
logInfo String @map("log_info")
203+
createdAt DateTime @default(dbgenerated("now()")) @map("created_at")
204+
//
205+
user UserProfile @relation(fields: [userId], references: [id], onDelete: Cascade)
206+
207+
@@map("qrlog")
208+
}

backend/prisma/seed.ts

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,38 @@ async function createUserTriggers() {
9494
}
9595
}
9696

97+
async function createUpdatedAtTriggers() {
98+
const queries = [
99+
Prisma.sql`
100+
CREATE OR REPLACE FUNCTION fnUpdateUpdatedAt()
101+
RETURNS trigger
102+
LANGUAGE plpgsql
103+
SECURITY DEFINER
104+
SET search_path = ''
105+
AS $$
106+
BEGIN
107+
NEW.updated_at = now();
108+
RETURN NEW;
109+
END;
110+
$$
111+
`,
112+
Prisma.sql`
113+
CREATE TRIGGER trUpdateUpdatedAtTeamLevelAssignment
114+
BEFORE UPDATE ON teamlevelassignment
115+
FOR EACH ROW EXECUTE PROCEDURE fnUpdateUpdatedAt();
116+
`,
117+
Prisma.sql`
118+
CREATE TRIGGER trUpdateUpdatedAtTeamSpellAttempt
119+
BEFORE UPDATE ON teamspellattempt
120+
FOR EACH ROW EXECUTE PROCEDURE fnUpdateUpdatedAt();
121+
`
122+
];
123+
124+
for (const query of queries) {
125+
await prisma.$executeRaw(query);
126+
}
127+
}
128+
97129
async function seedLevelTable() {
98130
const levels = [
99131
{ id: 1, name: 'Ex 1', targetScore: 10 },
@@ -129,17 +161,17 @@ async function seedSpellTable() {
129161

130162
async function seedLocationTable() {
131163
const locationsWithoutId = [
132-
{ name: 'Tower 6 FruitShop', latitude: 0, longitude: 0 },
133-
{ name: 'Research Block', latitude: 0, longitude: 0 },
134-
{ name: 'A Block', latitude: 0, longitude: 0 },
135-
{ name: 'CnD Atrium', latitude: 0, longitude: 0 },
136-
{ name: 'G Block', latitude: 0, longitude: 0 },
137-
{ name: 'Shopping Arcade', latitude: 0, longitude: 0 },
138-
{ name: 'UAC', latitude: 0, longitude: 0 },
139-
{ name: 'Cluster 1', latitude: 0, longitude: 0 },
140-
{ name: 'Dibang Cycle Shop', latitude: 0, longitude: 0 },
141-
{ name: 'Dining Hall 3', latitude: 0, longitude: 0 },
142-
{ name: 'Indoor Sports Complex', latitude: 0, longitude: 0 },
164+
{ name: 'Tower 6 FruitShop', latitude: 28.52836141567019, longitude: 77.57777379378554 },
165+
{ name: 'Research Block', latitude: 28.527471246410833, longitude: 77.57890336254033 },
166+
{ name: 'A Block', latitude: 28.52692140098942, longitude: 77.57706407672785 },
167+
{ name: 'CnD Atrium', latitude: 28.525519393291614, longitude: 77.57653461322104 },
168+
{ name: 'G Block', latitude: 28.52799850829152, longitude: 77.5749038301257 },
169+
{ name: 'Shopping Arcade', latitude: 28.52723498480195, longitude: 77.57292972432113 },
170+
{ name: 'UAC', latitude: 28.523582249548888, longitude: 77.57437275274097 },
171+
{ name: 'Cluster 1', latitude: 28.52440905859402, longitude: 77.57308311017349 },
172+
{ name: 'Dibang Cycle Shop', latitude: 28.525235547619324, longitude: 77.57072373132858 },
173+
{ name: 'Dining Hall 3', latitude: 28.52316003957018, longitude: 77.5696713403205 },
174+
{ name: 'Indoor Sports Complex', latitude: 28.521496152454514, longitude: 77.5712575598366 },
143175
];
144176

145177
const locations = locationsWithoutId.map(l => {
@@ -157,6 +189,10 @@ async function main() {
157189
.then(() => console.log('✅ userTriggers created'))
158190
.catch((e) => console.error(`🚨 ${e}`));
159191

192+
await createUpdatedAtTriggers()
193+
.then(() => console.log('✅ updatedAtTriggers created'))
194+
.catch((e) => console.error(`🚨 ${e}`));
195+
160196
await seedLevelTable()
161197
.then(() => console.log('✅ seeded Level table'))
162198
.catch((e) => console.error(`🚨 ${e}`));

0 commit comments

Comments
 (0)