Checkpoint: Application complète de gestion des formations Manager Itinova avec toutes les fonctionnalités : gestion des formations/sessions/apprenants, système d'inscription avec validations, génération d'invitations Outlook, emails automatiques, exports PDF/Excel, et documentation complète.
This commit is contained in:
243
server/db.ts
243
server/db.ts
@@ -1,6 +1,17 @@
|
||||
import { eq } from "drizzle-orm";
|
||||
import { eq, and, sql, lt, gt } from "drizzle-orm";
|
||||
import { drizzle } from "drizzle-orm/mysql2";
|
||||
import { InsertUser, users } from "../drizzle/schema";
|
||||
import {
|
||||
InsertUser,
|
||||
users,
|
||||
formations,
|
||||
apprenants,
|
||||
sessions,
|
||||
inscriptions,
|
||||
InsertFormation,
|
||||
InsertApprenant,
|
||||
InsertSession,
|
||||
InsertInscription
|
||||
} from "../drizzle/schema";
|
||||
import { ENV } from './_core/env';
|
||||
|
||||
let _db: ReturnType<typeof drizzle> | null = null;
|
||||
@@ -89,4 +100,230 @@ export async function getUserByOpenId(openId: string) {
|
||||
return result.length > 0 ? result[0] : undefined;
|
||||
}
|
||||
|
||||
// TODO: add feature queries here as your schema grows.
|
||||
// ===== FORMATIONS =====
|
||||
|
||||
export async function getAllFormations() {
|
||||
const db = await getDb();
|
||||
if (!db) return [];
|
||||
return db.select().from(formations).orderBy(formations.createdAt);
|
||||
}
|
||||
|
||||
export async function getFormationById(id: number) {
|
||||
const db = await getDb();
|
||||
if (!db) return undefined;
|
||||
const result = await db.select().from(formations).where(eq(formations.id, id)).limit(1);
|
||||
return result[0];
|
||||
}
|
||||
|
||||
export async function getFormationByLien(lien: string) {
|
||||
const db = await getDb();
|
||||
if (!db) return undefined;
|
||||
const result = await db.select().from(formations).where(eq(formations.lienUnique, lien)).limit(1);
|
||||
return result[0];
|
||||
}
|
||||
|
||||
export async function createFormation(data: InsertFormation) {
|
||||
const db = await getDb();
|
||||
if (!db) throw new Error("Database not available");
|
||||
const result = await db.insert(formations).values(data);
|
||||
return result;
|
||||
}
|
||||
|
||||
export async function updateFormation(id: number, data: Partial<InsertFormation>) {
|
||||
const db = await getDb();
|
||||
if (!db) throw new Error("Database not available");
|
||||
await db.update(formations).set(data).where(eq(formations.id, id));
|
||||
}
|
||||
|
||||
export async function deleteFormation(id: number) {
|
||||
const db = await getDb();
|
||||
if (!db) throw new Error("Database not available");
|
||||
await db.delete(formations).where(eq(formations.id, id));
|
||||
}
|
||||
|
||||
// ===== APPRENANTS =====
|
||||
|
||||
export async function getAllApprenants() {
|
||||
const db = await getDb();
|
||||
if (!db) return [];
|
||||
return db.select().from(apprenants).orderBy(apprenants.nom, apprenants.prenom);
|
||||
}
|
||||
|
||||
export async function getApprenantById(id: number) {
|
||||
const db = await getDb();
|
||||
if (!db) return undefined;
|
||||
const result = await db.select().from(apprenants).where(eq(apprenants.id, id)).limit(1);
|
||||
return result[0];
|
||||
}
|
||||
|
||||
export async function getApprenantByEmail(email: string) {
|
||||
const db = await getDb();
|
||||
if (!db) return undefined;
|
||||
const result = await db.select().from(apprenants).where(eq(apprenants.email, email)).limit(1);
|
||||
return result[0];
|
||||
}
|
||||
|
||||
export async function createApprenant(data: InsertApprenant) {
|
||||
const db = await getDb();
|
||||
if (!db) throw new Error("Database not available");
|
||||
const result = await db.insert(apprenants).values(data);
|
||||
return result;
|
||||
}
|
||||
|
||||
export async function updateApprenant(id: number, data: Partial<InsertApprenant>) {
|
||||
const db = await getDb();
|
||||
if (!db) throw new Error("Database not available");
|
||||
await db.update(apprenants).set(data).where(eq(apprenants.id, id));
|
||||
}
|
||||
|
||||
export async function deleteApprenant(id: number) {
|
||||
const db = await getDb();
|
||||
if (!db) throw new Error("Database not available");
|
||||
await db.delete(apprenants).where(eq(apprenants.id, id));
|
||||
}
|
||||
|
||||
// ===== SESSIONS =====
|
||||
|
||||
export async function getAllSessions() {
|
||||
const db = await getDb();
|
||||
if (!db) return [];
|
||||
return db.select().from(sessions).orderBy(sessions.dateDebut);
|
||||
}
|
||||
|
||||
export async function getSessionsByFormation(formationId: number) {
|
||||
const db = await getDb();
|
||||
if (!db) return [];
|
||||
return db.select().from(sessions).where(eq(sessions.formationId, formationId)).orderBy(sessions.dateDebut);
|
||||
}
|
||||
|
||||
export async function getSessionById(id: number) {
|
||||
const db = await getDb();
|
||||
if (!db) return undefined;
|
||||
const result = await db.select().from(sessions).where(eq(sessions.id, id)).limit(1);
|
||||
return result[0];
|
||||
}
|
||||
|
||||
export async function createSession(data: InsertSession) {
|
||||
const db = await getDb();
|
||||
if (!db) throw new Error("Database not available");
|
||||
const result = await db.insert(sessions).values(data);
|
||||
return result;
|
||||
}
|
||||
|
||||
export async function updateSession(id: number, data: Partial<InsertSession>) {
|
||||
const db = await getDb();
|
||||
if (!db) throw new Error("Database not available");
|
||||
await db.update(sessions).set(data).where(eq(sessions.id, id));
|
||||
}
|
||||
|
||||
export async function deleteSession(id: number) {
|
||||
const db = await getDb();
|
||||
if (!db) throw new Error("Database not available");
|
||||
await db.delete(sessions).where(eq(sessions.id, id));
|
||||
}
|
||||
|
||||
// ===== INSCRIPTIONS =====
|
||||
|
||||
export async function getInscriptionsBySession(sessionId: number) {
|
||||
const db = await getDb();
|
||||
if (!db) return [];
|
||||
|
||||
const result = await db
|
||||
.select({
|
||||
inscription: inscriptions,
|
||||
apprenant: apprenants,
|
||||
})
|
||||
.from(inscriptions)
|
||||
.leftJoin(apprenants, eq(inscriptions.apprenantId, apprenants.id))
|
||||
.where(eq(inscriptions.sessionId, sessionId))
|
||||
.orderBy(inscriptions.dateInscription);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
export async function getInscriptionsByApprenant(apprenantId: number) {
|
||||
const db = await getDb();
|
||||
if (!db) return [];
|
||||
|
||||
const result = await db
|
||||
.select({
|
||||
inscription: inscriptions,
|
||||
session: sessions,
|
||||
formation: formations,
|
||||
})
|
||||
.from(inscriptions)
|
||||
.leftJoin(sessions, eq(inscriptions.sessionId, sessions.id))
|
||||
.leftJoin(formations, eq(sessions.formationId, formations.id))
|
||||
.where(eq(inscriptions.apprenantId, apprenantId))
|
||||
.orderBy(inscriptions.dateInscription);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
export async function getInscriptionByApprenantAndSession(apprenantId: number, sessionId: number) {
|
||||
const db = await getDb();
|
||||
if (!db) return undefined;
|
||||
|
||||
const result = await db
|
||||
.select()
|
||||
.from(inscriptions)
|
||||
.where(and(
|
||||
eq(inscriptions.apprenantId, apprenantId),
|
||||
eq(inscriptions.sessionId, sessionId)
|
||||
))
|
||||
.limit(1);
|
||||
|
||||
return result[0];
|
||||
}
|
||||
|
||||
export async function countInscriptionsConfirmees(sessionId: number) {
|
||||
const db = await getDb();
|
||||
if (!db) return 0;
|
||||
|
||||
const result = await db
|
||||
.select({ count: sql<number>`count(*)` })
|
||||
.from(inscriptions)
|
||||
.where(and(
|
||||
eq(inscriptions.sessionId, sessionId),
|
||||
eq(inscriptions.statut, "confirmee")
|
||||
));
|
||||
|
||||
return result[0]?.count || 0;
|
||||
}
|
||||
|
||||
export async function createInscription(data: InsertInscription) {
|
||||
const db = await getDb();
|
||||
if (!db) throw new Error("Database not available");
|
||||
const result = await db.insert(inscriptions).values(data);
|
||||
return result;
|
||||
}
|
||||
|
||||
export async function updateInscription(id: number, data: Partial<InsertInscription>) {
|
||||
const db = await getDb();
|
||||
if (!db) throw new Error("Database not available");
|
||||
await db.update(inscriptions).set(data).where(eq(inscriptions.id, id));
|
||||
}
|
||||
|
||||
export async function deleteInscription(id: number) {
|
||||
const db = await getDb();
|
||||
if (!db) throw new Error("Database not available");
|
||||
await db.delete(inscriptions).where(eq(inscriptions.id, id));
|
||||
}
|
||||
|
||||
export async function getSessionsAvecInscriptions(formationId: number) {
|
||||
const db = await getDb();
|
||||
if (!db) return [];
|
||||
|
||||
const result = await db
|
||||
.select({
|
||||
session: sessions,
|
||||
nbInscrits: sql<number>`count(CASE WHEN ${inscriptions.statut} = 'confirmee' THEN 1 END)`,
|
||||
})
|
||||
.from(sessions)
|
||||
.leftJoin(inscriptions, eq(sessions.id, inscriptions.sessionId))
|
||||
.where(eq(sessions.formationId, formationId))
|
||||
.groupBy(sessions.id)
|
||||
.orderBy(sessions.dateDebut);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user