|
|
@@ -3,7 +3,7 @@ import { InjectConnection } from '@nestjs/typeorm';
|
|
|
import {
|
|
|
CreateProductInput,
|
|
|
DeletionResponse,
|
|
|
- DeletionResult, LanguageCode,
|
|
|
+ DeletionResult,
|
|
|
UpdateProductInput,
|
|
|
} from '@vendure/common/lib/generated-types';
|
|
|
import { normalizeString } from '@vendure/common/lib/normalize-string';
|
|
|
@@ -20,12 +20,12 @@ import { ProductTranslation } from '../../entity/product/product-translation.ent
|
|
|
import { Product } from '../../entity/product/product.entity';
|
|
|
import { EventBus } from '../../event-bus/event-bus';
|
|
|
import { CatalogModificationEvent } from '../../event-bus/events/catalog-modification-event';
|
|
|
-import { AssetUpdater } from '../helpers/asset-updater/asset-updater';
|
|
|
import { ListQueryBuilder } from '../helpers/list-query-builder/list-query-builder';
|
|
|
import { TranslatableSaver } from '../helpers/translatable-saver/translatable-saver';
|
|
|
import { getEntityOrThrow } from '../helpers/utils/get-entity-or-throw';
|
|
|
import { translateDeep } from '../helpers/utils/translate-entity';
|
|
|
|
|
|
+import { AssetService } from './asset.service';
|
|
|
import { ChannelService } from './channel.service';
|
|
|
import { CollectionService } from './collection.service';
|
|
|
import { FacetValueService } from './facet-value.service';
|
|
|
@@ -34,18 +34,12 @@ import { TaxRateService } from './tax-rate.service';
|
|
|
|
|
|
@Injectable()
|
|
|
export class ProductService {
|
|
|
- private readonly relations = [
|
|
|
- 'featuredAsset',
|
|
|
- 'assets',
|
|
|
- 'channels',
|
|
|
- 'facetValues',
|
|
|
- 'facetValues.facet',
|
|
|
- ];
|
|
|
+ private readonly relations = ['featuredAsset', 'assets', 'channels', 'facetValues', 'facetValues.facet'];
|
|
|
|
|
|
constructor(
|
|
|
@InjectConnection() private connection: Connection,
|
|
|
private channelService: ChannelService,
|
|
|
- private assetUpdater: AssetUpdater,
|
|
|
+ private assetService: AssetService,
|
|
|
private productVariantService: ProductVariantService,
|
|
|
private facetValueService: FacetValueService,
|
|
|
private taxRateService: TaxRateService,
|
|
|
@@ -68,10 +62,7 @@ export class ProductService {
|
|
|
.getManyAndCount()
|
|
|
.then(async ([products, totalItems]) => {
|
|
|
const items = products.map(product =>
|
|
|
- translateDeep(product, ctx.languageCode, [
|
|
|
- 'facetValues',
|
|
|
- ['facetValues', 'facet'],
|
|
|
- ]),
|
|
|
+ translateDeep(product, ctx.languageCode, ['facetValues', ['facetValues', 'facet']]),
|
|
|
);
|
|
|
return {
|
|
|
items,
|
|
|
@@ -90,10 +81,7 @@ export class ProductService {
|
|
|
if (!product) {
|
|
|
return;
|
|
|
}
|
|
|
- return translateDeep(product, ctx.languageCode, [
|
|
|
- 'facetValues',
|
|
|
- ['facetValues', 'facet'],
|
|
|
- ]);
|
|
|
+ return translateDeep(product, ctx.languageCode, ['facetValues', ['facetValues', 'facet']]);
|
|
|
}
|
|
|
|
|
|
async findOneBySlug(ctx: RequestContext, slug: string): Promise<Translated<Product> | undefined> {
|
|
|
@@ -120,9 +108,10 @@ export class ProductService {
|
|
|
if (input.facetValueIds) {
|
|
|
p.facetValues = await this.facetValueService.findByIds(input.facetValueIds);
|
|
|
}
|
|
|
- await this.assetUpdater.updateEntityAssets(p, input);
|
|
|
+ await this.assetService.updateFeaturedAsset(p, input.featuredAssetId);
|
|
|
},
|
|
|
});
|
|
|
+ await this.assetService.updateEntityAssets(product, input.assetIds);
|
|
|
this.eventBus.publish(new CatalogModificationEvent(ctx, product));
|
|
|
return assertFound(this.findOne(ctx, product.id));
|
|
|
}
|
|
|
@@ -138,7 +127,8 @@ export class ProductService {
|
|
|
if (input.facetValueIds) {
|
|
|
p.facetValues = await this.facetValueService.findByIds(input.facetValueIds);
|
|
|
}
|
|
|
- await this.assetUpdater.updateEntityAssets(p, input);
|
|
|
+ await this.assetService.updateFeaturedAsset(p, input.featuredAssetId);
|
|
|
+ await this.assetService.updateEntityAssets(p, input.assetIds);
|
|
|
},
|
|
|
});
|
|
|
this.eventBus.publish(new CatalogModificationEvent(ctx, product));
|
|
|
@@ -201,7 +191,10 @@ export class ProductService {
|
|
|
private async getProductWithOptionGroups(productId: ID): Promise<Product> {
|
|
|
const product = await this.connection
|
|
|
.getRepository(Product)
|
|
|
- .findOne(productId, { relations: ['optionGroups', 'variants', 'variants.options'], where: { deletedAt: null } });
|
|
|
+ .findOne(productId, {
|
|
|
+ relations: ['optionGroups', 'variants', 'variants.options'],
|
|
|
+ where: { deletedAt: null },
|
|
|
+ });
|
|
|
if (!product) {
|
|
|
throw new EntityNotFoundError('Product', productId);
|
|
|
}
|
|
|
@@ -220,9 +213,13 @@ export class ProductService {
|
|
|
let suffix = 1;
|
|
|
const alreadySuffixed = /-\d+$/;
|
|
|
do {
|
|
|
- const qb = this.connection.getRepository(ProductTranslation).createQueryBuilder('translation')
|
|
|
+ const qb = this.connection
|
|
|
+ .getRepository(ProductTranslation)
|
|
|
+ .createQueryBuilder('translation')
|
|
|
.where(`translation.slug = :slug`, { slug: t.slug })
|
|
|
- .andWhere(`translation.languageCode = :languageCode`, { languageCode: t.languageCode });
|
|
|
+ .andWhere(`translation.languageCode = :languageCode`, {
|
|
|
+ languageCode: t.languageCode,
|
|
|
+ });
|
|
|
if ((input as UpdateProductInput).id) {
|
|
|
qb.andWhere(`translation.base != :id`, { id: (input as UpdateProductInput).id });
|
|
|
}
|