-
-
Notifications
You must be signed in to change notification settings - Fork 944
/
populator.ts
334 lines (316 loc) · 13.3 KB
/
populator.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
import { Injectable } from '@nestjs/common';
import { ConfigurableOperationInput } from '@vendure/common/lib/generated-types';
import { normalizeString } from '@vendure/common/lib/normalize-string';
import { notNullOrUndefined } from '@vendure/common/lib/shared-utils';
import { RequestContext } from '../../../api/common/request-context';
import {
ConfigService,
defaultShippingCalculator,
defaultShippingEligibilityChecker,
Logger,
} from '../../../config';
import { manualFulfillmentHandler } from '../../../config/fulfillment/manual-fulfillment-handler';
import { TransactionalConnection } from '../../../connection/index';
import { Channel, Collection, FacetValue, TaxCategory, User } from '../../../entity';
import {
CollectionService,
FacetValueService,
PaymentMethodService,
RequestContextService,
RoleService,
ShippingMethodService,
} from '../../../service';
import { ChannelService } from '../../../service/services/channel.service';
import { CountryService } from '../../../service/services/country.service';
import { SearchService } from '../../../service/services/search.service';
import { TaxCategoryService } from '../../../service/services/tax-category.service';
import { TaxRateService } from '../../../service/services/tax-rate.service';
import { ZoneService } from '../../../service/services/zone.service';
import {
CollectionFilterDefinition,
CountryDefinition,
InitialData,
RoleDefinition,
ZoneMap,
} from '../../types';
import { AssetImporter } from '../asset-importer/asset-importer';
/**
* @description
* Responsible for populating the database with {@link InitialData}, i.e. non-product data such as countries, tax rates,
* shipping methods, payment methods & roles.
*
* @docsCategory import-export
*/
@Injectable()
export class Populator {
/** @internal */
constructor(
private countryService: CountryService,
private zoneService: ZoneService,
private channelService: ChannelService,
private taxRateService: TaxRateService,
private taxCategoryService: TaxCategoryService,
private shippingMethodService: ShippingMethodService,
private paymentMethodService: PaymentMethodService,
private collectionService: CollectionService,
private facetValueService: FacetValueService,
private searchService: SearchService,
private assetImporter: AssetImporter,
private roleService: RoleService,
private configService: ConfigService,
private connection: TransactionalConnection,
private requestContextService: RequestContextService,
) {}
/**
* @description
* Should be run *before* populating the products, so that there are TaxRates by which
* product prices can be set. If the `channel` argument is set, then any {@link ChannelAware}
* entities will be assigned to that Channel.
*/
async populateInitialData(data: InitialData, channel?: Channel) {
const ctx = await this.createRequestContext(data, channel);
let zoneMap: ZoneMap;
try {
zoneMap = await this.populateCountries(ctx, data.countries);
} catch (e: any) {
Logger.error('Could not populate countries');
Logger.error(e, 'populator', e.stack);
throw e;
}
try {
await this.populateTaxRates(ctx, data.taxRates, zoneMap);
} catch (e: any) {
Logger.error('Could not populate tax rates');
Logger.error(e, 'populator', e.stack);
}
try {
await this.populateShippingMethods(ctx, data.shippingMethods);
} catch (e: any) {
Logger.error('Could not populate shipping methods');
Logger.error(e, 'populator', e.stack);
}
try {
await this.populatePaymentMethods(ctx, data.paymentMethods);
} catch (e: any) {
Logger.error('Could not populate payment methods');
Logger.error(e, 'populator', e.stack);
}
try {
await this.setChannelDefaults(zoneMap, data, ctx.channel);
} catch (e: any) {
Logger.error('Could not set channel defaults');
Logger.error(e, 'populator', e.stack);
}
try {
await this.populateRoles(ctx, data.roles);
} catch (e: any) {
Logger.error('Could not populate roles');
Logger.error(e, 'populator', e.stack);
}
}
/**
* @description
* Should be run *after* the products have been populated, otherwise the expected FacetValues will not
* yet exist.
*/
async populateCollections(data: InitialData, channel?: Channel) {
const ctx = await this.createRequestContext(data, channel);
const allFacetValues = await this.facetValueService.findAll(ctx, ctx.languageCode);
const collectionMap = new Map<string, Collection>();
for (const collectionDef of data.collections) {
const parent = collectionDef.parentName && collectionMap.get(collectionDef.parentName);
const parentId = parent ? parent.id.toString() : undefined;
const { assets } = await this.assetImporter.getAssets(collectionDef.assetPaths || []);
let filters: ConfigurableOperationInput[] = [];
try {
filters = (collectionDef.filters || []).map(filter =>
this.processFilterDefinition(filter, allFacetValues),
);
} catch (e: any) {
Logger.error(e.message);
}
const collection = await this.collectionService.create(ctx, {
translations: [
{
languageCode: ctx.languageCode,
name: collectionDef.name,
description: collectionDef.description || '',
slug: collectionDef.slug ?? collectionDef.name,
},
],
isPrivate: collectionDef.private || false,
parentId,
assetIds: assets.map(a => a.id.toString()),
featuredAssetId: assets.length ? assets[0].id.toString() : undefined,
filters,
});
collectionMap.set(collectionDef.name, collection);
}
// Wait for the created collection operations to complete before running
// the reindex of the search index.
await new Promise(resolve => setTimeout(resolve, 50));
await this.searchService.reindex(ctx);
}
private processFilterDefinition(
filter: CollectionFilterDefinition,
allFacetValues: FacetValue[],
): ConfigurableOperationInput {
switch (filter.code) {
case 'facet-value-filter':
const facetValueIds = filter.args.facetValueNames
.map(name =>
allFacetValues.find(fv => {
let facetName;
let valueName = name;
if (name.includes(':')) {
[facetName, valueName] = name.split(':');
return (
(fv.name === valueName || fv.code === valueName) &&
(fv.facet.name === facetName || fv.facet.code === facetName)
);
} else {
return fv.name === valueName || fv.code === valueName;
}
}),
)
.filter(notNullOrUndefined)
.map(fv => fv.id);
return {
code: filter.code,
arguments: [
{
name: 'facetValueIds',
value: JSON.stringify(facetValueIds),
},
{
name: 'containsAny',
value: filter.args.containsAny.toString(),
},
],
};
default:
throw new Error(`Filter with code "${filter.code as string}" is not recognized.`);
}
}
private async createRequestContext(data: InitialData, channel?: Channel) {
const { superadminCredentials } = this.configService.authOptions;
const superAdminUser = await this.connection.rawConnection.getRepository(User).findOne({
where: {
identifier: superadminCredentials.identifier,
},
});
const ctx = await this.requestContextService.create({
user: superAdminUser ?? undefined,
apiType: 'admin',
languageCode: data.defaultLanguage,
channelOrToken: channel ?? (await this.channelService.getDefaultChannel()),
});
return ctx;
}
private async setChannelDefaults(zoneMap: ZoneMap, data: InitialData, channel: Channel) {
const defaultZone = zoneMap.get(data.defaultZone);
if (!defaultZone) {
throw new Error(
`The defaultZone (${data.defaultZone}) did not match any existing or created zone names`,
);
}
const defaultZoneId = defaultZone.entity.id;
await this.channelService.update(RequestContext.empty(), {
id: channel.id,
defaultTaxZoneId: defaultZoneId,
defaultShippingZoneId: defaultZoneId,
});
}
private async populateCountries(ctx: RequestContext, countries: CountryDefinition[]): Promise<ZoneMap> {
const zoneMap: ZoneMap = new Map();
const existingZones = await this.zoneService.getAllWithMembers(ctx);
for (const zone of existingZones) {
zoneMap.set(zone.name, { entity: zone, members: zone.members.map(m => m.id) });
}
for (const { name, code, zone } of countries) {
const countryEntity = await this.countryService.create(ctx, {
code,
enabled: true,
translations: [{ languageCode: ctx.languageCode, name }],
});
let zoneItem = zoneMap.get(zone);
if (!zoneItem) {
const zoneEntity = await this.zoneService.create(ctx, { name: zone });
zoneItem = { entity: zoneEntity, members: [] };
zoneMap.set(zone, zoneItem);
}
if (!zoneItem.members.includes(countryEntity.id)) {
zoneItem.members.push(countryEntity.id);
}
}
// add the countries to the respective zones
for (const zoneItem of zoneMap.values()) {
await this.zoneService.addMembersToZone(ctx, {
zoneId: zoneItem.entity.id,
memberIds: zoneItem.members,
});
}
return zoneMap;
}
private async populateTaxRates(
ctx: RequestContext,
taxRates: Array<{ name: string; percentage: number }>,
zoneMap: ZoneMap,
) {
const taxCategories: TaxCategory[] = [];
for (const taxRate of taxRates) {
const category = await this.taxCategoryService.create(ctx, { name: taxRate.name });
for (const { entity } of zoneMap.values()) {
await this.taxRateService.create(ctx, {
zoneId: entity.id,
value: taxRate.percentage,
categoryId: category.id,
name: `${taxRate.name} ${entity.name}`,
enabled: true,
});
}
}
}
private async populateShippingMethods(
ctx: RequestContext,
shippingMethods: Array<{ name: string; price: number }>,
) {
for (const method of shippingMethods) {
await this.shippingMethodService.create(ctx, {
fulfillmentHandler: manualFulfillmentHandler.code,
checker: {
code: defaultShippingEligibilityChecker.code,
arguments: [{ name: 'orderMinimum', value: '0' }],
},
calculator: {
code: defaultShippingCalculator.code,
arguments: [
{ name: 'rate', value: method.price.toString() },
{ name: 'taxRate', value: '0' },
{ name: 'includesTax', value: 'auto' },
],
},
code: normalizeString(method.name, '-'),
translations: [{ languageCode: ctx.languageCode, name: method.name, description: '' }],
});
}
}
private async populatePaymentMethods(ctx: RequestContext, paymentMethods: InitialData['paymentMethods']) {
for (const method of paymentMethods) {
await this.paymentMethodService.create(ctx, {
code: normalizeString(method.name, '-'),
enabled: true,
handler: method.handler,
translations: [{ languageCode: ctx.languageCode, name: method.name, description: '' }],
});
}
}
private async populateRoles(ctx: RequestContext, roles?: RoleDefinition[]) {
if (!roles) {
return;
}
for (const roleDef of roles) {
await this.roleService.create(ctx, roleDef);
}
}
}