-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): Implement facet duplicator
Relates to #627
- Loading branch information
1 parent
d457851
commit 8d20847
Showing
6 changed files
with
166 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
packages/core/src/config/entity/entity-duplicators/facet-duplicator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { | ||
CreateFacetInput, | ||
FacetTranslationInput, | ||
LanguageCode, | ||
Permission, | ||
} from '@vendure/common/lib/generated-types'; | ||
|
||
import { Injector } from '../../../common/index'; | ||
import { TransactionalConnection } from '../../../connection/index'; | ||
import { Facet } from '../../../entity/index'; | ||
import { FacetService, FacetValueService } from '../../../service/index'; | ||
import { EntityDuplicator } from '../entity-duplicator'; | ||
|
||
let connection: TransactionalConnection; | ||
let facetService: FacetService; | ||
let facetValueService: FacetValueService; | ||
|
||
/** | ||
* @description | ||
* Duplicates a Facet | ||
*/ | ||
export const facetDuplicator = new EntityDuplicator({ | ||
code: 'facet-duplicator', | ||
description: [ | ||
{ | ||
languageCode: LanguageCode.en, | ||
value: 'Default duplicator for Facets', | ||
}, | ||
], | ||
requiresPermission: [Permission.CreateFacet, Permission.CreateCatalog], | ||
forEntities: ['Facet'], | ||
args: { | ||
includeFacetValues: { | ||
type: 'boolean', | ||
defaultValue: true, | ||
label: [{ languageCode: LanguageCode.en, value: 'Include facet values' }], | ||
}, | ||
}, | ||
init(injector: Injector) { | ||
connection = injector.get(TransactionalConnection); | ||
facetService = injector.get(FacetService); | ||
facetValueService = injector.get(FacetValueService); | ||
}, | ||
async duplicate({ ctx, id, args }) { | ||
const facet = await connection.getEntityOrThrow(ctx, Facet, id, { | ||
relations: { | ||
values: true, | ||
}, | ||
}); | ||
const translations: FacetTranslationInput[] = facet.translations.map(translation => { | ||
return { | ||
name: translation.name + ' (copy)', | ||
languageCode: translation.languageCode, | ||
customFields: translation.customFields, | ||
}; | ||
}); | ||
const facetInput: CreateFacetInput = { | ||
isPrivate: true, | ||
translations, | ||
customFields: facet.customFields, | ||
code: facet.code + '-copy', | ||
}; | ||
|
||
const duplicatedFacet = await facetService.create(ctx, facetInput); | ||
if (args.includeFacetValues) { | ||
if (facet.values.length) { | ||
for (const value of facet.values) { | ||
const newValue = await facetValueService.create(ctx, duplicatedFacet, { | ||
code: value.code + '-copy', | ||
translations: value.translations.map(translation => ({ | ||
name: translation.name + ' (copy)', | ||
languageCode: translation.languageCode, | ||
customFields: translation.customFields, | ||
})), | ||
facetId: duplicatedFacet.id, | ||
customFields: value.customFields, | ||
}); | ||
duplicatedFacet.values.push(newValue); | ||
} | ||
} | ||
} | ||
return duplicatedFacet; | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import { collectionDuplicator } from './collection-duplicator'; | ||
import { facetDuplicator } from './facet-duplicator'; | ||
import { productDuplicator } from './product-duplicator'; | ||
|
||
export const defaultEntityDuplicators = [productDuplicator, collectionDuplicator]; | ||
export const defaultEntityDuplicators = [productDuplicator, collectionDuplicator, facetDuplicator]; |