267 行
10 KiB
TypeScript
267 行
10 KiB
TypeScript
import { eq, desc, and, sql } from "drizzle-orm";
|
|
import { drizzle } from "drizzle-orm/mysql2";
|
|
import {
|
|
InsertUser, users,
|
|
usernameAccounts,
|
|
trainingPlans, InsertTrainingPlan,
|
|
trainingVideos, InsertTrainingVideo,
|
|
poseAnalyses, InsertPoseAnalysis,
|
|
trainingRecords, InsertTrainingRecord,
|
|
ratingHistory, InsertRatingHistory,
|
|
} from "../drizzle/schema";
|
|
import { ENV } from './_core/env';
|
|
|
|
let _db: ReturnType<typeof drizzle> | null = null;
|
|
|
|
export async function getDb() {
|
|
if (!_db && process.env.DATABASE_URL) {
|
|
try {
|
|
_db = drizzle(process.env.DATABASE_URL);
|
|
} catch (error) {
|
|
console.warn("[Database] Failed to connect:", error);
|
|
_db = null;
|
|
}
|
|
}
|
|
return _db;
|
|
}
|
|
|
|
// ===== USER OPERATIONS =====
|
|
|
|
export async function upsertUser(user: InsertUser): Promise<void> {
|
|
if (!user.openId) throw new Error("User openId is required for upsert");
|
|
const db = await getDb();
|
|
if (!db) { console.warn("[Database] Cannot upsert user: database not available"); return; }
|
|
|
|
try {
|
|
const values: InsertUser = { openId: user.openId };
|
|
const updateSet: Record<string, unknown> = {};
|
|
|
|
const textFields = ["name", "email", "loginMethod"] as const;
|
|
type TextField = (typeof textFields)[number];
|
|
const assignNullable = (field: TextField) => {
|
|
const value = user[field];
|
|
if (value === undefined) return;
|
|
const normalized = value ?? null;
|
|
values[field] = normalized;
|
|
updateSet[field] = normalized;
|
|
};
|
|
textFields.forEach(assignNullable);
|
|
|
|
if (user.lastSignedIn !== undefined) { values.lastSignedIn = user.lastSignedIn; updateSet.lastSignedIn = user.lastSignedIn; }
|
|
if (user.role !== undefined) { values.role = user.role; updateSet.role = user.role; }
|
|
else if (user.openId === ENV.ownerOpenId) { values.role = 'admin'; updateSet.role = 'admin'; }
|
|
if (!values.lastSignedIn) values.lastSignedIn = new Date();
|
|
if (Object.keys(updateSet).length === 0) updateSet.lastSignedIn = new Date();
|
|
|
|
await db.insert(users).values(values).onDuplicateKeyUpdate({ set: updateSet });
|
|
} catch (error) { console.error("[Database] Failed to upsert user:", error); throw error; }
|
|
}
|
|
|
|
export async function getUserByOpenId(openId: string) {
|
|
const db = await getDb();
|
|
if (!db) return undefined;
|
|
const result = await db.select().from(users).where(eq(users.openId, openId)).limit(1);
|
|
return result.length > 0 ? result[0] : undefined;
|
|
}
|
|
|
|
export async function getUserByUsername(username: string) {
|
|
const db = await getDb();
|
|
if (!db) return undefined;
|
|
const result = await db.select().from(usernameAccounts).where(eq(usernameAccounts.username, username)).limit(1);
|
|
if (result.length === 0) return undefined;
|
|
const userResult = await db.select().from(users).where(eq(users.id, result[0].userId)).limit(1);
|
|
return userResult.length > 0 ? userResult[0] : undefined;
|
|
}
|
|
|
|
export async function createUsernameAccount(username: string): Promise<{ user: typeof users.$inferSelect; isNew: boolean }> {
|
|
const db = await getDb();
|
|
if (!db) throw new Error("Database not available");
|
|
|
|
// Check if username already exists
|
|
const existing = await db.select().from(usernameAccounts).where(eq(usernameAccounts.username, username)).limit(1);
|
|
if (existing.length > 0) {
|
|
const user = await db.select().from(users).where(eq(users.id, existing[0].userId)).limit(1);
|
|
if (user.length > 0) {
|
|
await db.update(users).set({ lastSignedIn: new Date() }).where(eq(users.id, user[0].id));
|
|
return { user: user[0], isNew: false };
|
|
}
|
|
}
|
|
|
|
// Create new user with username as openId
|
|
const openId = `username_${username}_${Date.now()}`;
|
|
await db.insert(users).values({
|
|
openId,
|
|
name: username,
|
|
loginMethod: "username",
|
|
lastSignedIn: new Date(),
|
|
ntrpRating: 1.5,
|
|
totalSessions: 0,
|
|
totalMinutes: 0,
|
|
});
|
|
|
|
const newUser = await db.select().from(users).where(eq(users.openId, openId)).limit(1);
|
|
if (newUser.length === 0) throw new Error("Failed to create user");
|
|
|
|
await db.insert(usernameAccounts).values({ username, userId: newUser[0].id });
|
|
return { user: newUser[0], isNew: true };
|
|
}
|
|
|
|
export async function updateUserProfile(userId: number, data: {
|
|
skillLevel?: "beginner" | "intermediate" | "advanced";
|
|
trainingGoals?: string;
|
|
ntrpRating?: number;
|
|
totalSessions?: number;
|
|
totalMinutes?: number;
|
|
}) {
|
|
const db = await getDb();
|
|
if (!db) return;
|
|
await db.update(users).set(data).where(eq(users.id, userId));
|
|
}
|
|
|
|
// ===== TRAINING PLAN OPERATIONS =====
|
|
|
|
export async function createTrainingPlan(plan: InsertTrainingPlan) {
|
|
const db = await getDb();
|
|
if (!db) throw new Error("Database not available");
|
|
// Deactivate existing active plans
|
|
await db.update(trainingPlans).set({ isActive: 0 }).where(and(eq(trainingPlans.userId, plan.userId), eq(trainingPlans.isActive, 1)));
|
|
const result = await db.insert(trainingPlans).values(plan);
|
|
return result[0].insertId;
|
|
}
|
|
|
|
export async function getUserTrainingPlans(userId: number) {
|
|
const db = await getDb();
|
|
if (!db) return [];
|
|
return db.select().from(trainingPlans).where(eq(trainingPlans.userId, userId)).orderBy(desc(trainingPlans.createdAt));
|
|
}
|
|
|
|
export async function getActivePlan(userId: number) {
|
|
const db = await getDb();
|
|
if (!db) return undefined;
|
|
const result = await db.select().from(trainingPlans).where(and(eq(trainingPlans.userId, userId), eq(trainingPlans.isActive, 1))).limit(1);
|
|
return result.length > 0 ? result[0] : undefined;
|
|
}
|
|
|
|
export async function updateTrainingPlan(planId: number, data: Partial<InsertTrainingPlan>) {
|
|
const db = await getDb();
|
|
if (!db) return;
|
|
await db.update(trainingPlans).set(data).where(eq(trainingPlans.id, planId));
|
|
}
|
|
|
|
// ===== VIDEO OPERATIONS =====
|
|
|
|
export async function createVideo(video: InsertTrainingVideo) {
|
|
const db = await getDb();
|
|
if (!db) throw new Error("Database not available");
|
|
const result = await db.insert(trainingVideos).values(video);
|
|
return result[0].insertId;
|
|
}
|
|
|
|
export async function getUserVideos(userId: number) {
|
|
const db = await getDb();
|
|
if (!db) return [];
|
|
return db.select().from(trainingVideos).where(eq(trainingVideos.userId, userId)).orderBy(desc(trainingVideos.createdAt));
|
|
}
|
|
|
|
export async function getVideoById(videoId: number) {
|
|
const db = await getDb();
|
|
if (!db) return undefined;
|
|
const result = await db.select().from(trainingVideos).where(eq(trainingVideos.id, videoId)).limit(1);
|
|
return result.length > 0 ? result[0] : undefined;
|
|
}
|
|
|
|
export async function updateVideoStatus(videoId: number, status: "pending" | "analyzing" | "completed" | "failed") {
|
|
const db = await getDb();
|
|
if (!db) return;
|
|
await db.update(trainingVideos).set({ analysisStatus: status }).where(eq(trainingVideos.id, videoId));
|
|
}
|
|
|
|
// ===== POSE ANALYSIS OPERATIONS =====
|
|
|
|
export async function createPoseAnalysis(analysis: InsertPoseAnalysis) {
|
|
const db = await getDb();
|
|
if (!db) throw new Error("Database not available");
|
|
const result = await db.insert(poseAnalyses).values(analysis);
|
|
return result[0].insertId;
|
|
}
|
|
|
|
export async function getAnalysisByVideoId(videoId: number) {
|
|
const db = await getDb();
|
|
if (!db) return undefined;
|
|
const result = await db.select().from(poseAnalyses).where(eq(poseAnalyses.videoId, videoId)).orderBy(desc(poseAnalyses.createdAt)).limit(1);
|
|
return result.length > 0 ? result[0] : undefined;
|
|
}
|
|
|
|
export async function getUserAnalyses(userId: number) {
|
|
const db = await getDb();
|
|
if (!db) return [];
|
|
return db.select().from(poseAnalyses).where(eq(poseAnalyses.userId, userId)).orderBy(desc(poseAnalyses.createdAt));
|
|
}
|
|
|
|
// ===== TRAINING RECORD OPERATIONS =====
|
|
|
|
export async function createTrainingRecord(record: InsertTrainingRecord) {
|
|
const db = await getDb();
|
|
if (!db) throw new Error("Database not available");
|
|
const result = await db.insert(trainingRecords).values(record);
|
|
return result[0].insertId;
|
|
}
|
|
|
|
export async function getUserTrainingRecords(userId: number, limit = 50) {
|
|
const db = await getDb();
|
|
if (!db) return [];
|
|
return db.select().from(trainingRecords).where(eq(trainingRecords.userId, userId)).orderBy(desc(trainingRecords.trainingDate)).limit(limit);
|
|
}
|
|
|
|
export async function markRecordCompleted(recordId: number, poseScore?: number) {
|
|
const db = await getDb();
|
|
if (!db) return;
|
|
await db.update(trainingRecords).set({ completed: 1, poseScore: poseScore ?? null }).where(eq(trainingRecords.id, recordId));
|
|
}
|
|
|
|
// ===== RATING HISTORY OPERATIONS =====
|
|
|
|
export async function createRatingEntry(entry: InsertRatingHistory) {
|
|
const db = await getDb();
|
|
if (!db) throw new Error("Database not available");
|
|
const result = await db.insert(ratingHistory).values(entry);
|
|
return result[0].insertId;
|
|
}
|
|
|
|
export async function getUserRatingHistory(userId: number, limit = 30) {
|
|
const db = await getDb();
|
|
if (!db) return [];
|
|
return db.select().from(ratingHistory).where(eq(ratingHistory.userId, userId)).orderBy(desc(ratingHistory.createdAt)).limit(limit);
|
|
}
|
|
|
|
// ===== STATS HELPERS =====
|
|
|
|
export async function getUserStats(userId: number) {
|
|
const db = await getDb();
|
|
if (!db) return null;
|
|
|
|
const [userRow] = await db.select().from(users).where(eq(users.id, userId)).limit(1);
|
|
if (!userRow) return null;
|
|
|
|
const analyses = await db.select().from(poseAnalyses).where(eq(poseAnalyses.userId, userId));
|
|
const records = await db.select().from(trainingRecords).where(eq(trainingRecords.userId, userId));
|
|
const videos = await db.select().from(trainingVideos).where(eq(trainingVideos.userId, userId));
|
|
const ratings = await db.select().from(ratingHistory).where(eq(ratingHistory.userId, userId)).orderBy(desc(ratingHistory.createdAt)).limit(30);
|
|
|
|
const completedRecords = records.filter(r => r.completed === 1);
|
|
const totalShots = analyses.reduce((sum, a) => sum + (a.shotCount || 0), 0);
|
|
const avgScore = analyses.length > 0 ? analyses.reduce((sum, a) => sum + (a.overallScore || 0), 0) / analyses.length : 0;
|
|
|
|
return {
|
|
ntrpRating: userRow.ntrpRating || 1.5,
|
|
totalSessions: completedRecords.length,
|
|
totalMinutes: records.reduce((sum, r) => sum + (r.durationMinutes || 0), 0),
|
|
totalVideos: videos.length,
|
|
analyzedVideos: videos.filter(v => v.analysisStatus === "completed").length,
|
|
totalShots,
|
|
averageScore: Math.round(avgScore * 10) / 10,
|
|
ratingHistory: ratings.reverse(),
|
|
recentAnalyses: analyses.slice(0, 10),
|
|
};
|
|
}
|