-
Notifications
You must be signed in to change notification settings - Fork 2
/
duplicator.service.ts
55 lines (53 loc) · 2.44 KB
/
duplicator.service.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
import { Injectable } from '@angular/core';
import { RequestContext, TransactionalConnection, Product, ID, EntityNotFoundError, ProductVariantService, ProductService } from '@vendure/core';
@Injectable()
export class DuplicatorService {
private readonly relations = ['featuredAsset', 'assets', 'channels', 'facetValues', 'facetValues.facet'];
constructor(
private connection: TransactionalConnection,
private productVariantsSvc: ProductVariantService,
private productSvc: ProductService
) {}
async duplicate(ctx: RequestContext, productId: ID) {
const product = await this.connection.getRepository(Product).findOne( productId, { relations: this.relations } );
const productVariants = (await this.productVariantsSvc.getVariantsByProductId(ctx, productId)).items;
if (!product) {
throw new EntityNotFoundError('Product', productId);
}
const translations = product.translations.map(translation => {
return {
name: translation.name + ' Copy',
slug: translation.slug + '-copy',
description: translation.description,
languageCode: translation.languageCode,
customFields: translation.customFields
}
});
const candidateProduct = {
featuredAssetId: product.featuredAsset.id,
enabled: false,
assetIds: product.assets.map(value => value.assetId),
facetValueIds: product.facetValues.map(value => value.id),
translations: translations,
customFields: product.customFields
}
const duplicatedProduct = await this.productSvc.create(ctx, candidateProduct);
const duplicatedProductVariants = await this.productVariantsSvc.create(ctx, productVariants.map((variant, i) => {
return {
productId: duplicatedProduct.id,
price: variant.price,
sku: variant.sku + '-' + i,
stockOnHand: variant.stockOnHand,
translations: variant.translations.map(translation => {
return {
languageCode: translation.languageCode,
name: translation.name + ' Copy'
};
}),
optionIds: []
}
}));
duplicatedProduct.variants = duplicatedProductVariants;
return duplicatedProduct;
}
}