@claspo/renderer 18.13.1 → 18.13.2

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.13.1",
3
+ "version": "18.13.2",
4
4
  "sideEffects": false,
5
5
  "scripts": {
6
6
  "dev": " tsc --project tsconfig.json --watch",
@@ -16,7 +16,7 @@
16
16
  "node": ">=18.16.0"
17
17
  },
18
18
  "dependencies": {
19
- "@claspo/common": "7.5.12",
19
+ "@claspo/common": "7.5.13",
20
20
  "@testing-library/dom": "^10.4.0"
21
21
  },
22
22
  "devDependencies": {
@@ -18,6 +18,8 @@ export default class PrizePool {
18
18
  _findPoolOption(rewardOrReservation: any, options: any): any;
19
19
  _mergeRewardIntoOption(reward: any, options: any): any;
20
20
  _fetchPool(url: string, prizePoolId: string): Promise<any>;
21
+ _assertPoolAvailable(pool: any, prizePoolId: string): any;
22
+ _findPoolFromScript(prizePoolId: string): any;
21
23
  _fetchJson(url: any, headers?: any): Promise<any>;
22
24
  getModel(): any;
23
25
  getPrize(): Promise<any>;
@@ -1 +1 @@
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";import ErrorLogger from"../error/ErrorLogger";const FETCHABLE_SOURCE_TYPES=[PrizeSourceType.MANUAL,PrizeSourceType.SHOPIFY],drawnPrizeCodePlaceholder="CLXXXXXXXX";export default class PrizePool{constructor(e,o,i){if(this.httpClient=o,this.configService=i,this.instanceModel=e||{},"FIXED"!==this.instanceModel.model){const e=this.configService.getConfig("prizePoolModels");if(e&&this.instanceModel.id){const o=e.find(e=>e.id===this.instanceModel.id);o&&(this.instanceModel=o)}}this.prizeOption=null,this.randomDistributor=new RandomDistributor,this._loadPromise=null,this._claimPromise=null,this._reserved=!1}async load(){return this._loadPromise||(this._loadPromise=this._load()),this._loadPromise}async claimCode(){return this._claimPromise||(this._claimPromise=this._claim()),this._claimPromise}async _claim(){if(await this.load(),!this._reserved)return;const{id:e}=this.instanceModel,o=this.configService.getConfig("hostUrl"),i=this.configService.getConfig("widgetId"),t=`${o}forms/v1/prizePool/${e}/reward?widgetId=${i}`;try{this.prizeOption=await this._fetchReward(t,this.instanceModel.options,e)}catch(o){throw console.error(`PrizePool: failed to claim a code for widgetId "${i}" with poolId "${e}"`,o),o}}async _load(){const{id:e,model:o,sourceType:i,options:t}=this.instanceModel;if(!(!t&&!!e&&"FIXED"!==o&&(!i||FETCHABLE_SOURCE_TYPES.includes(i))))return;const r=this.configService.getConfig("widgetId");try{const o=this.configService.getConfig("hostUrl"),t=`${o}forms/v2/prize/pool/${e}`,n=`${o}forms/v1/prizePool/${e}/reward?widgetId=${r}`,s=`${o}forms/v1/prizePool/${e}/reserve-reward?widgetId=${r}`;if(this._isPreview()){const o=await this._fetchPool(t,e);if(this.instanceModel=Object.assign(Object.assign({},this.instanceModel),{sourceType:i||o.sourceType,options:o.options}),this._mintsCodePerWinner(o.options))return;return void(this.prizeOption=await this._fetchReward(n,o.options,e))}const[a,l]=await Promise.all([this._fetchPool(t,e),this._fetchJson(s,{"cl-customer-id":this.configService.getConfig("clCustomerId")})]);this.instanceModel=Object.assign(Object.assign({},this.instanceModel),{sourceType:i||a.sourceType,options:a.options}),this.prizeOption=this._resolveReservation(l,a.options,e),this._reserved=!0}catch(o){throw console.error(`PrizePool: failed to load pool or reservation for widgetId "${r}" with poolId "${e}"`,o),o}}_resolveReservation(e,o,i){if(!this._findPoolOption(e,o))throw ErrorLogger.log(`PrizePool: the pool ${i} does not hold the reserved option ${null==e?void 0:e.id}`),new Error("PrizePool: the reserved option is not in the pool");return this._mergeRewardIntoOption(e,o)}async _fetchReward(e,o,i){const t=await this._fetchJson(e,{"cl-customer-id":this.configService.getConfig("clCustomerId")});return this._resolveReward(t,o,i)}_resolveReward(e,o,i){const t=this._mergeRewardIntoOption(e,o);if(null==e?void 0:e.unavailableReason)throw ErrorLogger.log(`PrizePool: the reward from pool ${i} cannot be handed out with a reason "${e.unavailableReason}"`),new Error("PrizePool: the reward cannot be handed out");if(t.valueType===PrizeOptionValueType.PROMOCODE&&!t.code)throw ErrorLogger.log(`PrizePool: the reward from pool ${i} came back without a promo code`),new Error("PrizePool: the reward cannot be handed out");return t}_findPoolOption(e,o){return(o||[]).find(o=>o.id===(null==e?void 0:e.id))}_mergeRewardIntoOption(e,o){return Object.assign(Object.assign({},this._findPoolOption(e,o)),e)}async _fetchPool(e,o){const i=await this._fetchJson(e);if(!1===(null==i?void 0:i.available))throw ErrorLogger.log(`PrizePool: the pool ${o} is not available and cannot be handed out with a reason "${i.unavailableReason}"`),new Error("PrizePool: the pool is not available and cannot be handed out");return i}async _fetchJson(e,o){const i=await this.httpClient.execute({url:e,headers:o});if(!i.ok)throw new Error(`PrizePool: request failed with status ${i.status} - ${e}`);return i.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){var o,i;const t=e.valueType===PrizeOptionValueType.PROMOCODE?e.value||"":this._mapLabelSuffix(e.value,e.valueType);return{id:e.id,label:t,value:e.code,navigateToViewId:null!==(i=null===(o=e.config)||void 0===o?void 0:o.navigateToViewId)&&void 0!==i?i:null}}_calculatePrize(){const e=sum(this.instanceModel.options.map(e=>parseFloat(e.weight))),o=this.randomDistributor.calculateFromWeights(this.instanceModel.options,e);return o&&this._isCodeMintedPerWinner(o)?Object.assign(Object.assign({},o),{code:"CLXXXXXXXX"}):o}_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 o;return(null===(o=null==e?void 0:e.config)||void 0===o?void 0:o.codeType)===PrizeCodeType.UNIQUE}_mapLabelSuffix(e,o){return o===PrizeOptionValueType.DISCOUNT_PERCENTAGE?`${e}%`:e}}
1
+ import sum from"@claspo/common/math/sum";import RandomDistributor from"@claspo/common/math/RandomDistributor";import{PrizeCodeType,PrizeOptionValueType}from"@claspo/common/PrizePool.interface";import{PreviewMode}from"../sdk/PreviewMode";import ErrorLogger from"../error/ErrorLogger";import{FETCHABLE_SOURCE_TYPES,isFetchedPrizePoolReference}from"./isFetchedPrizePoolReference";const drawnPrizeCodePlaceholder="CLXXXXXXXX";export default class PrizePool{constructor(e,o,i){if(this.httpClient=o,this.configService=i,this.instanceModel=e||{},"FIXED"!==this.instanceModel.model){const e=this.configService.getConfig("prizePoolModels");if(e&&this.instanceModel.id){const o=e.find(e=>e.id===this.instanceModel.id);o&&(this.instanceModel=o)}}this.prizeOption=null,this.randomDistributor=new RandomDistributor,this._loadPromise=null,this._claimPromise=null,this._reserved=!1}async load(){return this._loadPromise||(this._loadPromise=this._load()),this._loadPromise}async claimCode(){return this._claimPromise||(this._claimPromise=this._claim()),this._claimPromise}async _claim(){if(await this.load(),!this._reserved)return;const{id:e}=this.instanceModel,o=this.configService.getConfig("hostUrl"),i=this.configService.getConfig("widgetId"),t=`${o}forms/v1/prizePool/${e}/reward?widgetId=${i}`;try{this.prizeOption=await this._fetchReward(t,this.instanceModel.options,e)}catch(o){throw console.error(`PrizePool: failed to claim a code for widgetId "${i}" with poolId "${e}"`,o),o}}async _load(){const{id:e,sourceType:o}=this.instanceModel;if(!isFetchedPrizePoolReference(this.instanceModel))return;const i=this.configService.getConfig("widgetId");try{const t=this.configService.getConfig("hostUrl"),r=`${t}forms/v2/prize/pool/${e}`,n=`${t}forms/v1/prizePool/${e}/reward?widgetId=${i}`,s=`${t}forms/v1/prizePool/${e}/reserve-reward?widgetId=${i}`;if(this._isPreview()){const i=await this._fetchPool(r,e);if(this.instanceModel=Object.assign(Object.assign({},this.instanceModel),{sourceType:o||i.sourceType,options:i.options}),this._mintsCodePerWinner(i.options))return;return void(this.prizeOption=await this._fetchReward(n,i.options,e))}const a=this._findPoolFromScript(e);a&&this._assertPoolAvailable(a,e);const[l,d]=await Promise.all([a||this._fetchPool(r,e),this._fetchJson(s,{"cl-customer-id":this.configService.getConfig("clCustomerId")})]);this.instanceModel=Object.assign(Object.assign({},this.instanceModel),{sourceType:o||l.sourceType,options:l.options}),this.prizeOption=this._resolveReservation(d,l.options,e),this._reserved=!0}catch(o){throw console.error(`PrizePool: failed to load pool or reservation for widgetId "${i}" with poolId "${e}"`,o),o}}_resolveReservation(e,o,i){if(!this._findPoolOption(e,o))throw ErrorLogger.log(`PrizePool: the pool ${i} does not hold the reserved option ${null==e?void 0:e.id}`),new Error("PrizePool: the reserved option is not in the pool");return this._mergeRewardIntoOption(e,o)}async _fetchReward(e,o,i){const t=await this._fetchJson(e,{"cl-customer-id":this.configService.getConfig("clCustomerId")});return this._resolveReward(t,o,i)}_resolveReward(e,o,i){const t=this._mergeRewardIntoOption(e,o);if(null==e?void 0:e.unavailableReason)throw ErrorLogger.log(`PrizePool: the reward from pool ${i} cannot be handed out with a reason "${e.unavailableReason}"`),new Error("PrizePool: the reward cannot be handed out");if(t.valueType===PrizeOptionValueType.PROMOCODE&&!t.code)throw ErrorLogger.log(`PrizePool: the reward from pool ${i} came back without a promo code`),new Error("PrizePool: the reward cannot be handed out");return t}_findPoolOption(e,o){return(o||[]).find(o=>o.id===(null==e?void 0:e.id))}_mergeRewardIntoOption(e,o){return Object.assign(Object.assign({},this._findPoolOption(e,o)),e)}async _fetchPool(e,o){const i=await this._fetchJson(e);return this._assertPoolAvailable(i,o)}_assertPoolAvailable(e,o){if(!1===(null==e?void 0:e.available))throw ErrorLogger.log(`PrizePool: the pool ${o} is not available and cannot be handed out with a reason "${e.unavailableReason}"`),new Error("PrizePool: the pool is not available and cannot be handed out");return e}_findPoolFromScript(e){const o=this.configService.getConfig("prizePoolModelsFromScript");return null==o?void 0:o.find(o=>o.id===e)}async _fetchJson(e,o){const i=await this.httpClient.execute({url:e,headers:o});if(!i.ok)throw new Error(`PrizePool: request failed with status ${i.status} - ${e}`);return i.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){var o,i;const t=e.valueType===PrizeOptionValueType.PROMOCODE?e.value||"":this._mapLabelSuffix(e.value,e.valueType);return{id:e.id,label:t,value:e.code,navigateToViewId:null!==(i=null===(o=e.config)||void 0===o?void 0:o.navigateToViewId)&&void 0!==i?i:null}}_calculatePrize(){const e=sum(this.instanceModel.options.map(e=>parseFloat(e.weight))),o=this.randomDistributor.calculateFromWeights(this.instanceModel.options,e);return o&&this._isCodeMintedPerWinner(o)?Object.assign(Object.assign({},o),{code:"CLXXXXXXXX"}):o}_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 o;return(null===(o=null==e?void 0:e.config)||void 0===o?void 0:o.codeType)===PrizeCodeType.UNIQUE}_mapLabelSuffix(e,o){return o===PrizeOptionValueType.DISCOUNT_PERCENTAGE?`${e}%`:e}}
@@ -0,0 +1,2 @@
1
+ import { ClDocumentI } from "@claspo/common/document/Document.interface";
2
+ export default function getDocumentPrizePoolIds(documentModel: ClDocumentI | null | undefined): string[];
@@ -0,0 +1 @@
1
+ import DocumentUtils from"@claspo/common/document/DocumentUtils";import{ClComponentType}from"@claspo/common/document/Document.interface";import{isFetchedPrizePoolReference}from"./isFetchedPrizePoolReference";const PRIZE_CONSUMER_TYPES=[ClComponentType.PRIZE_BASED_GAMING,ClComponentType.PROMO_CODE];export default function getDocumentPrizePoolIds(e){const o=new Set;return((null==e?void 0:e.views)||[]).forEach(e=>{DocumentUtils.iterateDepthFirst(e,e=>{var t,n;if(!PRIZE_CONSUMER_TYPES.includes(e.type))return;const i=null===(n=null===(t=e.props)||void 0===t?void 0:t.content)||void 0===n?void 0:n.prize;isFetchedPrizePoolReference(i)&&o.add(i.id)})}),[...o]}
@@ -0,0 +1,9 @@
1
+ export declare const FETCHABLE_SOURCE_TYPES: string[];
2
+ interface PrizePoolReferenceI {
3
+ id?: string;
4
+ model?: string;
5
+ sourceType?: string;
6
+ options?: unknown[];
7
+ }
8
+ export declare function isFetchedPrizePoolReference(reference: PrizePoolReferenceI | null | undefined): boolean;
9
+ export {};
@@ -0,0 +1 @@
1
+ import{PrizeSourceType}from"@claspo/common/PrizePool.interface";export const FETCHABLE_SOURCE_TYPES=[PrizeSourceType.MANUAL,PrizeSourceType.SHOPIFY];export function isFetchedPrizePoolReference(e){if(!e)return!1;const{id:o,model:r,sourceType:c,options:i}=e;return!i&&!!o&&"FIXED"!==r&&(!c||FETCHABLE_SOURCE_TYPES.includes(c))}