licenses-api.js
274 lines
| 1 | import { dispatch, select } from '@wordpress/data'; |
| 2 | |
| 3 | import { STORE_NAME } from '@admin/store'; |
| 4 | import { licensesApiRequest } from '../utils'; |
| 5 | |
| 6 | const API_PATH = '/advanced-ads/v1/licenses'; |
| 7 | const AUTOUPDATE_API_PATH = '/advanced-ads/v1/plugin-autoupdate'; |
| 8 | |
| 9 | export function extractApiMessage( payload ) { |
| 10 | if ( |
| 11 | ! payload || |
| 12 | typeof payload !== 'object' || |
| 13 | Array.isArray( payload ) |
| 14 | ) { |
| 15 | return null; |
| 16 | } |
| 17 | |
| 18 | const message = payload.message ?? payload.data?.message; |
| 19 | |
| 20 | if ( message === undefined || message === null ) { |
| 21 | return null; |
| 22 | } |
| 23 | |
| 24 | const text = String( message ).trim(); |
| 25 | if ( text === '' ) { |
| 26 | return null; |
| 27 | } |
| 28 | |
| 29 | return text; |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Pull a user-facing message from a failed apiFetch / REST error. |
| 34 | * |
| 35 | * @param {unknown} err Error from apiFetch. |
| 36 | * @return {string|null} Message text or null when none is available. |
| 37 | */ |
| 38 | export function extractApiErrorMessage( err ) { |
| 39 | if ( ! err || typeof err !== 'object' ) { |
| 40 | return null; |
| 41 | } |
| 42 | |
| 43 | const dataMessage = err.data?.message; |
| 44 | if ( dataMessage !== undefined && dataMessage !== null ) { |
| 45 | const text = String( dataMessage ).trim(); |
| 46 | if ( text !== '' ) { |
| 47 | return text; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | if ( err instanceof Error ) { |
| 52 | const text = String( err.message ?? '' ).trim(); |
| 53 | if ( text !== '' ) { |
| 54 | return text; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | const message = err.message; |
| 59 | if ( message !== undefined && message !== null ) { |
| 60 | const text = String( message ).trim(); |
| 61 | if ( text !== '' ) { |
| 62 | return text; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | return null; |
| 67 | } |
| 68 | |
| 69 | function safeObject( val ) { |
| 70 | return val && typeof val === 'object' ? val : {}; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Normalize GET/POST licenses REST payload (array legacy or object with map). |
| 75 | * |
| 76 | * @param {Array|Object} payload REST licenses payload. |
| 77 | * @return {{ licenses: Array, appliedAddonKeyMap: Object, autoUpdateStates: Object, addonInstallStates: Object }} Normalized licenses response object. |
| 78 | */ |
| 79 | export function normalizeLicensesResponse( payload ) { |
| 80 | if ( Array.isArray( payload ) ) { |
| 81 | return { |
| 82 | licenses: payload, |
| 83 | appliedAddonKeyMap: {}, |
| 84 | currentActiveLicenses: '', |
| 85 | autoUpdateStates: {}, |
| 86 | addonInstallStates: {}, |
| 87 | lastSyncAt: 0, |
| 88 | expiryNoticeFlags: {}, |
| 89 | }; |
| 90 | } |
| 91 | |
| 92 | return { |
| 93 | licenses: Array.isArray( payload?.licenses ) ? payload.licenses : [], |
| 94 | appliedAddonKeyMap: safeObject( payload?.appliedAddonKeyMap ), |
| 95 | currentActiveLicenses: String( payload?.currentActiveLicenses ?? '' ), |
| 96 | autoUpdateStates: safeObject( payload?.autoUpdateStates ), |
| 97 | addonInstallStates: safeObject( payload?.addonInstallStates ), |
| 98 | lastSyncAt: payload?.lastSyncAt ?? 0, |
| 99 | expiryNoticeFlags: safeObject( payload?.expiryNoticeFlags ), |
| 100 | }; |
| 101 | } |
| 102 | |
| 103 | function applyLicensesPayloadToStore( payload ) { |
| 104 | const { |
| 105 | licenses, |
| 106 | appliedAddonKeyMap, |
| 107 | currentActiveLicenses, |
| 108 | autoUpdateStates, |
| 109 | addonInstallStates, |
| 110 | lastSyncAt, |
| 111 | expiryNoticeFlags, |
| 112 | } = normalizeLicensesResponse( payload ); |
| 113 | dispatch( STORE_NAME ).setLicenses( |
| 114 | licenses, |
| 115 | appliedAddonKeyMap, |
| 116 | autoUpdateStates, |
| 117 | addonInstallStates, |
| 118 | lastSyncAt, |
| 119 | expiryNoticeFlags, |
| 120 | currentActiveLicenses |
| 121 | ); |
| 122 | return licenses; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Toggle per-plugin auto-update (advanced-ads-{addon}-autoupdate). |
| 127 | * |
| 128 | * @param {string} addonId Short id (pro, tracking, …) or "main" for base plugin. |
| 129 | * @param {string} state "on" or "off". |
| 130 | */ |
| 131 | export async function togglePluginAutoUpdate( addonId, state ) { |
| 132 | const response = await licensesApiRequest( AUTOUPDATE_API_PATH, { |
| 133 | method: 'POST', |
| 134 | data: { |
| 135 | addonId: addonId || 'main', |
| 136 | state, |
| 137 | }, |
| 138 | } ); |
| 139 | |
| 140 | if ( |
| 141 | response?.autoUpdateStates && |
| 142 | typeof response.autoUpdateStates === 'object' |
| 143 | ) { |
| 144 | dispatch( STORE_NAME ).setAutoUpdateStates( response.autoUpdateStates ); |
| 145 | } |
| 146 | |
| 147 | return response; |
| 148 | } |
| 149 | |
| 150 | // ----------- Internal Api call --------------- // |
| 151 | |
| 152 | export function fetchLicenses() { |
| 153 | return licensesApiRequest( API_PATH, { method: 'GET' } ); |
| 154 | } |
| 155 | |
| 156 | export function saveLicenses( |
| 157 | licenses, |
| 158 | { |
| 159 | activate = false, |
| 160 | activatingLicenseKey = '', |
| 161 | activatingAddonId = '', |
| 162 | installOnly = false, |
| 163 | deactivatingAddonId = '', |
| 164 | deactivatingLicenseKey = '', |
| 165 | signal = undefined, |
| 166 | } = {} |
| 167 | ) { |
| 168 | return licensesApiRequest( API_PATH, { |
| 169 | method: 'POST', |
| 170 | signal, |
| 171 | data: { |
| 172 | licenses: Array.isArray( licenses ) ? licenses : [], |
| 173 | activate, |
| 174 | activatingLicenseKey: activatingLicenseKey || '', |
| 175 | activatingAddonId: activatingAddonId || '', |
| 176 | installOnly, |
| 177 | deactivatingAddonId: deactivatingAddonId || '', |
| 178 | deactivatingLicenseKey: deactivatingLicenseKey || '', |
| 179 | }, |
| 180 | } ); |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Remove one license from the current site (Tracking, Pro, All Access, …). |
| 185 | * |
| 186 | * @param {string} licenseKey License key for the card being deactivated. |
| 187 | * @param {Array} licenses Current license list for POST body. |
| 188 | */ |
| 189 | export async function deactivateLicenseOnSite( licenseKey, licenses ) { |
| 190 | const saved = await saveLicenses( licenses, { |
| 191 | deactivatingLicenseKey: licenseKey, |
| 192 | } ); |
| 193 | applyLicensesPayloadToStore( saved ); |
| 194 | return saved; |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Apply license and activate one All Access add-on on this site. |
| 199 | * |
| 200 | * @param {string} licenseKey All Access license key. |
| 201 | * @param {string} addonId Short add-on id (pro, tracking, …). |
| 202 | * @param {Array} licenses Current license list for POST body. |
| 203 | */ |
| 204 | export async function activateAddonFromLicense( |
| 205 | licenseKey, |
| 206 | addonId, |
| 207 | licenses |
| 208 | ) { |
| 209 | const saved = await saveLicenses( licenses, { |
| 210 | activatingLicenseKey: licenseKey, |
| 211 | activatingAddonId: addonId, |
| 212 | } ); |
| 213 | applyLicensesPayloadToStore( saved ); |
| 214 | return saved; |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Deactivate one All Access add-on plugin on this site (keeps package on disk). |
| 219 | * |
| 220 | * @param {string} addonId Short add-on id (pro, tracking, …). |
| 221 | * @param {Array} licenses Current license list for POST body. |
| 222 | */ |
| 223 | export async function deactivateAddonFromLicense( addonId, licenses ) { |
| 224 | const saved = await saveLicenses( licenses, { |
| 225 | deactivatingAddonId: addonId, |
| 226 | } ); |
| 227 | applyLicensesPayloadToStore( saved ); |
| 228 | return saved; |
| 229 | } |
| 230 | |
| 231 | // ----------- Store Helpers --------------- // |
| 232 | |
| 233 | export async function initLicenses() { |
| 234 | const payload = await fetchLicenses(); |
| 235 | applyLicensesPayloadToStore( payload ); |
| 236 | } |
| 237 | |
| 238 | export async function setLicensesAndSave( licenses, options = {} ) { |
| 239 | dispatch( STORE_NAME ).setLicenses( licenses ); |
| 240 | const saved = await saveLicenses( licenses, options ); |
| 241 | applyLicensesPayloadToStore( saved ); |
| 242 | } |
| 243 | |
| 244 | export async function addLicenseAndSave( license ) { |
| 245 | dispatch( STORE_NAME ).addLicense( license ); |
| 246 | const current = select( STORE_NAME ).getLicenses(); |
| 247 | const saved = await saveLicenses( current ); |
| 248 | applyLicensesPayloadToStore( saved ); |
| 249 | } |
| 250 | |
| 251 | export async function removeLicenseAndSave( licenseId ) { |
| 252 | dispatch( STORE_NAME ).removeLicense( licenseId ); |
| 253 | const current = select( STORE_NAME ).getLicenses(); |
| 254 | const saved = await saveLicenses( current ); |
| 255 | applyLicensesPayloadToStore( saved ); |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * Activate a license on the shop via plugin REST (shop-first on the server). |
| 260 | * |
| 261 | * @param {string} licenseKey License key. |
| 262 | * @param {AbortSignal} [signal] Optional abort signal. |
| 263 | * @return {Object} Licenses REST payload. |
| 264 | */ |
| 265 | export async function activateLicenseOnShop( licenseKey, signal ) { |
| 266 | const licenses = select( STORE_NAME ).getLicenses(); |
| 267 | const saved = await saveLicenses( licenses, { |
| 268 | activatingLicenseKey: licenseKey, |
| 269 | signal, |
| 270 | } ); |
| 271 | applyLicensesPayloadToStore( saved ); |
| 272 | return saved; |
| 273 | } |
| 274 |