Skip to content

Commit

Permalink
feat: return ID from ProvisionsStorage put (#869)
Browse files Browse the repository at this point in the history
We generate an ID when we create a new "provision" but we don't
currently return it - add it to the return value of `put`.

This is a bit muddled because the system is currently organized around
"provisions" which are really an amalgamation of "subscriptions" and
"consumers" - the plan since the D1 migration has been to break
ProvisionsStorage into SubscriptionsStorage and ConsumersStorage - in
that world this will be two steps - users will obtain a subscription
from a provider and then attach it to a consumer, and it will be much
clearer that this is actually the "subscription" ID.

I'd like to tackle this refactoring soon, but for now this makes it
easier to write tests and prepare for that future state.
  • Loading branch information
travis authored Aug 28, 2023
1 parent 7cad02f commit d165c23
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 6 deletions.
2 changes: 1 addition & 1 deletion packages/upload-api/src/types/provisions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export interface ProvisionsStorage<ProviderDID = Ucanto.DID<'web'>> {
*
* @param item - provision to store
*/
put: (item: Provision) => Promise<Ucanto.Result<{}, Ucanto.Failure>>
put: (item: Provision) => Promise<Ucanto.Result<{ id: string }, Ucanto.Failure>>

/**
* Returns information about a customer related to the given provider.
Expand Down
2 changes: 1 addition & 1 deletion packages/upload-api/test/provisions-storage-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export const test = {
assert.ok(!customerResult.error, 'error getting customer record')
assert.deepEqual(customerResult.ok, {
did: issuer.did(),
subscriptions: [`${issuerDID}@${provider}`]
subscriptions: [/** @type {string} */ (result.ok?.id)]
})

const fakeCustomerResult = await storage.getCustomer(
Expand Down
9 changes: 5 additions & 4 deletions packages/upload-api/test/provisions-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,11 @@ export class ProvisionsStorage {
/**
*
* @param {Types.Provision} item
* @returns
* @returns the ID generated by the system to identify this "provision"
*/
async put(item) {
const storedItem = this.provisions[itemKey(item)]
const key = itemKey(item)
const storedItem = this.provisions[key]
if (
storedItem &&
(storedItem.provider !== item.provider ||
Expand All @@ -80,8 +81,8 @@ export class ProvisionsStorage {
},
}
} else {
this.provisions[itemKey(item)] = item
return { ok: {} }
this.provisions[key] = item
return { ok: { id: key } }
}
}

Expand Down

0 comments on commit d165c23

Please sign in to comment.