import { Voucher } from '../lib/Voucher'; import { Configuration } from '../interfaces/configuration.interface'; import { VoucherSpConnector } from './VoucherSpConnector.service'; export class VoucherSpService { private static _instance: VoucherSpService = new VoucherSpService(); private static _apiEndPoint = ''; private _vouchers: Voucher[] = []; constructor() { if (VoucherSpService._instance) { throw new Error("Error: Instantiation failed: Use VoucherSpService.getInstance() instead of new."); } VoucherSpService._instance = this; } public static getInstance(apiEndPoint: string): VoucherSpService { VoucherSpService._apiEndPoint = apiEndPoint; return VoucherSpService._instance; } public setVoucherCode(couponCode: string, configuration: Configuration) { const voucher = this._vouchers.find( voucher => couponCode === voucher.getVoucherCode() ); if (!voucher) { const voucherNew = new Voucher(configuration, couponCode, VoucherSpService._apiEndPoint); this._vouchers.push(voucherNew); return voucherNew; } return voucher; } public getVoucherInstance(couponCode: string) { const voucher = this._vouchers.find( voucher => couponCode === voucher.getVoucherCode() ); if (voucher) { return voucher } else { throw new Error('Voucher not found'); } } public async getSlugName(serviceProviderkey: string,email:string) { return await VoucherSpConnector.getSlugName(serviceProviderkey,VoucherSpService._apiEndPoint,email) } public async getServiceProviderKey(email: string) { return await VoucherSpConnector.getServiceProviderKey(email,VoucherSpService._apiEndPoint) } }