11import { APIEndpoints , APIPaths } from "../../config/APIEndpoints" ;
2- import { getAPI , initializeAPIOptions } from "../../utility/API" ;
2+ import { initializeAPIOptions , postAPI } from "../../utility/API" ;
33import { fetchAuthenticatedUser } from "./UserActions" ;
44import { loaderActions } from "./" ;
55import { LoaderContent } from "../../utility/constants/LoaderContent" ;
66import { isEmpty } from "lodash" ;
77import { resetCurrentModelDetails , resetModelList } from "./ServiceTrainingActions" ;
8+ import { createFreecallStrategy } from "../../utility/sdk" ;
9+ import { initializingSdk } from "./SDKActions" ;
810
911export const UPDATE_SERVICE_DETAILS = "UPDATE_SERVICE_DETAILS" ;
1012export const RESET_SERVICE_DETAILS = "RESET_SERVICE_DETAILS" ;
1113export const UPDATE_FREE_CALLS_INFO = "UPDATE_FREE_CALLS_INFO" ;
1214export const UPDATE_TRAINING_DETAILS = "UPDATE_TRAINING_DETAILS" ;
15+ export const UPDATE_FREECALL_SIGNATURE = "UPDATE_FREECALL_SIGNATURE" ;
1316
1417const resetServiceDetails = ( dispatch ) => {
1518 dispatch ( { type : RESET_SERVICE_DETAILS } ) ;
1619} ;
1720
21+ const setFreeCallSignature = ( freeCallSignature ) => ( dispatch ) => {
22+ dispatch ( { type : UPDATE_FREECALL_SIGNATURE , payload : freeCallSignature } ) ;
23+ } ;
24+
1825const fetchServiceDetailsFailure = ( err ) => ( dispatch ) => {
1926 dispatch ( loaderActions . stopAppLoader ( ) ) ;
2027} ;
2128
2229const fetchServiceDetailsSuccess = ( serviceDetails ) => ( dispatch ) => {
23- // const enhancedServiceDetails = {
24- // ...serviceDetails,
25- // data: { ...serviceDetails.data, media: serviceDetails.data.media.map(el => ({ ...el, url: cacheS3Url(el.url) })) },
26- // };
2730 dispatch ( loaderActions . stopAppLoader ( ) ) ;
28- dispatch ( { type : UPDATE_SERVICE_DETAILS , payload : serviceDetails . data } ) ;
31+ dispatch ( { type : UPDATE_SERVICE_DETAILS , payload : serviceDetails } ) ;
2932} ;
3033
3134const fetchServiceDetailsAPI = async ( orgId , serviceId ) => {
32- const url = ` ${ APIEndpoints . CONTRACT . endpoint } /org/ ${ orgId } /service/ ${ serviceId } ` ;
35+ const url = APIEndpoints . CONTRACT . endpoint + APIPaths . SERVICE_DETAILS ( orgId , serviceId ) ;
3336 const response = await fetch ( url ) ;
3437 return response . json ( ) ;
3538} ;
@@ -40,18 +43,21 @@ export const fetchServiceDetails = (orgId, serviceId) => async (dispatch) => {
4043 dispatch ( resetServiceDetails ) ;
4144 dispatch ( resetCurrentModelDetails ( ) ) ;
4245 dispatch ( resetModelList ( ) ) ;
43- const serviceDetails = await fetchServiceDetailsAPI ( orgId , serviceId ) ;
46+ const { data : serviceDetails } = await fetchServiceDetailsAPI ( orgId , serviceId ) ;
4447 dispatch ( fetchServiceDetailsSuccess ( serviceDetails ) ) ;
4548 } catch ( error ) {
4649 dispatch ( fetchServiceDetailsFailure ( error ) ) ;
4750 throw error ;
4851 }
4952} ;
5053
51- const fetchMeteringDataSuccess = ( usageData ) => ( dispatch ) => {
54+ const fetchMeteringDataSuccess = ( freeCallsAvailable , freeCallsTotal ) => ( dispatch ) => {
5255 dispatch ( {
5356 type : UPDATE_FREE_CALLS_INFO ,
54- payload : usageData . total_calls_made ,
57+ payload : {
58+ freeCallsTotal,
59+ freeCallsAvailable,
60+ } ,
5561 } ) ;
5662} ;
5763
@@ -75,21 +81,73 @@ export const fetchTrainingModel = (orgId, serviceId) => async (dispatch) => {
7581 dispatch ( fetchTrainingModelSuccess ( serviceTrainingData ) ) ;
7682} ;
7783
78- const meteringAPI = ( token , orgId , serviceId , groupId , userId ) => {
79- const apiName = APIEndpoints . USER . name ;
80- const apiPath = APIPaths . FREE_CALL_USAGE ;
81- const queryParams = { organization_id : orgId , service_id : serviceId , group_id : groupId , username : userId } ;
82- const apiOptions = initializeAPIOptions ( token , null , queryParams ) ;
83- return getAPI ( apiName , apiPath , apiOptions ) ;
84+ const getAvailableFreeCalls = ( orgId , serviceId , groupId ) => async ( dispatch ) => {
85+ const {
86+ signature,
87+ currentBlockNumber,
88+ userId,
89+ freeCallToken,
90+ signerAddress : address ,
91+ } = await dispatch ( getFreeCallSign ( orgId , serviceId , groupId ) ) ;
92+
93+ try {
94+ const freecallStrategy = await createFreecallStrategy ( orgId , serviceId ) ;
95+ const availableFreeCalls = await freecallStrategy . getFreeCallsAvailable ( {
96+ signature,
97+ currentBlockNumber,
98+ userId,
99+ token : freeCallToken ,
100+ address,
101+ } ) ;
102+ return availableFreeCalls ;
103+ } catch ( err ) {
104+ console . error ( "error on getting available free calls:" , err ) ;
105+
106+ throw new Error ( err ) ;
107+ }
108+ } ;
109+
110+ export const getFreeCallSign = ( orgId , serviceId , groupId ) => async ( dispatch , getState ) => {
111+ try {
112+ const sdk = await dispatch ( initializingSdk ( ) ) ;
113+ const currentBlock = await sdk . account . getCurrentBlockNumber ( ) ;
114+ const { email, token } = await dispatch ( fetchAuthenticatedUser ( ) ) ;
115+ const freeCallSignatureDetails = getState ( ) . serviceDetailsReducer . freeCallSignature ;
116+
117+ if ( freeCallSignatureDetails . expirationBlock && freeCallSignatureDetails . expirationBlock < currentBlock ) {
118+ return { ...freeCallSignatureDetails , userId : email } ;
119+ }
120+ const payload = {
121+ organization_id : orgId ,
122+ service_id : serviceId ,
123+ group_id : groupId ,
124+ } ;
125+ const apiName = APIEndpoints . SIGNER_SERVICE . name ;
126+ const apiOptions = initializeAPIOptions ( token , payload ) ;
127+ const { data : freeCallSignerResp } = await postAPI ( apiName , APIPaths . SIGNER_FREE_CALL , apiOptions ) ;
128+
129+ const freeCallSignature = {
130+ signature : freeCallSignerResp . signature_hex ,
131+ expirationBlock : freeCallSignerResp . expiration_block_number ,
132+ currentBlockNumber : freeCallSignerResp . current_block_number ,
133+ freeCallToken : freeCallSignerResp . free_call_token_hex ,
134+ signerAddress : freeCallSignerResp . signer_address ,
135+ userId : email ,
136+ } ;
137+ dispatch ( setFreeCallSignature ( freeCallSignature ) ) ;
138+ return freeCallSignature ;
139+ } catch ( err ) {
140+ console . error ( err ) ;
141+ throw err ;
142+ }
84143} ;
85144
86145export const fetchMeteringData =
87- ( { orgId, serviceId, groupId } ) =>
146+ ( { orgId, serviceId, groupId, freeCallsTotal } ) =>
88147 async ( dispatch ) => {
89- const { email, token } = await dispatch ( fetchAuthenticatedUser ( ) ) ;
90- const usageData = await meteringAPI ( token , orgId , serviceId , groupId , email ) ;
91- dispatch ( fetchMeteringDataSuccess ( usageData ) ) ;
92- return usageData ;
148+ const freeCallsAvailable = await dispatch ( getAvailableFreeCalls ( orgId , serviceId , groupId ) ) ;
149+ dispatch ( fetchMeteringDataSuccess ( freeCallsAvailable , freeCallsTotal ) ) ;
150+ return { freeCallsAvailable, freeCallsTotal } ;
93151 } ;
94152
95153export const getIsTrainingAvailable = ( detailsTraining , isLoggedIn ) => {
0 commit comments