reduced code duplication

This commit is contained in:
hannathkadher
2024-12-12 09:21:56 +04:00
parent 8af29cddfd
commit ba002ae474
6 changed files with 244 additions and 110 deletions

View File

@ -14,22 +14,24 @@ import {
} from '../dtos';
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
import { CommunityRepository } from '@app/common/modules/community/repositories';
import { SpaceEntity } from '@app/common/modules/space/entities';
import { generateRandomString } from '@app/common/helper/randomString';
import { SpaceLinkService } from './space-link';
import { SpaceProductService } from './space-products';
import { ProjectRepository } from '@app/common/modules/project/repositiories';
import { CreateSubspaceModelDto } from 'src/space-model/dtos';
import { SubSpaceService } from './subspace';
import { DataSource } from 'typeorm';
import { ValidationService } from './space-validation.service';
@Injectable()
export class SpaceService {
constructor(
private readonly dataSource: DataSource,
private readonly spaceRepository: SpaceRepository,
private readonly communityRepository: CommunityRepository,
private readonly spaceLinkService: SpaceLinkService,
private readonly spaceProductService: SpaceProductService,
private readonly projectRepository: ProjectRepository,
private readonly subSpaceService: SubSpaceService,
private readonly validationService: ValidationService,
) {}
async createSpace(
@ -40,21 +42,35 @@ export class SpaceService {
addSpaceDto;
const { communityUuid, projectUuid } = params;
await this.validateProject(projectUuid);
const queryRunner = this.dataSource.createQueryRunner();
await queryRunner.connect();
await queryRunner.startTransaction();
const community = await this.validationService.validateCommunityAndProject(
communityUuid,
projectUuid,
);
this.validateSpaceCreation(spaceModelUuid, products, subspaces);
const community = await this.validateCommunity(communityUuid);
const parent = parentUuid
? await this.validationService.validateSpace(parentUuid)
: null;
const spaceModel = spaceModelUuid
? await this.validationService.validateSpaceModel(spaceModelUuid)
: null;
const parent = parentUuid ? await this.validateSpace(parentUuid) : null;
try {
const newSpace = this.spaceRepository.create({
...addSpaceDto,
community,
spaceModel,
parent: parentUuid ? parent : null,
community,
});
await this.spaceRepository.save(newSpace);
await queryRunner.manager.save(newSpace);
if (direction && parent) {
await this.spaceLinkService.saveSpaceLink(
@ -64,12 +80,27 @@ export class SpaceService {
);
}
if (subspaces) {
await this.subSpaceService.createSubspacesFromNames(
subspaces,
newSpace,
queryRunner,
);
} else {
await this.subSpaceService.createSubSpaceFromModel(
spaceModel,
newSpace,
queryRunner,
);
}
if (products && products.length > 0) {
await this.spaceProductService.assignProductsToSpace(
newSpace,
products,
);
}
await queryRunner.commitTransaction();
return new SuccessResponseDto({
statusCode: HttpStatus.CREATED,
@ -77,7 +108,14 @@ export class SpaceService {
message: 'Space created successfully',
});
} catch (error) {
await queryRunner.rollbackTransaction();
if (error instanceof HttpException) {
throw error;
}
throw new HttpException(error.message, HttpStatus.INTERNAL_SERVER_ERROR);
} finally {
await queryRunner.release();
}
}
@ -85,8 +123,10 @@ export class SpaceService {
params: CommunitySpaceParam,
): Promise<BaseResponseDto> {
const { communityUuid, projectUuid } = params;
await this.validateCommunity(communityUuid);
await this.validateProject(projectUuid);
await this.validationService.validateCommunityAndProject(
communityUuid,
projectUuid,
);
try {
// Get all spaces related to the community, including the parent-child relations
const spaces = await this.spaceRepository.find({
@ -119,11 +159,12 @@ export class SpaceService {
async findOne(params: GetSpaceParam): Promise<BaseResponseDto> {
const { communityUuid, spaceUuid, projectUuid } = params;
try {
const space = await this.validateCommunityAndSpace(
communityUuid,
spaceUuid,
projectUuid,
);
const space =
await this.validationService.validateSpaceWithinCommunityAndProject(
communityUuid,
projectUuid,
spaceUuid,
);
return new SuccessResponseDto({
message: `Space with ID ${spaceUuid} successfully fetched`,
@ -144,12 +185,13 @@ export class SpaceService {
async delete(params: GetSpaceParam): Promise<BaseResponseDto> {
try {
const { communityUuid, spaceUuid, projectUuid } = params;
// First, check if the community exists
const space = await this.validateCommunityAndSpace(
communityUuid,
spaceUuid,
projectUuid,
);
const space =
await this.validationService.validateSpaceWithinCommunityAndProject(
communityUuid,
projectUuid,
spaceUuid,
);
// Delete the space
await this.spaceRepository.remove(space);
@ -175,15 +217,18 @@ export class SpaceService {
): Promise<BaseResponseDto> {
const { communityUuid, spaceUuid, projectUuid } = params;
try {
const space = await this.validateCommunityAndSpace(
communityUuid,
spaceUuid,
projectUuid,
);
const space =
await this.validationService.validateSpaceWithinCommunityAndProject(
communityUuid,
projectUuid,
spaceUuid,
);
// If a parentId is provided, check if the parent exists
const { parentUuid, products } = updateSpaceDto;
const parent = parentUuid ? await this.validateSpace(parentUuid) : null;
const parent = parentUuid
? await this.validationService.validateSpace(parentUuid)
: null;
// Update other space properties from updateSpaceDto
Object.assign(space, updateSpaceDto, { parent });
@ -218,7 +263,11 @@ export class SpaceService {
params: GetSpaceParam,
): Promise<BaseResponseDto> {
const { spaceUuid, communityUuid, projectUuid } = params;
await this.validateCommunityAndSpace(communityUuid, spaceUuid, projectUuid);
await this.validationService.validateSpaceWithinCommunityAndProject(
communityUuid,
projectUuid,
spaceUuid,
);
try {
// Get all spaces that are children of the provided space, including the parent-child relations
@ -248,11 +297,12 @@ export class SpaceService {
try {
const invitationCode = generateRandomString(6);
const space = await this.validateCommunityAndSpace(
communityUuid,
spaceUuid,
projectUuid,
);
const space =
await this.validationService.validateSpaceWithinCommunityAndProject(
communityUuid,
projectUuid,
spaceUuid,
);
space.invitationCode = invitationCode;
await this.spaceRepository.save(space);
@ -298,51 +348,6 @@ export class SpaceService {
return rootSpaces;
}
private async validateCommunity(communityId: string) {
const community = await this.communityRepository.findOne({
where: { uuid: communityId },
});
if (!community) {
throw new HttpException(
`Community with ID ${communityId} not found`,
HttpStatus.NOT_FOUND,
);
}
return community;
}
async validateCommunityAndSpace(
communityUuid: string,
spaceUuid: string,
projectUuid: string,
) {
await this.validateProject(projectUuid);
const community = await this.validateCommunity(communityUuid);
if (!community) {
this.throwNotFound('Community', communityUuid);
}
const space = await this.validateSpace(spaceUuid);
return space;
}
private async validateSpace(spaceUuid: string) {
const space = await this.spaceRepository.findOne({
where: { uuid: spaceUuid },
});
if (!space) this.throwNotFound('Space', spaceUuid);
return space;
}
private async validateProject(uuid: string) {
const project = await this.projectRepository.findOne({
where: { uuid },
});
if (!project) this.throwNotFound('Project', uuid);
}
private throwNotFound(entity: string, uuid: string) {
throw new HttpException(
`${entity} with ID ${uuid} not found`,