mirror of
https://github.com/SyncrowIOT/backend.git
synced 2026-10-25 01:17:54 +00:00
create orphan community and space for project
This commit is contained in:
@@ -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}.`,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
export * from './create-orphan-space.handler.service'
|
||||
Reference in New Issue
Block a user