@claspo/renderer 18.9.0-sp7 → 18.9.0-sp9
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.
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@claspo/renderer",
|
|
3
|
-
"version": "18.9.0-
|
|
3
|
+
"version": "18.9.0-sp9",
|
|
4
4
|
"scripts": {
|
|
5
5
|
"dev": " tsc --project tsconfig.json --watch",
|
|
6
6
|
"build": "rm -rf out && tsc --project tsconfig.json && npm run minify",
|
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
"node": ">=18.16.0"
|
|
16
16
|
},
|
|
17
17
|
"dependencies": {
|
|
18
|
-
"@claspo/common": "7.5.0-
|
|
18
|
+
"@claspo/common": "7.5.0-sp5",
|
|
19
19
|
"@testing-library/dom": "^10.4.0"
|
|
20
20
|
},
|
|
21
21
|
"devDependencies": {
|
|
@@ -6,6 +6,8 @@ export default class PrizePool {
|
|
|
6
6
|
randomDistributor: any;
|
|
7
7
|
constructor(config: any, httpClient: any, configService: any);
|
|
8
8
|
load(): Promise<void>;
|
|
9
|
+
_fetchReward(url: any, options: any): Promise<any>;
|
|
10
|
+
_mergeRewardIntoOption(reward: any, options: any): any;
|
|
9
11
|
_fetchPool(url: any): Promise<any>;
|
|
10
12
|
_fetchJson(url: any, headers?: any): Promise<any>;
|
|
11
13
|
getModel(): any;
|
|
@@ -17,6 +19,8 @@ export default class PrizePool {
|
|
|
17
19
|
value: any;
|
|
18
20
|
};
|
|
19
21
|
_calculatePrize(): any;
|
|
22
|
+
_mintsCodePerWinner(options: any): any;
|
|
20
23
|
_isPreview(): boolean;
|
|
24
|
+
_isCodeMintedPerWinner(option: any): boolean;
|
|
21
25
|
_mapLabelSuffix(value: any, valueType: any): any;
|
|
22
26
|
}
|
package/prize-pool/PrizePool.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import sum from"@claspo/common/math/sum";import RandomDistributor from"@claspo/common/math/RandomDistributor";import{PrizeOptionValueType,PrizeSourceType}from"@claspo/common/PrizePool.interface";import{PreviewMode}from"../sdk/PreviewMode";const FETCHABLE_SOURCE_TYPES=[PrizeSourceType.MANUAL,PrizeSourceType.SHOPIFY],drawnPrizeCodePlaceholder="
|
|
1
|
+
import sum from"@claspo/common/math/sum";import RandomDistributor from"@claspo/common/math/RandomDistributor";import{PrizeCodeType,PrizeOptionValueType,PrizeSourceType}from"@claspo/common/PrizePool.interface";import{PreviewMode}from"../sdk/PreviewMode";const FETCHABLE_SOURCE_TYPES=[PrizeSourceType.MANUAL,PrizeSourceType.SHOPIFY],drawnPrizeCodePlaceholder="CLXXXXXXXX";export default class PrizePool{constructor(e,i,t){if(this.httpClient=i,this.configService=t,this.instanceModel=e||{},"FIXED"!==this.instanceModel.model){const e=this.configService.getConfig("prizePoolModels");if(e&&this.instanceModel.id){const i=e.find(e=>e.id===this.instanceModel.id);i&&(this.instanceModel=i)}}this.prizeOption=null,this.randomDistributor=new RandomDistributor}async load(){const{id:e,model:i,sourceType:t,options:o}=this.instanceModel;if(!(!o&&!!e&&"FIXED"!==i&&(!t||FETCHABLE_SOURCE_TYPES.includes(t))))return;const n=this.configService.getConfig("widgetId");try{const i=this.configService.getConfig("hostUrl"),o=`${i}forms/v2/prize/pool/${e}`,s=`${i}forms/v1/prizePool/${e}/reward?widgetId=${n}`;if(this._isPreview()){const e=await this._fetchPool(o);if(this.instanceModel=Object.assign(Object.assign({},this.instanceModel),{sourceType:t||e.sourceType,options:e.options}),this._mintsCodePerWinner(e.options))return;return void(this.prizeOption=await this._fetchReward(s,e.options))}const[r,c]=await Promise.all([this._fetchPool(o),this._fetchJson(s,{"cl-customer-id":this.configService.getConfig("clCustomerId")})]);this.instanceModel=Object.assign(Object.assign({},this.instanceModel),{sourceType:t||r.sourceType,options:r.options}),this.prizeOption=this._mergeRewardIntoOption(c,r.options)}catch(i){throw console.error(`PrizePool: failed to load pool or reward for widgetId "${n}" with poolId "${e}"`,i),i}}async _fetchReward(e,i){const t=await this._fetchJson(e,{"cl-customer-id":this.configService.getConfig("clCustomerId")});return this._mergeRewardIntoOption(t,i)}_mergeRewardIntoOption(e,i){return Object.assign(Object.assign({},i.find(i=>i.id===e.id)),e)}async _fetchPool(e){const i=await this._fetchJson(e);if(!1===(null==i?void 0:i.available))throw new Error("PrizePool: the pool is not available and cannot be handed out");return i}async _fetchJson(e,i){const t=await(i?this.httpClient.execute(e,"GET",null,i):this.httpClient.execute(e,"GET"));if(!t.ok)throw new Error(`PrizePool: request failed with status ${t.status} - ${e}`);return t.json()}getModel(){return this.instanceModel}async getPrize(){return this.instanceModel?"FIXED"===this.instanceModel.model?this.instanceModel.data:FETCHABLE_SOURCE_TYPES.includes(this.instanceModel.sourceType)?(this.instanceModel.options&&!this.prizeOption&&(this.prizeOption=this._calculatePrize()),this.prizeOption?this.mapPrizeOption(this.prizeOption):null):null:null}submitPrize(){}mapPrizeOption(e){const i=e.valueType===PrizeOptionValueType.PROMOCODE?e.value||"":this._mapLabelSuffix(e.value,e.valueType);return{id:e.id,label:i,value:e.code}}_calculatePrize(){const e=sum(this.instanceModel.options.map(e=>parseFloat(e.weight))),i=this.randomDistributor.calculateFromWeights(this.instanceModel.options,e);return i&&this._isCodeMintedPerWinner(i)?Object.assign(Object.assign({},i),{code:"CLXXXXXXXX"}):i}_mintsCodePerWinner(e){return(e||[]).some(e=>this._isCodeMintedPerWinner(e))}_isPreview(){return!!(this.configService.getConfig(PreviewMode.CABINET_PREVIEW)||this.configService.getConfig(PreviewMode.EDITOR_PREVIEW)||this.configService.getConfig(PreviewMode.DEMO))}_isCodeMintedPerWinner(e){var i;return(null===(i=null==e?void 0:e.config)||void 0===i?void 0:i.codeType)===PrizeCodeType.UNIQUE}_mapLabelSuffix(e,i){return i===PrizeOptionValueType.DISCOUNT_PERCENTAGE?`${e}%`:e}}
|