phase 1-6 implemented

This commit is contained in:
dimitar committed 2026-03-10 04:14:03 +01:00
1 parent 22c274bb83
commit 2c4927478a
73 files changed
+9702 -2354

No files matched your search

@@ -0,0 +1,174 @@
CREATE TABLE `attendance` (
`id` text PRIMARY KEY NOT NULL,
`user_id` text NOT NULL,
`check_in_time` integer NOT NULL,
`check_out_time` integer,
`type` text DEFAULT 'gym' NOT NULL,
`notes` text,
`created_at` integer NOT NULL,
FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE INDEX `attendance_user_id_idx` ON `attendance` (`user_id`);--> statement-breakpoint
CREATE INDEX `attendance_check_in_time_idx` ON `attendance` (`check_in_time`);--> statement-breakpoint
CREATE INDEX `attendance_user_check_in_idx` ON `attendance` (`user_id`,`check_in_time`);--> statement-breakpoint
CREATE TABLE `clients` (
`id` text PRIMARY KEY NOT NULL,
`user_id` text NOT NULL,
`membership_type` text DEFAULT 'basic' NOT NULL,
`membership_status` text DEFAULT 'active' NOT NULL,
`join_date` integer NOT NULL,
`last_visit` integer,
`emergency_contact_name` text,
`emergency_contact_phone` text,
`emergency_contact_relationship` text,
`created_at` integer NOT NULL,
`updated_at` integer NOT NULL,
FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE UNIQUE INDEX `clients_user_id_unique` ON `clients` (`user_id`);--> statement-breakpoint
CREATE INDEX `clients_user_id_idx` ON `clients` (`user_id`);--> statement-breakpoint
CREATE INDEX `clients_membership_status_idx` ON `clients` (`membership_status`);--> statement-breakpoint
CREATE TABLE `fitness_goals` (
`id` text PRIMARY KEY NOT NULL,
`user_id` text NOT NULL,
`fitness_profile_id` text,
`goal_type` text NOT NULL,
`title` text NOT NULL,
`description` text,
`target_value` real,
`current_value` real,
`unit` text,
`start_date` integer NOT NULL,
`target_date` integer,
`completed_date` integer,
`status` text DEFAULT 'active' NOT NULL,
`progress` real DEFAULT 0,
`priority` text DEFAULT 'medium',
`notes` text,
`created_at` integer NOT NULL,
`updated_at` integer NOT NULL,
FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON UPDATE no action ON DELETE cascade,
FOREIGN KEY (`fitness_profile_id`) REFERENCES `fitness_profiles`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE INDEX `fitness_goals_user_id_idx` ON `fitness_goals` (`user_id`);--> statement-breakpoint
CREATE INDEX `fitness_goals_status_idx` ON `fitness_goals` (`status`);--> statement-breakpoint
CREATE INDEX `fitness_goals_user_status_idx` ON `fitness_goals` (`user_id`,`status`);--> statement-breakpoint
CREATE TABLE `fitness_profiles` (
`id` text PRIMARY KEY NOT NULL,
`user_id` text NOT NULL,
`height` real,
`weight` real,
`age` integer,
`gender` text,
`fitness_goals` text,
`activity_level` text,
`medical_conditions` text,
`allergies` text,
`injuries` text,
`created_at` integer NOT NULL,
`updated_at` integer NOT NULL,
FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE UNIQUE INDEX `fitness_profiles_user_id_unique` ON `fitness_profiles` (`user_id`);--> statement-breakpoint
CREATE INDEX `fitness_profiles_user_id_idx` ON `fitness_profiles` (`user_id`);--> statement-breakpoint
CREATE TABLE `gyms` (
`id` text PRIMARY KEY NOT NULL,
`name` text NOT NULL,
`location` text,
`status` text DEFAULT 'active' NOT NULL,
`admin_user_id` text NOT NULL,
`created_at` integer NOT NULL,
`updated_at` integer NOT NULL,
FOREIGN KEY (`admin_user_id`) REFERENCES `users`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE INDEX `gyms_admin_user_id_idx` ON `gyms` (`admin_user_id`);--> statement-breakpoint
CREATE INDEX `gyms_status_idx` ON `gyms` (`status`);--> statement-breakpoint
CREATE TABLE `notifications` (
`id` text PRIMARY KEY NOT NULL,
`user_id` text NOT NULL,
`title` text NOT NULL,
`message` text NOT NULL,
`type` text NOT NULL,
`read` integer DEFAULT false NOT NULL,
`created_at` integer NOT NULL,
FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE INDEX `notifications_user_id_idx` ON `notifications` (`user_id`);--> statement-breakpoint
CREATE INDEX `notifications_read_idx` ON `notifications` (`read`);--> statement-breakpoint
CREATE INDEX `notifications_user_read_idx` ON `notifications` (`user_id`,`read`);--> statement-breakpoint
CREATE TABLE `payments` (
`id` text PRIMARY KEY NOT NULL,
`client_id` text NOT NULL,
`amount` real NOT NULL,
`currency` text DEFAULT 'USD' NOT NULL,
`status` text DEFAULT 'pending' NOT NULL,
`payment_method` text NOT NULL,
`due_date` integer NOT NULL,
`paid_at` integer,
`description` text NOT NULL,
`created_at` integer NOT NULL,
`updated_at` integer NOT NULL,
FOREIGN KEY (`client_id`) REFERENCES `clients`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE INDEX `payments_client_id_idx` ON `payments` (`client_id`);--> statement-breakpoint
CREATE INDEX `payments_status_idx` ON `payments` (`status`);--> statement-breakpoint
CREATE INDEX `payments_due_date_idx` ON `payments` (`due_date`);--> statement-breakpoint
CREATE TABLE `recommendations` (
`id` text PRIMARY KEY NOT NULL,
`user_id` text NOT NULL,
`fitness_profile_id` text NOT NULL,
`recommendation_text` text NOT NULL,
`activity_plan` text NOT NULL,
`diet_plan` text NOT NULL,
`status` text DEFAULT 'pending' NOT NULL,
`generated_at` integer NOT NULL,
`approved_at` integer,
`approved_by` text,
`created_at` integer NOT NULL,
`updated_at` integer NOT NULL,
FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON UPDATE no action ON DELETE cascade,
FOREIGN KEY (`fitness_profile_id`) REFERENCES `fitness_profiles`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE INDEX `recommendations_user_id_idx` ON `recommendations` (`user_id`);--> statement-breakpoint
CREATE INDEX `recommendations_status_idx` ON `recommendations` (`status`);--> statement-breakpoint
CREATE INDEX `recommendations_fitness_profile_id_idx` ON `recommendations` (`fitness_profile_id`);--> statement-breakpoint
CREATE TABLE `trainer_clients` (
`id` text PRIMARY KEY NOT NULL,
`trainer_user_id` text NOT NULL,
`client_user_id` text NOT NULL,
`gym_id` text NOT NULL,
`created_at` integer NOT NULL,
FOREIGN KEY (`trainer_user_id`) REFERENCES `users`(`id`) ON UPDATE no action ON DELETE cascade,
FOREIGN KEY (`client_user_id`) REFERENCES `users`(`id`) ON UPDATE no action ON DELETE cascade,
FOREIGN KEY (`gym_id`) REFERENCES `gyms`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE INDEX `trainer_clients_trainer_id_idx` ON `trainer_clients` (`trainer_user_id`);--> statement-breakpoint
CREATE INDEX `trainer_clients_client_id_idx` ON `trainer_clients` (`client_user_id`);--> statement-breakpoint
CREATE INDEX `trainer_clients_gym_id_idx` ON `trainer_clients` (`gym_id`);--> statement-breakpoint
CREATE UNIQUE INDEX `trainer_client_unique` ON `trainer_clients` (`trainer_user_id`,`client_user_id`);--> statement-breakpoint
CREATE TABLE `users` (
`id` text PRIMARY KEY NOT NULL,
`email` text NOT NULL,
`first_name` text NOT NULL,
`last_name` text NOT NULL,
`password` text,
`role` text DEFAULT 'client' NOT NULL,
`phone` text,
`gym_id` text,
`created_at` integer NOT NULL,
`updated_at` integer NOT NULL
);
--> statement-breakpoint
CREATE UNIQUE INDEX `users_email_unique` ON `users` (`email`);--> statement-breakpoint
CREATE INDEX `users_email_idx` ON `users` (`email`);--> statement-breakpoint
CREATE INDEX `users_gym_id_idx` ON `users` (`gym_id`);--> statement-breakpoint
CREATE INDEX `users_role_idx` ON `users` (`role`);
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,13 @@
{
"version": "7",
"dialect": "sqlite",
"entries": [
{
"idx": 0,
"version": "6",
"when": 1773106919464,
"tag": "0000_rich_rictor",
"breakpoints": true
}
]
}
+4 -2
View File
@@ -7,7 +7,9 @@
"build": "tsc",
"dev": "tsc --watch",
"typecheck": "tsc --noEmit",
"db:push": "drizzle-kit push:sqlite",
"db:generate": "drizzle-kit generate",
"db:migrate": "drizzle-kit migrate",
"db:push": "drizzle-kit push",
"db:studio": "drizzle-kit studio"
},
"dependencies": {
@@ -19,4 +21,4 @@
"typescript": "^5.9.3",
"drizzle-kit": "^0.31.6"
}
}
}
+359 -251
View File
@@ -1,271 +1,379 @@
import { sqliteTable, text, integer, real } from "drizzle-orm/sqlite-core";
import {
sqliteTable,
text,
integer,
real,
index,
unique,
} from "drizzle-orm/sqlite-core";
export const users = sqliteTable("users", {
id: text("id").primaryKey(),
email: text("email").notNull().unique(),
firstName: text("first_name").notNull(),
lastName: text("last_name").notNull(),
password: text("password"), // Optional - Clerk handles authentication
role: text("role", {
enum: ["superAdmin", "admin", "trainer", "client", "generalUser"],
})
.notNull()
.default("client"),
phone: text("phone"),
// Remove direct foreign key reference to avoid circular dependency; validate at application level
gymId: text("gym_id"),
createdAt: integer("created_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
updatedAt: integer("updated_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
});
export const gyms = sqliteTable("gyms", {
id: text("id").primaryKey(),
name: text("name").notNull(),
location: text("location"),
status: text("status", { enum: ["active", "inactive"] })
.notNull()
.default("active"),
adminUserId: text("admin_user_id")
.notNull()
.references(() => users.id, { onDelete: "cascade" }),
createdAt: integer("created_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
updatedAt: integer("updated_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
});
export const clients = sqliteTable("clients", {
id: text("id").primaryKey(),
userId: text("user_id")
.notNull()
.references(() => users.id, { onDelete: "cascade" }),
membershipType: text("membership_type", { enum: ["basic", "premium", "vip"] })
.notNull()
.default("basic"),
membershipStatus: text("membership_status", {
enum: ["active", "inactive", "suspended"],
})
.notNull()
.default("active"),
joinDate: integer("join_date", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
lastVisit: integer("last_visit", { mode: "timestamp" }),
emergencyContactName: text("emergency_contact_name"),
emergencyContactPhone: text("emergency_contact_phone"),
emergencyContactRelationship: text("emergency_contact_relationship"),
createdAt: integer("created_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
updatedAt: integer("updated_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
});
export const payments = sqliteTable("payments", {
id: text("id").primaryKey(),
clientId: text("client_id")
.notNull()
.references(() => clients.id, { onDelete: "cascade" }),
amount: real("amount").notNull(),
currency: text("currency").notNull().default("USD"),
status: text("status", {
enum: ["pending", "completed", "failed", "refunded"],
})
.notNull()
.default("pending"),
paymentMethod: text("payment_method", {
enum: ["cash", "card", "bank_transfer"],
}).notNull(),
dueDate: integer("due_date", { mode: "timestamp" }).notNull(),
paidAt: integer("paid_at", { mode: "timestamp" }),
description: text("description").notNull(),
createdAt: integer("created_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
updatedAt: integer("updated_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
});
export const attendance = sqliteTable("attendance", {
id: text("id").primaryKey(),
userId: text("user_id")
.notNull()
.references(() => users.id, { onDelete: "cascade" }),
checkInTime: integer("check_in_time", { mode: "timestamp" }).notNull(),
checkOutTime: integer("check_out_time", { mode: "timestamp" }),
type: text("type", { enum: ["gym", "class", "personal_training"] })
.notNull()
.default("gym"),
notes: text("notes"),
createdAt: integer("created_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
});
export const notifications = sqliteTable("notifications", {
id: text("id").primaryKey(),
userId: text("user_id")
.notNull()
.references(() => users.id, { onDelete: "cascade" }),
title: text("title").notNull(),
message: text("message").notNull(),
type: text("type", {
enum: ["payment_reminder", "attendance", "promotion", "system"],
}).notNull(),
read: integer("read", { mode: "boolean" }).notNull().default(false),
createdAt: integer("created_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
});
export const fitnessProfiles = sqliteTable("fitness_profiles", {
id: text("id").primaryKey(),
userId: text("user_id")
.notNull()
.unique()
.references(() => users.id, { onDelete: "cascade" }),
height: real("height"), // in cm
weight: real("weight"), // in kg
age: integer("age"),
gender: text("gender", {
enum: ["male", "female", "other", "prefer_not_to_say"],
export const users = sqliteTable(
"users",
{
id: text("id").primaryKey(),
email: text("email").notNull().unique(),
firstName: text("first_name").notNull(),
lastName: text("last_name").notNull(),
password: text("password"), // Optional - Clerk handles authentication
role: text("role", {
enum: ["superAdmin", "admin", "trainer", "client"],
})
.notNull()
.default("client"),
phone: text("phone"),
gymId: text("gym_id"), // FK reference added after gyms table
createdAt: integer("created_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
updatedAt: integer("updated_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
},
(table) => ({
emailIdx: index("users_email_idx").on(table.email),
gymIdIdx: index("users_gym_id_idx").on(table.gymId),
roleIdx: index("users_role_idx").on(table.role),
}),
fitnessGoals: text("fitness_goals", { mode: "json" }).$type<string[]>(),
activityLevel: text("activity_level", {
enum: [
"sedentary",
"lightly_active",
"moderately_active",
"very_active",
"extremely_active",
],
);
export const gyms = sqliteTable(
"gyms",
{
id: text("id").primaryKey(),
name: text("name").notNull(),
location: text("location"),
status: text("status", { enum: ["active", "inactive"] })
.notNull()
.default("active"),
adminUserId: text("admin_user_id")
.notNull()
.references(() => users.id, { onDelete: "cascade" }),
createdAt: integer("created_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
updatedAt: integer("updated_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
},
(table) => ({
adminUserIdIdx: index("gyms_admin_user_id_idx").on(table.adminUserId),
statusIdx: index("gyms_status_idx").on(table.status),
}),
medicalConditions: text("medical_conditions"),
allergies: text("allergies"),
injuries: text("injuries"),
createdAt: integer("created_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
updatedAt: integer("updated_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
});
);
export const fitnessGoals = sqliteTable("fitness_goals", {
id: text("id").primaryKey(),
userId: text("user_id")
.notNull()
.references(() => users.id, { onDelete: "cascade" }),
fitnessProfileId: text("fitness_profile_id").references(
() => fitnessProfiles.id,
{ onDelete: "cascade" },
),
export const clients = sqliteTable(
"clients",
{
id: text("id").primaryKey(),
userId: text("user_id")
.notNull()
.unique()
.references(() => users.id, { onDelete: "cascade" }),
membershipType: text("membership_type", {
enum: ["basic", "premium", "vip"],
})
.notNull()
.default("basic"),
membershipStatus: text("membership_status", {
enum: ["active", "inactive", "suspended"],
})
.notNull()
.default("active"),
joinDate: integer("join_date", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
lastVisit: integer("last_visit", { mode: "timestamp" }),
emergencyContactName: text("emergency_contact_name"),
emergencyContactPhone: text("emergency_contact_phone"),
emergencyContactRelationship: text("emergency_contact_relationship"),
createdAt: integer("created_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
updatedAt: integer("updated_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
},
(table) => ({
userIdIdx: index("clients_user_id_idx").on(table.userId),
membershipStatusIdx: index("clients_membership_status_idx").on(
table.membershipStatus,
),
}),
);
// Goal details
goalType: text("goal_type", {
enum: [
"weight_target",
"strength_milestone",
"endurance_target",
"flexibility_goal",
"habit_building",
"custom",
],
}).notNull(),
export const payments = sqliteTable(
"payments",
{
id: text("id").primaryKey(),
clientId: text("client_id")
.notNull()
.references(() => clients.id, { onDelete: "cascade" }),
amount: real("amount").notNull(),
currency: text("currency").notNull().default("USD"),
status: text("status", {
enum: ["pending", "completed", "failed", "refunded"],
})
.notNull()
.default("pending"),
paymentMethod: text("payment_method", {
enum: ["cash", "card", "bank_transfer"],
}).notNull(),
dueDate: integer("due_date", { mode: "timestamp" }).notNull(),
paidAt: integer("paid_at", { mode: "timestamp" }),
description: text("description").notNull(),
createdAt: integer("created_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
updatedAt: integer("updated_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
},
(table) => ({
clientIdIdx: index("payments_client_id_idx").on(table.clientId),
statusIdx: index("payments_status_idx").on(table.status),
dueDateIdx: index("payments_due_date_idx").on(table.dueDate),
}),
);
title: text("title").notNull(),
description: text("description"),
export const attendance = sqliteTable(
"attendance",
{
id: text("id").primaryKey(),
userId: text("user_id")
.notNull()
.references(() => users.id, { onDelete: "cascade" }),
checkInTime: integer("check_in_time", { mode: "timestamp" }).notNull(),
checkOutTime: integer("check_out_time", { mode: "timestamp" }),
type: text("type", { enum: ["gym", "class", "personal_training"] })
.notNull()
.default("gym"),
notes: text("notes"),
createdAt: integer("created_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
},
(table) => ({
userIdIdx: index("attendance_user_id_idx").on(table.userId),
checkInTimeIdx: index("attendance_check_in_time_idx").on(table.checkInTime),
// Composite index for common query: get user's attendance history
userCheckInIdx: index("attendance_user_check_in_idx").on(
table.userId,
table.checkInTime,
),
}),
);
// Measurable targets
targetValue: real("target_value"), // e.g., 70 (kg), 100 (kg bench press)
currentValue: real("current_value"), // Current progress
unit: text("unit"), // kg, km, reps, etc.
export const notifications = sqliteTable(
"notifications",
{
id: text("id").primaryKey(),
userId: text("user_id")
.notNull()
.references(() => users.id, { onDelete: "cascade" }),
title: text("title").notNull(),
message: text("message").notNull(),
type: text("type", {
enum: ["payment_reminder", "attendance", "promotion", "system"],
}).notNull(),
read: integer("read", { mode: "boolean" }).notNull().default(false),
createdAt: integer("created_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
},
(table) => ({
userIdIdx: index("notifications_user_id_idx").on(table.userId),
readIdx: index("notifications_read_idx").on(table.read),
// Composite index for common query: get user's unread notifications
userReadIdx: index("notifications_user_read_idx").on(
table.userId,
table.read,
),
}),
);
// Timeline
startDate: integer("start_date", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
targetDate: integer("target_date", { mode: "timestamp" }),
completedDate: integer("completed_date", { mode: "timestamp" }),
export const fitnessProfiles = sqliteTable(
"fitness_profiles",
{
id: text("id").primaryKey(),
userId: text("user_id")
.notNull()
.unique()
.references(() => users.id, { onDelete: "cascade" }),
height: real("height"), // in cm
weight: real("weight"), // in kg
age: integer("age"),
gender: text("gender", {
enum: ["male", "female", "other", "prefer_not_to_say"],
}),
fitnessGoals: text("fitness_goals", { mode: "json" }).$type<string[]>(),
activityLevel: text("activity_level", {
enum: [
"sedentary",
"lightly_active",
"moderately_active",
"very_active",
"extremely_active",
],
}),
medicalConditions: text("medical_conditions"),
allergies: text("allergies"),
injuries: text("injuries"),
createdAt: integer("created_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
updatedAt: integer("updated_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
},
(table) => ({
userIdIdx: index("fitness_profiles_user_id_idx").on(table.userId),
}),
);
// Status tracking
status: text("status", {
enum: ["active", "completed", "abandoned", "paused"],
})
.notNull()
.default("active"),
export const fitnessGoals = sqliteTable(
"fitness_goals",
{
id: text("id").primaryKey(),
userId: text("user_id")
.notNull()
.references(() => users.id, { onDelete: "cascade" }),
fitnessProfileId: text("fitness_profile_id").references(
() => fitnessProfiles.id,
{ onDelete: "cascade" },
),
progress: real("progress").default(0), // 0-100 percentage
// Goal details
goalType: text("goal_type", {
enum: [
"weight_target",
"strength_milestone",
"endurance_target",
"flexibility_goal",
"habit_building",
"custom",
],
}).notNull(),
// Metadata
priority: text("priority", {
enum: ["low", "medium", "high"],
}).default("medium"),
title: text("title").notNull(),
description: text("description"),
notes: text("notes"),
// Measurable targets
targetValue: real("target_value"), // e.g., 70 (kg), 100 (kg bench press)
currentValue: real("current_value"), // Current progress
unit: text("unit"), // kg, km, reps, etc.
createdAt: integer("created_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
updatedAt: integer("updated_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
});
// Timeline
startDate: integer("start_date", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
targetDate: integer("target_date", { mode: "timestamp" }),
completedDate: integer("completed_date", { mode: "timestamp" }),
// Status tracking
status: text("status", {
enum: ["active", "completed", "abandoned", "paused"],
})
.notNull()
.default("active"),
progress: real("progress").default(0), // 0-100 percentage
// Metadata
priority: text("priority", {
enum: ["low", "medium", "high"],
}).default("medium"),
notes: text("notes"),
createdAt: integer("created_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
updatedAt: integer("updated_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
},
(table) => ({
userIdIdx: index("fitness_goals_user_id_idx").on(table.userId),
statusIdx: index("fitness_goals_status_idx").on(table.status),
// Composite index for common query: get user's active goals
userStatusIdx: index("fitness_goals_user_status_idx").on(
table.userId,
table.status,
),
}),
);
// Removed local invitations table; Clerk invitations are the source of truth
export const trainerClients = sqliteTable("trainer_clients", {
id: text("id").primaryKey(),
trainerUserId: text("trainer_user_id")
.notNull()
.references(() => users.id, { onDelete: "cascade" }),
clientUserId: text("client_user_id")
.notNull()
.references(() => users.id, { onDelete: "cascade" }),
gymId: text("gym_id")
.notNull()
.references(() => gyms.id, { onDelete: "cascade" }),
createdAt: integer("created_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
});
export const trainerClients = sqliteTable(
"trainer_clients",
{
id: text("id").primaryKey(),
trainerUserId: text("trainer_user_id")
.notNull()
.references(() => users.id, { onDelete: "cascade" }),
clientUserId: text("client_user_id")
.notNull()
.references(() => users.id, { onDelete: "cascade" }),
gymId: text("gym_id")
.notNull()
.references(() => gyms.id, { onDelete: "cascade" }),
createdAt: integer("created_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
},
(table) => ({
trainerIdIdx: index("trainer_clients_trainer_id_idx").on(
table.trainerUserId,
),
clientIdIdx: index("trainer_clients_client_id_idx").on(table.clientUserId),
gymIdIdx: index("trainer_clients_gym_id_idx").on(table.gymId),
// Composite unique constraint: prevent duplicate trainer-client assignments
trainerClientUnique: unique("trainer_client_unique").on(
table.trainerUserId,
table.clientUserId,
),
}),
);
export const recommendations = sqliteTable("recommendations", {
id: text("id").primaryKey(),
userId: text("user_id")
.notNull()
.references(() => users.id, { onDelete: "cascade" }),
fitnessProfileId: text("fitness_profile_id")
.notNull()
.references(() => fitnessProfiles.id, { onDelete: "cascade" }),
recommendationText: text("recommendation_text").notNull(),
activityPlan: text("activity_plan").notNull(),
dietPlan: text("diet_plan").notNull(),
status: text("status", {
enum: ["pending", "approved", "rejected"],
})
.notNull()
.default("pending"),
generatedAt: integer("generated_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
approvedAt: integer("approved_at", { mode: "timestamp" }),
approvedBy: text("approved_by"), // User ID of admin/trainer
createdAt: integer("created_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
updatedAt: integer("updated_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
});
export const recommendations = sqliteTable(
"recommendations",
{
id: text("id").primaryKey(),
userId: text("user_id")
.notNull()
.references(() => users.id, { onDelete: "cascade" }),
fitnessProfileId: text("fitness_profile_id")
.notNull()
.references(() => fitnessProfiles.id, { onDelete: "cascade" }),
recommendationText: text("recommendation_text").notNull(),
activityPlan: text("activity_plan").notNull(),
dietPlan: text("diet_plan").notNull(),
status: text("status", {
enum: ["pending", "approved", "rejected"],
})
.notNull()
.default("pending"),
generatedAt: integer("generated_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
approvedAt: integer("approved_at", { mode: "timestamp" }),
approvedBy: text("approved_by"), // User ID of admin/trainer
createdAt: integer("created_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
updatedAt: integer("updated_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
},
(table) => ({
userIdIdx: index("recommendations_user_id_idx").on(table.userId),
statusIdx: index("recommendations_status_idx").on(table.status),
fitnessProfileIdIdx: index("recommendations_fitness_profile_id_idx").on(
table.fitnessProfileId,
),
}),
);
export type User = typeof users.$inferSelect;
export type NewUser = typeof users.$inferInsert;