import { Base } from './base'; /** Class representing B2c Partnership Service. */ export class B2cPartnershipService extends Base { private membershipApi = this.config.get('membershipApi'); private membershipPartnershipsApiKey = this.config.get('membershipPartnershipsApiKey'); /** * hasFreeTokens * This function checks if partner has free tokens available. * @param partnerId - Partner ID * @returns true|false */ public async hasFreeTokens(partnerId: string): Promise { const url = `${this.membershipApi}/partnerships/partner/${partnerId}/has-free-tokens`; const opts = { headers: { 'Content-Type': 'application/json', 'X-api-key': this.membershipPartnershipsApiKey, }, method: 'GET', }; const response = await this.fetch(url, opts); return response.ok; } /** * tokenInfo * This function checks if partner has free tokens available. * @param partnerId - Partner ID * @returns Partnership Token Info */ public async tokenInfo(partnerId: string, token: string): Promise { const url = `${this.membershipApi}/partnerships/partner/${partnerId}/token/${token}`; const opts = { headers: { 'Content-Type': 'application/json', 'X-api-key': this.membershipPartnershipsApiKey, }, method: 'GET', }; const response = await this.fetch(url, opts); if (response.ok) { try { const { status, partner, user } = await response.json(); return { status, partner, user }; } catch (e) { // Not throwing err here as in this case will try create new token if possible for user. } } return {}; } /** * bookPartnershipToken * This function books a PartnershipToken for 30 mins. * @param partnerId - Partner ID * @throws {NotFoundError} * @returns Partnership Token */ public async bookPartnershipToken(partnerId: string): Promise { const url = `${this.membershipApi}/partnerships/partner/${partnerId}/book-token`; // We are using fetch directly here, since this API doesn't return a response body which the requestPost method expects. const opts = { headers: { 'Content-Type': 'application/json', 'X-api-key': this.membershipPartnershipsApiKey, }, method: 'POST', body: JSON.stringify({}) }; const response = await this.fetch(url, opts); if (!response.ok) { throw new Error('noTokenForPartner'); } const result = await response.json(); return result.token; } /** * assignPartnershipToken * This function assigns the given PartnershipToken to the given user. * @param partnerId - Partner ID * @param token - The token to redeem * @param userId User's ID * @returns Whether the assignment was successful */ public async assignPartnershipToken(partnerId: string, token: string, userId: string): Promise { const url = `${this.membershipApi}/partnerships/partner/${partnerId}/assign-token`; // We are using fetch directly here, since this API doesn't return a response body which the requestPost method expects. const opts = { headers: { 'Content-Type': 'application/json', 'X-api-key': this.membershipPartnershipsApiKey, }, method: 'POST', body: JSON.stringify({ token, user: userId }) }; const response = await this.fetch(url, opts); return response.ok; } }