"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CreateOrganizationCommand = void 0; const Result_1 = require("../../../../shared/domain/Result"); const Organization_1 = require("../../domain/entities/Organization"); const crypto_1 = require("crypto"); class CreateOrganizationCommand { constructor(orgRepository, userRepository, eventBus) { this.orgRepository = orgRepository; this.userRepository = userRepository; this.eventBus = eventBus; } async execute(request) { const user = await this.userRepository.findById(request.ownerId); if (!user) { return (0, Result_1.Err)('User not found'); } const slug = Organization_1.Organization.slugify(request.name); if (!slug) { return (0, Result_1.Err)('Invalid organization name'); } const existing = await this.orgRepository.findBySlug(slug); if (existing) { return (0, Result_1.Err)('Organization name already taken'); } const org = Organization_1.Organization.create({ name: request.name, slug }); await this.orgRepository.save(org); await this.orgRepository.addMember({ id: (0, crypto_1.randomUUID)(), orgId: org.id.toString(), userId: request.ownerId, role: 'owner', joinedAt: new Date(), }); user.assignToOrg(org.id.toString()); await this.userRepository.save(user); for (const event of org.domainEvents) { await this.eventBus.publish(event); } org.clearEvents(); return (0, Result_1.Ok)({ orgId: org.id.toString(), name: org.name, slug: org.slug, }); } } exports.CreateOrganizationCommand = CreateOrganizationCommand;