updated space endpoint to follow project

This commit is contained in:
hannathkadher
2024-12-09 17:33:19 +04:00
parent 19825540dd
commit cdc95056ae
9 changed files with 87 additions and 39 deletions

View File

@ -5,7 +5,7 @@ import {
HttpStatus,
Injectable,
} from '@nestjs/common';
import { AddSpaceDto } from '../dtos';
import { AddSpaceDto, CommunitySpaceParam, GetSpaceParam, UpdateSpaceDto } 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';
@ -13,6 +13,7 @@ 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';
@Injectable()
export class SpaceService {
@ -21,15 +22,19 @@ export class SpaceService {
private readonly communityRepository: CommunityRepository,
private readonly spaceLinkService: SpaceLinkService,
private readonly spaceProductService: SpaceProductService,
private readonly projectRepository: ProjectRepository,
) {}
async createSpace(
addSpaceDto: AddSpaceDto,
communityId: string,
params: CommunitySpaceParam,
): Promise<BaseResponseDto> {
const { parentUuid, direction, products } = addSpaceDto;
const { communityUuid, projectUuid } = params;
const community = await this.validateCommunity(communityId);
await this.validateProject(projectUuid);
const community = await this.validateCommunity(communityUuid);
const parent = parentUuid ? await this.validateSpace(parentUuid) : null;
try {
@ -67,9 +72,11 @@ export class SpaceService {
}
async getSpacesHierarchyForCommunity(
communityUuid: string,
params: CommunitySpaceParam,
): Promise<BaseResponseDto> {
const { communityUuid, projectUuid } = params;
await this.validateCommunity(communityUuid);
await this.validateProject(projectUuid);
try {
// Get all spaces related to the community, including the parent-child relations
const spaces = await this.spaceRepository.find({
@ -80,7 +87,7 @@ export class SpaceService {
'incomingConnections',
'spaceProducts',
'spaceProducts.product',
], // Include parent and children relations
],
});
// Organize spaces into a hierarchical structure
@ -99,9 +106,14 @@ export class SpaceService {
}
}
async findOne(spaceUuid: string): Promise<BaseResponseDto> {
async findOne(params: GetSpaceParam): Promise<BaseResponseDto> {
const { communityUuid, spaceUuid, projectUuid } = params;
try {
const space = await this.validateSpace(spaceUuid);
const space = await this.validateCommunityAndSpace(
communityUuid,
spaceUuid,
projectUuid,
);
return new SuccessResponseDto({
message: `Space with ID ${spaceUuid} successfully fetched`,
@ -119,15 +131,14 @@ export class SpaceService {
}
}
async delete(
spaceUuid: string,
communityUuid: string,
): Promise<BaseResponseDto> {
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,
);
// Delete the space
@ -149,14 +160,15 @@ export class SpaceService {
}
async updateSpace(
spaceUuid: string,
communityId: string,
updateSpaceDto: AddSpaceDto,
params: GetSpaceParam,
updateSpaceDto: UpdateSpaceDto,
): Promise<BaseResponseDto> {
const { communityUuid, spaceUuid, projectUuid } = params;
try {
const space = await this.validateCommunityAndSpace(
communityId,
communityUuid,
spaceUuid,
projectUuid,
);
// If a parentId is provided, check if the parent exists
@ -193,9 +205,10 @@ export class SpaceService {
}
async getSpacesHierarchyForSpace(
spaceUuid: string,
params: GetSpaceParam,
): Promise<BaseResponseDto> {
await this.validateSpace(spaceUuid);
const { spaceUuid, communityUuid, projectUuid } = params;
await this.validateCommunityAndSpace(communityUuid, spaceUuid, projectUuid);
try {
// Get all spaces that are children of the provided space, including the parent-child relations
@ -220,11 +233,16 @@ export class SpaceService {
}
}
async getSpaceInvitationCode(spaceUuid: string): Promise<any> {
async getSpaceInvitationCode(params: GetSpaceParam): Promise<any> {
const { communityUuid, spaceUuid, projectUuid } = params;
try {
const invitationCode = generateRandomString(6);
const space = await this.validateSpace(spaceUuid);
const space = await this.validateCommunityAndSpace(
communityUuid,
spaceUuid,
projectUuid,
);
space.invitationCode = invitationCode;
await this.spaceRepository.save(space);
@ -283,7 +301,13 @@ export class SpaceService {
return community;
}
async validateCommunityAndSpace(communityUuid: string, spaceUuid: string) {
async validateCommunityAndSpace(
communityUuid: string,
spaceUuid: string,
projectUuid: string,
) {
await this.validateProject(projectUuid);
const community = await this.validateCommunity(communityUuid);
if (!community) {
this.throwNotFound('Community', communityUuid);
@ -301,6 +325,14 @@ export class SpaceService {
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`,