Files
SerpentRace/SerpentRace_Backend/src/Application/Organization/commands/CreateOrganizationCommandHandler.ts
T

33 lines
1.4 KiB
TypeScript

import { IOrganizationRepository } from '../../../Domain/IRepository/IOrganizationRepository';
import { CreateOrganizationCommand } from './CreateOrganizationCommand';
import { ShortOrganizationDto } from '../../DTOs/OrganizationDto';
import { OrganizationAggregate, OrganizationState } from '../../../Domain/Organization/OrganizationAggregate';
import { OrganizationMapper } from '../../DTOs/Mappers/OrganizationMapper';
export class CreateOrganizationCommandHandler {
constructor(private readonly orgRepo: IOrganizationRepository) {}
async execute(cmd: CreateOrganizationCommand): Promise<ShortOrganizationDto> {
try {
const org = new OrganizationAggregate();
org.name = cmd.name;
org.contactfname = cmd.contactfname;
org.contactlname = cmd.contactlname;
org.contactphone = cmd.contactphone;
org.contactemail = cmd.contactemail;
org.url = cmd.url || null;
org.state = OrganizationState.REGISTERED;
const created = await this.orgRepo.create(org);
return OrganizationMapper.toShortDto(created);
} catch (error) {
if (error instanceof Error) {
if (error.message.includes('duplicate key value violates unique constraint')) {
throw new Error('Organization with this name or contact email already exists');
}
}
throw new Error('Failed to create organization');
}
}
}