mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-15 10:25:23 +00:00
product update
This commit is contained in:
@ -20,8 +20,20 @@ export class SpaceProductService {
|
||||
const uniqueProducts = this.validateUniqueProducts(products);
|
||||
const productEntities = await this.getProductEntities(uniqueProducts);
|
||||
|
||||
const spaceProductEntities = uniqueProducts.map(
|
||||
({ productId, count }) => {
|
||||
// Fetch existing space products
|
||||
const existingSpaceProducts = await this.spaceProductRepository.find({
|
||||
where: {
|
||||
space: {
|
||||
uuid: space.uuid,
|
||||
},
|
||||
},
|
||||
relations: ['product'],
|
||||
});
|
||||
|
||||
const updatedProducts = [];
|
||||
const newProducts = [];
|
||||
|
||||
for (const { productId, count } of uniqueProducts) {
|
||||
const product = productEntities.get(productId);
|
||||
if (!product) {
|
||||
throw new HttpException(
|
||||
@ -30,15 +42,39 @@ export class SpaceProductService {
|
||||
);
|
||||
}
|
||||
|
||||
return this.spaceProductRepository.create({
|
||||
// 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,
|
||||
});
|
||||
},
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return await this.spaceProductRepository.save(spaceProductEntities);
|
||||
// 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) {
|
||||
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
|
||||
const { parentUuid } = updateSpaceDto;
|
||||
const { parentUuid, products } = updateSpaceDto;
|
||||
const parent = parentUuid ? await this.validateSpace(parentUuid) : null;
|
||||
|
||||
// Update other space properties from updateSpaceDto
|
||||
Object.assign(space, updateSpaceDto, { parent });
|
||||
|
||||
// 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({
|
||||
message: `Space with ID ${spaceUuid} successfully updated`,
|
||||
|
Reference in New Issue
Block a user