@@ -3,7 +3,10 @@ import { db } from '@sim/db'
33import type { AwsMarketplaceClaim } from '@/lib/api/contracts/aws-marketplace'
44import { awsMarketplaceOperations } from '@/lib/billing/aws-marketplace/application/operations'
55import { hashClaimToken } from '@/lib/billing/aws-marketplace/claim'
6- import { getAwsMarketplaceProductCode } from '@/lib/billing/aws-marketplace/config'
6+ import {
7+ findAwsMarketplaceProduct ,
8+ isAwsMarketplaceEnabled ,
9+ } from '@/lib/billing/aws-marketplace/config'
710import { verifyLicense } from '@/lib/billing/aws-marketplace/entitlements'
811import {
912 type AwsMarketplaceLicenseRecord ,
@@ -27,10 +30,15 @@ import { requireOrganizationSubjectMembership } from '@/lib/core/application/org
2730import { OrchestrationError } from '@/lib/core/orchestration/types'
2831
2932/** The same answer as an unknown route, so a deployment that does not sell on AWS shows nothing. */
30- function requireProductCode ( ) : string {
31- const productCode = getAwsMarketplaceProductCode ( )
32- if ( ! productCode ) throw new OrchestrationError ( 'not_found' , 'Not found' )
33- return productCode
33+ function requireMarketplaceEnabled ( ) : void {
34+ if ( ! isAwsMarketplaceEnabled ( ) ) throw new OrchestrationError ( 'not_found' , 'Not found' )
35+ }
36+
37+ /** The listing a license was sold under; one this deployment no longer sells reads as unknown. */
38+ function requireLicenseProduct ( license : AwsMarketplaceLicenseRecord ) {
39+ const product = findAwsMarketplaceProduct ( license . productCode )
40+ if ( ! product ) throw new OrchestrationError ( 'not_found' , 'Not found' )
41+ return product
3442}
3543
3644function isClaimOpen ( license : AwsMarketplaceLicenseRecord , now : Date ) : boolean {
@@ -115,7 +123,7 @@ export const readAwsMarketplaceClaim: OperationUseCase<
115123 operation : awsMarketplaceOperations . readClaim ,
116124 async execute ( { principal, input } ) {
117125 assertOperationPrincipal ( principal , awsMarketplaceOperations . readClaim )
118- requireProductCode ( )
126+ requireMarketplaceEnabled ( )
119127 if ( ! input . claimToken ) return { status : 'missing' }
120128 const license = await findLicenseByClaimHash ( hashClaimToken ( input . claimToken ) )
121129 if ( ! license || ! isClaimOpen ( license , new Date ( ) ) ) return { status : 'missing' }
@@ -169,7 +177,7 @@ function contactOf(input: BuyerContact) {
169177 * rechecks the claim under the row lock, so two tabs racing one claim take it at most once.
170178 */
171179async function verifyClaimedLicense ( input : ClaimInput , organizationId : string | null ) {
172- const productCode = requireProductCode ( )
180+ requireMarketplaceEnabled ( )
173181 const claimHash = input . claimToken ? hashClaimToken ( input . claimToken ) : null
174182 const claimed = requireOpenClaim (
175183 claimHash ? await findLicenseByClaimHash ( claimHash ) : null ,
@@ -179,12 +187,12 @@ async function verifyClaimedLicense(input: ClaimInput, organizationId: string |
179187 requireLinkableTo ( claimed , organizationId )
180188
181189 const verifiedAt = new Date ( )
182- const summary = await verifyLicense ( productCode , claimed . licenseArn )
190+ const summary = await verifyLicense ( requireLicenseProduct ( claimed ) , claimed . licenseArn )
183191 const verification = { summary, verifiedAt }
184192 if ( ! summary . active ) {
185193 const outcome = await recordLicenseVerification ( {
186194 licenseArn : claimed . licenseArn ,
187- productCode,
195+ productCode : claimed . productCode ,
188196 verification,
189197 } )
190198 throw new OrchestrationError (
@@ -321,15 +329,15 @@ export const saveAwsMarketplaceClaimContact: OperationUseCase<
321329 operation : awsMarketplaceOperations . saveClaimContact ,
322330 async execute ( { principal, input } ) {
323331 assertOperationPrincipal ( principal , awsMarketplaceOperations . saveClaimContact )
324- const productCode = requireProductCode ( )
332+ requireMarketplaceEnabled ( )
325333 const claimHash = input . claimToken ? hashClaimToken ( input . claimToken ) : null
326334 const claimed = requireOpenClaim (
327335 claimHash ? await findLicenseByClaimHash ( claimHash ) : null ,
328336 input . licenseId ,
329337 claimHash
330338 )
331339 const verifiedAt = new Date ( )
332- const summary = await verifyLicense ( productCode , claimed . licenseArn )
340+ const summary = await verifyLicense ( requireLicenseProduct ( claimed ) , claimed . licenseArn )
333341 return db . transaction ( async ( tx ) => {
334342 const locked = requireOpenClaim (
335343 await lockLicense ( tx , input . licenseId ) ,
@@ -360,7 +368,7 @@ function toLicenseStatusList(rows: Awaited<ReturnType<typeof listOrganizationLic
360368export const listOrganizationAwsMarketplaceLicenses = defineAuthorizedOrganizationUseCase ( {
361369 operation : awsMarketplaceOperations . listOrganizationLicenses ,
362370 async execute ( { context } : OrganizationUseCaseContext < { organizationId : string } > ) {
363- if ( ! getAwsMarketplaceProductCode ( ) ) return [ ]
371+ if ( ! isAwsMarketplaceEnabled ( ) ) return [ ]
364372 return toLicenseStatusList ( await listOrganizationLicenses ( context . organizationId ) )
365373 } ,
366374} )
@@ -374,7 +382,7 @@ export const listAccountAwsMarketplaceLicenses: OperationUseCase<
374382 operation : awsMarketplaceOperations . listAccountLicenses ,
375383 async execute ( { principal } ) {
376384 assertOperationPrincipal ( principal , awsMarketplaceOperations . listAccountLicenses )
377- if ( ! getAwsMarketplaceProductCode ( ) ) return [ ]
385+ if ( ! isAwsMarketplaceEnabled ( ) ) return [ ]
378386 return toLicenseStatusList ( await listAccountLicenses ( principal . userId ) )
379387 } ,
380388}
0 commit comments