@haus-tech/product-import-export-plugin 3.0.8 → 3.0.10

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.
@@ -56,105 +56,79 @@ let ExtendedAssetImporter = class ExtendedAssetImporter {
56
56
  const errors = [];
57
57
  const uniqueAssetPaths = new Set(assetPaths);
58
58
  for (const assetPath of uniqueAssetPaths.values()) {
59
- const cachedAsset = this.assetMap.get(assetPath.url);
60
- if (cachedAsset) {
61
- if (ctx?.channelId &&
62
- cachedAsset.channels.some((channel) => channel.id !== ctx?.channelId)) {
63
- await this.connection
64
- .getRepository(ctx, core_1.Asset)
65
- .createQueryBuilder()
66
- .relation(core_1.Asset, 'channels')
67
- .of(cachedAsset.id)
68
- .add(ctx?.channel);
69
- cachedAsset.channels.push(ctx?.channel);
70
- }
71
- if (cachedAsset.name !== assetPath.name && assetPath.name) {
72
- this.connection.getRepository(ctx, core_1.Asset).update(cachedAsset.id, {
73
- name: assetPath.name,
74
- });
75
- // Update the cached asset with the new name
76
- this.assetMap.set(assetPath.url, { ...cachedAsset, name: assetPath.name });
77
- assets.push({ ...cachedAsset, name: assetPath.name });
78
- }
79
- else {
80
- assets.push(cachedAsset);
81
- }
82
- }
83
- else {
84
- try {
85
- const assetHash = (0, generate_asset_hash_1.generateAssetHash)({
86
- source: assetPath.url,
87
- });
88
- const assetFromHash = await this.connection.getRepository(ctx, core_1.Asset).findOne({
89
- where: {
90
- customFields: {
91
- hash: assetHash,
92
- },
59
+ try {
60
+ const assetHash = (0, generate_asset_hash_1.generateAssetHash)({
61
+ source: assetPath.url,
62
+ });
63
+ const assetFromHash = await this.connection.getRepository(ctx, core_1.Asset).findOne({
64
+ where: {
65
+ customFields: {
66
+ hash: assetHash,
93
67
  },
94
- relations: ['channels'],
95
- });
96
- if (assetFromHash) {
97
- // Check if the asset name has changed
98
- if ((assetPath.name && assetFromHash.name !== assetPath.name) ||
99
- assetFromHash.channels.some((channel) => channel.id !== ctx?.channelId)) {
100
- assetFromHash.name = assetPath.name || assetFromHash.name;
101
- assetFromHash.channels.push(ctx?.channel);
102
- await this.connection.getRepository(ctx, core_1.Asset).save(assetFromHash);
103
- }
104
- this.assetMap.set(assetPath.url, assetFromHash);
105
- assets.push(assetFromHash);
106
- continue;
68
+ },
69
+ relations: ['channels'],
70
+ });
71
+ if (assetFromHash) {
72
+ // Check if the asset name has changed
73
+ if ((assetPath.name && assetFromHash.name !== assetPath.name) ||
74
+ assetFromHash.channels.some((channel) => channel.id !== ctx?.channelId)) {
75
+ assetFromHash.name = assetPath.name || assetFromHash.name;
76
+ assetFromHash.channels.push(ctx?.channel);
77
+ await this.connection.getRepository(ctx, core_1.Asset).save(assetFromHash);
107
78
  }
108
- const stream = await this.getStreamFromPath(assetPath.url);
109
- if (stream) {
110
- const createdAsset = await this.assetService.createFromFileStream(stream, assetPath.url, ctx);
111
- if ((0, core_1.isGraphQlErrorResult)(createdAsset)) {
112
- errors.push(createdAsset.message);
113
- }
114
- else {
115
- let assetToUpdate = createdAsset;
116
- let shouldDeleteCreatedAsset = false;
117
- const assetFromHash = await this.connection.getRepository(ctx, core_1.Asset).findOne({
118
- where: {
119
- customFields: {
120
- hash: assetHash,
121
- },
79
+ this.assetMap.set(assetPath.url, assetFromHash);
80
+ assets.push(assetFromHash);
81
+ continue;
82
+ }
83
+ const stream = await this.getStreamFromPath(assetPath.url);
84
+ if (stream) {
85
+ const createdAsset = await this.assetService.createFromFileStream(stream, assetPath.url, ctx);
86
+ if ((0, core_1.isGraphQlErrorResult)(createdAsset)) {
87
+ errors.push(createdAsset.message);
88
+ }
89
+ else {
90
+ let assetToUpdate = createdAsset;
91
+ let shouldDeleteCreatedAsset = false;
92
+ const assetFromHash = await this.connection.getRepository(ctx, core_1.Asset).findOne({
93
+ where: {
94
+ customFields: {
95
+ hash: assetHash,
122
96
  },
123
- relations: ['channels'],
124
- });
125
- if (assetFromHash) {
126
- shouldDeleteCreatedAsset = true;
127
- assetToUpdate = { ...assetFromHash, name: assetPath.name || assetFromHash.name };
128
- }
129
- if (assetPath.id) {
130
- shouldDeleteCreatedAsset = true;
131
- assetToUpdate = {
132
- ...assetToUpdate,
133
- id: assetPath.id,
134
- };
135
- }
136
- this.assetMap.set(assetPath.url, assetToUpdate);
137
- assets.push(assetToUpdate);
138
- assetToUpdate.name = assetPath.name || assetToUpdate.name;
139
- assetToUpdate.channels.push(ctx?.channel);
140
- assetToUpdate.customFields = {
141
- ...assetToUpdate.customFields,
142
- hash: assetHash,
97
+ },
98
+ relations: ['channels'],
99
+ });
100
+ if (assetFromHash) {
101
+ shouldDeleteCreatedAsset = true;
102
+ assetToUpdate = { ...assetFromHash, name: assetPath.name || assetFromHash.name };
103
+ }
104
+ if (assetPath.id) {
105
+ shouldDeleteCreatedAsset = true;
106
+ assetToUpdate = {
107
+ ...assetToUpdate,
108
+ id: assetPath.id,
143
109
  };
144
- await this.connection.getRepository(ctx, core_1.Asset).save(assetToUpdate);
145
- if (shouldDeleteCreatedAsset) {
146
- this.connection.getRepository(ctx, core_1.Asset).delete(createdAsset.id);
147
- }
110
+ }
111
+ this.assetMap.set(assetPath.url, assetToUpdate);
112
+ assets.push(assetToUpdate);
113
+ assetToUpdate.name = assetPath.name || assetToUpdate.name;
114
+ assetToUpdate.channels.push(ctx?.channel);
115
+ assetToUpdate.customFields = {
116
+ ...assetToUpdate.customFields,
117
+ hash: assetHash,
118
+ };
119
+ await this.connection.getRepository(ctx, core_1.Asset).save(assetToUpdate);
120
+ if (shouldDeleteCreatedAsset) {
121
+ this.connection.getRepository(ctx, core_1.Asset).delete(createdAsset.id);
148
122
  }
149
123
  }
150
124
  }
151
- catch (e) {
152
- if (e.message.includes('no elements in sequence')) {
153
- errors.push(`Could not find asset at path "${assetPath.url}"`);
154
- }
155
- else {
156
- errors.push(e.message);
157
- }
125
+ }
126
+ catch (e) {
127
+ if (e.message.includes('no elements in sequence')) {
128
+ errors.push(`Could not find asset at path "${assetPath.url}"`);
129
+ }
130
+ else {
131
+ errors.push(e.message);
158
132
  }
159
133
  }
160
134
  }
@@ -13,6 +13,7 @@ exports.ProductImporter = void 0;
13
13
  const common_1 = require("@nestjs/common");
14
14
  const core_1 = require("@vendure/core");
15
15
  const rxjs_1 = require("rxjs");
16
+ const generated_types_1 = require("@vendure/common/lib/generated-types");
16
17
  const extended_fast_importer_service_1 = require("../../services/extended-fast-importer.service");
17
18
  const lodash_1 = require("lodash");
18
19
  const normalize_string_1 = require("../../helpers/normalize-string");
@@ -312,12 +313,16 @@ let ProductImporter = class ProductImporter {
312
313
  if (existingProduct && updatingStrategy === 'replace') {
313
314
  await this.fastImporter.removeAllAssetsFromProduct(existingProduct);
314
315
  }
316
+ const getFeaturedAssetId = (product) => {
317
+ if (product?.featuredAsset) {
318
+ const assetIsImage = product.featuredAsset.type === generated_types_1.AssetType.IMAGE;
319
+ return assetIsImage ? product.featuredAsset.id : undefined;
320
+ }
321
+ const firstAsset = productAssets.find((a) => a.type === generated_types_1.AssetType.IMAGE);
322
+ return firstAsset ? firstAsset.id : undefined;
323
+ };
315
324
  const productData = {
316
- featuredAssetId: existingProduct?.featuredAsset
317
- ? undefined
318
- : productAssets.length
319
- ? productAssets[0].id
320
- : undefined,
325
+ featuredAssetId: getFeaturedAssetId(existingProduct),
321
326
  assetIds: productAssets.map((a) => a.id),
322
327
  facetValueIds: updatingStrategy === 'replace' ? [] : undefined,
323
328
  translations: await Promise.all(product.translations.map(async (translation) => {
@@ -440,14 +445,18 @@ let ProductImporter = class ProductImporter {
440
445
  // Remove all assets from the product
441
446
  await this.fastImporter.removeAllAssetsFromVariant(existingVariant);
442
447
  }
448
+ const getFeaturedAssetId = (variant) => {
449
+ if (variant?.featuredAsset) {
450
+ const assetIsImage = variant.featuredAsset.type === generated_types_1.AssetType.IMAGE;
451
+ return assetIsImage ? variant.featuredAsset.id : undefined;
452
+ }
453
+ const firstAsset = variantAssets.find((a) => a.type === generated_types_1.AssetType.IMAGE);
454
+ return firstAsset ? firstAsset.id : undefined;
455
+ };
443
456
  const variantData = {
444
457
  productId: createdProductId,
445
458
  facetValueIds: updatingStrategy === 'replace' ? [] : undefined,
446
- featuredAssetId: existingVariant?.featuredAsset
447
- ? undefined
448
- : variantAssets.length
449
- ? variantAssets[0].id
450
- : undefined,
459
+ featuredAssetId: getFeaturedAssetId(existingVariant),
451
460
  assetIds: variantAssets.map((a) => a.id),
452
461
  sku: variant.sku,
453
462
  taxCategoryId: this.getMatchingTaxCategoryId(variant.taxCategory, taxCategories.items),
@@ -69,6 +69,9 @@ let ExtendedFastImporterService = class ExtendedFastImporterService {
69
69
  if (input.featuredAssetId) {
70
70
  p.featuredAsset = { id: input.featuredAssetId };
71
71
  }
72
+ else {
73
+ (0, lodash_1.set)(p, 'featuredAssetId', null);
74
+ }
72
75
  },
73
76
  });
74
77
  if (input.assetIds) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@haus-tech/product-import-export-plugin",
3
- "version": "3.0.8",
3
+ "version": "3.0.10",
4
4
  "description": "A Vendure plugin for importing and exporting products using a CSV file",
5
5
  "author": "Haus Tech",
6
6
  "repository": {