Skip to content

Commit

Permalink
Activate rewards balance (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
ardier16 authored Mar 27, 2024
1 parent d84e96c commit ea0478a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
14 changes: 14 additions & 0 deletions src/api/modules/points/helpers/balance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,20 @@ export const createPointsBalance = async (did: string, referredBy: string) => {
})
}

export const activatePointsBalance = async (did: string, referredBy: string) => {
return api.patch<Balance>(`${ApiServicePaths.Points}/v1/public/balances/${did}`, {
body: {
data: {
id: did,
type: 'update_balance',
attributes: {
referred_by: referredBy,
},
},
},
})
}

export const getLeaderboard = async () => {
return api.get<Balance[]>(`${ApiServicePaths.Points}/v1/public/balances`)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default function InvitationForm() {
disableForm()

try {
await rewardsStore.createBalance(formData[FieldNames.Code])
await rewardsStore.activateBalance(formData[FieldNames.Code])
} catch (error) {
if (error instanceof NotFoundError) {
setError(FieldNames.Code, {
Expand Down
13 changes: 10 additions & 3 deletions src/store/modules/rewards.module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { NotFoundError, UnauthorizedError } from '@distributedlab/jac'

import { authorizeUser } from '@/api/modules/auth'
import { Balance, createPointsBalance, getPointsBalance } from '@/api/modules/points'
import {
activatePointsBalance,
Balance,
createPointsBalance,
getPointsBalance,
} from '@/api/modules/points'
import { createStore } from '@/helpers'

import { identityStore } from './identity.module'
Expand Down Expand Up @@ -42,8 +47,10 @@ const [rewardsStore, useRewardsState] = createStore(
await authorizeUser({ userDid: identityStore.userDid })
state.isAuthorized = true
},
createBalance: async (referredBy: string) => {
const { data } = await createPointsBalance(identityStore.userDid, referredBy)
activateBalance: async (referredBy: string) => {
const { data } = state.balance
? await activatePointsBalance(identityStore.userDid, referredBy)
: await createPointsBalance(identityStore.userDid, referredBy)
state.balance = data
},
}),
Expand Down

0 comments on commit ea0478a

Please sign in to comment.