mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-11 07:38:49 +00:00
added formatting for list space model
This commit is contained in:
@ -128,31 +128,36 @@ export class SpaceModelService {
|
|||||||
disabled: false,
|
disabled: false,
|
||||||
};
|
};
|
||||||
pageable.include =
|
pageable.include =
|
||||||
'subspaceModels,tags,subspaceModels.tags,tags.product,subspaceModels.tags.product';
|
'subspaceModels.productAllocations,subspaceModelProductAllocations.tags,subspaceModels, productAllocations, productAllocations.tags';
|
||||||
|
|
||||||
const queryBuilder = await this.spaceModelRepository
|
const queryBuilder = this.spaceModelRepository
|
||||||
.createQueryBuilder('spaceModel')
|
.createQueryBuilder('spaceModel')
|
||||||
.leftJoinAndSelect(
|
.leftJoinAndSelect(
|
||||||
'spaceModel.subspaceModels',
|
'spaceModel.subspaceModels',
|
||||||
'subspaceModels',
|
'subspaceModels',
|
||||||
'subspaceModels.disabled = :subspaceDisabled',
|
'subspaceModels.disabled = false',
|
||||||
{ subspaceDisabled: false },
|
|
||||||
)
|
)
|
||||||
.leftJoinAndSelect(
|
.leftJoinAndSelect(
|
||||||
'spaceModel.tags',
|
'subspaceModels.productAllocations',
|
||||||
'tags',
|
'subspaceModelProductAllocations',
|
||||||
'tags.disabled = :tagsDisabled',
|
|
||||||
{ tagsDisabled: false },
|
|
||||||
)
|
)
|
||||||
.leftJoinAndSelect('tags.product', 'spaceTagproduct')
|
// Ensure correct aliasing for the ManyToMany relation
|
||||||
.leftJoinAndSelect(
|
.leftJoinAndSelect(
|
||||||
'subspaceModels.tags',
|
'subspaceModelProductAllocations.tags',
|
||||||
'subspaceModelTags',
|
'subspaceModelTags',
|
||||||
'subspaceModelTags.disabled = :subspaceModelTagsDisabled',
|
|
||||||
{ subspaceModelTagsDisabled: false },
|
|
||||||
)
|
)
|
||||||
.leftJoinAndSelect('subspaceModelTags.product', 'subspaceTagproduct')
|
.leftJoinAndSelect(
|
||||||
.where('spaceModel.disabled = :disabled', { disabled: false })
|
'subspaceModelTags.product',
|
||||||
|
'subspaceModelTagProduct',
|
||||||
|
)
|
||||||
|
.leftJoinAndSelect(
|
||||||
|
'spaceModel.productAllocations',
|
||||||
|
'productAllocations',
|
||||||
|
)
|
||||||
|
.leftJoinAndSelect('productAllocations.product', 'allocatedProduct')
|
||||||
|
.leftJoinAndSelect('productAllocations.tags', 'productTags')
|
||||||
|
.leftJoinAndSelect('productTags.product', 'productTagProduct')
|
||||||
|
.where('spaceModel.disabled = false')
|
||||||
.andWhere('spaceModel.project = :projectUuid', {
|
.andWhere('spaceModel.project = :projectUuid', {
|
||||||
projectUuid: param.projectUuid,
|
projectUuid: param.projectUuid,
|
||||||
});
|
});
|
||||||
@ -168,10 +173,87 @@ export class SpaceModelService {
|
|||||||
queryBuilder,
|
queryBuilder,
|
||||||
);
|
);
|
||||||
|
|
||||||
return new PageResponse<SpaceModelDto>(
|
// Ensure baseResponseDto is an array
|
||||||
baseResponseDto,
|
const spaceModelsArray = Array.isArray(baseResponseDto.data)
|
||||||
paginationResponseDto,
|
? baseResponseDto.data
|
||||||
|
: [baseResponseDto.data];
|
||||||
|
|
||||||
|
// Log to verify correct data retrieval
|
||||||
|
console.log(
|
||||||
|
'SpaceModelsArray:',
|
||||||
|
JSON.stringify(spaceModelsArray, null, 2),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Format response
|
||||||
|
const formattedData = spaceModelsArray.map((spaceModel) => ({
|
||||||
|
uuid: spaceModel.uuid,
|
||||||
|
createdAt: spaceModel.createdAt,
|
||||||
|
updatedAt: spaceModel.updatedAt,
|
||||||
|
modelName: spaceModel.modelName,
|
||||||
|
disabled: spaceModel.disabled,
|
||||||
|
subspaceModels: (spaceModel.subspaceModels ?? []).map((subspace) => ({
|
||||||
|
uuid: subspace.uuid,
|
||||||
|
createdAt: subspace.createdAt,
|
||||||
|
updatedAt: subspace.updatedAt,
|
||||||
|
subspaceName: subspace.subspaceName,
|
||||||
|
disabled: subspace.disabled,
|
||||||
|
tags: (subspace.productAllocations ?? [])
|
||||||
|
.flatMap((allocation) => allocation.tags ?? [])
|
||||||
|
.map((tag) => ({
|
||||||
|
uuid: tag.uuid,
|
||||||
|
createdAt: tag.createdAt,
|
||||||
|
updatedAt: tag.updatedAt,
|
||||||
|
tag: tag.tag,
|
||||||
|
disabled: tag.disabled,
|
||||||
|
product: tag.product
|
||||||
|
? {
|
||||||
|
uuid: tag.product.uuid,
|
||||||
|
createdAt: tag.product.createdAt,
|
||||||
|
updatedAt: tag.product.updatedAt,
|
||||||
|
catName: tag.product.catName,
|
||||||
|
prodId: tag.product.prodId,
|
||||||
|
name: tag.product.name,
|
||||||
|
prodType: tag.product.prodType,
|
||||||
|
}
|
||||||
|
: null,
|
||||||
|
})),
|
||||||
|
})),
|
||||||
|
tags: (spaceModel.productAllocations ?? [])
|
||||||
|
.flatMap((allocation) => allocation.tags ?? [])
|
||||||
|
.map((tag) => ({
|
||||||
|
uuid: tag.uuid,
|
||||||
|
createdAt: tag.createdAt,
|
||||||
|
updatedAt: tag.updatedAt,
|
||||||
|
tag: tag.tag,
|
||||||
|
disabled: tag.disabled,
|
||||||
|
product: tag.product
|
||||||
|
? {
|
||||||
|
uuid: tag.product.uuid,
|
||||||
|
createdAt: tag.product.createdAt,
|
||||||
|
updatedAt: tag.product.updatedAt,
|
||||||
|
catName: tag.product.catName,
|
||||||
|
prodId: tag.product.prodId,
|
||||||
|
name: tag.product.name,
|
||||||
|
prodType: tag.product.prodType,
|
||||||
|
}
|
||||||
|
: null,
|
||||||
|
})),
|
||||||
|
}));
|
||||||
|
|
||||||
|
// Log to verify formatted response
|
||||||
|
console.log('FormattedData:', JSON.stringify(formattedData, null, 2));
|
||||||
|
|
||||||
|
return {
|
||||||
|
code: 200,
|
||||||
|
data: formattedData,
|
||||||
|
message: 'Success get list spaceModel',
|
||||||
|
page: paginationResponseDto.page,
|
||||||
|
size: paginationResponseDto.size,
|
||||||
|
totalItem: paginationResponseDto.totalItem,
|
||||||
|
totalPage: paginationResponseDto.totalPage,
|
||||||
|
hasNext: paginationResponseDto.hasNext,
|
||||||
|
hasPrevious: paginationResponseDto.hasPrevious,
|
||||||
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
`Error fetching paginated list: ${error.message}`,
|
`Error fetching paginated list: ${error.message}`,
|
||||||
|
Reference in New Issue
Block a user