import{and as a,eq as n,isNull as c}from"drizzle-orm";import{scorecardsConfigTable as r}from"../../../../providers/database/databases/sqlite-db/schemas/scorecards-config-table.js";import{DatabaseConnectionFactory as s}from"../../../../providers/database/database-connection-factory.js";class o{#t;constructor(t){this.#t=t}static async create(t){const e=await s.create(t);if(!e)throw new Error("Failed to create db connection for scorecards config repository");return new o(e.client)}async sync(){await this.#t.sync()}async close(){await this.#t.close()}async findActiveConfigByKey(t){return(await this.#t.client.select().from(r).where(a(n(r.key,t),c(r.archivedAt))).limit(1))[0]??null}async findAllActiveConfigs(){return await this.#t.client.select().from(r).where(c(r.archivedAt))}async insertConfig(t){const e=await this.#t.client.insert(r).values(t).returning();if(!e[0])throw new Error(`Failed to insert scorecard config with key: ${t.key}`);return e[0]}async updateConfigById(t,e){const i=await this.#t.client.update(r).set(e).where(n(r.id,t)).returning();if(!i[0])throw new Error(`Failed to update scorecard config with id: ${t}`);return i[0]}async archiveConfigById(t,e){const i=await this.#t.client.update(r).set({archivedAt:e}).where(n(r.id,t)).returning();if(!i[0])throw new Error(`Failed to archive scorecard config with id: ${t}`);return i[0]}}export{o as ScorecardsConfigRepository};
