UNPKG

@paritydeals/node-sdk

Version:

Node.js SDK for interacting with the ParityDeals API.

101 lines 5.35 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.EntitlementsOperations = void 0; const constants_1 = require("./constants"); // import { InvalidRequestError } from './exceptions'; // Only if doing runtime Zod validation /** * Handles 'entitlements' related API operations. * This class is instantiated by the ParityDeals client and accessed * via `client.entitlements`. All methods are asynchronous. * These operations target the Edge API URL. */ class EntitlementsOperations { constructor(client) { this.client = client; this.client.logger.debug("EntitlementsOperations initialized."); } /** * Checks if a customer has access to a specific feature. * @param params - Parameters for checking access. * - customerId: string - Customer's unique identifier. * - featureId: string - Feature's unique identifier. * @returns A Promise resolving to a boolean indicating access status. */ async hasAccess(params) { this.client.logger.info(`Initiating hasAccess check for customerId: ${params.customerId}, featureId: ${params.featureId}`); const endpointModulePath = `${constants_1.ENTITLEMENTS_MODULE_PREFIX}`; const queryParams = { customerId: params.customerId, featureId: params.featureId }; this.client.logger.debug(`hasAccess query params (camelCase, before client processing): ${JSON.stringify(queryParams)}`); const response = await this.client._request("GET", endpointModulePath, undefined, queryParams, true // Indicate to use the edge URL ); for (const entitlement of response.entitlements) { if (entitlement.featureId === params.featureId) { let statusToReturn; if (entitlement.featureType === 'METER') { const isHardLimitActive = !!entitlement.hardLimit; // Corrected logic for noUsageRemaining let noUsageRemaining = false; if (entitlement.remaining === null || typeof entitlement.remaining === 'undefined') { noUsageRemaining = true; // Consider null or undefined as no usage left for hard limit check } else if (entitlement.remaining <= 0) { noUsageRemaining = true; } // Alternatively, more concise: // const noUsageRemaining = entitlement.remaining == null || entitlement.remaining <= 0; // (using == null checks for both null and undefined) if (isHardLimitActive && noUsageRemaining) { statusToReturn = false; } else { statusToReturn = true; } } else { statusToReturn = entitlement.hasAccess; } this.client.logger.info(`Access check for customerId: ${params.customerId}, featureId: ${params.featureId} - Result: ${statusToReturn}`); return statusToReturn; } } this.client.logger.warn(`FeatureId '${params.featureId}' not found for customerId: ${params.customerId}. Returning hasAccess=False.`); return false; } /** * Retrieves detailed entitlement information for a specific feature for a customer. * @param params - Parameters for retrieving a specific entitlement. * - customerId: string - Customer's unique identifier. * - featureId: string - Feature's unique identifier. * @returns A Promise resolving to CheckEntitlementsResponse (typically containing one entitlement). */ async getEntitlement(params) { this.client.logger.info(`Initiating getEntitlement for customerId: ${params.customerId}, featureId: ${params.featureId}`); const endpointModulePath = `${constants_1.ENTITLEMENTS_MODULE_PREFIX}`; const queryParams = { customerId: params.customerId, featureId: params.featureId }; this.client.logger.debug(`getEntitlement query params (camelCase, before client processing): ${JSON.stringify(queryParams)}`); return this.client._request("GET", endpointModulePath, undefined, queryParams, true // Indicate to use the edge URL ); } /** * Retrieves all entitlements for a given customer. * @param params - Parameters for retrieving all entitlements. * - customerId: string - Customer's unique identifier. * @returns A Promise resolving to CheckEntitlementsResponse. */ async getAllEntitlements(params) { this.client.logger.info(`Initiating getAllEntitlements for customerId: ${params.customerId}`); const endpointModulePath = `${constants_1.ENTITLEMENTS_MODULE_PREFIX}`; const queryParams = { customerId: params.customerId }; this.client.logger.debug(`getAllEntitlements query params (camelCase, before client processing): ${JSON.stringify(queryParams)}`); return this.client._request("GET", endpointModulePath, undefined, queryParams, true // Indicate to use the edge URL ); } } exports.EntitlementsOperations = EntitlementsOperations; //# sourceMappingURL=entitlements.js.map