forked from nextjs/saas-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueries.ts
More file actions
139 lines (124 loc) · 3.36 KB
/
queries.ts
File metadata and controls
139 lines (124 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import { desc, and, eq, isNull } from 'drizzle-orm';
import { db } from './drizzle';
import { activityLogs, teamMembers, teams, users } from './schema';
import { cookies } from 'next/headers';
import { verifyToken } from '@/lib/auth/session';
import { cache } from 'react';
// ⚡ OPTIMIZATION: Memoize user and team lookups to deduplicate database queries
// and JWT verifications within a single request lifecycle. We use a conditional
// check for 'cache' to maintain compatibility with the Edge Runtime/Middleware.
export const getUser = (typeof cache === 'function'
? cache
: (fn: any) => fn)(async () => {
const sessionCookie = (await cookies()).get('session');
if (!sessionCookie || !sessionCookie.value) {
return null;
}
const sessionData = await verifyToken(sessionCookie.value);
if (
!sessionData ||
!sessionData.user ||
typeof sessionData.user.id !== 'number'
) {
return null;
}
if (new Date(sessionData.expires) < new Date()) {
return null;
}
const user = await db
.select()
.from(users)
.where(and(eq(users.id, sessionData.user.id), isNull(users.deletedAt)))
.limit(1);
if (user.length === 0) {
return null;
}
return user[0];
});
export async function getTeamByStripeCustomerId(customerId: string) {
const result = await db
.select()
.from(teams)
.where(eq(teams.stripeCustomerId, customerId))
.limit(1);
return result.length > 0 ? result[0] : null;
}
export async function updateTeamSubscription(
teamId: number,
subscriptionData: {
stripeSubscriptionId: string | null;
stripeProductId: string | null;
planName: string | null;
subscriptionStatus: string;
}
) {
await db
.update(teams)
.set({
...subscriptionData,
updatedAt: new Date(),
})
.where(eq(teams.id, teamId));
}
export const getUserWithTeam = (typeof cache === 'function'
? cache
: (fn: any) => fn)(async (userId: number) => {
const result = await db
.select({
user: users,
teamId: teamMembers.teamId,
})
.from(users)
.leftJoin(teamMembers, eq(users.id, teamMembers.userId))
.where(eq(users.id, userId))
.limit(1);
return result[0];
});
export async function getActivityLogs() {
const user = await getUser();
if (!user) {
throw new Error('User not authenticated');
}
return await db
.select({
id: activityLogs.id,
action: activityLogs.action,
timestamp: activityLogs.timestamp,
ipAddress: activityLogs.ipAddress,
userName: users.name,
})
.from(activityLogs)
.leftJoin(users, eq(activityLogs.userId, users.id))
.where(eq(activityLogs.userId, user.id))
.orderBy(desc(activityLogs.timestamp))
.limit(10);
}
export const getTeamForUser = (typeof cache === 'function'
? cache
: (fn: any) => fn)(async (userId: number) => {
const result = await db.query.users.findFirst({
where: eq(users.id, userId),
with: {
teamMembers: {
with: {
team: {
with: {
teamMembers: {
with: {
user: {
columns: {
id: true,
name: true,
email: true,
},
},
},
},
},
},
},
},
},
});
return result?.teamMembers[0]?.team || null;
});