c
This commit is contained in:
1 parent
a5c297b911
commit
d7d270510f
4 files changed
+145
-46
No files matched your search
@@ -106,19 +106,61 @@ export class SQLiteDatabase implements IDatabase {
|
||||
CREATE INDEX IF NOT EXISTS idx_fitness_profiles_userId ON fitness_profiles(userId);
|
||||
`)
|
||||
|
||||
// Attendance table
|
||||
this.db.exec(`
|
||||
CREATE TABLE IF NOT EXISTS attendance (
|
||||
id TEXT PRIMARY KEY,
|
||||
clientId TEXT NOT NULL,
|
||||
checkInTime DATETIME NOT NULL,
|
||||
checkOutTime DATETIME,
|
||||
type TEXT NOT NULL CHECK (type IN ('gym', 'class', 'personal_training')),
|
||||
notes TEXT,
|
||||
createdAt DATETIME NOT NULL,
|
||||
FOREIGN KEY (clientId) REFERENCES clients (id) ON DELETE CASCADE
|
||||
)
|
||||
`)
|
||||
// Attendance table migration: change from clientId to userId
|
||||
// Check if old table exists and migrate
|
||||
const tableInfo = this.db.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name='attendance'").get() as any;
|
||||
|
||||
if (tableInfo) {
|
||||
// Check if table has clientId column (old schema)
|
||||
const columns = this.db.prepare("PRAGMA table_info(attendance)").all() as any[];
|
||||
const hasClientId = columns.some((col: any) => col.name === 'clientId');
|
||||
|
||||
if (hasClientId) {
|
||||
console.log('Migrating attendance table from clientId to userId...');
|
||||
|
||||
// Create new table with userId
|
||||
this.db.exec(`
|
||||
CREATE TABLE IF NOT EXISTS attendance_new (
|
||||
id TEXT PRIMARY KEY,
|
||||
userId TEXT NOT NULL,
|
||||
checkInTime DATETIME NOT NULL,
|
||||
checkOutTime DATETIME,
|
||||
type TEXT NOT NULL CHECK (type IN ('gym', 'class', 'personal_training')),
|
||||
notes TEXT,
|
||||
createdAt DATETIME NOT NULL,
|
||||
FOREIGN KEY (userId) REFERENCES users (id) ON DELETE CASCADE
|
||||
)
|
||||
`);
|
||||
|
||||
// Migrate data: map clientId to userId via clients table
|
||||
this.db.exec(`
|
||||
INSERT INTO attendance_new (id, userId, checkInTime, checkOutTime, type, notes, createdAt)
|
||||
SELECT a.id, c.userId, a.checkInTime, a.checkOutTime, a.type, a.notes, a.createdAt
|
||||
FROM attendance a
|
||||
JOIN clients c ON a.clientId = c.id
|
||||
`);
|
||||
|
||||
// Drop old table and rename new one
|
||||
this.db.exec(`DROP TABLE attendance`);
|
||||
this.db.exec(`ALTER TABLE attendance_new RENAME TO attendance`);
|
||||
|
||||
console.log('Attendance table migration completed');
|
||||
}
|
||||
} else {
|
||||
// Create new attendance table with userId
|
||||
this.db.exec(`
|
||||
CREATE TABLE IF NOT EXISTS attendance (
|
||||
id TEXT PRIMARY KEY,
|
||||
userId TEXT NOT NULL,
|
||||
checkInTime DATETIME NOT NULL,
|
||||
checkOutTime DATETIME,
|
||||
type TEXT NOT NULL CHECK (type IN ('gym', 'class', 'personal_training')),
|
||||
notes TEXT,
|
||||
createdAt DATETIME NOT NULL,
|
||||
FOREIGN KEY (userId) REFERENCES users (id) ON DELETE CASCADE
|
||||
)
|
||||
`);
|
||||
}
|
||||
|
||||
// Recommendations table
|
||||
// Removed DROP TABLE to persist data. Schema is now stable.
|
||||
@@ -140,16 +182,16 @@ export class SQLiteDatabase implements IDatabase {
|
||||
FOREIGN KEY (userId) REFERENCES users (id) ON DELETE CASCADE,
|
||||
FOREIGN KEY (fitnessProfileId) REFERENCES fitness_profiles (userId)
|
||||
)
|
||||
`)
|
||||
`);
|
||||
|
||||
this.db.exec(`
|
||||
CREATE INDEX IF NOT EXISTS idx_recommendations_userId ON recommendations(userId);
|
||||
`)
|
||||
`);
|
||||
|
||||
this.db.exec(`
|
||||
CREATE INDEX IF NOT EXISTS idx_attendance_clientId ON attendance(clientId);
|
||||
CREATE INDEX IF NOT EXISTS idx_attendance_userId ON attendance(userId);
|
||||
CREATE INDEX IF NOT EXISTS idx_attendance_checkInTime ON attendance(checkInTime);
|
||||
`)
|
||||
`);
|
||||
}
|
||||
|
||||
// User operations
|
||||
@@ -389,7 +431,7 @@ export class SQLiteDatabase implements IDatabase {
|
||||
}
|
||||
|
||||
// Attendance operations
|
||||
async checkIn(clientId: string, type: 'gym' | 'class' | 'personal_training', notes?: string): Promise<Attendance> {
|
||||
async checkIn(userId: string, type: 'gym' | 'class' | 'personal_training', notes?: string): Promise<Attendance> {
|
||||
if (!this.db) throw new Error('Database not connected')
|
||||
|
||||
const id = Math.random().toString(36).substr(2, 9)
|
||||
@@ -397,7 +439,7 @@ export class SQLiteDatabase implements IDatabase {
|
||||
|
||||
const attendance: Attendance = {
|
||||
id,
|
||||
clientId,
|
||||
userId,
|
||||
checkInTime: now,
|
||||
type,
|
||||
notes,
|
||||
@@ -405,20 +447,23 @@ export class SQLiteDatabase implements IDatabase {
|
||||
}
|
||||
|
||||
const stmt = this.db.prepare(
|
||||
`INSERT INTO attendance(id, clientId, checkInTime, type, notes, createdAt)
|
||||
`INSERT INTO attendance(id, userId, checkInTime, type, notes, createdAt)
|
||||
VALUES(?, ?, ?, ?, ?, ?)`
|
||||
)
|
||||
|
||||
stmt.run(
|
||||
attendance.id, attendance.clientId, attendance.checkInTime.toISOString(),
|
||||
attendance.id, attendance.userId, attendance.checkInTime.toISOString(),
|
||||
attendance.type, attendance.notes, attendance.createdAt.toISOString()
|
||||
)
|
||||
|
||||
// Update client last visit
|
||||
this.db.prepare('UPDATE clients SET lastVisit = ? WHERE id = ?').run(
|
||||
now.toISOString(),
|
||||
clientId
|
||||
)
|
||||
// Update client last visit if user is a client
|
||||
const client = await this.getClientByUserId(userId);
|
||||
if (client) {
|
||||
this.db.prepare('UPDATE clients SET lastVisit = ? WHERE id = ?').run(
|
||||
now.toISOString(),
|
||||
client.id
|
||||
);
|
||||
}
|
||||
|
||||
return attendance
|
||||
}
|
||||
@@ -440,10 +485,10 @@ export class SQLiteDatabase implements IDatabase {
|
||||
return row ? this.mapRowToAttendance(row) : null
|
||||
}
|
||||
|
||||
async getAttendanceHistory(clientId: string): Promise<Attendance[]> {
|
||||
async getAttendanceHistory(userId: string): Promise<Attendance[]> {
|
||||
if (!this.db) throw new Error('Database not connected')
|
||||
const stmt = this.db.prepare('SELECT * FROM attendance WHERE clientId = ? ORDER BY checkInTime DESC')
|
||||
const rows = stmt.all(clientId)
|
||||
const stmt = this.db.prepare('SELECT * FROM attendance WHERE userId = ? ORDER BY checkInTime DESC')
|
||||
const rows = stmt.all(userId)
|
||||
return rows.map(row => this.mapRowToAttendance(row))
|
||||
}
|
||||
|
||||
@@ -454,10 +499,10 @@ export class SQLiteDatabase implements IDatabase {
|
||||
return rows.map(row => this.mapRowToAttendance(row))
|
||||
}
|
||||
|
||||
async getActiveCheckIn(clientId: string): Promise<Attendance | null> {
|
||||
async getActiveCheckIn(userId: string): Promise<Attendance | null> {
|
||||
if (!this.db) throw new Error('Database not connected')
|
||||
const stmt = this.db.prepare('SELECT * FROM attendance WHERE clientId = ? AND checkOutTime IS NULL ORDER BY checkInTime DESC LIMIT 1')
|
||||
const row = stmt.get(clientId)
|
||||
const stmt = this.db.prepare('SELECT * FROM attendance WHERE userId = ? AND checkOutTime IS NULL ORDER BY checkInTime DESC LIMIT 1')
|
||||
const row = stmt.get(userId)
|
||||
return row ? this.mapRowToAttendance(row) : null
|
||||
}
|
||||
|
||||
@@ -509,7 +554,7 @@ export class SQLiteDatabase implements IDatabase {
|
||||
private mapRowToAttendance(row: any): Attendance {
|
||||
return {
|
||||
id: row.id,
|
||||
clientId: row.clientId,
|
||||
userId: row.userId,
|
||||
checkInTime: new Date(row.checkInTime),
|
||||
checkOutTime: row.checkOutTime ? new Date(row.checkOutTime) : undefined,
|
||||
type: row.type,
|
||||
|
||||
Reference in new issue
Block a user