@haus-tech/product-import-export-plugin 3.0.0 → 3.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,5 @@
1
1
  /// <reference types="node" />
2
- import { ConfigService, ChannelService, FacetService, FacetValueService, TaxCategoryService, ID, ImportProgress, LanguageCode, RequestContext, OnProgressFn, ParsedFacet, ProductService, TransactionalConnection, TranslatableSaver, AssetService, Asset, ProductOptionGroupService } from '@vendure/core';
2
+ import { ConfigService, ChannelService, FacetService, FacetValueService, TaxCategoryService, ID, ImportProgress, LanguageCode, RequestContext, OnProgressFn, ParsedFacet, ProductService, Product, TransactionalConnection, TranslatableSaver, AssetService, Asset, ProductOptionGroupService } from '@vendure/core';
3
3
  import { Stream } from 'stream';
4
4
  import { Observable } from 'rxjs';
5
5
  import { ExtendedFastImporterService } from '../../services/extended-fast-importer.service';
@@ -44,4 +44,5 @@ export declare class ProductImporter {
44
44
  private getMatchingTaxCategoryId;
45
45
  private getTranslationByCodeOrFirst;
46
46
  createFacetAndValue(facet: ParsedFacet, facetCode: string, valueCode: string): Promise<ID>;
47
+ getProductByIdWithRelations(ctx: RequestContext, id?: ID): Promise<Product | undefined>;
47
48
  }
@@ -207,6 +207,7 @@ let ProductImporter = class ProductImporter {
207
207
  };
208
208
  }
209
209
  catch (err) {
210
+ core_1.Logger.error('Error while importing products:', 'ProductImporter', err?.stack);
210
211
  return {
211
212
  errors: [err.message],
212
213
  imported: 0,
@@ -282,22 +283,7 @@ let ProductImporter = class ProductImporter {
282
283
  checkExistingProduct = { id: foundVariants[0].productId };
283
284
  }
284
285
  }
285
- const existingProduct = checkExistingProduct
286
- ? await this.connection.getRepository(ctx, core_1.Product).findOne({
287
- where: { id: checkExistingProduct?.id },
288
- withDeleted: true,
289
- relations: [
290
- 'facetValues',
291
- 'facetValues.facet',
292
- 'optionGroups',
293
- 'optionGroups.options',
294
- 'variants',
295
- 'variants.facetValues',
296
- 'assets',
297
- 'featuredAsset',
298
- ],
299
- })
300
- : undefined;
286
+ const existingProduct = await this.getProductByIdWithRelations(ctx, checkExistingProduct?.id);
301
287
  try {
302
288
  if (existingProduct?.deletedAt) {
303
289
  existingProduct.deletedAt = null;
@@ -306,13 +292,13 @@ let ProductImporter = class ProductImporter {
306
292
  }
307
293
  catch (error) {
308
294
  errors.push(`Error while restoring the product: ${error}`);
309
- console.error('Error while restoring the product:', error);
295
+ core_1.Logger.error('Error while restoring the product:', 'ProductImporter', error?.stack);
310
296
  }
311
297
  }
312
298
  }
313
299
  catch (error) {
314
300
  errors.push(`Error while restoring the product: ${error}`);
315
- console.error('Error while restoring the product:', error);
301
+ core_1.Logger.error('Error while restoring the product:', 'ProductImporter', error?.stack);
316
302
  }
317
303
  const existingProductName = existingProduct?.translations.find((t) => t.languageCode === languageCode)?.name;
318
304
  const productNameHasChanged = existingProduct && existingProductName !== productMainTranslation.name
@@ -447,7 +433,7 @@ let ProductImporter = class ProductImporter {
447
433
  }
448
434
  catch (error) {
449
435
  errors.push(`Error while restoring the variant: ${error}`);
450
- console.error('Error while restoring the variant:', error);
436
+ core_1.Logger.error('Error while restoring the variant:', 'ProductImporter', error?.stack);
451
437
  }
452
438
  }
453
439
  if (existingVariant && updatingStrategy === 'replace') {
@@ -494,18 +480,7 @@ let ProductImporter = class ProductImporter {
494
480
  }
495
481
  }
496
482
  if (existingProduct) {
497
- const productAfterUpdate = (await this.connection.getRepository(ctx, core_1.Product).findOne({
498
- where: { id: createdProductId },
499
- relations: [
500
- 'facetValues',
501
- 'facetValues.facet',
502
- 'optionGroups',
503
- 'optionGroups.options',
504
- 'variants',
505
- 'variants.facetValues',
506
- 'assets',
507
- ],
508
- })); // We know that the product exists, so we can safely cast it to Product
483
+ const productAfterUpdate = (await this.getProductByIdWithRelations(ctx, createdProductId)); // We know that the product exists, so we can safely cast it to Product
509
484
  const existingProductHash = (0, crypto_1.createHash)('md5')
510
485
  .update(JSON.stringify((0, remove_update_at_fields_1.removeUpdatedAtFields)(existingProduct)))
511
486
  .digest('hex');
@@ -726,6 +701,38 @@ let ProductImporter = class ProductImporter {
726
701
  });
727
702
  return facetValueId;
728
703
  }
704
+ async getProductByIdWithRelations(ctx, id) {
705
+ if (!id) {
706
+ return undefined;
707
+ }
708
+ const product = (await this.connection.getRepository(ctx, core_1.Product).findOne({
709
+ where: { id },
710
+ withDeleted: true,
711
+ })) ?? undefined;
712
+ if (product) {
713
+ const [facetValues, optionGroups, variants, assets, featuredAsset] = await Promise.all([
714
+ this.connection
715
+ .getRepository(ctx, core_1.FacetValue)
716
+ .find({ where: { products: { id: product.id } }, relations: ['facet'] }),
717
+ this.connection
718
+ .getRepository(ctx, core_1.ProductOptionGroup)
719
+ .find({ where: { product: { id: product.id } }, relations: ['options'] }),
720
+ this.connection
721
+ .getRepository(ctx, core_1.ProductVariant)
722
+ .find({ where: { product: { id: product.id } }, relations: ['facetValues'] }),
723
+ this.connection.getRepository(ctx, core_1.ProductAsset).find({ where: { productId: product.id } }),
724
+ this.connection
725
+ .getRepository(ctx, core_1.Asset)
726
+ .findOne({ where: { id: product.featuredAssetId } }),
727
+ ]);
728
+ product.facetValues = facetValues;
729
+ product.optionGroups = optionGroups;
730
+ product.variants = variants;
731
+ product.assets = assets;
732
+ featuredAsset && (product.featuredAsset = featuredAsset);
733
+ }
734
+ return product;
735
+ }
729
736
  };
730
737
  ProductImporter = __decorate([
731
738
  (0, common_1.Injectable)(),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@haus-tech/product-import-export-plugin",
3
- "version": "3.0.0",
3
+ "version": "3.0.2",
4
4
  "description": "A Vendure plugin for importing and exporting products using a CSV file",
5
5
  "author": "Haus Tech",
6
6
  "repository": {
@@ -32,4 +32,4 @@
32
32
  "csv-stringify": "^6.5.1",
33
33
  "slug": "^9.1.0"
34
34
  }
35
- }
35
+ }