import { RestAPI } from "../lib/RestAPI"; import { ValidateVoucherAPIDto } from "../dto/validate-voucher-api-dto"; import { RedemptionVoucherAPIDto } from "../dto/redemption-voucher.dto"; import { VerifyVoucherDto } from "../dto/redemption-vouchers.dto"; import { ShareCodeDto } from "../dto/voucher-data.dto"; export class VoucherSpConnector { static async validateApi( validateVoucherAPIDto: ValidateVoucherAPIDto, apiEndPoint: string ) { try { const voucher: RestAPI = new RestAPI(); return voucher.executeCall( "POST", `${apiEndPoint}/redemption-receipts/validate`, validateVoucherAPIDto ); } catch (error) { throw new Error(error); } } static async verifyVoucherApi( redemptionVoucherDto: VerifyVoucherDto, apiEndPoint: string ) { try { const voucher: RestAPI = new RestAPI(); let newServices; let newVoucherData; const { serialNumber, voucherCode, serviceProviderId } = redemptionVoucherDto; const response = await voucher.executeCall( "GET", `${apiEndPoint}/voucher-instances/voucher-code?serialNumber=${serialNumber}&voucherCode=${voucherCode}` ); if (response) { newVoucherData = response?.data; newServices = response?.data?.data[0]?.services.filter( (service: any) => service.service.serviceProvider.id === +serviceProviderId ); } newVoucherData.data[0].services = newServices; if (newServices?.length) return newVoucherData; } catch (error) { throw new Error(error); } } static async redemptionApi( redemptionVoucherAPIDto: RedemptionVoucherAPIDto, apiEndPoint: string ) { try { const voucher: RestAPI = new RestAPI(); return voucher.executeCall( "POST", `${apiEndPoint}/redemption-receipts`, redemptionVoucherAPIDto ); } catch (error) { throw new Error(error); } } static async shareVoucherCodeApi(data: ShareCodeDto, apiEndPoint: string) { try { const voucher: RestAPI = new RestAPI(); return voucher.executeCall( "POST", `${apiEndPoint}/voucher-instances/share-code`, data ); } catch (error) { throw new Error(error); } } static async getSlugName(serviceProviderKey:string,apiEndPoint: string,serviceProviderEmail:string) { try { const voucher: RestAPI = new RestAPI(); const data={ serviceProviderEmail, serviceProviderKey } const slugNames= await voucher.executeCall( "POST", `${apiEndPoint}/service-providers/slug-name`, data ); if(!slugNames || !slugNames.data){ throw new Error('No slugname found '); } return slugNames?.data } catch (error) { throw new Error(error); } } static async getServiceProviderKey(serviceProviderEmail:string,apiEndPoint: string) { try { const voucher: RestAPI = new RestAPI(); const data={ serviceProviderEmail } const serviceProvider= await voucher.executeCall( "POST", `${apiEndPoint}/service-providers/service-provider-key`, data ); if(!serviceProvider){ throw new Error('No slugname found '); } return serviceProvider?.data } catch (error) { throw new Error(error); } } }