Skip to content

Commit

Permalink
Implement suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelbsky committed Dec 12, 2024
1 parent b43fdcc commit 9f00bf6
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 25 deletions.
25 changes: 13 additions & 12 deletions packages/bsync/src/purchases/purchasesClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ type SubscriptionGroupId = 'core'

type SubscriptionOfferingId = 'core:monthly' | 'core:annual'

const assertSubscriptionGroup: (
export const assertSubscriptionGroup: (
group: string,
) => asserts group is SubscriptionGroupId = (group: string) => {
if (['core'].includes(group)) {
Expand All @@ -16,9 +16,9 @@ const assertSubscriptionGroup: (
throw new Error(`invalid subscription group: '${group}'`)
}

const assertPlatform: (platform: string) => asserts platform is PlatformId = (
export const assertPlatform: (
platform: string,
) => {
) => asserts platform is PlatformId = (platform: string) => {
if (['web', 'ios', 'android'].includes(platform)) {
return
}
Expand Down Expand Up @@ -128,10 +128,11 @@ export class PurchasesClient {
return this.revenueCatClient.isWebhookAuthorizationValid(authorization)
}

getSubscriptionGroup(group: string, platform: string) {
assertSubscriptionGroup(group)
assertPlatform(platform)
return this.GROUPS[group][platform]
getSubscriptionGroup(
subscriptionGroupId: SubscriptionGroupId,
platformId: PlatformId,
) {
return this.GROUPS[subscriptionGroupId][platformId]
}

async getEntitlements(did: string): Promise<string[]> {
Expand Down Expand Up @@ -187,10 +188,10 @@ export class PurchasesClient {

let status = 'unknown'
if (s.auto_resume_date) {
if (now > expiresAt) {
if (now >= expiresAt) {
status = 'paused'
}
} else if (now > expiresAt) {
} else if (now >= expiresAt) {
status = 'expired'
} else if (now < expiresAt) {
status = 'active'
Expand All @@ -200,21 +201,21 @@ export class PurchasesClient {
if (s.auto_resume_date) {
if (now < expiresAt) {
renewalStatus = 'will_pause'
} else if (now > expiresAt) {
} else if (now >= expiresAt) {
renewalStatus = 'will_renew'
}
} else if (now < expiresAt) {
renewalStatus = 'will_renew'
if (s.unsubscribe_detected_at) {
renewalStatus = 'will_not_renew'
}
} else if (now > expiresAt) {
} else if (now >= expiresAt) {
renewalStatus = 'will_not_renew'
}

let periodEndsAt = s.expires_date
if (s.auto_resume_date) {
if (now > expiresAt) {
if (now >= expiresAt) {
periodEndsAt = s.auto_resume_date
}
}
Expand Down
14 changes: 9 additions & 5 deletions packages/bsync/src/routes/get-subscription-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Service } from '../proto/bsync_connect'
import { GetSubscriptionGroupResponse } from '../proto/bsync_pb'
import AppContext from '../context'
import { authWithApiKey } from './auth'
import { assertPlatform, assertSubscriptionGroup } from '../purchases'

export default (ctx: AppContext): Partial<ServiceImpl<typeof Service>> => ({
async getSubscriptionGroup(req, handlerCtx) {
Expand All @@ -18,13 +19,16 @@ export default (ctx: AppContext): Partial<ServiceImpl<typeof Service>> => ({

const { group, platform } = req
try {
const offerings = purchasesClient.getSubscriptionGroup(group, platform)

return new GetSubscriptionGroupResponse({
offerings,
})
assertSubscriptionGroup(group)
assertPlatform(platform)
} catch (error) {
throw new ConnectError((error as Error).message, Code.InvalidArgument)
}

const offerings = purchasesClient.getSubscriptionGroup(group, platform)

return new GetSubscriptionGroupResponse({
offerings,
})
},
})
19 changes: 11 additions & 8 deletions packages/bsync/tests/purchases.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import http from 'node:http'
import { once } from 'node:events'
import http from 'node:http'

import { Timestamp } from '@bufbuild/protobuf'
import { Code, ConnectError } from '@connectrpc/connect'
import getPort from 'get-port'

import { DAY, wait } from '@atproto/common'

import {
authWithApiKey,
BsyncClient,
Expand All @@ -9,18 +15,15 @@ import {
Database,
envToCfg,
} from '../src'
import {
GetSubscriptionsResponse,
PurchaseOperation,
} from '../src/proto/bsync_pb'
import {
RcEntitlement,
RcEventBody,
RcGetSubscriberResponse,
} from '../src/purchases'
import { Code, ConnectError } from '@connectrpc/connect'
import {
GetSubscriptionsResponse,
PurchaseOperation,
} from '../src/proto/bsync_pb'
import { Timestamp } from '@bufbuild/protobuf'
import { DAY, wait } from '@atproto/common'

const revenueCatWebhookAuthorization = 'Bearer any-token'

Expand Down

0 comments on commit 9f00bf6

Please sign in to comment.