@@ -17,15 +17,43 @@ import { freemius } from './freemius';
1717// #region Freemius SDK Supporting Functions for User Entitlements
1818
1919export async function processPurchaseInfo ( fsPurchase : PurchaseInfo ) : Promise < void > {
20- // Todo: fill me
20+ const user = await getUserByEmail ( fsPurchase . email ) ;
21+
22+ if ( ! user ) {
23+ return ;
24+ }
25+
26+ const credit = await getCreditsForUserPurchase ( user , fsPurchase ) ;
27+
28+ // Save purchase info in our DB
29+ await prisma . userFsEntitlement . upsert ( {
30+ where : { fsLicenseId : fsPurchase . licenseId } ,
31+ create : fsPurchase . toEntitlementRecord ( { userId : user . id } ) ,
32+ update : fsPurchase . toEntitlementRecord ( ) ,
33+ } ) ;
34+
35+ if ( credit > 0 ) {
36+ await addCredits ( user . id , credit ) ;
37+ }
2138}
2239
2340export async function getUserEntitlement ( userId : string ) : Promise < UserFsEntitlement | null > {
24- // Todo: fill me
41+ const entitlements = await prisma . userFsEntitlement . findMany ( {
42+ where : { userId, type : 'subscription' } ,
43+ } ) ;
44+
45+ return freemius . entitlement . getActive ( entitlements ) ;
2546}
2647
2748export const getFsUser : UserRetriever = async ( ) => {
28- // Todo: fill me
49+ const session = await auth . api . getSession ( {
50+ headers : await headers ( ) ,
51+ } ) ;
52+
53+ const entitlement = session ? await getUserEntitlement ( session . user . id ) : null ;
54+ const email = session ?. user . email ?? undefined ;
55+
56+ return freemius . entitlement . getFsUser ( entitlement , email ) ;
2957} ;
3058
3159function getEntitledCredits ( fsPurchase : PurchaseInfo ) : number {
@@ -46,13 +74,6 @@ async function getCreditsForUserPurchase(user: User, fsPurchase: PurchaseInfo):
4674 credits = getEntitledCredits ( fsPurchase ) ;
4775 }
4876
49- // Save purchase info in our DB
50- await prisma . userFsEntitlement . upsert ( {
51- where : { fsLicenseId : fsPurchase . licenseId } ,
52- create : fsPurchase . toEntitlementRecord ( { userId : user . id } ) ,
53- update : fsPurchase . toEntitlementRecord ( ) ,
54- } ) ;
55-
5677 return credits ;
5778}
5879
0 commit comments