The recruiter placement/unlock backend was end-to-end (routes, talentBillingService, Creem checkout + signed webhook settlement) but had no frontend, so recruiters literally could not pay. This closes the only fully dead-ended money surface. Web (gitgram/web): - talentGraphService: add listUnlocks, requestUnlock (idempotent, optional placement success-fee), and getCandidateContact mapping the existing owner-gated backend routes, with typed placement/contact envelopes. - utils/talentUnlock: pure gating helpers (deriveUnlockState, findPlacement, interpretUnlockResult, formatFee) so the owner-only logic is unit tested without rendering. - talent-network page: owner-only "Unlock candidate" action on each match card — opens the Creem checkout, reveals contact + full evidence once the entitlement is paid (poll via "check payment" and on-return via query params), with not-owner / pending / unlocked / already-unlocked states. Backend (gitgram/backend): - talentBillingController: point the checkout successUrl at the real /talent-network page (with opportunity + unlocked params) instead of a non-existent /talent/opportunities route, so payment return is not a 404. Tests: unit-test the three service methods (idempotency header, success-fee body, already-unlocked, 402-when-unpaid, id encoding) and the gating helper (not-owner/unlockable/pending/unlocked, checkout-vs-reveal). 21 new tests. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
100 lines
3.8 KiB
TypeScript
100 lines
3.8 KiB
TypeScript
/// <reference types="vitest/globals" />
|
|
import { describe, it, expect } from 'vitest'
|
|
import {
|
|
deriveUnlockState,
|
|
findPlacement,
|
|
formatFee,
|
|
interpretUnlockResult,
|
|
} from '@/utils/talentUnlock'
|
|
import type { RequestUnlockResult, TalentPlacement } from '@/services/talentGraphService'
|
|
|
|
function placement(overrides: Partial<TalentPlacement> = {}): TalentPlacement {
|
|
return {
|
|
id: 'plc-1',
|
|
opportunityId: 'opp-1',
|
|
candidateId: 'cand-1',
|
|
recruiterId: 'rec-1',
|
|
feeCents: 4900,
|
|
currency: 'usd',
|
|
feeModel: 'unlock',
|
|
status: 'pending',
|
|
provider: 'creem',
|
|
checkoutId: 'chk-1',
|
|
providerOrderId: null,
|
|
paidAt: null,
|
|
createdAt: '2026-09-06T00:00:00.000Z',
|
|
updatedAt: '2026-09-06T00:00:00.000Z',
|
|
...overrides,
|
|
}
|
|
}
|
|
|
|
describe('deriveUnlockState', () => {
|
|
it('returns not-owner when the viewer does not own the opportunity', () => {
|
|
expect(deriveUnlockState({ isOwner: false, placement: null })).toBe('not-owner')
|
|
// Ownership gates even when a paid entitlement somehow exists.
|
|
expect(deriveUnlockState({ isOwner: false, placement: placement({ status: 'paid' }) })).toBe('not-owner')
|
|
})
|
|
|
|
it('returns unlockable for an owner with no entitlement yet', () => {
|
|
expect(deriveUnlockState({ isOwner: true, placement: null })).toBe('unlockable')
|
|
expect(deriveUnlockState({ isOwner: true })).toBe('unlockable')
|
|
})
|
|
|
|
it('returns pending for an owner with an open (unpaid) checkout', () => {
|
|
expect(deriveUnlockState({ isOwner: true, placement: placement({ status: 'pending' }) })).toBe('pending')
|
|
})
|
|
|
|
it('returns unlocked for an owner with a paid entitlement', () => {
|
|
expect(deriveUnlockState({ isOwner: true, placement: placement({ status: 'paid' }) })).toBe('unlocked')
|
|
})
|
|
|
|
it('treats failed/refunded entitlements as unlockable (a fresh charge can be opened)', () => {
|
|
expect(deriveUnlockState({ isOwner: true, placement: placement({ status: 'failed' }) })).toBe('unlockable')
|
|
expect(deriveUnlockState({ isOwner: true, placement: placement({ status: 'refunded' }) })).toBe('unlockable')
|
|
})
|
|
})
|
|
|
|
describe('findPlacement', () => {
|
|
it('finds the entitlement matching the candidate id', () => {
|
|
const list = [placement({ id: 'a', candidateId: 'cand-1' }), placement({ id: 'b', candidateId: 'cand-2' })]
|
|
expect(findPlacement(list, 'cand-2')?.id).toBe('b')
|
|
})
|
|
|
|
it('returns null when no entitlement matches', () => {
|
|
expect(findPlacement([placement({ candidateId: 'cand-1' })], 'cand-9')).toBeNull()
|
|
expect(findPlacement([], 'cand-1')).toBeNull()
|
|
})
|
|
})
|
|
|
|
describe('interpretUnlockResult', () => {
|
|
it('reveals immediately when already unlocked', () => {
|
|
const result: RequestUnlockResult = { placement: placement({ status: 'paid' }), checkoutUrl: null, alreadyUnlocked: true }
|
|
expect(interpretUnlockResult(result)).toEqual({ action: 'reveal', checkoutUrl: null })
|
|
})
|
|
|
|
it('opens a checkout when an unpaid entitlement returns a checkout URL', () => {
|
|
const result: RequestUnlockResult = {
|
|
placement: placement({ status: 'pending' }),
|
|
checkoutUrl: 'https://test-api.creem.io/checkout/abc',
|
|
alreadyUnlocked: false,
|
|
}
|
|
expect(interpretUnlockResult(result)).toEqual({ action: 'checkout', checkoutUrl: 'https://test-api.creem.io/checkout/abc' })
|
|
})
|
|
|
|
it('falls back to reveal when no checkout URL is present', () => {
|
|
const result: RequestUnlockResult = { placement: placement(), checkoutUrl: null, alreadyUnlocked: false }
|
|
expect(interpretUnlockResult(result)).toEqual({ action: 'reveal', checkoutUrl: null })
|
|
})
|
|
})
|
|
|
|
describe('formatFee', () => {
|
|
it('formats usd cents as a dollar amount', () => {
|
|
expect(formatFee(4900)).toBe('$49.00')
|
|
expect(formatFee(199900, 'usd')).toBe('$1999.00')
|
|
})
|
|
|
|
it('formats non-usd currencies with an ISO suffix', () => {
|
|
expect(formatFee(4900, 'eur')).toBe('49.00 EUR')
|
|
})
|
|
})
|