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

feat: add set method to AccountPlan #1281

Merged
merged 3 commits into from
Jan 29, 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
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
Loading