use query runner for space link

This commit is contained in:
hannathkadher
2024-12-25 11:46:24 +04:00
parent ba00a85910
commit f9fe209076
2 changed files with 43 additions and 8 deletions

View File

@ -1,8 +1,10 @@
import { SpaceEntity, SpaceLinkEntity } from '@app/common/modules/space';
import {
SpaceLinkRepository,
SpaceRepository,
} from '@app/common/modules/space/repositories';
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { QueryRunner } from 'typeorm';
@Injectable()
export class SpaceLinkService {
@ -15,10 +17,11 @@ export class SpaceLinkService {
startSpaceId: string,
endSpaceId: string,
direction: string,
queryRunner: QueryRunner,
): Promise<void> {
try {
// Check if a link between the startSpace and endSpace already exists
const existingLink = await this.spaceLinkRepository.findOne({
const existingLink = await queryRunner.manager.findOne(SpaceLinkEntity, {
where: {
startSpace: { uuid: startSpaceId },
endSpace: { uuid: endSpaceId },
@ -29,13 +32,16 @@ export class SpaceLinkService {
if (existingLink) {
// Update the direction if the link exists
existingLink.direction = direction;
await this.spaceLinkRepository.save(existingLink);
await queryRunner.manager.save(SpaceLinkEntity, existingLink);
return;
}
const existingEndSpaceLink = await this.spaceLinkRepository.findOne({
where: { endSpace: { uuid: endSpaceId } },
});
const existingEndSpaceLink = await queryRunner.manager.findOne(
SpaceLinkEntity,
{
where: { endSpace: { uuid: endSpaceId } },
},
);
if (
existingEndSpaceLink &&
@ -47,7 +53,7 @@ export class SpaceLinkService {
}
// Find start space
const startSpace = await this.spaceRepository.findOne({
const startSpace = await queryRunner.manager.findOne(SpaceEntity, {
where: { uuid: startSpaceId },
});
@ -59,7 +65,7 @@ export class SpaceLinkService {
}
// Find end space
const endSpace = await this.spaceRepository.findOne({
const endSpace = await queryRunner.manager.findOne(SpaceEntity, {
where: { uuid: endSpaceId },
});
@ -77,7 +83,7 @@ export class SpaceLinkService {
direction,
});
await this.spaceLinkRepository.save(spaceLink);
await queryRunner.manager.save(SpaceLinkEntity, spaceLink);
} catch (error) {
throw new HttpException(
error.message ||

View File

@ -90,6 +90,7 @@ export class SpaceService {
parent.uuid,
newSpace.uuid,
direction,
queryRunner,
)
: Promise.resolve(),
subspaces?.length
@ -361,6 +362,34 @@ export class SpaceService {
}
}
async deleteSpace(params: GetSpaceParam) {
try {
const { communityUuid, spaceUuid, projectUuid } = params;
const space =
await this.validationService.validateSpaceWithinCommunityAndProject(
communityUuid,
projectUuid,
spaceUuid,
);
const spaces = await this.spaceRepository.find({
where: { parent: { uuid: spaceUuid }, disabled: false },
relations: ['parent', 'children'], // Include parent and children relations
});
console.log(spaces);
// space.disabled = true;
await this.spaceRepository.update({ uuid: space.uuid }, space);
} catch (error) {
if (error instanceof HttpException) {
throw error;
}
throw new HttpException(
'An error occurred while deleting the space',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
private updateSpaceProperties(
space: SpaceEntity,
updateSpaceDto: UpdateSpaceDto,