mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-10 07:07:21 +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',
|
||||
type: [UpdateSubspaceDto],
|
||||
})
|
||||
@ArrayUnique((subspace) => subspace.subspaceName, {
|
||||
@ArrayUnique((subspace) => subspace.subspaceName ?? subspace.uuid, {
|
||||
message(validationArguments) {
|
||||
const subspaces = validationArguments.value;
|
||||
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;
|
||||
}, {});
|
||||
// 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(
|
||||
SubspaceEntity,
|
||||
[
|
||||
...newSubspaces,
|
||||
...subspaceDtos
|
||||
.filter((dto) => dto.uuid)
|
||||
.map((dto) => ({
|
||||
subspaceName: dto.subspaceName,
|
||||
space,
|
||||
})),
|
||||
],
|
||||
newSubspaces,
|
||||
);
|
||||
const allSubspaces = [...updatedSubspaces, ...existingSubspaces];
|
||||
// create or update allocations for the subspaces
|
||||
if (updatedSubspaces.length > 0) {
|
||||
if (allSubspaces.length > 0) {
|
||||
await this.subspaceProductAllocationService.updateSubspaceProductAllocationsV2(
|
||||
subspaceDtos.map((dto) => ({
|
||||
...dto,
|
||||
uuid:
|
||||
dto.uuid ||
|
||||
updatedSubspaces.find((s) => s.subspaceName === dto.subspaceName)
|
||||
allSubspaces.find((s) => s.subspaceName === dto.subspaceName)
|
||||
?.uuid,
|
||||
})),
|
||||
projectUuid,
|
||||
|
Reference in New Issue
Block a user