Skip to content

Commit

Permalink
Merge pull request #2926 from dev-protocol/feat-add-passport-payment-…
Browse files Browse the repository at this point in the history
…overrides

feat: add passport plugin payment overrides in super user ui.
  • Loading branch information
yashdesu authored Nov 19, 2024
2 parents 30054fd + 76d4aab commit 90052c2
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 11 deletions.
141 changes: 137 additions & 4 deletions src/components/SuperUser/Page.vue
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import {
changePassportDiscountEnd,
changePassportDiscountStart,
} from './utils/passportDiscount'
import type { Override } from '@devprotocol/clubs-plugin-payments'
dayjs.extend(utc)
Expand Down Expand Up @@ -75,6 +76,8 @@ const passportOffering = ref<Partial<PassportOffering>>({})
const achievement = ref<Partial<ReqBodyAchievement['achievement']>>({})
const passportItem = ref<Partial<CreatePassportItemReq['passportItem']>>({})
const passportDiscount = ref<Partial<PassportOptionsDiscount>>({})
const passportDiscountRate = ref<number>(0)
const passportOverride = ref<Partial<Override>>({})
const sign = async () => {
const msg = message()
Expand Down Expand Up @@ -157,6 +160,9 @@ const onChangePassportOfferingBeneficiary =
const onChangePassportItemAssetType = changePassportItemAssetType(
passportItem,
passportOffering,
passportDiscount,
passportDiscountRate,
passportOverride,
)
const onChangePassportDiscountEnd = changePassportDiscountEnd(passportDiscount)
const onChangePassportDiscountStart =
Expand All @@ -165,13 +171,17 @@ const onChangePassportDiscountRate = changePassportDiscount(
passportDiscount,
passportOffering,
passportItem,
passportDiscountRate,
passportOverride,
)
onMounted(async () => {
passportPayload.value = randomBytes(8)
const id = bytes32Hex(randomBytes(8))
passportOffering.value = {
...passportOffering.value,
id,
payload: passportPayload.value,
}
passportItem.value = {
Expand All @@ -182,6 +192,11 @@ onMounted(async () => {
...passportDiscount.value,
payload: bytes32Hex(passportPayload.value),
}
passportOverride.value = {
...passportOverride.value,
id,
payload: bytes32Hex(passportPayload.value),
}
const { connection } = await import('@devprotocol/clubs-core/connection')
combineLatest([
Expand All @@ -201,6 +216,18 @@ const addPassportdOfferingInConfig = async () => {
const { signature: sig, message: msg } = await sign()
apiCalling.value = { progress: true }
if (
!passportItem.value.itemAssetType ||
!passportItem.value.itemAssetValue ||
!passportOffering.value.price
) {
apiCalling.value = {
progress: false,
result: null,
error: 'missing itemAssetType or itemAssetValue or price',
}
}
const currentConfig = whenDefined(
(await whenDefined(club.value, fetchClubs))?.content,
decode,
Expand All @@ -211,7 +238,6 @@ const addPassportdOfferingInConfig = async () => {
...(base?.offerings ?? []),
{
...passportOffering.value,
id: bytes32Hex(randomBytes(8)),
managedBy: PASSPORT_PLUGIN_ID,
},
],
Expand All @@ -237,6 +263,18 @@ const addPassportdDiscountInConfig = async () => {
const { signature: sig, message: msg } = await sign()
apiCalling.value = { progress: true }
if (
!passportItem.value.itemAssetType ||
!passportItem.value.itemAssetValue ||
!passportOffering.value.price
) {
apiCalling.value = {
progress: false,
result: null,
error: 'missing itemAssetType or itemAssetValue or price',
}
}
const currentConfig = whenDefined(
(await whenDefined(club.value, fetchClubs))?.content,
decode,
Expand Down Expand Up @@ -278,7 +316,75 @@ const addPassportdDiscountInConfig = async () => {
],
}),
)
console.log('Next config', currentConfig, passportPlugin, nextConfig)
const api = await whenDefined(nextConfig, (conf) =>
fetch('/api/superuser/config', {
method: 'POST',
body: JSON.stringify({
site: club.value,
message: msg,
signature: sig,
config: encode(conf),
}),
}),
)
const res = (await api?.json()) as { result: string; error?: string }
apiCalling.value = { progress: false, result: res.result, error: res.error }
}
const addPassportdOverrideInConfig = async () => {
const { signature: sig, message: msg } = await sign()
apiCalling.value = { progress: true }
if (
!passportItem.value.itemAssetType ||
!passportItem.value.itemAssetValue ||
!passportOffering.value.price
) {
apiCalling.value = {
progress: false,
result: null,
error: 'missing itemAssetType or itemAssetValue or price',
}
}
const currentConfig = whenDefined(
(await whenDefined(club.value, fetchClubs))?.content,
decode,
)
const passportPlugin = currentConfig?.plugins?.find(
(plgn) => plgn.id === PASSPORT_PLUGIN_ID,
)
const nextConfig = whenDefinedAll(
[currentConfig, passportPlugin],
([base, _passportPlugin]) => ({
...base,
plugins: [
...base.plugins.filter((plgn) => plgn.id !== PASSPORT_PLUGIN_ID),
{
..._passportPlugin,
options: [
...(_passportPlugin?.options?.filter(
(option) => option.key !== 'override',
) || []),
{
key: 'override',
value: [
{
...passportOverride.value,
},
...((_passportPlugin?.options?.find(
(option) => option.key === 'override',
)?.value || []) as Override[]),
],
},
],
},
],
}),
)
const api = await whenDefined(nextConfig, (conf) =>
fetch('/api/superuser/config', {
method: 'POST',
Expand All @@ -301,6 +407,17 @@ const updatePassportOfferingOnChain = async () => {
}
apiCalling.value = { progress: true }
if (
!passportItem.value.itemAssetType ||
!passportItem.value.itemAssetValue ||
!passportOffering.value.price
) {
apiCalling.value = {
progress: false,
result: null,
error: 'missing itemAssetType or itemAssetValue or price',
}
}
const currentConfig = whenDefined(
(await whenDefined(club.value, fetchClubs))?.content,
Expand Down Expand Up @@ -629,6 +746,7 @@ const updatePassportOfferingOnChain = async () => {
type="number"
min="0"
max="1"
:value="passportDiscountRate"
class="w-full hs-form-field__input"
@change="onChangePassportDiscountRate"
/>
Expand All @@ -652,7 +770,7 @@ const updatePassportOfferingOnChain = async () => {
<dl class="grid grid-cols-[auto,1fr] gap-2 gap-y-4">
<dt class="font-bold">Club</dt>
<dd>{{ club }}</dd>
<dt class="font-bold">Install Plugins</dt>
<dt class="font-bold mb-8">Install Plugins</dt>
<dd>
<p v-for="plugin of plugins.filter((x) => x.willInstall)">
{{ plugin.name }}
Expand All @@ -665,7 +783,7 @@ const updatePassportOfferingOnChain = async () => {
</dd>

<dt class="font-bold">Add Achievement</dt>
<dd>
<dd class="mb-16">
<pre class="text-sm">{{
achievement ? JSON.stringify(achievement, null, 2) : ''
}}</pre>
Expand Down Expand Up @@ -731,6 +849,21 @@ const updatePassportOfferingOnChain = async () => {
</button>
</p>
</dd>

<dt class="font-bold">Add Passport Override</dt>
<dd>
<pre class="text-sm">{{
passportOverride ? JSON.stringify(passportOverride, null, 2) : ''
}}</pre>
<p>
<button
class="hs-button is-small is-filled"
@click="addPassportdOverrideInConfig"
>
Add in override
</button>
</p>
</dd>
</dl>
</div>
<div
Expand Down
3 changes: 3 additions & 0 deletions src/components/SuperUser/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import type { Ref } from 'vue'
import type { Override } from '@devprotocol/clubs-plugin-payments'

export type RefPassportOverride = Ref<Partial<Override>>

export type RefApiCalling = Ref<
| {
Expand Down
15 changes: 14 additions & 1 deletion src/components/SuperUser/utils/passportDiscount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import type {
} from '@devprotocol/clubs-plugin-passports/src/types'

import type { RefPassportItem } from './passportItem'
import type { RefPassportOverride } from '.'

export type RefPassportDiscountRate = Ref<number>
export type RefPassportOffering = Ref<Partial<PassportOffering>>
export type RefPassportDiscount = Ref<Partial<PassportOptionsDiscount>>

Expand All @@ -15,13 +17,17 @@ export const changePassportDiscount =
discountRef: RefPassportDiscount,
offeringRef: RefPassportOffering,
itemRef: RefPassportItem,
discountRateRef: RefPassportDiscountRate,
overrideRef: RefPassportOverride,
) =>
(ev: Event) => {
const value = Number((ev.target as HTMLInputElement).value)
if (!value || !offeringRef?.value?.price || !itemRef.value.itemAssetType) {
if (!offeringRef?.value?.price || !itemRef.value.itemAssetType) {
return
}

discountRateRef.value = value

const yenPrice = Prices[itemRef.value.itemAssetType].yen
discountRef.value = {
...discountRef.value,
Expand All @@ -30,6 +36,13 @@ export const changePassportDiscount =
yen: yenPrice - yenPrice * value,
},
}

overrideRef.value = {
...overrideRef.value,
price: {
yen: yenPrice - yenPrice * value,
},
}
}

export const changePassportDiscountStart =
Expand Down
36 changes: 30 additions & 6 deletions src/components/SuperUser/utils/passportOffering.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ import {
address,
callSimpleCollections,
} from '@plugins/memberships/utils/simpleCollections'
import {
Prices,
type CreatePassportItemReq,
} from '@devprotocol/clubs-plugin-passports'
import { Prices } from '@devprotocol/clubs-plugin-passports'
import type { RefPassportItem } from './passportItem'
import type { RefPassportDiscount } from './passportDiscount'
import type {
RefPassportDiscount,
RefPassportDiscountRate,
} from './passportDiscount'
import type { RefPassportOverride } from '.'

export type RefPassportOffering = Ref<Partial<PassportOffering>>

Expand All @@ -44,7 +45,13 @@ export const changePassportOfferingFee =
}

export const changePassportItemAssetType =
(itemRef: RefPassportItem, offeringRef: RefPassportOffering) =>
(
itemRef: RefPassportItem,
offeringRef: RefPassportOffering,
discountRef: RefPassportDiscount,
discountRateRef: RefPassportDiscountRate,
overrideRef: RefPassportOverride,
) =>
(ev: Event) => {
const value = (ev.target as HTMLInputElement).value
if (!value) {
Expand All @@ -61,6 +68,23 @@ export const changePassportItemAssetType =
price: Prices[value as PassportItemAssetType].usdc,
currency: 'USDC',
}

const usdcPrice = Prices[value as PassportItemAssetType].usdc
const yenPrice = Prices[value as PassportItemAssetType].yen
discountRef.value = {
...discountRef.value,
price: {
usdc: usdcPrice - usdcPrice * discountRateRef.value,
yen: yenPrice - yenPrice * discountRateRef.value,
},
}

overrideRef.value = {
...overrideRef.value,
price: {
yen: yenPrice - yenPrice * discountRateRef.value,
},
}
}

export const changePassportOfferingBeneficiary =
Expand Down

0 comments on commit 90052c2

Please sign in to comment.