mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-16 18:56:22 +00:00
use query runner for space link
This commit is contained in:
@ -1,8 +1,10 @@
|
|||||||
|
import { SpaceEntity, SpaceLinkEntity } from '@app/common/modules/space';
|
||||||
import {
|
import {
|
||||||
SpaceLinkRepository,
|
SpaceLinkRepository,
|
||||||
SpaceRepository,
|
SpaceRepository,
|
||||||
} from '@app/common/modules/space/repositories';
|
} from '@app/common/modules/space/repositories';
|
||||||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
||||||
|
import { QueryRunner } from 'typeorm';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class SpaceLinkService {
|
export class SpaceLinkService {
|
||||||
@ -15,10 +17,11 @@ export class SpaceLinkService {
|
|||||||
startSpaceId: string,
|
startSpaceId: string,
|
||||||
endSpaceId: string,
|
endSpaceId: string,
|
||||||
direction: string,
|
direction: string,
|
||||||
|
queryRunner: QueryRunner,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
try {
|
try {
|
||||||
// Check if a link between the startSpace and endSpace already exists
|
// 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: {
|
where: {
|
||||||
startSpace: { uuid: startSpaceId },
|
startSpace: { uuid: startSpaceId },
|
||||||
endSpace: { uuid: endSpaceId },
|
endSpace: { uuid: endSpaceId },
|
||||||
@ -29,13 +32,16 @@ export class SpaceLinkService {
|
|||||||
if (existingLink) {
|
if (existingLink) {
|
||||||
// Update the direction if the link exists
|
// Update the direction if the link exists
|
||||||
existingLink.direction = direction;
|
existingLink.direction = direction;
|
||||||
await this.spaceLinkRepository.save(existingLink);
|
await queryRunner.manager.save(SpaceLinkEntity, existingLink);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const existingEndSpaceLink = await this.spaceLinkRepository.findOne({
|
const existingEndSpaceLink = await queryRunner.manager.findOne(
|
||||||
|
SpaceLinkEntity,
|
||||||
|
{
|
||||||
where: { endSpace: { uuid: endSpaceId } },
|
where: { endSpace: { uuid: endSpaceId } },
|
||||||
});
|
},
|
||||||
|
);
|
||||||
|
|
||||||
if (
|
if (
|
||||||
existingEndSpaceLink &&
|
existingEndSpaceLink &&
|
||||||
@ -47,7 +53,7 @@ export class SpaceLinkService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Find start space
|
// Find start space
|
||||||
const startSpace = await this.spaceRepository.findOne({
|
const startSpace = await queryRunner.manager.findOne(SpaceEntity, {
|
||||||
where: { uuid: startSpaceId },
|
where: { uuid: startSpaceId },
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -59,7 +65,7 @@ export class SpaceLinkService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Find end space
|
// Find end space
|
||||||
const endSpace = await this.spaceRepository.findOne({
|
const endSpace = await queryRunner.manager.findOne(SpaceEntity, {
|
||||||
where: { uuid: endSpaceId },
|
where: { uuid: endSpaceId },
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -77,7 +83,7 @@ export class SpaceLinkService {
|
|||||||
direction,
|
direction,
|
||||||
});
|
});
|
||||||
|
|
||||||
await this.spaceLinkRepository.save(spaceLink);
|
await queryRunner.manager.save(SpaceLinkEntity, spaceLink);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
error.message ||
|
error.message ||
|
||||||
|
@ -90,6 +90,7 @@ export class SpaceService {
|
|||||||
parent.uuid,
|
parent.uuid,
|
||||||
newSpace.uuid,
|
newSpace.uuid,
|
||||||
direction,
|
direction,
|
||||||
|
queryRunner,
|
||||||
)
|
)
|
||||||
: Promise.resolve(),
|
: Promise.resolve(),
|
||||||
subspaces?.length
|
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(
|
private updateSpaceProperties(
|
||||||
space: SpaceEntity,
|
space: SpaceEntity,
|
||||||
updateSpaceDto: UpdateSpaceDto,
|
updateSpaceDto: UpdateSpaceDto,
|
||||||
|
Reference in New Issue
Block a user