Checkpoint: Refonte majeure : transformation du système de sessions en séquences avec support de jusqu'à 4 dates par séquence. L'inscription à une séquence inscrit automatiquement l'apprenant à toutes les dates. Mise à jour complète du backend (schéma DB, procédures tRPC, emails, exports) et du frontend (AdminSequences, AdminSequenceInscrits, Inscription, Admin). Toutes les fonctionnalités existantes (filtres, statistiques, exports PDF/Excel, emails automatiques) sont préservées et adaptées au nouveau modèle.
This commit is contained in:
284
server/db.ts
284
server/db.ts
@@ -1,16 +1,22 @@
|
||||
import { eq, and, sql, lt, gt } from "drizzle-orm";
|
||||
import { eq, and, sql } from "drizzle-orm";
|
||||
import { drizzle } from "drizzle-orm/mysql2";
|
||||
import {
|
||||
InsertUser,
|
||||
users,
|
||||
formations,
|
||||
apprenants,
|
||||
sessions,
|
||||
sequences,
|
||||
datesFormation,
|
||||
inscriptions,
|
||||
InsertFormation,
|
||||
InsertApprenant,
|
||||
InsertSession,
|
||||
InsertInscription
|
||||
InsertSequence,
|
||||
InsertDateFormation,
|
||||
InsertInscription,
|
||||
Sequence,
|
||||
DateFormation,
|
||||
Apprenant,
|
||||
Formation
|
||||
} from "../drizzle/schema";
|
||||
import { ENV } from './_core/env';
|
||||
|
||||
@@ -100,230 +106,256 @@ export async function getUserByOpenId(openId: string) {
|
||||
return result.length > 0 ? result[0] : undefined;
|
||||
}
|
||||
|
||||
// ===== FORMATIONS =====
|
||||
// ==================== FORMATIONS ====================
|
||||
|
||||
export async function getAllFormations() {
|
||||
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 getFormations() {
|
||||
const db = await getDb();
|
||||
if (!db) return [];
|
||||
return db.select().from(formations).orderBy(formations.createdAt);
|
||||
|
||||
return await db.select().from(formations);
|
||||
}
|
||||
|
||||
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];
|
||||
return result.length > 0 ? result[0] : undefined;
|
||||
}
|
||||
|
||||
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;
|
||||
return result.length > 0 ? result[0] : undefined;
|
||||
}
|
||||
|
||||
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 =====
|
||||
// ==================== SÉQUENCES ====================
|
||||
|
||||
export async function getAllApprenants() {
|
||||
export async function createSequence(data: InsertSequence) {
|
||||
const db = await getDb();
|
||||
if (!db) return [];
|
||||
return db.select().from(apprenants).orderBy(apprenants.nom, apprenants.prenom);
|
||||
if (!db) throw new Error("Database not available");
|
||||
|
||||
const result = await db.insert(sequences).values(data);
|
||||
return result;
|
||||
}
|
||||
|
||||
export async function getApprenantById(id: number) {
|
||||
export async function getSequences() {
|
||||
const db = await getDb();
|
||||
if (!db) return [];
|
||||
|
||||
return await db.select().from(sequences);
|
||||
}
|
||||
|
||||
export async function getSequenceById(id: number): Promise<Sequence | undefined> {
|
||||
const db = await getDb();
|
||||
if (!db) return undefined;
|
||||
|
||||
const result = await db.select().from(sequences).where(eq(sequences.id, id)).limit(1);
|
||||
return result.length > 0 ? result[0] : undefined;
|
||||
}
|
||||
|
||||
export async function getSequencesByFormation(formationId: number) {
|
||||
const db = await getDb();
|
||||
if (!db) return [];
|
||||
|
||||
return await db.select().from(sequences).where(eq(sequences.formationId, formationId));
|
||||
}
|
||||
|
||||
export async function updateSequence(id: number, data: Partial<InsertSequence>) {
|
||||
const db = await getDb();
|
||||
if (!db) throw new Error("Database not available");
|
||||
|
||||
await db.update(sequences).set(data).where(eq(sequences.id, id));
|
||||
}
|
||||
|
||||
export async function deleteSequence(id: number) {
|
||||
const db = await getDb();
|
||||
if (!db) throw new Error("Database not available");
|
||||
|
||||
await db.delete(sequences).where(eq(sequences.id, id));
|
||||
}
|
||||
|
||||
// ==================== DATES DE FORMATION ====================
|
||||
|
||||
export async function createDateFormation(data: InsertDateFormation) {
|
||||
const db = await getDb();
|
||||
if (!db) throw new Error("Database not available");
|
||||
|
||||
const result = await db.insert(datesFormation).values(data);
|
||||
return result;
|
||||
}
|
||||
|
||||
export async function getDatesBySequence(sequenceId: number): Promise<DateFormation[]> {
|
||||
const db = await getDb();
|
||||
if (!db) return [];
|
||||
|
||||
const results = await db.select()
|
||||
.from(datesFormation)
|
||||
.where(eq(datesFormation.sequenceId, sequenceId))
|
||||
.orderBy(datesFormation.ordre);
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
export async function deleteDatesBySequence(sequenceId: number) {
|
||||
const db = await getDb();
|
||||
if (!db) throw new Error("Database not available");
|
||||
|
||||
await db.delete(datesFormation).where(eq(datesFormation.sequenceId, sequenceId));
|
||||
}
|
||||
|
||||
// ==================== APPRENANTS ====================
|
||||
|
||||
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 getApprenants() {
|
||||
const db = await getDb();
|
||||
if (!db) return [];
|
||||
|
||||
return await db.select().from(apprenants);
|
||||
}
|
||||
|
||||
export async function getApprenantById(id: number): Promise<Apprenant | undefined> {
|
||||
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];
|
||||
return result.length > 0 ? result[0] : undefined;
|
||||
}
|
||||
|
||||
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;
|
||||
return result.length > 0 ? result[0] : undefined;
|
||||
}
|
||||
|
||||
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 =====
|
||||
// ==================== INSCRIPTIONS ====================
|
||||
|
||||
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) {
|
||||
export async function createInscription(data: InsertInscription) {
|
||||
const db = await getDb();
|
||||
if (!db) throw new Error("Database not available");
|
||||
const result = await db.insert(sessions).values(data);
|
||||
|
||||
const result = await db.insert(inscriptions).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) {
|
||||
export async function getInscriptionsBySequence(sequenceId: 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);
|
||||
const results = await db.select({
|
||||
inscription: inscriptions,
|
||||
apprenant: apprenants,
|
||||
})
|
||||
.from(inscriptions)
|
||||
.leftJoin(apprenants, eq(inscriptions.apprenantId, apprenants.id))
|
||||
.where(eq(inscriptions.sequenceId, sequenceId));
|
||||
|
||||
return result;
|
||||
return results;
|
||||
}
|
||||
|
||||
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);
|
||||
const results = await db.select({
|
||||
inscription: inscriptions,
|
||||
sequence: sequences,
|
||||
})
|
||||
.from(inscriptions)
|
||||
.leftJoin(sequences, eq(inscriptions.sequenceId, sequences.id))
|
||||
.where(eq(inscriptions.apprenantId, apprenantId));
|
||||
|
||||
return result;
|
||||
return results;
|
||||
}
|
||||
|
||||
export async function getInscriptionByApprenantAndSession(apprenantId: number, sessionId: number) {
|
||||
export async function checkExistingInscription(apprenantId: number, sequenceId: number) {
|
||||
const db = await getDb();
|
||||
if (!db) return undefined;
|
||||
|
||||
const result = await db
|
||||
.select()
|
||||
const result = await db.select()
|
||||
.from(inscriptions)
|
||||
.where(and(
|
||||
eq(inscriptions.apprenantId, apprenantId),
|
||||
eq(inscriptions.sessionId, sessionId)
|
||||
eq(inscriptions.sequenceId, sequenceId)
|
||||
))
|
||||
.limit(1);
|
||||
|
||||
return result[0];
|
||||
return result.length > 0 ? result[0] : undefined;
|
||||
}
|
||||
|
||||
export async function countInscriptionsConfirmees(sessionId: number) {
|
||||
export async function countInscriptionsBySequence(sequenceId: number, statut?: string) {
|
||||
const db = await getDb();
|
||||
if (!db) return 0;
|
||||
|
||||
const result = await db
|
||||
.select({ count: sql<number>`count(*)` })
|
||||
const conditions = [eq(inscriptions.sequenceId, sequenceId)];
|
||||
if (statut) {
|
||||
conditions.push(eq(inscriptions.statut, statut as any));
|
||||
}
|
||||
|
||||
const result = await db.select({ count: sql<number>`count(*)` })
|
||||
.from(inscriptions)
|
||||
.where(and(
|
||||
eq(inscriptions.sessionId, sessionId),
|
||||
eq(inscriptions.statut, "confirmee")
|
||||
));
|
||||
.where(and(...conditions));
|
||||
|
||||
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