diff --git a/src/space/services/space-products/space-products.service.ts b/src/space/services/space-products/space-products.service.ts index 8484426..5b218e4 100644 --- a/src/space/services/space-products/space-products.service.ts +++ b/src/space/services/space-products/space-products.service.ts @@ -20,25 +20,61 @@ export class SpaceProductService { const uniqueProducts = this.validateUniqueProducts(products); const productEntities = await this.getProductEntities(uniqueProducts); - const spaceProductEntities = uniqueProducts.map( - ({ productId, count }) => { - const product = productEntities.get(productId); - if (!product) { - throw new HttpException( - `Product with ID ${productId} not found`, - HttpStatus.NOT_FOUND, - ); - } - - return this.spaceProductRepository.create({ - space, - product, - productCount: count, - }); + // Fetch existing space products + const existingSpaceProducts = await this.spaceProductRepository.find({ + where: { + space: { + uuid: space.uuid, + }, }, - ); + 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) { console.error('Error assigning products to space:', error); diff --git a/src/space/services/space.service.ts b/src/space/services/space.service.ts index 7ee2041..44d27ba 100644 --- a/src/space/services/space.service.ts +++ b/src/space/services/space.service.ts @@ -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`,