mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-14 18:05:48 +00:00
create orphan community and space for project
This commit is contained in:
4
libs/common/src/constants/orphan-constant.ts
Normal file
4
libs/common/src/constants/orphan-constant.ts
Normal 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';
|
@ -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, {
|
||||
|
6
src/project/command/create-orphan-space-command.ts
Normal file
6
src/project/command/create-orphan-space-command.ts
Normal 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) {}
|
||||
}
|
0
src/project/command/index.ts
Normal file
0
src/project/command/index.ts
Normal file
52
src/project/handler/create-orphan-space.handler.service.ts
Normal file
52
src/project/handler/create-orphan-space.handler.service.ts
Normal 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}.`,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
1
src/project/handler/index.ts
Normal file
1
src/project/handler/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from './create-orphan-space.handler.service'
|
@ -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 {}
|
||||
|
@ -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,
|
||||
);
|
||||
}
|
||||
|
Reference in New Issue
Block a user