11import { PostgrestError } from '@supabase/supabase-js'
22import { licenseRepo } from '@/db/supabase/licenseRepo'
33import { DeviceInfo } from '@/api/ebbApi/deviceApi'
4+ import { platformApiRequest } from '../platformRequest'
5+ import { DateTime } from 'luxon'
46
57export type LicenseStatus = 'active' | 'expired'
6- export type LicenseType = 'perpetual' | 'subscription'
8+ export type LicenseType = 'perpetual' | 'subscription' | 'free_trial'
79export interface RawLicense {
810 id : string
911 status : string
10- license_type : 'perpetual' | 'subscription'
12+ license_type : LicenseType
1113 expiration_date : string | null
1214}
1315export interface License {
@@ -34,12 +36,9 @@ export interface LicenseDevice {
3436export interface LicensePermissions {
3537 canUseHardDifficulty : boolean
3638 canUseAllowList : boolean
37- canUseTypewriter : boolean
3839 canUseMultipleProfiles : boolean
3940 canUseMultipleDevices : boolean
4041 hasProAccess : boolean
41- maxDevices : number
42- isUpdateEligible : boolean
4342}
4443
4544export type LicenseInfo = {
@@ -51,12 +50,9 @@ export type LicenseInfo = {
5150export const defaultPermissions : LicensePermissions = {
5251 canUseHardDifficulty : false ,
5352 canUseAllowList : false ,
54- canUseTypewriter : false ,
5553 canUseMultipleProfiles : false ,
5654 canUseMultipleDevices : false ,
5755 hasProAccess : false ,
58- maxDevices : 1 ,
59- isUpdateEligible : false ,
6056}
6157
6258const transformLicense = ( rawLicense : RawLicense | null ) : License | null => {
@@ -77,25 +73,18 @@ const transformLicense = (rawLicense: RawLicense | null): License | null => {
7773
7874const calculatePermissions = ( license : License | null ) : LicensePermissions | null => {
7975 if ( ! license ) return null
80- const hasProAccess = license ?. status === 'active'
76+ let hasProAccess = license ?. status === 'active'
8177
82- const isUpdateEligible = hasProAccess && license && (
83- license . licenseType === 'subscription' ||
84- ( license . licenseType === 'perpetual' &&
85- ! ! license . expirationDate &&
86- license . expirationDate > new Date ( )
87- )
88- )
78+ if ( license . licenseType === 'free_trial' && DateTime . fromJSDate ( license . expirationDate ) < DateTime . now ( ) ) {
79+ hasProAccess = false
80+ }
8981
9082 return {
9183 canUseHardDifficulty : hasProAccess ,
9284 canUseAllowList : hasProAccess ,
93- canUseTypewriter : hasProAccess ,
9485 canUseMultipleProfiles : hasProAccess ,
9586 canUseMultipleDevices : hasProAccess ,
9687 hasProAccess,
97- maxDevices : hasProAccess ? 3 : 1 ,
98- isUpdateEligible,
9988 }
10089}
10190
@@ -105,16 +94,48 @@ const getLicenseInfo = async (userId: string): Promise<{data: LicenseInfo, error
10594 const permissions = calculatePermissions ( license ) || defaultPermissions
10695 const deviceInfo : DeviceInfo = {
10796 devices : [ ] ,
108- maxDevices : permissions . maxDevices ,
10997 isDeviceLimitReached : false ,
11098 }
11199
112100 return { data : { license, permissions, deviceInfo } , error}
113101}
114102
103+ export interface StartTrialResponse {
104+ success : boolean
105+ message : string
106+ }
107+
108+ export interface CancelLicenseResponse {
109+ success : boolean
110+ message : string
111+ data ?: {
112+ license_id : number
113+ stripe_subscription_id : string
114+ canceled_at : number
115+ cancel_at_period_end : boolean
116+ }
117+ }
118+
119+ const startTrial = async ( ) : Promise < StartTrialResponse > => {
120+ const response = await platformApiRequest ( {
121+ url : '/api/license/start-trial' ,
122+ method : 'POST'
123+ } )
124+ return response as StartTrialResponse
125+ }
126+
127+ const cancelLicense = async ( ) : Promise < CancelLicenseResponse > => {
128+ const response = await platformApiRequest ( {
129+ url : '/api/license/cancel' ,
130+ method : 'POST'
131+ } )
132+ return response as CancelLicenseResponse
133+ }
115134
116135export const licenseApi = {
117136 getLicenseInfo,
118137 calculatePermissions,
138+ startTrial,
139+ cancelLicense,
119140}
120141
0 commit comments