-
-
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 promotion duplicator
Relates to #627
- Loading branch information
1 parent
8d20847
commit da58b0b
Showing
7 changed files
with
261 additions
and
36 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
Large diffs are not rendered by default.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,11 @@ | ||
import { collectionDuplicator } from './collection-duplicator'; | ||
import { facetDuplicator } from './facet-duplicator'; | ||
import { productDuplicator } from './product-duplicator'; | ||
import { promotionDuplicator } from './promotion-duplicator'; | ||
|
||
export const defaultEntityDuplicators = [productDuplicator, collectionDuplicator, facetDuplicator]; | ||
export const defaultEntityDuplicators = [ | ||
productDuplicator, | ||
collectionDuplicator, | ||
facetDuplicator, | ||
promotionDuplicator, | ||
]; |
73 changes: 73 additions & 0 deletions
73
packages/core/src/config/entity/entity-duplicators/promotion-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,73 @@ | ||
import { | ||
CreateFacetInput, | ||
CreatePromotionInput, | ||
FacetTranslationInput, | ||
LanguageCode, | ||
Permission, | ||
PromotionTranslationInput, | ||
} from '@vendure/common/lib/generated-types'; | ||
|
||
import { Injector, isGraphQlErrorResult } from '../../../common/index'; | ||
import { TransactionalConnection } from '../../../connection/index'; | ||
import { Facet, Promotion } from '../../../entity/index'; | ||
import { FacetService, FacetValueService, PromotionService } from '../../../service/index'; | ||
import { EntityDuplicator } from '../entity-duplicator'; | ||
|
||
let connection: TransactionalConnection; | ||
let promotionService: PromotionService; | ||
|
||
/** | ||
* @description | ||
* Duplicates a Promotion | ||
*/ | ||
export const promotionDuplicator = new EntityDuplicator({ | ||
code: 'promotion-duplicator', | ||
description: [ | ||
{ | ||
languageCode: LanguageCode.en, | ||
value: 'Default duplicator for Promotions', | ||
}, | ||
], | ||
requiresPermission: [Permission.CreatePromotion], | ||
forEntities: ['Promotion'], | ||
args: {}, | ||
init(injector: Injector) { | ||
connection = injector.get(TransactionalConnection); | ||
promotionService = injector.get(PromotionService); | ||
}, | ||
async duplicate({ ctx, id }) { | ||
const promotion = await connection.getEntityOrThrow(ctx, Promotion, id); | ||
const translations: PromotionTranslationInput[] = promotion.translations.map(translation => { | ||
return { | ||
name: translation.name + ' (copy)', | ||
description: translation.description, | ||
languageCode: translation.languageCode, | ||
customFields: translation.customFields, | ||
}; | ||
}); | ||
const promotionInput: CreatePromotionInput = { | ||
couponCode: promotion.couponCode, | ||
startsAt: promotion.startsAt, | ||
endsAt: promotion.endsAt, | ||
perCustomerUsageLimit: promotion.perCustomerUsageLimit, | ||
usageLimit: promotion.usageLimit, | ||
conditions: promotion.conditions.map(condition => ({ | ||
code: condition.code, | ||
arguments: condition.args, | ||
})), | ||
actions: promotion.actions.map(action => ({ | ||
code: action.code, | ||
arguments: action.args, | ||
})), | ||
enabled: false, | ||
translations, | ||
customFields: promotion.customFields, | ||
}; | ||
|
||
const duplicatedPromotion = await promotionService.createPromotion(ctx, promotionInput); | ||
if (isGraphQlErrorResult(duplicatedPromotion)) { | ||
throw new Error(duplicatedPromotion.message); | ||
} | ||
return duplicatedPromotion; | ||
}, | ||
}); |