mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-15 18:27:05 +00:00
product update
This commit is contained in:
@ -20,25 +20,61 @@ export class SpaceProductService {
|
|||||||
const uniqueProducts = this.validateUniqueProducts(products);
|
const uniqueProducts = this.validateUniqueProducts(products);
|
||||||
const productEntities = await this.getProductEntities(uniqueProducts);
|
const productEntities = await this.getProductEntities(uniqueProducts);
|
||||||
|
|
||||||
const spaceProductEntities = uniqueProducts.map(
|
// Fetch existing space products
|
||||||
({ productId, count }) => {
|
const existingSpaceProducts = await this.spaceProductRepository.find({
|
||||||
const product = productEntities.get(productId);
|
where: {
|
||||||
if (!product) {
|
space: {
|
||||||
throw new HttpException(
|
uuid: space.uuid,
|
||||||
`Product with ID ${productId} not found`,
|
},
|
||||||
HttpStatus.NOT_FOUND,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return this.spaceProductRepository.create({
|
|
||||||
space,
|
|
||||||
product,
|
|
||||||
productCount: count,
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
);
|
relations: ['product'],
|
||||||
|
});
|
||||||
|
|
||||||
return await this.spaceProductRepository.save(spaceProductEntities);
|
const updatedProducts = [];
|
||||||
|
const newProducts = [];
|
||||||
|
|
||||||
|
for (const { productId, count } of uniqueProducts) {
|
||||||
|
const product = productEntities.get(productId);
|
||||||
|
if (!product) {
|
||||||
|
throw new HttpException(
|
||||||
|
`Product with ID ${productId} not found`,
|
||||||
|
HttpStatus.NOT_FOUND,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if product already exists in the space
|
||||||
|
const existingProduct = existingSpaceProducts.find(
|
||||||
|
(spaceProduct) => spaceProduct.product.uuid === productId,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (existingProduct) {
|
||||||
|
// If count is different, update the existing record
|
||||||
|
if (existingProduct.productCount !== count) {
|
||||||
|
existingProduct.productCount = count;
|
||||||
|
updatedProducts.push(existingProduct);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Add new product if it doesn't exist
|
||||||
|
newProducts.push(
|
||||||
|
this.spaceProductRepository.create({
|
||||||
|
space,
|
||||||
|
product,
|
||||||
|
productCount: count,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Save updates and new records
|
||||||
|
if (updatedProducts.length > 0) {
|
||||||
|
await this.spaceProductRepository.save(updatedProducts);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (newProducts.length > 0) {
|
||||||
|
await this.spaceProductRepository.save(newProducts);
|
||||||
|
}
|
||||||
|
|
||||||
|
return [...updatedProducts, ...newProducts];
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error assigning products to space:', error);
|
console.error('Error assigning products to space:', error);
|
||||||
|
|
||||||
|
@ -160,14 +160,21 @@ export class SpaceService {
|
|||||||
);
|
);
|
||||||
|
|
||||||
// If a parentId is provided, check if the parent exists
|
// If a parentId is provided, check if the parent exists
|
||||||
const { parentUuid } = updateSpaceDto;
|
const { parentUuid, products } = updateSpaceDto;
|
||||||
const parent = parentUuid ? await this.validateSpace(parentUuid) : null;
|
const parent = parentUuid ? await this.validateSpace(parentUuid) : null;
|
||||||
|
|
||||||
// Update other space properties from updateSpaceDto
|
// Update other space properties from updateSpaceDto
|
||||||
Object.assign(space, updateSpaceDto, { parent });
|
Object.assign(space, updateSpaceDto, { parent });
|
||||||
|
|
||||||
// Save the updated space
|
// Save the updated space
|
||||||
await this.spaceRepository.save(space);
|
const updatedSpace = await this.spaceRepository.save(space);
|
||||||
|
|
||||||
|
if (products && products.length > 0) {
|
||||||
|
await this.spaceProductService.assignProductsToSpace(
|
||||||
|
updatedSpace,
|
||||||
|
products,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
return new SuccessResponseDto({
|
return new SuccessResponseDto({
|
||||||
message: `Space with ID ${spaceUuid} successfully updated`,
|
message: `Space with ID ${spaceUuid} successfully updated`,
|
||||||
|
Reference in New Issue
Block a user