Skip to content

Commit cf6202f

Browse files
authored
Merge pull request #57 from DataScience-GT/refactor/routes
Refactor/routes
2 parents a02693c + 08fdd4e commit cf6202f

File tree

41 files changed

+1248
-1319
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1248
-1319
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ coverage
1111
out/
1212
build
1313
dist/
14+
seed-super.ts
1415

1516
# Production
1617
build
@@ -32,6 +33,7 @@ yarn-error.log*
3233
.env.production
3334

3435
# Vercel
36+
.reset.ts
3537
.vercel
3638

3739
# Turbo

.nvmrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20.11.0
1+
22.11.0

deploy-cloudrun.bat

Lines changed: 0 additions & 105 deletions
This file was deleted.

deploy-cloudrun.sh

Lines changed: 0 additions & 78 deletions
This file was deleted.

packages/api/src/context.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ async function getAuth() {
2828
}
2929

3030
export async function createContext(
31-
opts?: FetchCreateContextFnOptions & { clientIp?: string; req?: Request }
31+
opts?: Partial<FetchCreateContextFnOptions> & { clientIp?: string; req?: Request }
3232
) {
3333
let session = null;
3434

@@ -50,7 +50,7 @@ export async function createContext(
5050
session,
5151
userId: session?.user?.id,
5252
cache,
53-
clientIp: opts?.clientIp || 'unknown',
53+
clientIp: opts?.clientIp || req?.headers.get("x-forwarded-for")?.split(",")[0] || 'unknown',
5454
req
5555
};
5656
}

packages/api/src/routers/admin.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import { isAdmin, isSuperAdmin } from "../middleware/procedures";
88

99
export const adminRouter = createTRPCRouter({
1010
isAdmin: protectedProcedure.query(async ({ ctx }) => {
11-
// Check cache first
1211
const cacheKey = CacheKeys.admin(ctx.userId!);
1312
const cached = ctx.cache.get<{
1413
isAdmin: boolean;
@@ -30,7 +29,6 @@ export const adminRouter = createTRPCRouter({
3029
permissions: admin?.permissions || [],
3130
};
3231

33-
// Cache for 60 seconds
3432
ctx.cache.set(cacheKey, result, 60);
3533

3634
return result;

packages/api/src/routers/events.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import { randomUUID } from "crypto";
77
import { isAdmin } from "../middleware/procedures";
88

99
export const eventRouter = createTRPCRouter({
10-
// ADMIN: Create event
1110
create: isAdmin
1211
.input(
1312
z.object({
@@ -33,7 +32,6 @@ export const eventRouter = createTRPCRouter({
3332
return newEvent;
3433
}),
3534

36-
// ADMIN: Regenerate QR code for an event
3735
regenerateQR: isAdmin
3836
.input(z.object({ eventId: z.string().uuid() }))
3937
.mutation(async ({ ctx, input }) => {
@@ -58,7 +56,6 @@ export const eventRouter = createTRPCRouter({
5856
return updatedEvent;
5957
}),
6058

61-
// ADMIN: List all events
6259
listAll: isAdmin.query(async ({ ctx }) => {
6360
const allEvents = await ctx.db!.query.events.findMany({
6461
orderBy: (events, { desc }) => [desc(events.eventDate)],
@@ -76,7 +73,6 @@ export const eventRouter = createTRPCRouter({
7673
return allEvents;
7774
}),
7875

79-
// ADMIN: Get event with check-ins
8076
getById: isAdmin
8177
.input(z.object({ id: z.string().uuid() }))
8278
.query(async ({ ctx, input }) => {
@@ -115,7 +111,6 @@ export const eventRouter = createTRPCRouter({
115111
return event;
116112
}),
117113

118-
// ADMIN: Toggle check-in enabled
119114
toggleCheckIn: isAdmin
120115
.input(
121116
z.object({
@@ -136,21 +131,17 @@ export const eventRouter = createTRPCRouter({
136131
return updatedEvent;
137132
}),
138133

139-
// ADMIN: Delete event
140134
delete: isAdmin
141135
.input(z.object({ eventId: z.string().uuid() }))
142136
.mutation(async ({ ctx, input }) => {
143137
await ctx.db!.delete(events).where(eq(events.id, input.eventId));
144138
return { success: true };
145139
}),
146140

147-
// MEMBER: Check in to event via QR code (OPTIMIZED - Single Query)
148141
checkIn: protectedProcedure
149142
.input(z.object({ qrCode: z.string().uuid() }))
150143
.mutation(async ({ ctx, input }) => {
151-
// Single transaction with all checks and inserts
152144
return await ctx.db!.transaction(async (tx) => {
153-
// 1. Get event and validate in one query
154145
const event = await tx.query.events.findFirst({
155146
where: eq(events.qrCode, input.qrCode),
156147
});
@@ -169,15 +160,13 @@ export const eventRouter = createTRPCRouter({
169160
});
170161
}
171162

172-
// 2. Check max capacity
173163
if (event.maxCheckIns && event.currentCheckIns >= event.maxCheckIns) {
174164
throw new TRPCError({
175165
code: "BAD_REQUEST",
176166
message: "Event is full",
177167
});
178168
}
179169

180-
// 3. Get member and check for existing check-in in parallel
181170
const [member, existingCheckIn] = await Promise.all([
182171
tx.query.members.findFirst({
183172
where: eq(members.userId, ctx.userId!),
@@ -204,7 +193,6 @@ export const eventRouter = createTRPCRouter({
204193
});
205194
}
206195

207-
// 4. Create check-in and update counter
208196
await Promise.all([
209197
tx.insert(eventCheckIns).values({
210198
eventId: event.id,
@@ -227,7 +215,6 @@ export const eventRouter = createTRPCRouter({
227215
});
228216
}),
229217

230-
// MEMBER: Get my attended events
231218
myEvents: protectedProcedure.query(async ({ ctx }) => {
232219
const checkIns = await ctx.db!.query.eventCheckIns.findMany({
233220
where: eq(eventCheckIns.userId, ctx.userId!),
@@ -249,7 +236,6 @@ export const eventRouter = createTRPCRouter({
249236
return checkIns;
250237
}),
251238

252-
// MEMBER: Get my stats (OPTIMIZED - Single Aggregation Query)
253239
myStats: protectedProcedure.query(async ({ ctx }) => {
254240
const result = await ctx.db!
255241
.select({

packages/api/src/routers/hackathon.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ export const hackathonRouter = createTRPCRouter({
2323
})
2424
)
2525
.query(async ({ ctx, input }) => {
26-
// Generate cache key based on input parameters
2726
const cacheKey = `hackathons:list:${input.status || 'all'}:${input.upcoming ? 'upcoming' : 'all'}:${input.limit}:${input.offset}`;
2827

2928
// Check cache first
@@ -43,7 +42,6 @@ export const hackathonRouter = createTRPCRouter({
4342
orderBy: (hackathons, { desc }) => [desc(hackathons.startDate)],
4443
});
4544

46-
// Cache for 60 seconds (hackathon lists don't change often)
4745
ctx.cache.set(cacheKey, allHackathons, 60);
4846

4947
return allHackathons;
@@ -68,7 +66,6 @@ export const hackathonRouter = createTRPCRouter({
6866
});
6967
}
7068

71-
// Cache for 120 seconds
7269
ctx.cache.set(cacheKey, hackathon, 120);
7370

7471
return hackathon;
@@ -109,7 +106,6 @@ export const hackathonRouter = createTRPCRouter({
109106
})
110107
.returning();
111108

112-
// Invalidate hackathon list cache
113109
ctx.cache.deletePattern('hackathons:*');
114110

115111
return newHackathon;
@@ -143,7 +139,6 @@ export const hackathonRouter = createTRPCRouter({
143139
.mutation(async ({ ctx, input }) => {
144140
const { id, ...updateData } = input;
145141

146-
// Verify hackathon exists
147142
const existing = await ctx.db!.query.hackathons.findFirst({
148143
where: eq(hackathons.id, id),
149144
});
@@ -164,7 +159,6 @@ export const hackathonRouter = createTRPCRouter({
164159
.where(eq(hackathons.id, id))
165160
.returning();
166161

167-
// Invalidate hackathon caches
168162
ctx.cache.delete(CacheKeys.hackathon(id));
169163
ctx.cache.deletePattern('hackathons:*');
170164

@@ -182,7 +176,6 @@ export const hackathonRouter = createTRPCRouter({
182176
})
183177
)
184178
.mutation(async ({ ctx, input }) => {
185-
// Use transaction to prevent race conditions
186179
return await ctx.db!.transaction(async (tx) => {
187180
const hackathon = await tx.query.hackathons.findFirst({
188181
where: eq(hackathons.id, input.hackathonId),

0 commit comments

Comments
 (0)