Repository navigation
feat(referral): polymorfa referral link #61
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| import {BaseCommand} from '../../base-command.js' | ||
| import {getReferral, renderReferralLink} from '../../services/referral.js' | ||
|
|
||
| export default class ReferralLink extends BaseCommand { | ||
| static override description = | ||
| 'A team created from the link gets extra credits on its first top-up, and your team earns bonus credits. Use --json for the code and the current program terms. Requires a team API key with sessions:read.' | ||
|
|
||
| static override examples = [ | ||
| '<%= config.bin %> referral link', | ||
| '<%= config.bin %> referral link --json', | ||
| ] | ||
|
|
||
| static override summary = "Print your team's referral link." | ||
|
|
||
| async run(): Promise<void> { | ||
| const {flags} = await this.parse(ReferralLink) | ||
| const result = await getReferral(await this.platformClient(flags)) | ||
| this.writeValue(result, renderReferralLink(result.data), flags) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| import { | ||
| type ClientInstance, | ||
| PolymorfaServerError, | ||
| type ResponseMetadata, | ||
| } from '@polymorfa/sdk' | ||
|
|
||
| import {terminalText} from '../developer/render.js' | ||
| import type {PlatformResult} from './platform-management.js' | ||
|
|
||
| // `GET /platform/referral`. The pinned SDK does not yet have a typed | ||
| // `client.referral.retrieve()`; until it is published (polymorfa/sdks | ||
| // `t3code/referrals`), this reads the route through the SDK's raw client and | ||
| // validates the response shape here. Replace with the typed method once the | ||
| // CLI pins an SDK build that includes it. | ||
|
|
||
| export interface ReferralWelcomeBonus { | ||
| readonly percent: number | ||
| /** Decimal string, such as `"5000"`. */ | ||
| readonly capCredits: string | ||
| } | ||
|
|
||
| export interface ReferralTier { | ||
| readonly minReferrals: number | ||
| readonly percent: number | ||
| } | ||
|
|
||
| export interface Referral { | ||
| readonly code: string | ||
| readonly link: string | ||
| readonly enabled: boolean | ||
| readonly welcomeBonus: ReferralWelcomeBonus | ||
| readonly tiers: readonly ReferralTier[] | ||
| /** Decimal string, such as `"10000"`. */ | ||
| readonly rewardCapCredits: string | ||
| readonly holdDays: number | ||
| readonly groupShareEnabled: boolean | ||
| } | ||
|
|
||
| export async function getReferral( | ||
| client: ClientInstance<'organization'>, | ||
| ): Promise<PlatformResult<Referral>> { | ||
| const response = await client.raw.request<unknown>({ | ||
| method: 'GET', | ||
| path: '/platform/referral', | ||
| }) | ||
| const data = isRecord(response.data) ? response.data.data : undefined | ||
| if (!isReferral(data)) throw invalidResponse(response.data, response.metadata) | ||
| return { | ||
| data, | ||
| metadata: { | ||
| attempts: response.metadata.attempts, | ||
| status: response.metadata.status, | ||
| ...(response.metadata.apiVersion === undefined | ||
| ? {} | ||
| : {apiVersion: response.metadata.apiVersion}), | ||
| ...(response.metadata.requestId === undefined | ||
| ? {} | ||
| : {requestId: response.metadata.requestId}), | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| export function renderReferralLink(referral: Referral): string { | ||
| return terminalText(referral.link) | ||
| } | ||
|
|
||
| function isReferral(value: unknown): value is Referral { | ||
| return ( | ||
| isRecord(value) && | ||
| typeof value.code === 'string' && | ||
| typeof value.link === 'string' && | ||
| typeof value.enabled === 'boolean' && | ||
| isRecord(value.welcomeBonus) && | ||
| typeof value.welcomeBonus.percent === 'number' && | ||
| typeof value.welcomeBonus.capCredits === 'string' && | ||
| Array.isArray(value.tiers) && | ||
| value.tiers.every( | ||
| (tier) => | ||
| isRecord(tier) && | ||
| typeof tier.minReferrals === 'number' && | ||
| typeof tier.percent === 'number', | ||
| ) && | ||
| typeof value.rewardCapCredits === 'string' && | ||
| typeof value.holdDays === 'number' && | ||
| typeof value.groupShareEnabled === 'boolean' | ||
| ) | ||
| } | ||
|
|
||
| function isRecord(value: unknown): value is Record<string, unknown> { | ||
| return typeof value === 'object' && value !== null && !Array.isArray(value) | ||
| } | ||
|
|
||
| function invalidResponse( | ||
| details: unknown, | ||
| metadata: ResponseMetadata, | ||
| ): PolymorfaServerError { | ||
| return new PolymorfaServerError( | ||
| 'The Polymorfa API returned an invalid referral response.', | ||
| { | ||
| code: 'invalid_response', | ||
| details, | ||
| metadata, | ||
| status: metadata.status, | ||
| ...(metadata.requestId === undefined | ||
| ? {} | ||
| : {requestId: metadata.requestId}), | ||
| }, | ||
| ) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| import {runCommand} from '@oclif/test' | ||
| import {afterEach, describe, expect, test} from 'vitest' | ||
|
|
||
| import { | ||
| startHttpServer, | ||
| withEnvironment, | ||
| type TestHttpServer, | ||
| } from '../helpers/http-server.js' | ||
|
|
||
| const REFERRAL = { | ||
| code: 'k3m9p2q8rt', | ||
| link: 'https://polymorfa.com/r/k3m9p2q8rt', | ||
| enabled: true, | ||
| welcomeBonus: {percent: 25, capCredits: '5000'}, | ||
| tiers: [ | ||
| {minReferrals: 1, percent: 10}, | ||
| {minReferrals: 4, percent: 15}, | ||
| {minReferrals: 25, percent: 20}, | ||
| ], | ||
| rewardCapCredits: '10000', | ||
| holdDays: 30, | ||
| groupShareEnabled: true, | ||
| } | ||
|
|
||
| describe('referral link', () => { | ||
| let server: TestHttpServer | undefined | ||
|
|
||
| afterEach(async () => { | ||
| await server?.close() | ||
| server = undefined | ||
| }) | ||
|
|
||
| test('prints only the link', async () => { | ||
| server = await startHttpServer({ | ||
| body: {data: REFERRAL}, | ||
| headers: {'request-id': 'req_referral'}, | ||
| }) | ||
|
|
||
| const result = await platformEnvironment(() => runCommand('referral link')) | ||
|
|
||
| expect(result.error).toBeUndefined() | ||
| expect(result.stdout.trim()).toBe('https://polymorfa.com/r/k3m9p2q8rt') | ||
| expect(server.requests.map(({method, url}) => `${method} ${url}`)).toEqual([ | ||
| 'GET /platform/referral', | ||
| ]) | ||
| expect(server.requests[0]?.headers.authorization).toMatch(/^Bearer pmfa_/) | ||
| }) | ||
|
|
||
| test('prints the full response with --json', async () => { | ||
| server = await startHttpServer({ | ||
| body: {data: REFERRAL}, | ||
| headers: {'request-id': 'req_referral'}, | ||
| }) | ||
|
|
||
| const result = await platformEnvironment(() => | ||
| runCommand('referral link --json'), | ||
| ) | ||
|
|
||
| expect(result.error).toBeUndefined() | ||
| expect(JSON.parse(result.stdout)).toMatchObject({ | ||
| data: REFERRAL, | ||
| metadata: {requestId: 'req_referral', status: 200}, | ||
| }) | ||
| }) | ||
|
|
||
| test('reports a team without referral access as a failure', async () => { | ||
| server = await startHttpServer({ | ||
| status: 403, | ||
| body: { | ||
| error: { | ||
| type: 'permission_error', | ||
| code: 'feature_unavailable', | ||
| message: 'Referrals are not available to this team.', | ||
| param: null, | ||
| request_id: 'req_denied', | ||
| }, | ||
| data: null, | ||
| }, | ||
| }) | ||
|
|
||
| const result = await platformEnvironment(() => runCommand('referral link')) | ||
|
|
||
| expect(exitCode(result.error)).toBe(3) | ||
| expect(result.stdout).toBe('') | ||
| }) | ||
|
|
||
| test('rejects a response without a referral link', async () => { | ||
| server = await startHttpServer({body: {data: {code: 'k3m9p2q8rt'}}}) | ||
|
|
||
| const result = await platformEnvironment(() => runCommand('referral link')) | ||
|
|
||
| expect(exitCode(result.error)).toBe(5) | ||
| expect(result.stdout).toBe('') | ||
| }) | ||
|
|
||
| async function platformEnvironment<T>(run: () => Promise<T>): Promise<T> { | ||
| if (server === undefined) throw new Error('Test server was not started.') | ||
| return withEnvironment( | ||
| { | ||
| POLYMORFA_API_KEY: | ||
| 'pmfa_AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA', | ||
| POLYMORFA_BASE_URL: server.baseUrl, | ||
| POLYMORFA_CLIENT_TOKEN: undefined, | ||
| }, | ||
| run, | ||
| ) | ||
| } | ||
| }) | ||
|
|
||
| function exitCode(error: unknown): number | undefined { | ||
| return (error as {oclif?: {exit?: number}} | undefined)?.oclif?.exit | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
Reject an empty referral link before reporting success.
If the API returns an otherwise valid referral with
link: '',isReferralaccepts it. The command then succeeds without printing a usable link. Require a non-empty shareable link here so this malformed response producesinvalid_response. The SDK describeslinkas a shareable URL. (raw.githubusercontent.com)🤖 Prompt for AI Agents