Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: redemption endpoints #173

Merged
merged 1 commit into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 39 additions & 31 deletions src/interfaces/HposInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,14 @@ type HposHolochainCallResponse =
| ServiceLogsResponse
| ZomeCallResponse
| EarningsData
| RedemptionHistory

export interface RedemptionHistory {
accepted: Redemption[]
completed: Redemption[]
declined: Redemption[]
pending: Redemption[]
}

type HposAdminCallResponse = HposConfigResponse

Expand Down Expand Up @@ -380,24 +388,24 @@ export function useHposInterface(): HposInterface {
let response

switch (method) {
case 'get':
response = await axios.get(fullUrl, { params, headers })
return response.data
case 'get':
response = await axios.get(fullUrl, { params, headers })
return response.data

case 'post':
case 'post':
response = await axios.post(fullUrl, params, { responseType, headers })
return response.data
return response.data

case 'put':
response = await axios.put(fullUrl, params, { headers })
return response.data
case 'put':
response = await axios.put(fullUrl, params, { headers })
return response.data

case 'delete':
response = await axios.delete(fullUrl, { params, headers })
return response.data
case 'delete':
response = await axios.delete(fullUrl, { params, headers })
return response.data

default:
throw new Error(`No case in hposCall for ${method} method`)
default:
throw new Error(`No case in hposCall for ${method} method`)
}
}

Expand Down Expand Up @@ -585,32 +593,32 @@ export function useHposInterface(): HposInterface {
const params =
value === EHostingPlan.paid
? // When the hosting plan is set to paid, we need to remove the prices values
// so the default values are used, that is why we are not sending the prices,
// we use use_default_happ_preferences instead of set_happ_preferences
{
// so the default values are used, that is why we are not sending the prices,
// we use use_default_happ_preferences instead of set_happ_preferences
{
appId: localStorage.getItem(kCoreAppVersionLSKey),
roleId: 'core-app',
zomeName: 'hha',
fnName: 'use_default_happ_preferences',
payload: id
}
: // When the hosting plan is set to free, we need to set the prices values to 0,
// that will override the default values
{
appId: localStorage.getItem(kCoreAppVersionLSKey),
roleId: 'core-app',
zomeName: 'hha',
fnName: 'set_happ_preferences',
payload: {
happ_id: id,
max_fuel_before_invoice: '0',
price_compute: '0',
price_storage: '0',
price_bandwidth: '0',
max_time_before_invoice: { secs: 15000, nanos: 0 },
invoice_due_in_days: 7
// that will override the default values
{
appId: localStorage.getItem(kCoreAppVersionLSKey),
roleId: 'core-app',
zomeName: 'hha',
fnName: 'set_happ_preferences',
payload: {
happ_id: id,
max_fuel_before_invoice: '0',
price_compute: '0',
price_storage: '0',
price_bandwidth: '0',
max_time_before_invoice: { secs: 15000, nanos: 0 },
invoice_due_in_days: 7
}
}
}

await hposHolochainCall({
method: 'post',
Expand Down
37 changes: 17 additions & 20 deletions src/store/earnings.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { defineStore } from 'pinia'
import { Earnings, Redemption, Transaction, useHposInterface } from '@/interfaces/HposInterface'
import { Earnings, Redemption, RedemptionHistory, Transaction, useHposInterface } from '@/interfaces/HposInterface'
import { isRedemptionsArray, isTransactionsArray } from '@/types/predicates'
import { parseRedemption } from '@/utils/redemptions'

Expand Down Expand Up @@ -48,26 +48,23 @@ export const useEarningsStore = defineStore('earnings', {
},

async getRedemptionHistory(): Promise<boolean> {
const redemptions = await getRedemptionHistory()
const redemptions = await getRedemptionHistory() as RedemptionHistory;
if (!isRedemptionsArray(redemptions.accepted))
return false;
if (!isRedemptionsArray(redemptions.completed))
return false;
if (!isRedemptionsArray(redemptions.declined))
return false;
if (!isRedemptionsArray(redemptions.pending))
return false;

if (isRedemptionsArray(redemptions)) {
// Turn an object of arrays into a single array with proper status assigned
const flattenedRedemptions = []

Object.entries(redemptions.redemptions).forEach(([key, value]) =>
// status is a key an array of redemptions with that status is the value
value.forEach((redemption) => flattenedRedemptions.push({ ...redemption, status: key }))
)

const parsedRedemptions = flattenedRedemptions.map((redemption) =>
parseRedemption(redemption)
)

this.redemptions = parsedRedemptions
return true
}

return false
this.redemptions = [
...redemptions.accepted,
...redemptions.completed,
...redemptions.declined,
...redemptions.pending
]
return true;
}
}
})
Loading