File size: 14,428 Bytes
062a991
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import { sql } from "drizzle-orm";
import {
  index,
  integer,
  primaryKey,
  sqliteTable,
  text,
  uniqueIndex,
} from "drizzle-orm/sqlite-core";

const timestamps = {
  createdAt: integer("created_at", { mode: "timestamp" }).notNull().default(sql`(unixepoch())`),
  updatedAt: integer("updated_at", { mode: "timestamp" }).notNull().default(sql`(unixepoch())`),
};

const newId = () => crypto.randomUUID();

export const users = sqliteTable("users", {
  id: text("id").primaryKey().$defaultFn(newId),
  email: text("email").notNull(),
  username: text("username").notNull(),
  passwordHash: text("password_hash").notNull(),
  emailVerifiedAt: integer("email_verified_at", { mode: "timestamp" }),
  name: text("name").notNull(),
  headline: text("headline").notNull().default(""),
  about: text("about").notNull().default(""),
  location: text("location").notNull().default(""),
  avatarUrl: text("avatar_url"),
  coverUrl: text("cover_url"),
  openToWork: integer("open_to_work", { mode: "boolean" }).notNull().default(false),
  ...timestamps,
}, (table) => [
  uniqueIndex("users_email_unique").on(table.email),
  uniqueIndex("users_username_unique").on(table.username),
  index("users_name_idx").on(table.name),
]);

export const sessions = sqliteTable("sessions", {
  id: text("id").primaryKey().$defaultFn(newId),
  userId: text("user_id").notNull().references(() => users.id, { onDelete: "cascade" }),
  tokenHash: text("token_hash").notNull(),
  expiresAt: integer("expires_at", { mode: "timestamp" }).notNull(),
  lastSeenAt: integer("last_seen_at", { mode: "timestamp" }).notNull().default(sql`(unixepoch())`),
  createdAt: integer("created_at", { mode: "timestamp" }).notNull().default(sql`(unixepoch())`),
}, (table) => [
  uniqueIndex("sessions_token_hash_unique").on(table.tokenHash),
  index("sessions_user_idx").on(table.userId),
  index("sessions_expiry_idx").on(table.expiresAt),
]);

export const emailVerificationTokens = sqliteTable("email_verification_tokens", {
  id: text("id").primaryKey().$defaultFn(newId),
  userId: text("user_id").notNull().references(() => users.id, { onDelete: "cascade" }),
  tokenHash: text("token_hash").notNull(),
  sentToEmail: text("sent_to_email").notNull(),
  expiresAt: integer("expires_at", { mode: "timestamp" }).notNull(),
  consumedAt: integer("consumed_at", { mode: "timestamp" }),
  createdAt: integer("created_at", { mode: "timestamp" }).notNull().default(sql`(unixepoch())`),
}, (table) => [
  uniqueIndex("email_verification_tokens_hash_unique").on(table.tokenHash),
  index("email_verification_tokens_user_idx").on(table.userId),
  index("email_verification_tokens_expiry_idx").on(table.expiresAt),
]);

export const subscriptions = sqliteTable("subscriptions", {
  id: text("id").primaryKey().$defaultFn(newId),
  userId: text("user_id").notNull().references(() => users.id, { onDelete: "cascade" }),
  stripeCustomerId: text("stripe_customer_id"),
  stripeSubscriptionId: text("stripe_subscription_id"),
  stripePriceId: text("stripe_price_id").notNull(),
  status: text("status").notNull().default("incomplete"),
  currentPeriodStart: integer("current_period_start", { mode: "timestamp" }),
  currentPeriodEnd: integer("current_period_end", { mode: "timestamp" }),
  cancelAtPeriodEnd: integer("cancel_at_period_end", { mode: "boolean" }).notNull().default(false),
  checkoutSessionId: text("checkout_session_id"),
  ...timestamps,
}, (table) => [
  uniqueIndex("subscriptions_user_unique").on(table.userId),
  uniqueIndex("subscriptions_stripe_customer_unique").on(table.stripeCustomerId),
  uniqueIndex("subscriptions_stripe_subscription_unique").on(table.stripeSubscriptionId),
  index("subscriptions_status_idx").on(table.status),
]);

export const organizations = sqliteTable("organizations", {
  id: text("id").primaryKey().$defaultFn(newId),
  name: text("name").notNull(),
  slug: text("slug").notNull(),
  tagline: text("tagline").notNull().default(""),
  description: text("description").notNull().default(""),
  industry: text("industry").notNull().default(""),
  website: text("website"),
  location: text("location").notNull().default(""),
  employeeCount: integer("employee_count").notNull().default(0),
  logoUrl: text("logo_url"),
  settings: text("settings", { mode: "json" }).$type<{ allowMemberPosts: boolean; defaultJobVisibility: "public" | "tenant" }>()
    .notNull().default({ allowMemberPosts: true, defaultJobVisibility: "public" }),
  ...timestamps,
}, (table) => [uniqueIndex("organizations_slug_unique").on(table.slug)]);

export const memberships = sqliteTable("memberships", {
  organizationId: text("organization_id").notNull().references(() => organizations.id, { onDelete: "cascade" }),
  userId: text("user_id").notNull().references(() => users.id, { onDelete: "cascade" }),
  role: text("role", { enum: ["owner", "admin", "recruiter", "member"] }).notNull().default("member"),
  title: text("title").notNull().default(""),
  joinedAt: integer("joined_at", { mode: "timestamp" }).notNull().default(sql`(unixepoch())`),
}, (table) => [
  primaryKey({ columns: [table.organizationId, table.userId] }),
  index("memberships_user_idx").on(table.userId),
]);

export const experiences = sqliteTable("experiences", {
  id: text("id").primaryKey().$defaultFn(newId),
  userId: text("user_id").notNull().references(() => users.id, { onDelete: "cascade" }),
  organizationId: text("organization_id").references(() => organizations.id, { onDelete: "set null" }),
  companyName: text("company_name").notNull(),
  title: text("title").notNull(),
  location: text("location").notNull().default(""),
  description: text("description").notNull().default(""),
  startDate: text("start_date").notNull(),
  endDate: text("end_date"),
  current: integer("current", { mode: "boolean" }).notNull().default(false),
}, (table) => [index("experiences_user_idx").on(table.userId)]);

export const posts = sqliteTable("posts", {
  id: text("id").primaryKey().$defaultFn(newId),
  authorId: text("author_id").notNull().references(() => users.id, { onDelete: "cascade" }),
  organizationId: text("organization_id").references(() => organizations.id, { onDelete: "set null" }),
  content: text("content").notNull(),
  mediaUrl: text("media_url"),
  visibility: text("visibility").notNull().default("network"),
  ...timestamps,
}, (table) => [
  index("posts_created_idx").on(table.createdAt),
  index("posts_author_idx").on(table.authorId),
  index("posts_org_idx").on(table.organizationId),
]);

export const postLikes = sqliteTable("post_likes", {
  postId: text("post_id").notNull().references(() => posts.id, { onDelete: "cascade" }),
  userId: text("user_id").notNull().references(() => users.id, { onDelete: "cascade" }),
  createdAt: integer("created_at", { mode: "timestamp" }).notNull().default(sql`(unixepoch())`),
}, (table) => [primaryKey({ columns: [table.postId, table.userId] })]);

export const comments = sqliteTable("comments", {
  id: text("id").primaryKey().$defaultFn(newId),
  postId: text("post_id").notNull().references(() => posts.id, { onDelete: "cascade" }),
  authorId: text("author_id").notNull().references(() => users.id, { onDelete: "cascade" }),
  content: text("content").notNull(),
  createdAt: integer("created_at", { mode: "timestamp" }).notNull().default(sql`(unixepoch())`),
}, (table) => [
  index("comments_post_idx").on(table.postId, table.createdAt),
  index("comments_author_idx").on(table.authorId),
]);

export const connections = sqliteTable("connections", {
  id: text("id").primaryKey().$defaultFn(newId),
  requesterId: text("requester_id").notNull().references(() => users.id, { onDelete: "cascade" }),
  recipientId: text("recipient_id").notNull().references(() => users.id, { onDelete: "cascade" }),
  status: text("status", { enum: ["pending", "accepted", "declined"] }).notNull().default("pending"),
  createdAt: integer("created_at", { mode: "timestamp" }).notNull().default(sql`(unixepoch())`),
  respondedAt: integer("responded_at", { mode: "timestamp" }),
}, (table) => [
  uniqueIndex("connections_pair_unique").on(table.requesterId, table.recipientId),
  index("connections_recipient_status_idx").on(table.recipientId, table.status),
]);

export const follows = sqliteTable("follows", {
  userId: text("user_id").notNull().references(() => users.id, { onDelete: "cascade" }),
  targetUserId: text("target_user_id").notNull().references(() => users.id, { onDelete: "cascade" }),
  createdAt: integer("created_at", { mode: "timestamp" }).notNull().default(sql`(unixepoch())`),
}, (table) => [
  primaryKey({ columns: [table.userId, table.targetUserId] }),
  index("follows_target_idx").on(table.targetUserId, table.createdAt),
]);

export const conversations = sqliteTable("conversations", {
  id: text("id").primaryKey().$defaultFn(newId),
  tenantId: text("tenant_id").references(() => organizations.id, { onDelete: "set null" }),
  subject: text("subject").notNull().default(""),
  updatedAt: integer("updated_at", { mode: "timestamp" }).notNull().default(sql`(unixepoch())`),
  createdAt: integer("created_at", { mode: "timestamp" }).notNull().default(sql`(unixepoch())`),
});

export const conversationMembers = sqliteTable("conversation_members", {
  conversationId: text("conversation_id").notNull().references(() => conversations.id, { onDelete: "cascade" }),
  userId: text("user_id").notNull().references(() => users.id, { onDelete: "cascade" }),
  lastReadAt: integer("last_read_at", { mode: "timestamp" }),
}, (table) => [
  primaryKey({ columns: [table.conversationId, table.userId] }),
  index("conversation_members_user_idx").on(table.userId),
]);

export const messages = sqliteTable("messages", {
  id: text("id").primaryKey().$defaultFn(newId),
  conversationId: text("conversation_id").notNull().references(() => conversations.id, { onDelete: "cascade" }),
  senderId: text("sender_id").notNull().references(() => users.id, { onDelete: "cascade" }),
  body: text("body").notNull(),
  createdAt: integer("created_at", { mode: "timestamp" }).notNull().default(sql`(unixepoch())`),
}, (table) => [index("messages_conversation_idx").on(table.conversationId, table.createdAt)]);

export const notifications = sqliteTable("notifications", {
  id: text("id").primaryKey().$defaultFn(newId),
  userId: text("user_id").notNull().references(() => users.id, { onDelete: "cascade" }),
  actorId: text("actor_id").references(() => users.id, { onDelete: "set null" }),
  type: text("type", { enum: ["connection", "post_like", "comment", "message", "job"] }).notNull(),
  title: text("title").notNull(),
  body: text("body").notNull().default(""),
  href: text("href").notNull().default("/notifications"),
  readAt: integer("read_at", { mode: "timestamp" }),
  createdAt: integer("created_at", { mode: "timestamp" }).notNull().default(sql`(unixepoch())`),
}, (table) => [index("notifications_user_read_idx").on(table.userId, table.readAt, table.createdAt)]);

export const jobs = sqliteTable("jobs", {
  id: text("id").primaryKey().$defaultFn(newId),
  organizationId: text("organization_id").notNull().references(() => organizations.id, { onDelete: "cascade" }),
  createdBy: text("created_by").notNull().references(() => users.id),
  title: text("title").notNull(),
  description: text("description").notNull(),
  requirements: text("requirements").notNull().default(""),
  location: text("location").notNull(),
  workplaceType: text("workplace_type").notNull().default("Hybrid"),
  employmentType: text("employment_type").notNull().default("Full-time"),
  seniority: text("seniority").notNull().default("Mid-Senior level"),
  salaryMin: integer("salary_min"),
  salaryMax: integer("salary_max"),
  currency: text("currency").notNull().default("USD"),
  status: text("status").notNull().default("published"),
  tenantVisibility: text("tenant_visibility").notNull().default("public"),
  publishedAt: integer("published_at", { mode: "timestamp" }).default(sql`(unixepoch())`),
  ...timestamps,
}, (table) => [
  index("jobs_org_status_idx").on(table.organizationId, table.status),
  index("jobs_location_idx").on(table.location),
  index("jobs_published_idx").on(table.publishedAt),
]);

export const jobApplications = sqliteTable("job_applications", {
  id: text("id").primaryKey().$defaultFn(newId),
  jobId: text("job_id").notNull().references(() => jobs.id, { onDelete: "cascade" }),
  applicantId: text("applicant_id").notNull().references(() => users.id, { onDelete: "cascade" }),
  status: text("status", { enum: ["submitted", "reviewing", "interview", "rejected", "hired"] }).notNull().default("submitted"),
  coverNote: text("cover_note").notNull().default(""),
  resumeUrl: text("resume_url"),
  answers: text("answers", { mode: "json" }).$type<Record<string, string>>().notNull().default({}),
  createdAt: integer("created_at", { mode: "timestamp" }).notNull().default(sql`(unixepoch())`),
  updatedAt: integer("updated_at", { mode: "timestamp" }).notNull().default(sql`(unixepoch())`),
}, (table) => [
  uniqueIndex("job_applications_job_applicant_unique").on(table.jobId, table.applicantId),
  index("job_applications_applicant_idx").on(table.applicantId),
]);

export const savedJobs = sqliteTable("saved_jobs", {
  jobId: text("job_id").notNull().references(() => jobs.id, { onDelete: "cascade" }),
  userId: text("user_id").notNull().references(() => users.id, { onDelete: "cascade" }),
  createdAt: integer("created_at", { mode: "timestamp" }).notNull().default(sql`(unixepoch())`),
}, (table) => [
  primaryKey({ columns: [table.jobId, table.userId] }),
  index("saved_jobs_user_idx").on(table.userId, table.createdAt),
]);

export const auditLogs = sqliteTable("audit_logs", {
  id: text("id").primaryKey().$defaultFn(newId),
  tenantId: text("tenant_id").references(() => organizations.id, { onDelete: "set null" }),
  actorId: text("actor_id").references(() => users.id, { onDelete: "set null" }),
  action: text("action").notNull(),
  entityType: text("entity_type").notNull(),
  entityId: text("entity_id").notNull(),
  metadata: text("metadata", { mode: "json" }).$type<Record<string, unknown>>().notNull().default({}),
  createdAt: integer("created_at", { mode: "timestamp" }).notNull().default(sql`(unixepoch())`),
}, (table) => [index("audit_logs_tenant_created_idx").on(table.tenantId, table.createdAt)]);

export type User = typeof users.$inferSelect;
export type Job = typeof jobs.$inferSelect;
export type Organization = typeof organizations.$inferSelect;