import type {Pool} from "mysql2/promise"; import type Scope from "$lib/models/scope"; import type { ScopeEntry, ScopeInput } from "$lib/models/scope"; import type { ScopeRepo } from "../interfaces"; export default class MysqlDBScopes implements ScopeRepo { userId: string; connection: Pool; constructor(connection: Pool, userId: string) { this.connection = connection; this.userId = userId; } async find(id: number): Promise { const [[scopeRows], [projectRows], [statRows]] = await Promise.all([ this.connection.execute(` SELECT scope.*, scope_member.name as display_name FROM scope INNER JOIN scope_member ON scope.id = scope_member.scope_id WHERE scope.id = ? `, [id]), this.connection.execute(` SELECT id,name,status FROM project WHERE scope_id = ? `, [id]).catch(() => []), this.connection.execute(` SELECT * FROM stats WHERE scope_id = ? `, [id]).catch(() => []), ]); const r = scopeRows[0] as any; if (!r) { return null; } return { id: r.id, name: r.name, abbreviation: r.abbreviation, displayName: r.display_name, projects: ((projectRows||[]) as any[]).map((p:any) => ({ id: p.id, name: p.name, status: p.status, })), stats: ((statRows||[]) as any[]).map((s:any) => ({ id: s.id, name: s.name, weight: s.weight, description: s.description, allowedAmounts: s.allowed_amounts || void(0), })), } } async list(): Promise { const [rows] = await this.connection.execute(` SELECT scope.*, scope_member.name as display_name FROM scope INNER JOIN scope_member ON id = scope_id WHERE user_id = ? `, [this.userId]); return (rows as any[]).map(r => ({ id: r.id, name: r.name, abbreviation: r.abbreviation, displayName: r.display_name, })) } create(input: ScopeInput): Promise { throw new Error("Method not implemented."); } update(id: number, input: Partial): Promise { throw new Error("Method not implemented."); } delete(id: number): Promise { throw new Error("Method not implemented."); } }