|
|
@@ -50,7 +50,6 @@ const REINDEX_CHUNK_SIZE = 2500;
|
|
|
const REINDEX_OPERATION_CHUNK_SIZE = 3000;
|
|
|
|
|
|
export const defaultProductRelations: Array<EntityRelationPaths<Product>> = [
|
|
|
- 'variants',
|
|
|
'featuredAsset',
|
|
|
'facetValues',
|
|
|
'facetValues.facet',
|
|
|
@@ -515,79 +514,49 @@ export class ElasticsearchIndexerController implements OnModuleInit, OnModuleDes
|
|
|
Logger.error(e.message, loggerCtx, e.stack);
|
|
|
throw e;
|
|
|
}
|
|
|
- if (product) {
|
|
|
- const updatedProductVariants = await this.connection.getRepository(ProductVariant).findByIds(
|
|
|
- product.variants.map(v => v.id),
|
|
|
- {
|
|
|
- relations: this.variantRelations,
|
|
|
- where: {
|
|
|
- deletedAt: null,
|
|
|
- },
|
|
|
- order: {
|
|
|
- id: 'ASC',
|
|
|
- },
|
|
|
- },
|
|
|
+ if (!product) {
|
|
|
+ return operations;
|
|
|
+ }
|
|
|
+ const updatedProductVariants = await this.connection.getRepository(ProductVariant).find({
|
|
|
+ relations: this.variantRelations,
|
|
|
+ where: {
|
|
|
+ productId,
|
|
|
+ deletedAt: null,
|
|
|
+ },
|
|
|
+ order: {
|
|
|
+ id: 'ASC',
|
|
|
+ },
|
|
|
+ });
|
|
|
+ // tslint:disable-next-line:no-non-null-assertion
|
|
|
+ updatedProductVariants.forEach(variant => (variant.product = product!));
|
|
|
+ if (!product.enabled) {
|
|
|
+ updatedProductVariants.forEach(v => (v.enabled = false));
|
|
|
+ }
|
|
|
+ Logger.debug(`Updating Product (${productId})`, loggerCtx);
|
|
|
+ const languageVariants: LanguageCode[] = [];
|
|
|
+ languageVariants.push(...product.translations.map(t => t.languageCode));
|
|
|
+ for (const variant of updatedProductVariants) {
|
|
|
+ languageVariants.push(...variant.translations.map(t => t.languageCode));
|
|
|
+ }
|
|
|
+ const uniqueLanguageVariants = unique(languageVariants);
|
|
|
+ for (const channel of product.channels) {
|
|
|
+ ctx.setChannel(channel);
|
|
|
+ const variantsInChannel = updatedProductVariants.filter(v =>
|
|
|
+ v.channels.map(c => c.id).includes(ctx.channelId),
|
|
|
);
|
|
|
- // tslint:disable-next-line:no-non-null-assertion
|
|
|
- updatedProductVariants.forEach(variant => (variant.product = product!));
|
|
|
- if (!product.enabled) {
|
|
|
- updatedProductVariants.forEach(v => (v.enabled = false));
|
|
|
- }
|
|
|
- Logger.debug(`Updating Product (${productId})`, loggerCtx);
|
|
|
- const languageVariants: LanguageCode[] = [];
|
|
|
- languageVariants.push(...product.translations.map(t => t.languageCode));
|
|
|
- for (const variant of product.variants) {
|
|
|
- languageVariants.push(...variant.translations.map(t => t.languageCode));
|
|
|
+ for (const variant of variantsInChannel) {
|
|
|
+ await this.productPriceApplicator.applyChannelPriceAndTax(variant, ctx);
|
|
|
}
|
|
|
- const uniqueLanguageVariants = unique(languageVariants);
|
|
|
-
|
|
|
- for (const channel of product.channels) {
|
|
|
- ctx.setChannel(channel);
|
|
|
-
|
|
|
- const variantsInChannel = updatedProductVariants.filter(v =>
|
|
|
- v.channels.map(c => c.id).includes(ctx.channelId),
|
|
|
- );
|
|
|
- for (const variant of variantsInChannel) {
|
|
|
- await this.productPriceApplicator.applyChannelPriceAndTax(variant, ctx);
|
|
|
- }
|
|
|
- for (const languageCode of uniqueLanguageVariants) {
|
|
|
- if (variantsInChannel.length) {
|
|
|
- for (const variant of variantsInChannel) {
|
|
|
- operations.push(
|
|
|
- {
|
|
|
- index: VARIANT_INDEX_NAME,
|
|
|
- operation: {
|
|
|
- update: {
|
|
|
- _id: ElasticsearchIndexerController.getId(
|
|
|
- variant.id,
|
|
|
- ctx.channelId,
|
|
|
- languageCode,
|
|
|
- ),
|
|
|
- },
|
|
|
- },
|
|
|
- },
|
|
|
- {
|
|
|
- index: VARIANT_INDEX_NAME,
|
|
|
- operation: {
|
|
|
- doc: await this.createVariantIndexItem(
|
|
|
- variant,
|
|
|
- variantsInChannel,
|
|
|
- ctx,
|
|
|
- languageCode,
|
|
|
- ),
|
|
|
- doc_as_upsert: true,
|
|
|
- },
|
|
|
- },
|
|
|
- );
|
|
|
- }
|
|
|
- } else {
|
|
|
+ for (const languageCode of uniqueLanguageVariants) {
|
|
|
+ if (variantsInChannel.length) {
|
|
|
+ for (const variant of variantsInChannel) {
|
|
|
operations.push(
|
|
|
{
|
|
|
index: VARIANT_INDEX_NAME,
|
|
|
operation: {
|
|
|
update: {
|
|
|
_id: ElasticsearchIndexerController.getId(
|
|
|
- -product.id,
|
|
|
+ variant.id,
|
|
|
ctx.channelId,
|
|
|
languageCode,
|
|
|
),
|
|
|
@@ -597,12 +566,39 @@ export class ElasticsearchIndexerController implements OnModuleInit, OnModuleDes
|
|
|
{
|
|
|
index: VARIANT_INDEX_NAME,
|
|
|
operation: {
|
|
|
- doc: this.createSyntheticProductIndexItem(product, ctx, languageCode),
|
|
|
+ doc: await this.createVariantIndexItem(
|
|
|
+ variant,
|
|
|
+ variantsInChannel,
|
|
|
+ ctx,
|
|
|
+ languageCode,
|
|
|
+ ),
|
|
|
doc_as_upsert: true,
|
|
|
},
|
|
|
},
|
|
|
);
|
|
|
}
|
|
|
+ } else {
|
|
|
+ operations.push(
|
|
|
+ {
|
|
|
+ index: VARIANT_INDEX_NAME,
|
|
|
+ operation: {
|
|
|
+ update: {
|
|
|
+ _id: ElasticsearchIndexerController.getId(
|
|
|
+ -product.id,
|
|
|
+ ctx.channelId,
|
|
|
+ languageCode,
|
|
|
+ ),
|
|
|
+ },
|
|
|
+ },
|
|
|
+ },
|
|
|
+ {
|
|
|
+ index: VARIANT_INDEX_NAME,
|
|
|
+ operation: {
|
|
|
+ doc: this.createSyntheticProductIndexItem(product, ctx, languageCode),
|
|
|
+ doc_as_upsert: true,
|
|
|
+ },
|
|
|
+ },
|
|
|
+ );
|
|
|
}
|
|
|
}
|
|
|
}
|