Skip to content

Commit

Permalink
feat: add set method to AccountPlan (#1281)
Browse files Browse the repository at this point in the history
Also update test to use new `PlanStorage#initialize` function.
  • Loading branch information
travis authored Jan 29, 2024
1 parent 5a44565 commit b94f0d4
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
2 changes: 2 additions & 0 deletions packages/upload-api/src/plan.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as Types from './types.js'
import * as Get from './plan/get.js'
import * as Set from './plan/set.js'

import { Failure } from '@ucanto/server'

Expand Down Expand Up @@ -50,4 +51,5 @@ export class CustomerExists extends Failure {
*/
export const createService = (context) => ({
get: Get.provide(context),
set: Set.provide(context),
})
13 changes: 13 additions & 0 deletions packages/w3up-client/src/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,19 @@ export class AccountPlan {
})
}

/**
* Sets the plan associated with this account.
*
* @param {import('@ucanto/interface').DID} productDID
*/
async set(productDID) {
return await Plan.set(this.model, {
account: this.model.id,
product: productDID,
proofs: this.model.proofs,
})
}

async subscriptions() {
return await Subscription.list(this.model, {
account: this.model.id,
Expand Down
18 changes: 18 additions & 0 deletions packages/w3up-client/src/capability/plan.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,21 @@ export const get = async ({ agent }, { account, proofs = [] }) => {
})
return receipt.out
}

/**
* Sets the plan currently associated with the account.
*
* @param {{agent: API.Agent}} client
* @param {object} options
* @param {API.DID} options.product
* @param {API.AccountDID} options.account
* @param {API.Delegation[]} [options.proofs]
*/
export const set = async ({ agent }, { account, product, proofs = [] }) => {
const receipt = await agent.invokeAndExecute(Plan.set, {
with: account,
nb: { product },
proofs,
})
return receipt.out
}
14 changes: 12 additions & 2 deletions packages/w3up-client/test/account.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ export const testAccount = {
assert.deepEqual(client.spaces().length, 1, 'spaces had been added')
},

'check account plan': async (
'check and set account plan': async (
assert,
{ client, mail, grantAccess, plansStorage }
) => {
Expand All @@ -232,12 +232,22 @@ export const testAccount = {
assert.ok(error)

Result.unwrap(
await plansStorage.set(account.did(), 'did:web:free.web3.storage')
await plansStorage.initialize(
account.did(),
'stripe:123xyz',
'did:web:free.web3.storage'
)
)

const { ok: plan } = await account.plan.get()

assert.ok(plan?.product, 'did:web:free.web3.storage')

Result.unwrap(await account.plan.set('did:web:lite.web3.storage'))

const { ok: newPlan } = await account.plan.get()

assert.ok(newPlan?.product, 'did:web:lite.web3.storage')
},

'check account subscriptions': async (
Expand Down

0 comments on commit b94f0d4

Please sign in to comment.