|
|
@@ -3,14 +3,21 @@ import { InjectConnection } from '@nestjs/typeorm';
|
|
|
import { Connection } from 'typeorm';
|
|
|
import { DEFAULT_LANGUAGE_CODE } from '../common/constants';
|
|
|
import { ProductOptionGroupTranslation } from '../entity/product-option-group/product-option-group-translation.entity';
|
|
|
-import { CreateProductOptionGroupDto } from '../entity/product-option-group/product-option-group.dto';
|
|
|
+import {
|
|
|
+ CreateProductOptionGroupDto,
|
|
|
+ UpdateProductOptionGroupDto,
|
|
|
+} from '../entity/product-option-group/product-option-group.dto';
|
|
|
import { ProductOptionGroup } from '../entity/product-option-group/product-option-group.entity';
|
|
|
import { LanguageCode } from '../locale/language-code';
|
|
|
import { translateDeep } from '../locale/translate-entity';
|
|
|
+import { TranslationUpdaterService } from '../locale/translation-updater.service';
|
|
|
|
|
|
@Injectable()
|
|
|
export class ProductOptionGroupService {
|
|
|
- constructor(@InjectConnection() private connection: Connection) {}
|
|
|
+ constructor(
|
|
|
+ @InjectConnection() private connection: Connection,
|
|
|
+ private translationUpdaterService: TranslationUpdaterService,
|
|
|
+ ) {}
|
|
|
|
|
|
findAll(lang: LanguageCode): Promise<ProductOptionGroup[]> {
|
|
|
return this.connection.manager
|
|
|
@@ -43,4 +50,22 @@ export class ProductOptionGroupService {
|
|
|
|
|
|
return this.findOne(createdGroup.id, DEFAULT_LANGUAGE_CODE) as Promise<ProductOptionGroup>;
|
|
|
}
|
|
|
+
|
|
|
+ async update(updateProductOptionGroupDto: UpdateProductOptionGroupDto): Promise<ProductOptionGroup> {
|
|
|
+ const existingTranslations = await this.connection.getRepository(ProductOptionGroupTranslation).find({
|
|
|
+ where: { base: updateProductOptionGroupDto.id },
|
|
|
+ relations: ['base'],
|
|
|
+ });
|
|
|
+
|
|
|
+ const translationUpdater = this.translationUpdaterService.create(ProductOptionGroupTranslation);
|
|
|
+ const diff = translationUpdater.diff(existingTranslations, updateProductOptionGroupDto.translations);
|
|
|
+
|
|
|
+ const productOptionGroup = await translationUpdater.applyDiff(
|
|
|
+ new ProductOptionGroup(updateProductOptionGroupDto),
|
|
|
+ diff,
|
|
|
+ );
|
|
|
+ await this.connection.manager.save(productOptionGroup);
|
|
|
+
|
|
|
+ return this.findOne(productOptionGroup.id, DEFAULT_LANGUAGE_CODE) as Promise<ProductOptionGroup>;
|
|
|
+ }
|
|
|
}
|