Skip to content

Commit

Permalink
fix: consumerTable.get returns customer
Browse files Browse the repository at this point in the history
  • Loading branch information
fforbeck committed Nov 12, 2024
1 parent 9cdaa9b commit 94f3a4e
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 5 deletions.
8 changes: 6 additions & 2 deletions billing/data/consumer.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ import { DecodeFailure, EncodeFailure, Schema } from './lib.js'
* @typedef {import('../types').InferStoreRecord<ConsumerKey>} ConsumerKeyStoreRecord
* @typedef {import('../lib/api').ConsumerListKey} ConsumerListKey
* @typedef {import('../types').InferStoreRecord<ConsumerListKey>} ConsumerListKeyStoreRecord
* @typedef {Pick<Consumer, 'consumer'|'provider'|'subscription'>} ConsumerList
* @typedef {Pick<Consumer, 'consumer'|'provider'|'subscription'|'customer'>} ConsumerList
*/

const schema = Schema.struct({
consumer: Schema.did(),
provider: Schema.did({ method: 'web' }),
subscription: Schema.text(),
customer: Schema.did({ method: 'mailto' }),
cause: Schema.link({ version: 1 }).optional(),
insertedAt: Schema.date(),
updatedAt: Schema.date().optional()
Expand All @@ -32,6 +33,7 @@ export const encode = input => {
consumer: input.consumer,
provider: input.provider,
subscription: input.subscription,
customer: input.customer,
cause: input.cause ? input.cause.toString() : undefined,
insertedAt: input.insertedAt.toISOString(),
updatedAt: input.updatedAt ? input.updatedAt.toISOString() : undefined
Expand All @@ -52,6 +54,7 @@ export const decode = input => {
consumer: Schema.did().from(input.consumer),
provider: Schema.did({ method: 'web' }).from(input.provider),
subscription: /** @type {string} */ (input.subscription),
customer: Schema.did({ method: 'mailto' }).from(input.customer),
cause: input.cause ? Link.parse(/** @type {string} */ (input.cause)) : undefined,
insertedAt: new Date(input.insertedAt),
updatedAt: input.updatedAt ? new Date(input.updatedAt) : undefined
Expand Down Expand Up @@ -83,7 +86,8 @@ export const lister = {
ok: {
consumer: Schema.did().from(input.consumer),
provider: Schema.did({ method: 'web' }).from(input.provider),
subscription: String(input.subscription)
subscription: String(input.subscription),
customer: Schema.did({ method: 'mailto' }).from(input.customer)
}
}
} catch (/** @type {any} */ err) {
Expand Down
3 changes: 2 additions & 1 deletion billing/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ export interface Consumer {
consumer: ConsumerDID
provider: ProviderDID
subscription: string
customer: CustomerDID
/** This became a required field after 2023-07-10T23:12:38.000Z. */
cause?: Link
insertedAt: Date
Expand All @@ -221,7 +222,7 @@ export interface ConsumerListKey { consumer: ConsumerDID }

export type ConsumerStore =
& StoreGetter<ConsumerKey, Consumer>
& StoreLister<ConsumerListKey, Pick<Consumer, 'consumer'|'provider'|'subscription'>>
& StoreLister<ConsumerListKey, Pick<Consumer, 'consumer'|'provider'|'subscription'|'customer'>>

export interface Subscription {
customer: CustomerDID
Expand Down
1 change: 1 addition & 0 deletions billing/lib/ucan-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export const storeSpaceUsageDeltas = async (deltas, ctx) => {
diffs.push({
provider: consumer.provider,
subscription: consumer.subscription,
customer: consumer.customer,
space: delta.resource,
cause: delta.cause,
delta: delta.delta,
Expand Down
2 changes: 2 additions & 0 deletions billing/test/helpers/consumer.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Schema } from '../../data/lib.js'
import { randomCustomer } from './customer.js'
import { randomLink } from './dag.js'
import { randomDID } from './did.js'
import { randomInteger } from './math.js'
Expand All @@ -11,6 +12,7 @@ export const randomConsumer = async (base = {}) => ({
consumer: await randomDID(),
provider: Schema.did({ method: 'web' }).from(['did:web:web3.storage', 'did:web:nft.storage'][randomInteger(0, 2)]),
subscription: randomLink().toString(),
customer: randomCustomer().customer,
cause: randomLink(),
insertedAt: new Date(),
updatedAt: new Date(),
Expand Down
3 changes: 2 additions & 1 deletion upload-api/tables/consumer.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ export function useConsumerTable (dynamoDb, tableName) {
KeyConditionExpression: "consumer = :consumer",
ExpressionAttributeValues: {
':consumer': { S: consumer },
}
},
ProjectionExpression: 'provider, subscription, customer'
}))
// we may need to worry about pagination in the future if we end up supporting many many subscriptions for a single
// provider/consumer pair, but I suspect we'll never get there
Expand Down
2 changes: 1 addition & 1 deletion upload-api/tables/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export const consumerTableProps = {
},
primaryIndex: { partitionKey: 'subscription', sortKey: 'provider' },
globalIndexes: {
consumer: { partitionKey: 'consumer', projection: ['provider', 'subscription'] },
consumer: { partitionKey: 'consumer', projection: ['provider', 'subscription', 'customer'] },
provider: { partitionKey: 'provider', projection: ['consumer'] },
customer: { partitionKey: 'customer', projection: ['consumer', 'provider', 'subscription', 'cause'] },
}
Expand Down

0 comments on commit 94f3a4e

Please sign in to comment.