Refactor/space management (#404)

* refactor: reducing used queries on get communities (#385)

* refactor: fix create space logic (#394)

* Remove unique constraint on subspace and product in SubspaceProductAllocationEntity; update product relation to nullable in NewTagEntity

* refactor: fix create space logic

* device model updated to include the fixes and final columns

* updated space models to include suggested fixes, update final logic and column names

* task: removing old references of the old tag-product relation

* task: remove old use of tags

* task: remove old tag & tag model usage

* refactor: delete space

* task: remove unused functions

* fix lint rule
This commit is contained in:
ZaydSkaff
2025-06-11 13:15:21 +03:00
committed by GitHub
parent 4f5e1b23f6
commit 8503ee728d
64 changed files with 2314 additions and 4372 deletions

View File

@ -1,15 +1,14 @@
import { CommandHandler, ICommandHandler } from '@nestjs/cqrs';
import { PropogateUpdateSpaceModelCommand } from '../commands';
import { ModifyAction } from '@app/common/constants/modify-action.enum';
import { SpaceProductAllocationRepository } from '@app/common/modules/space';
import { SubspaceModelProductAllocationRepoitory } from '@app/common/modules/space-model';
import { SpaceEntity } from '@app/common/modules/space/entities/space.entity';
import {
SubspaceProductAllocationRepository,
SubspaceRepository,
} from '@app/common/modules/space/repositories/subspace.repository';
import { SubspaceModelProductAllocationRepoitory } from '@app/common/modules/space-model';
import { In } from 'typeorm';
import { CommandHandler, ICommandHandler } from '@nestjs/cqrs';
import { PropogateUpdateSpaceModelCommand } from '../commands';
import { ISingleSubspaceModel } from '../interfaces';
import { SpaceEntity } from '@app/common/modules/space/entities/space.entity';
import { ModifyAction } from '@app/common/constants/modify-action.enum';
import { IUpdatedAllocations } from '../interfaces/subspace-product-allocation-update-result.interface';
@CommandHandler(PropogateUpdateSpaceModelCommand)
@ -72,48 +71,48 @@ export class PropogateUpdateSpaceModelHandler
for (const allocation of allocations) {
if (!allocation) continue;
if (allocation.allocation) {
try {
const subspaceAllocations =
await this.subspaceProductRepository.find({
where: {
inheritedFromModel: { uuid: allocation.allocation.uuid },
},
relations: ['tags'],
});
// if (allocation.allocation) {
// try {
// const subspaceAllocations =
// await this.subspaceProductRepository.find({
// where: {
// inheritedFromModel: { uuid: allocation.allocation.uuid },
// },
// relations: ['tags'],
// });
if (!subspaceAllocations || subspaceAllocations.length === 0)
continue;
// if (!subspaceAllocations || subspaceAllocations.length === 0)
// continue;
if (allocation.tagsAdded?.length) {
for (const subspaceAllocation of subspaceAllocations) {
subspaceAllocation.tags.push(...allocation.tagsAdded);
}
await this.subspaceProductRepository.save(subspaceAllocations);
console.log(
`Added tags to ${subspaceAllocations.length} subspace allocations.`,
);
}
// if (allocation.tagsAdded?.length) {
// for (const subspaceAllocation of subspaceAllocations) {
// subspaceAllocation.tags.push(...allocation.tagsAdded);
// }
// await this.subspaceProductRepository.save(subspaceAllocations);
// console.log(
// `Added tags to ${subspaceAllocations.length} subspace allocations.`,
// );
// }
if (allocation.tagsRemoved?.length) {
const tagsToRemoveUUIDs = allocation.tagsRemoved.map(
(tag) => tag.uuid,
);
// if (allocation.tagsRemoved?.length) {
// const tagsToRemoveUUIDs = allocation.tagsRemoved.map(
// (tag) => tag.uuid,
// );
for (const subspaceAllocation of subspaceAllocations) {
subspaceAllocation.tags = subspaceAllocation.tags.filter(
(tag) => !tagsToRemoveUUIDs.includes(tag.uuid),
);
}
await this.subspaceProductRepository.save(subspaceAllocations);
console.log(
`Removed tags from ${subspaceAllocations.length} subspace allocations.`,
);
}
} catch (error) {
console.error('Error processing allocation update:', error);
}
}
// for (const subspaceAllocation of subspaceAllocations) {
// subspaceAllocation.tags = subspaceAllocation.tags.filter(
// (tag) => !tagsToRemoveUUIDs.includes(tag.uuid),
// );
// }
// await this.subspaceProductRepository.save(subspaceAllocations);
// console.log(
// `Removed tags from ${subspaceAllocations.length} subspace allocations.`,
// );
// }
// } catch (error) {
// console.error('Error processing allocation update:', error);
// }
// }
if (allocation.newAllocation) {
try {
@ -127,7 +126,7 @@ export class PropogateUpdateSpaceModelHandler
const newAllocations = subspaces.map((subspace) =>
this.subspaceProductRepository.create({
product: allocation.newAllocation.product,
tags: allocation.newAllocation.tags,
tag: allocation.newAllocation.tag,
subspace,
inheritedFromModel: allocation.newAllocation,
}),
@ -198,7 +197,7 @@ export class PropogateUpdateSpaceModelHandler
const subspaceAllocation = this.subspaceProductRepository.create({
subspace: subspace,
product: allocation.product,
tags: allocation.tags,
tag: allocation.tag,
inheritedFromModel: allocation,
});
await this.subspaceProductRepository.save(subspaceAllocation);
@ -211,67 +210,59 @@ export class PropogateUpdateSpaceModelHandler
subspaceModel: ISingleSubspaceModel,
spaces: SpaceEntity[],
) {
const subspaces = await this.subspaceRepository.find({
where: {
subSpaceModel: { uuid: subspaceModel.subspaceModel.uuid },
disabled: false,
},
relations: [
'productAllocations',
'productAllocations.product',
'productAllocations.tags',
],
});
if (!subspaces.length) {
return;
}
const allocationUuidsToRemove = subspaces.flatMap((subspace) =>
subspace.productAllocations.map((allocation) => allocation.uuid),
);
if (allocationUuidsToRemove.length) {
await this.subspaceProductRepository.delete(allocationUuidsToRemove);
}
await this.subspaceRepository.update(
{ uuid: In(subspaces.map((s) => s.uuid)) },
{ disabled: true },
);
const relocatedAllocations = subspaceModel.relocatedAllocations || [];
if (!relocatedAllocations.length) {
return;
}
for (const space of spaces) {
for (const { allocation, tags = [] } of relocatedAllocations) {
const spaceAllocation = await this.spaceProductRepository.findOne({
where: {
inheritedFromModel: { uuid: allocation.uuid },
space: { uuid: space.uuid },
},
relations: ['tags'],
});
if (spaceAllocation) {
if (tags.length) {
spaceAllocation.tags.push(...tags);
await this.spaceProductRepository.save(spaceAllocation);
}
} else {
const newSpaceAllocation = this.spaceProductRepository.create({
space,
inheritedFromModel: allocation,
tags: allocation.tags,
product: allocation.product,
});
await this.spaceProductRepository.save(newSpaceAllocation);
}
}
}
// const subspaces = await this.subspaceRepository.find({
// where: {
// subSpaceModel: { uuid: subspaceModel.subspaceModel.uuid },
// disabled: false,
// },
// relations: [
// 'productAllocations',
// 'productAllocations.product',
// 'productAllocations.tags',
// ],
// });
// if (!subspaces.length) {
// return;
// }
// const allocationUuidsToRemove = subspaces.flatMap((subspace) =>
// subspace.productAllocations.map((allocation) => allocation.uuid),
// );
// if (allocationUuidsToRemove.length) {
// await this.subspaceProductRepository.delete(allocationUuidsToRemove);
// }
// await this.subspaceRepository.update(
// { uuid: In(subspaces.map((s) => s.uuid)) },
// { disabled: true },
// );
// const relocatedAllocations = subspaceModel.relocatedAllocations || [];
// if (!relocatedAllocations.length) {
// return;
// }
// for (const space of spaces) {
// for (const { allocation, tags = [] } of relocatedAllocations) {
// const spaceAllocation = await this.spaceProductRepository.findOne({
// where: {
// inheritedFromModel: { uuid: allocation.uuid },
// space: { uuid: space.uuid },
// },
// relations: ['tags'],
// });
// if (spaceAllocation) {
// if (tags.length) {
// spaceAllocation.tags.push(...tags);
// await this.spaceProductRepository.save(spaceAllocation);
// }
// } else {
// const newSpaceAllocation = this.spaceProductRepository.create({
// space,
// inheritedFromModel: allocation,
// tag: allocation.tag,
// product: allocation.product,
// });
// await this.spaceProductRepository.save(newSpaceAllocation);
// }
// }
// }
}
async updateSubspaceModel(subspaceModel: ISingleSubspaceModel) {