|
| 1 | +import fetch from 'node-fetch'; |
| 2 | +import type { PromoKeys } from '../../../constants.promos'; |
| 3 | +import { SubscriptionState } from '../../../constants.subscription'; |
| 4 | +import { wait } from '../../../system/promise'; |
| 5 | +import { pickApplicablePromo } from '../utils/promo.utils'; |
| 6 | + |
| 7 | +export type PromoLocation = 'account' | 'badge' | 'gate' | 'home'; |
| 8 | + |
| 9 | +export interface Promo { |
| 10 | + readonly key: PromoKeys; |
| 11 | + readonly code?: string; |
| 12 | + readonly states?: SubscriptionState[]; |
| 13 | + readonly expiresOn?: number; |
| 14 | + readonly startsOn?: number; |
| 15 | + |
| 16 | + readonly command?: { |
| 17 | + command?: `command:${string}`; |
| 18 | + tooltip: string; |
| 19 | + }; |
| 20 | + readonly locations?: PromoLocation[]; |
| 21 | + readonly quickpick: { detail: string }; |
| 22 | +} |
| 23 | + |
| 24 | +function isValidDate(d: Date) { |
| 25 | + // @ts-expect-error isNaN expects number, but works with Date instance |
| 26 | + return d instanceof Date && !isNaN(d); |
| 27 | +} |
| 28 | + |
| 29 | +type Modify<T, R> = Omit<T, keyof R> & R; |
| 30 | +type SerializedPromo = Modify< |
| 31 | + Promo, |
| 32 | + { |
| 33 | + startsOn?: string; |
| 34 | + expiresOn?: string; |
| 35 | + states?: string[]; |
| 36 | + } |
| 37 | +>; |
| 38 | + |
| 39 | +function deserializePromo(input: object): Promo[] { |
| 40 | + try { |
| 41 | + const object = input as Array<SerializedPromo>; |
| 42 | + const validPromos: Array<Promo> = []; |
| 43 | + if (typeof object !== 'object' || !Array.isArray(object)) { |
| 44 | + throw new Error('deserializePromo: input is not array'); |
| 45 | + } |
| 46 | + const allowedPromoKeys: Record<PromoKeys, boolean> = { gkholiday: true, pro50: true }; |
| 47 | + for (const promoItem of object) { |
| 48 | + let states: SubscriptionState[] | undefined = undefined; |
| 49 | + let locations: PromoLocation[] | undefined = undefined; |
| 50 | + if (!promoItem.key || !allowedPromoKeys[promoItem.key]) { |
| 51 | + console.warn('deserializePromo: promo item with no id detected and skipped'); |
| 52 | + continue; |
| 53 | + } |
| 54 | + if (!promoItem.quickpick?.detail) { |
| 55 | + console.warn( |
| 56 | + `deserializePromo: no detail provided for promo with key ${promoItem.key} detected and skipped`, |
| 57 | + ); |
| 58 | + continue; |
| 59 | + } |
| 60 | + if (promoItem.states && !Array.isArray(promoItem.states)) { |
| 61 | + console.warn( |
| 62 | + `deserializePromo: promo with key ${promoItem.key} is skipped because of incorrect states value`, |
| 63 | + ); |
| 64 | + continue; |
| 65 | + } |
| 66 | + if (promoItem.states) { |
| 67 | + states = []; |
| 68 | + for (const state of promoItem.states) { |
| 69 | + // @ts-expect-error unsafe work with enum object |
| 70 | + if (Object.hasOwn(SubscriptionState, state)) { |
| 71 | + // @ts-expect-error unsafe work with enum object |
| 72 | + states.push(SubscriptionState[state]); |
| 73 | + } else { |
| 74 | + console.warn( |
| 75 | + `deserializePromo: invalid state value "${state}" detected and skipped at promo with key ${promoItem.key}`, |
| 76 | + ); |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + if (promoItem.locations && !Array.isArray(promoItem.locations)) { |
| 81 | + console.warn( |
| 82 | + `deserializePromo: promo with key ${promoItem.key} is skipped because of incorrect locations value`, |
| 83 | + ); |
| 84 | + continue; |
| 85 | + } |
| 86 | + if (promoItem.locations) { |
| 87 | + locations = []; |
| 88 | + const allowedLocations: Record<PromoLocation, true> = { |
| 89 | + account: true, |
| 90 | + badge: true, |
| 91 | + gate: true, |
| 92 | + home: true, |
| 93 | + }; |
| 94 | + for (const location of promoItem.locations) { |
| 95 | + if (allowedLocations[location]) { |
| 96 | + locations.push(location); |
| 97 | + } else { |
| 98 | + console.warn( |
| 99 | + `deserializePromo: invalid location value "${location}" detected and skipped at promo with key ${promoItem.key}`, |
| 100 | + ); |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + if (promoItem.code && typeof promoItem.code !== 'string') { |
| 105 | + console.warn( |
| 106 | + `deserializePromo: promo with key ${promoItem.key} is skipped because of incorrect code value`, |
| 107 | + ); |
| 108 | + continue; |
| 109 | + } |
| 110 | + if ( |
| 111 | + promoItem.command && |
| 112 | + (typeof promoItem.command.tooltip !== 'string' || |
| 113 | + (promoItem.command.command && typeof promoItem.command.command !== 'string')) |
| 114 | + ) { |
| 115 | + console.warn( |
| 116 | + `deserializePromo: promo with key ${promoItem.key} is skipped because of incorrect code value`, |
| 117 | + ); |
| 118 | + continue; |
| 119 | + } |
| 120 | + if ( |
| 121 | + promoItem.expiresOn && |
| 122 | + (typeof promoItem.expiresOn !== 'string' || !isValidDate(new Date(promoItem.expiresOn))) |
| 123 | + ) { |
| 124 | + console.warn( |
| 125 | + `deserializePromo: promo with key ${promoItem.key} is skipped because of incorrect expiresOn value: ISO date string is expected`, |
| 126 | + ); |
| 127 | + continue; |
| 128 | + } |
| 129 | + if ( |
| 130 | + promoItem.startsOn && |
| 131 | + (typeof promoItem.startsOn !== 'string' || !isValidDate(new Date(promoItem.startsOn))) |
| 132 | + ) { |
| 133 | + console.warn( |
| 134 | + `deserializePromo: promo with key ${promoItem.key} is skipped because of incorrect startsOn value: ISO date string is expected`, |
| 135 | + ); |
| 136 | + continue; |
| 137 | + } |
| 138 | + validPromos.push({ |
| 139 | + ...promoItem, |
| 140 | + expiresOn: promoItem.expiresOn ? new Date(promoItem.expiresOn).getTime() : undefined, |
| 141 | + startsOn: promoItem.startsOn ? new Date(promoItem.startsOn).getTime() : undefined, |
| 142 | + states: states, |
| 143 | + locations: locations, |
| 144 | + }); |
| 145 | + } |
| 146 | + return validPromos; |
| 147 | + } catch (e) { |
| 148 | + throw new Error(`deserializePromo: Could not deserialize promo: ${e.message ?? e}`); |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +export class PromoProvider { |
| 153 | + private _isInitialized: boolean = false; |
| 154 | + private _initPromise: Promise<void> | undefined; |
| 155 | + private _promo: Array<Promo> | undefined; |
| 156 | + constructor() { |
| 157 | + void this.waitForFirstRefreshInitialized(); |
| 158 | + } |
| 159 | + |
| 160 | + private async waitForFirstRefreshInitialized() { |
| 161 | + if (this._isInitialized) { |
| 162 | + return; |
| 163 | + } |
| 164 | + if (!this._initPromise) { |
| 165 | + this._initPromise = this.initialize().then(() => { |
| 166 | + this._isInitialized = true; |
| 167 | + }); |
| 168 | + } |
| 169 | + await this._initPromise; |
| 170 | + } |
| 171 | + |
| 172 | + async initialize(): Promise<void> { |
| 173 | + await wait(1000); |
| 174 | + if (this._isInitialized) { |
| 175 | + return; |
| 176 | + } |
| 177 | + try { |
| 178 | + console.log('PromoProvider GL_PROMO_URI', GL_PROMO_URI); |
| 179 | + if (!GL_PROMO_URI) { |
| 180 | + throw new Error('No GL_PROMO_URI env variable provided'); |
| 181 | + } |
| 182 | + const jsonBody = JSON.parse(await fetch(GL_PROMO_URI).then(x => x.text())); |
| 183 | + this._promo = deserializePromo(jsonBody); |
| 184 | + } catch (e) { |
| 185 | + console.error('PromoProvider error', e); |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + async getPromoList(): Promise<Promo[] | undefined> { |
| 190 | + try { |
| 191 | + await this.waitForFirstRefreshInitialized(); |
| 192 | + return this._promo!; |
| 193 | + } catch { |
| 194 | + return undefined; |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + async getApplicablePromo( |
| 199 | + state: number | undefined, |
| 200 | + location?: PromoLocation, |
| 201 | + key?: PromoKeys, |
| 202 | + ): Promise<Promo | undefined> { |
| 203 | + try { |
| 204 | + await this.waitForFirstRefreshInitialized(); |
| 205 | + return pickApplicablePromo(this._promo, state, location, key); |
| 206 | + } catch { |
| 207 | + return undefined; |
| 208 | + } |
| 209 | + } |
| 210 | +} |
| 211 | + |
| 212 | +export const promoProvider = new PromoProvider(); |
| 213 | + |
| 214 | +export const getApplicablePromo = promoProvider.getApplicablePromo.bind(promoProvider); |
0 commit comments