@haus-tech/product-import-export-plugin 3.0.3 → 3.0.4
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,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { OnModuleInit } from '@nestjs/common';
|
|
2
|
+
import { Asset, AssetService, ChannelService, ConfigService, RequestContext, TransactionalConnection } from '@vendure/core';
|
|
2
3
|
import { JsonAsset } from '../../types';
|
|
3
4
|
/**
|
|
4
5
|
* @description
|
|
@@ -8,13 +9,16 @@ import { JsonAsset } from '../../types';
|
|
|
8
9
|
*
|
|
9
10
|
* @docsCategory import-export
|
|
10
11
|
*/
|
|
11
|
-
export declare class ExtendedAssetImporter {
|
|
12
|
+
export declare class ExtendedAssetImporter implements OnModuleInit {
|
|
12
13
|
private configService;
|
|
13
14
|
private assetService;
|
|
14
15
|
private connection;
|
|
16
|
+
private channelService;
|
|
15
17
|
private assetMap;
|
|
18
|
+
private defaultChannel;
|
|
16
19
|
/** @internal */
|
|
17
|
-
constructor(configService: ConfigService, assetService: AssetService, connection: TransactionalConnection);
|
|
20
|
+
constructor(configService: ConfigService, assetService: AssetService, connection: TransactionalConnection, channelService: ChannelService);
|
|
21
|
+
onModuleInit(): Promise<void>;
|
|
18
22
|
/**
|
|
19
23
|
* @description
|
|
20
24
|
* Creates Asset entities for the given paths, using the assetMap cache to prevent the
|
|
@@ -25,12 +25,18 @@ let ExtendedAssetImporter = class ExtendedAssetImporter {
|
|
|
25
25
|
configService;
|
|
26
26
|
assetService;
|
|
27
27
|
connection;
|
|
28
|
+
channelService;
|
|
28
29
|
assetMap = new Map();
|
|
30
|
+
defaultChannel;
|
|
29
31
|
/** @internal */
|
|
30
|
-
constructor(configService, assetService, connection) {
|
|
32
|
+
constructor(configService, assetService, connection, channelService) {
|
|
31
33
|
this.configService = configService;
|
|
32
34
|
this.assetService = assetService;
|
|
33
35
|
this.connection = connection;
|
|
36
|
+
this.channelService = channelService;
|
|
37
|
+
}
|
|
38
|
+
async onModuleInit() {
|
|
39
|
+
this.defaultChannel = await this.channelService.getDefaultChannel();
|
|
34
40
|
}
|
|
35
41
|
/**
|
|
36
42
|
* @description
|
|
@@ -45,10 +51,20 @@ let ExtendedAssetImporter = class ExtendedAssetImporter {
|
|
|
45
51
|
for (const assetPath of uniqueAssetPaths.values()) {
|
|
46
52
|
const cachedAsset = this.assetMap.get(assetPath.url);
|
|
47
53
|
if (cachedAsset) {
|
|
54
|
+
if (cachedAsset.channels.some((channel) => channel.id !== ctx?.channelId)) {
|
|
55
|
+
await this.connection
|
|
56
|
+
.getRepository(ctx, core_1.Asset)
|
|
57
|
+
.createQueryBuilder()
|
|
58
|
+
.relation(core_1.Asset, 'channels')
|
|
59
|
+
.of(cachedAsset.id)
|
|
60
|
+
.add(ctx?.channel);
|
|
61
|
+
cachedAsset.channels.push(ctx?.channel);
|
|
62
|
+
}
|
|
48
63
|
if (cachedAsset.name !== assetPath.name && assetPath.name) {
|
|
49
64
|
this.connection.getRepository(ctx, core_1.Asset).update(cachedAsset.id, {
|
|
50
65
|
name: assetPath.name,
|
|
51
66
|
});
|
|
67
|
+
// Update the cached asset with the new name
|
|
52
68
|
this.assetMap.set(assetPath.url, { ...cachedAsset, name: assetPath.name });
|
|
53
69
|
assets.push({ ...cachedAsset, name: assetPath.name });
|
|
54
70
|
}
|
|
@@ -67,13 +83,15 @@ let ExtendedAssetImporter = class ExtendedAssetImporter {
|
|
|
67
83
|
hash: assetHash,
|
|
68
84
|
},
|
|
69
85
|
},
|
|
86
|
+
relations: ['channels'],
|
|
70
87
|
});
|
|
71
88
|
if (assetFromHash) {
|
|
72
89
|
// Check if the asset name has changed
|
|
73
|
-
if (assetPath.name && assetFromHash.name !== assetPath.name)
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
90
|
+
if ((assetPath.name && assetFromHash.name !== assetPath.name) ||
|
|
91
|
+
assetFromHash.channels.some((channel) => channel.id !== ctx?.channelId)) {
|
|
92
|
+
assetFromHash.name = assetPath.name || assetFromHash.name;
|
|
93
|
+
assetFromHash.channels.push(ctx?.channel);
|
|
94
|
+
await this.connection.getRepository(ctx, core_1.Asset).save(assetFromHash);
|
|
77
95
|
}
|
|
78
96
|
this.assetMap.set(assetPath.url, assetFromHash);
|
|
79
97
|
assets.push(assetFromHash);
|
|
@@ -94,12 +112,12 @@ let ExtendedAssetImporter = class ExtendedAssetImporter {
|
|
|
94
112
|
hash: assetHash,
|
|
95
113
|
},
|
|
96
114
|
},
|
|
115
|
+
relations: ['channels'],
|
|
97
116
|
});
|
|
98
117
|
if (assetFromHash) {
|
|
99
118
|
shouldDeleteCreatedAsset = true;
|
|
100
119
|
assetToUpdate = { ...assetFromHash, name: assetPath.name || assetFromHash.name };
|
|
101
120
|
}
|
|
102
|
-
console.log('assetFromHash', assetFromHash);
|
|
103
121
|
if (assetPath.id) {
|
|
104
122
|
shouldDeleteCreatedAsset = true;
|
|
105
123
|
assetToUpdate = {
|
|
@@ -109,12 +127,13 @@ let ExtendedAssetImporter = class ExtendedAssetImporter {
|
|
|
109
127
|
}
|
|
110
128
|
this.assetMap.set(assetPath.url, assetToUpdate);
|
|
111
129
|
assets.push(assetToUpdate);
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
}
|
|
130
|
+
assetToUpdate.name = assetPath.name || assetToUpdate.name;
|
|
131
|
+
assetToUpdate.channels.push(ctx?.channel);
|
|
132
|
+
assetToUpdate.customFields = {
|
|
133
|
+
...assetToUpdate.customFields,
|
|
134
|
+
hash: assetHash,
|
|
135
|
+
};
|
|
136
|
+
await this.connection.getRepository(ctx, core_1.Asset).save(assetToUpdate);
|
|
118
137
|
if (shouldDeleteCreatedAsset) {
|
|
119
138
|
this.connection.getRepository(ctx, core_1.Asset).delete(createdAsset.id);
|
|
120
139
|
}
|
|
@@ -133,6 +152,7 @@ ExtendedAssetImporter = __decorate([
|
|
|
133
152
|
(0, common_1.Injectable)(),
|
|
134
153
|
__metadata("design:paramtypes", [core_1.ConfigService,
|
|
135
154
|
core_1.AssetService,
|
|
136
|
-
core_1.TransactionalConnection
|
|
155
|
+
core_1.TransactionalConnection,
|
|
156
|
+
core_1.ChannelService])
|
|
137
157
|
], ExtendedAssetImporter);
|
|
138
158
|
exports.ExtendedAssetImporter = ExtendedAssetImporter;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@haus-tech/product-import-export-plugin",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.4",
|
|
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
|
+
}
|