create orphan community and space for project

This commit is contained in:
hannathkadher
2024-12-18 08:22:53 +04:00
parent 885d43d834
commit a5ab883957
8 changed files with 88 additions and 6 deletions

View File

@ -0,0 +1,4 @@
export const ORPHAN_COMMUNITY_NAME = 'orphan-community';
export const ORPHAN_COMMUNITY_DESCRIPTION =
'Default community for orphan spaces';
export const ORPHAN_SPACE_NAME = 'orphan-space';

View File

@ -117,7 +117,7 @@ export class UserEntity extends AbstractEntity<UserDto> {
public visitorPasswords: VisitorPasswordEntity[];
@ManyToOne(() => RoleTypeEntity, (roleType) => roleType.users, {
nullable: false,
nullable: true,
})
public roleType: RoleTypeEntity;
@OneToOne(() => InviteUserEntity, (inviteUser) => inviteUser.user, {

View File

@ -0,0 +1,6 @@
import { ICommand } from '@nestjs/cqrs';
import { ProjectEntity } from '@app/common/modules/project/entities';
export class CreateOrphanSpaceCommand implements ICommand {
constructor(public readonly project: ProjectEntity) {}
}

View File

View File

@ -0,0 +1,52 @@
import { CommandHandler, ICommandHandler } from '@nestjs/cqrs';
import { CreateOrphanSpaceCommand } from '../command/create-orphan-space-command';
import { SpaceRepository } from '@app/common/modules/space';
import { CommunityRepository } from '@app/common/modules/community/repositories';
import { Logger } from '@nestjs/common';
import {
ORPHAN_COMMUNITY_DESCRIPTION,
ORPHAN_COMMUNITY_NAME,
ORPHAN_SPACE_NAME,
} from '@app/common/constants/orphan-constant';
@CommandHandler(CreateOrphanSpaceCommand)
export class CreateOrphanSpaceHandler
implements ICommandHandler<CreateOrphanSpaceCommand>
{
private readonly logger = new Logger(CreateOrphanSpaceHandler.name);
constructor(
private readonly spaceRepository: SpaceRepository,
private readonly communityRepository: CommunityRepository,
) {}
async execute(command: CreateOrphanSpaceCommand): Promise<void> {
try {
const { project } = command;
let orphanCommunity = await this.communityRepository.findOne({
where: { name: ORPHAN_COMMUNITY_NAME, project },
});
if (!orphanCommunity) {
orphanCommunity = this.communityRepository.create({
name: ORPHAN_COMMUNITY_NAME,
description: ORPHAN_COMMUNITY_DESCRIPTION,
project,
});
orphanCommunity = await this.communityRepository.save(orphanCommunity);
}
const orphanSpace = this.spaceRepository.create({
spaceName: ORPHAN_SPACE_NAME,
community: orphanCommunity,
});
await this.spaceRepository.save(orphanSpace);
} catch (error) {
this.logger.error(
`Error when creating orphan space for project ${JSON.stringify(
command.project.uuid,
)} - ERROR ${error}.`,
);
}
}
}

View File

@ -0,0 +1 @@
export * from './create-orphan-space.handler.service'

View File

@ -1,13 +1,25 @@
import { Global, Module } from '@nestjs/common';
import { CqrsModule } from '@nestjs/cqrs';
import { ProjectController } from './controllers';
import { ProjectService } from './services';
import { ProjectRepository } from '@app/common/modules/project/repositiories';
import { CreateOrphanSpaceHandler } from './handler';
import { SpaceRepository } from '@app/common/modules/space';
import { CommunityRepository } from '@app/common/modules/community/repositories';
const CommandHandlers = [CreateOrphanSpaceHandler];
@Global()
@Module({
imports: [],
imports: [CqrsModule],
controllers: [ProjectController],
providers: [ProjectService, ProjectRepository],
exports: [ProjectService],
providers: [
...CommandHandlers,
SpaceRepository,
CommunityRepository,
ProjectService,
ProjectRepository,
],
exports: [ProjectService, CqrsModule],
})
export class ProjectModule {}

View File

@ -10,10 +10,15 @@ import {
} from '@app/common/models/typeOrmCustom.model';
import { ProjectDto } from '@app/common/modules/project/dtos';
import { PageResponse } from '@app/common/dto/pagination.response.dto';
import { CommandBus } from '@nestjs/cqrs';
import { CreateOrphanSpaceCommand } from '../command/create-orphan-space-command';
@Injectable()
export class ProjectService {
constructor(private readonly projectRepository: ProjectRepository) {}
constructor(
private readonly projectRepository: ProjectRepository,
private commandBus: CommandBus,
) {}
async createProject(
createProjectDto: CreateProjectDto,
@ -33,6 +38,8 @@ export class ProjectService {
const newProject = this.projectRepository.create(createProjectDto);
const savedProject = await this.projectRepository.save(newProject);
await this.commandBus.execute(new CreateOrphanSpaceCommand(savedProject));
return new SuccessResponseDto({
message: `Project with ID ${savedProject.uuid} successfully created`,
data: savedProject,
@ -44,7 +51,7 @@ export class ProjectService {
}
throw new HttpException(
'An error occurred while creating the project. Please try again later.',
`An error occurred while creating the project. Please try again later. ${error}`,
HttpStatus.INTERNAL_SERVER_ERROR,
);
}