-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Expand file tree
/
Copy pathschema.ts
More file actions
142 lines (130 loc) · 4.43 KB
/
Copy pathschema.ts
File metadata and controls
142 lines (130 loc) · 4.43 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
140
141
142
import {
pgTable,
serial,
varchar,
text,
timestamp,
integer,
} from 'drizzle-orm/pg-core';
import { relations } from 'drizzle-orm';
export const users = pgTable('users', {
id: serial('id').primaryKey(),
name: varchar('name', { length: 100 }),
email: varchar('email', { length: 255 }).notNull().unique(),
passwordHash: text('password_hash').notNull(),
role: varchar('role', { length: 20 }).notNull().default('member'),
createdAt: timestamp('created_at').notNull().defaultNow(),
updatedAt: timestamp('updated_at').notNull().defaultNow(),
deletedAt: timestamp('deleted_at'),
});
export const teams = pgTable('teams', {
id: serial('id').primaryKey(),
name: varchar('name', { length: 100 }).notNull(),
createdAt: timestamp('created_at').notNull().defaultNow(),
updatedAt: timestamp('updated_at').notNull().defaultNow(),
stripeCustomerId: text('stripe_customer_id').unique(),
stripeSubscriptionId: text('stripe_subscription_id').unique(),
stripeProductId: text('stripe_product_id'),
planName: varchar('plan_name', { length: 50 }),
subscriptionStatus: varchar('subscription_status', { length: 20 }),
});
export const teamMembers = pgTable('team_members', {
id: serial('id').primaryKey(),
userId: integer('user_id')
.notNull()
.references(() => users.id),
teamId: integer('team_id')
.notNull()
.references(() => teams.id),
role: varchar('role', { length: 50 }).notNull(),
joinedAt: timestamp('joined_at').notNull().defaultNow(),
});
export const activityLogs = pgTable('activity_logs', {
id: serial('id').primaryKey(),
teamId: integer('team_id')
.notNull()
.references(() => teams.id),
userId: integer('user_id').references(() => users.id),
action: text('action').notNull(),
timestamp: timestamp('timestamp').notNull().defaultNow(),
ipAddress: varchar('ip_address', { length: 45 }),
});
export const invitations = pgTable('invitations', {
id: serial('id').primaryKey(),
teamId: integer('team_id')
.notNull()
.references(() => teams.id),
email: varchar('email', { length: 255 }).notNull(),
role: varchar('role', { length: 50 }).notNull(),
invitedBy: integer('invited_by')
.notNull()
.references(() => users.id),
invitedAt: timestamp('invited_at').notNull().defaultNow(),
status: varchar('status', { length: 20 }).notNull().default('pending'),
});
export const teamsRelations = relations(teams, ({ many }) => ({
teamMembers: many(teamMembers),
activityLogs: many(activityLogs),
invitations: many(invitations),
}));
export const usersRelations = relations(users, ({ many }) => ({
teamMembers: many(teamMembers),
invitationsSent: many(invitations),
}));
export const invitationsRelations = relations(invitations, ({ one }) => ({
team: one(teams, {
fields: [invitations.teamId],
references: [teams.id],
}),
invitedBy: one(users, {
fields: [invitations.invitedBy],
references: [users.id],
}),
}));
export const teamMembersRelations = relations(teamMembers, ({ one }) => ({
user: one(users, {
fields: [teamMembers.userId],
references: [users.id],
}),
team: one(teams, {
fields: [teamMembers.teamId],
references: [teams.id],
}),
}));
export const activityLogsRelations = relations(activityLogs, ({ one }) => ({
team: one(teams, {
fields: [activityLogs.teamId],
references: [teams.id],
}),
user: one(users, {
fields: [activityLogs.userId],
references: [users.id],
}),
}));
export type User = typeof users.$inferSelect;
export type NewUser = typeof users.$inferInsert;
export type Team = typeof teams.$inferSelect;
export type NewTeam = typeof teams.$inferInsert;
export type TeamMember = typeof teamMembers.$inferSelect;
export type NewTeamMember = typeof teamMembers.$inferInsert;
export type ActivityLog = typeof activityLogs.$inferSelect;
export type NewActivityLog = typeof activityLogs.$inferInsert;
export type Invitation = typeof invitations.$inferSelect;
export type NewInvitation = typeof invitations.$inferInsert;
export type TeamDataWithMembers = Team & {
teamMembers: (TeamMember & {
user: Pick<User, 'id' | 'name' | 'email'>;
})[];
};
export enum ActivityType {
SIGN_UP = 'SIGN_UP',
SIGN_IN = 'SIGN_IN',
SIGN_OUT = 'SIGN_OUT',
UPDATE_PASSWORD = 'UPDATE_PASSWORD',
DELETE_ACCOUNT = 'DELETE_ACCOUNT',
UPDATE_ACCOUNT = 'UPDATE_ACCOUNT',
CREATE_TEAM = 'CREATE_TEAM',
REMOVE_TEAM_MEMBER = 'REMOVE_TEAM_MEMBER',
INVITE_TEAM_MEMBER = 'INVITE_TEAM_MEMBER',
ACCEPT_INVITATION = 'ACCEPT_INVITATION',
}