diff --git a/packages/upload-api/src/plan.js b/packages/upload-api/src/plan.js index 47a80a224..ea0c45210 100644 --- a/packages/upload-api/src/plan.js +++ b/packages/upload-api/src/plan.js @@ -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' @@ -50,4 +51,5 @@ export class CustomerExists extends Failure { */ export const createService = (context) => ({ get: Get.provide(context), + set: Set.provide(context), }) diff --git a/packages/w3up-client/src/account.js b/packages/w3up-client/src/account.js index 60689f352..389ac37b0 100644 --- a/packages/w3up-client/src/account.js +++ b/packages/w3up-client/src/account.js @@ -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, diff --git a/packages/w3up-client/src/capability/plan.js b/packages/w3up-client/src/capability/plan.js index aa729ee05..21ae4c687 100644 --- a/packages/w3up-client/src/capability/plan.js +++ b/packages/w3up-client/src/capability/plan.js @@ -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 +} diff --git a/packages/w3up-client/test/account.test.js b/packages/w3up-client/test/account.test.js index c5e5fdf0a..26a1fbf5c 100644 --- a/packages/w3up-client/test/account.test.js +++ b/packages/w3up-client/test/account.test.js @@ -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 } ) => { @@ -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 (