import { Configuration } from "../interfaces/configuration.interface"; import { VoucherSpConnector } from "../services/VoucherSpConnector.service"; import { ValidateVoucherDto } from "../dto/validate-voucher.dto"; import { RedemptionVoucherAPIDto } from "../dto/redemption-voucher.dto"; import { Service } from "./Service"; import { VerifyVoucherDto } from "../dto/redemption-vouchers.dto"; import { ShareCodeDto } from "../dto/voucher-data.dto"; export class Voucher { private _configuration: Configuration; private _voucherData: any; private _voucherCode: string; private _allowedRedemptionCount = 0; private _services: Service[] = []; private static _apiEndPoint = ""; constructor( config: Configuration, code: string = "", apiEndPoint: string = "" ) { this._configuration = config; this._voucherCode = code; console.log(this._configuration, "-------Constructor--------"); if (!Voucher._apiEndPoint) { Voucher._apiEndPoint = apiEndPoint; } } setConfig(config: Configuration) { this._configuration = config; } getConfig() { return this._configuration; } async validate(validateVoucherDto: ValidateVoucherDto) { try { if ( this._configuration.services.includes(validateVoucherDto.serviceSlug) && (this._allowedRedemptionCount === 0 || this._allowedRedemptionCount > this._services.length) ) { const voucherData: any = await VoucherSpConnector.validateApi( { ...validateVoucherDto, configuration: this._configuration }, Voucher._apiEndPoint ); if (!voucherData) { return false; } else { const voucherSetData = voucherData && voucherData.data && voucherData.data.data ? voucherData.data.data : undefined; this.setVoucherData(voucherSetData); this._allowedRedemptionCount = voucherSetData.allowedRedemptionCount; const service = new Service( this._voucherData, validateVoucherDto.serviceSlug, this ); this._services.push(service); if (this._voucherData) { return true; } return false; } } else { throw new Error("service not available to validate voucher"); } } catch (error) { throw new Error(error); } } async verifyVoucher(redemptionVoucherDto: VerifyVoucherDto) { try { const voucherData: any = await VoucherSpConnector.verifyVoucherApi( redemptionVoucherDto, Voucher._apiEndPoint ); if (!voucherData) { throw new Error("Voucher code not found 2"); } else { return voucherData; } } catch (error) { throw new Error(error); } } async shareVoucher(shareVoucherDto: ShareCodeDto) { const { email, voucherData } = shareVoucherDto; const { voucherInstanceId, voucherCode, serialNumber } = voucherData[0]; try { const voucherData: any = await VoucherSpConnector.shareVoucherCodeApi( shareVoucherDto, Voucher._apiEndPoint ); if (!email) throw new Error("Email is required"); if (!voucherInstanceId || !voucherCode || !serialNumber) throw new Error( "voucherInstanceId ,voucherCode ,serialNumber required" ); return voucherData; } catch (error) { throw new Error(error); } } setVoucherData(voucherData: any) { this._voucherData = voucherData; } getVoucherData() { if (this._voucherData) { return this._voucherData; } else { throw new Error("Please validate voucher code first"); } } getVoucherCode() { return this._voucherCode; } getServiceInstance(serviceSlug: string): Service { const service = this._services.find( (service) => serviceSlug === service._serviceName ); if (service) { return service; } else { throw new Error("Service not found"); } } removeServiceInstance(serviceSlug: string) { const service = this._services.find( (service) => serviceSlug === service._serviceName ); if (service) { const indexToRemove = this._services.indexOf(service); this._services.splice(indexToRemove, 1); return this._services; } else { throw new Error("Service not found"); } } async redemption(redemptionVoucherAPIDto: RedemptionVoucherAPIDto) { try { const response = await VoucherSpConnector.redemptionApi( { ...redemptionVoucherAPIDto, configuration: redemptionVoucherAPIDto.configuration, }, Voucher._apiEndPoint ); if (response) return response; throw new Error("service not available to redeem voucher"); } catch (error) { throw new Error(error); } } }