mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-10 15:17:41 +00:00
fix: check if subspaces not exists in update space
This commit is contained in:
@ -50,11 +50,12 @@ export class UpdateSpaceDto {
|
|||||||
description: 'List of subspace modifications',
|
description: 'List of subspace modifications',
|
||||||
type: [UpdateSubspaceDto],
|
type: [UpdateSubspaceDto],
|
||||||
})
|
})
|
||||||
@ArrayUnique((subspace) => subspace.subspaceName, {
|
@ArrayUnique((subspace) => subspace.subspaceName ?? subspace.uuid, {
|
||||||
message(validationArguments) {
|
message(validationArguments) {
|
||||||
const subspaces = validationArguments.value;
|
const subspaces = validationArguments.value;
|
||||||
const nameCounts = subspaces.reduce((acc, curr) => {
|
const nameCounts = subspaces.reduce((acc, curr) => {
|
||||||
acc[curr.subspaceName] = (acc[curr.subspaceName] || 0) + 1;
|
acc[curr.subspaceName ?? curr.uuid] =
|
||||||
|
(acc[curr.subspaceName ?? curr.uuid] || 0) + 1;
|
||||||
return acc;
|
return acc;
|
||||||
}, {});
|
}, {});
|
||||||
// Find duplicates
|
// Find duplicates
|
||||||
|
@ -342,26 +342,37 @@ export class SubSpaceService {
|
|||||||
})),
|
})),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const existingSubspaces = await this.subspaceRepository.find({
|
||||||
|
where: {
|
||||||
|
uuid: In(
|
||||||
|
subspaceDtos.filter((dto) => dto.uuid).map((dto) => dto.uuid),
|
||||||
|
),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (
|
||||||
|
existingSubspaces.length !==
|
||||||
|
subspaceDtos.filter((dto) => dto.uuid).length
|
||||||
|
) {
|
||||||
|
throw new HttpException(
|
||||||
|
`Some subspaces with provided UUIDs do not exist in the space.`,
|
||||||
|
HttpStatus.NOT_FOUND,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
const updatedSubspaces: SubspaceEntity[] = await queryRunner.manager.save(
|
const updatedSubspaces: SubspaceEntity[] = await queryRunner.manager.save(
|
||||||
SubspaceEntity,
|
SubspaceEntity,
|
||||||
[
|
newSubspaces,
|
||||||
...newSubspaces,
|
|
||||||
...subspaceDtos
|
|
||||||
.filter((dto) => dto.uuid)
|
|
||||||
.map((dto) => ({
|
|
||||||
subspaceName: dto.subspaceName,
|
|
||||||
space,
|
|
||||||
})),
|
|
||||||
],
|
|
||||||
);
|
);
|
||||||
|
const allSubspaces = [...updatedSubspaces, ...existingSubspaces];
|
||||||
// create or update allocations for the subspaces
|
// create or update allocations for the subspaces
|
||||||
if (updatedSubspaces.length > 0) {
|
if (allSubspaces.length > 0) {
|
||||||
await this.subspaceProductAllocationService.updateSubspaceProductAllocationsV2(
|
await this.subspaceProductAllocationService.updateSubspaceProductAllocationsV2(
|
||||||
subspaceDtos.map((dto) => ({
|
subspaceDtos.map((dto) => ({
|
||||||
...dto,
|
...dto,
|
||||||
uuid:
|
uuid:
|
||||||
dto.uuid ||
|
dto.uuid ||
|
||||||
updatedSubspaces.find((s) => s.subspaceName === dto.subspaceName)
|
allSubspaces.find((s) => s.subspaceName === dto.subspaceName)
|
||||||
?.uuid,
|
?.uuid,
|
||||||
})),
|
})),
|
||||||
projectUuid,
|
projectUuid,
|
||||||
|
Reference in New Issue
Block a user