"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.KyselyOrganizationRepository = void 0; const Organization_1 = require("../../domain/entities/Organization"); const UniqueId_1 = require("../../../../shared/domain/UniqueId"); class KyselyOrganizationRepository { constructor(db) { this.db = db; } async save(org) { await this.db .insertInto('organizations') .values({ id: org.id.toString(), name: org.name, slug: org.slug, created_at: org.createdAt.getTime(), }) .onConflict((oc) => oc.column('id').doUpdateSet({ name: org.name })) .execute(); } async findById(id) { const row = await this.db .selectFrom('organizations') .selectAll() .where('id', '=', id) .executeTakeFirst(); return row ? this.toDomain(row) : undefined; } async findBySlug(slug) { const row = await this.db .selectFrom('organizations') .selectAll() .where('slug', '=', slug) .executeTakeFirst(); return row ? this.toDomain(row) : undefined; } async findAll() { const rows = await this.db.selectFrom('organizations').selectAll().execute(); return rows.map((r) => this.toDomain(r)); } async addMember(member) { await this.db .insertInto('org_members') .values({ id: member.id, org_id: member.orgId, user_id: member.userId, role: member.role, joined_at: member.joinedAt.getTime(), }) .execute(); } async getMember(orgId, userId) { const row = await this.db .selectFrom('org_members') .selectAll() .where('org_id', '=', orgId) .where('user_id', '=', userId) .executeTakeFirst(); return row ? { id: row.id, orgId: row.org_id, userId: row.user_id, role: row.role, joinedAt: new Date(row.joined_at) } : undefined; } async listMembers(orgId) { const rows = await this.db .selectFrom('org_members') .selectAll() .where('org_id', '=', orgId) .execute(); return rows.map((r) => ({ id: r.id, orgId: r.org_id, userId: r.user_id, role: r.role, joinedAt: new Date(r.joined_at), })); } async updateMemberRole(orgId, userId, role) { await this.db .updateTable('org_members') .set({ role }) .where('org_id', '=', orgId) .where('user_id', '=', userId) .execute(); } async removeMember(orgId, userId) { await this.db .deleteFrom('org_members') .where('org_id', '=', orgId) .where('user_id', '=', userId) .execute(); } toDomain(row) { return Organization_1.Organization.reconstitute({ name: row.name, slug: row.slug, createdAt: new Date(row.created_at) }, UniqueId_1.UniqueId.from(row.id)); } } exports.KyselyOrganizationRepository = KyselyOrganizationRepository;