import { eq, and, type Db, missions } from '@mosaicstack/db'; export type Mission = typeof missions.$inferSelect; export type NewMission = typeof missions.$inferInsert; export function createMissionsRepo(db: Db) { return { async findAll(): Promise { return db.select().from(missions); }, async findAllByUser(userId: string): Promise { return db.select().from(missions).where(eq(missions.userId, userId)); }, async findById(id: string): Promise { const rows = await db.select().from(missions).where(eq(missions.id, id)); return rows[0]; }, async findByIdAndUser(id: string, userId: string): Promise { const rows = await db .select() .from(missions) .where(and(eq(missions.id, id), eq(missions.userId, userId))); return rows[0]; }, async findByProject(projectId: string): Promise { return db.select().from(missions).where(eq(missions.projectId, projectId)); }, async findByProjectAndUser(projectId: string, userId: string): Promise { return db .select() .from(missions) .where(and(eq(missions.projectId, projectId), eq(missions.userId, userId))); }, async create(data: NewMission): Promise { const rows = await db.insert(missions).values(data).returning(); return rows[0]!; }, async update(id: string, data: Partial): Promise { const rows = await db .update(missions) .set({ ...data, updatedAt: new Date() }) .where(eq(missions.id, id)) .returning(); return rows[0]; }, async remove(id: string): Promise { const rows = await db.delete(missions).where(eq(missions.id, id)).returning(); return rows.length > 0; }, }; } export type MissionsRepo = ReturnType;