1+ /*
2+ * GNU AGPL-3.0 License
3+ *
4+ * Copyright (c) 2021 - present core.ai . All rights reserved.
5+ *
6+ * This program is free software: you can redistribute it and/or modify it under
7+ * the terms of the GNU Affero General Public License as published by the Free
8+ * Software Foundation, either version 3 of the License, or (at your option) any later version.
9+ *
10+ * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
11+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12+ * See the GNU Affero General Public License for more details.
13+ *
14+ * You should have received a copy of the GNU Affero General Public License
15+ * along with this program. If not, see https://opensource.org/licenses/AGPL-3.0.
16+ *
17+ */
18+
19+ /**
20+ * Login Service Utilities
21+ *
22+ * This module contains utility functions for login service operations,
23+ * including entitlements expiration checking and change detection.
24+ */
25+
26+ define ( function ( require , exports , module ) {
27+
28+ /**
29+ * Check if any validTill time has expired
30+ *
31+ * @param {Object|null } entitlements - Current entitlements object
32+ * @param {Object|null } lastRecordedEntitlement - Previously recorded entitlements
33+ * @returns {string|null } - Name of expired plan/entitlement or null if none expired
34+ */
35+ function validTillExpired ( entitlements , lastRecordedEntitlement ) {
36+ if ( ! entitlements ) {
37+ return null ;
38+ }
39+
40+ const now = Date . now ( ) ;
41+
42+ function isNewlyExpired ( validTill , lastValidTill ) {
43+ return (
44+ validTill &&
45+ validTill < now && // expired now
46+ ( ! lastValidTill || lastValidTill >= now ) // but wasn't expired before
47+ ) ;
48+ }
49+
50+ // Check plan validTill
51+ if ( entitlements . plan ) {
52+ const validTill = entitlements . plan . validTill ;
53+ const lastValidTill = ( lastRecordedEntitlement && lastRecordedEntitlement . plan )
54+ ? lastRecordedEntitlement . plan . validTill
55+ : null ;
56+
57+ if ( isNewlyExpired ( validTill , lastValidTill ) ) {
58+ return entitlements . plan . name || brackets . config . main_pro_plan ;
59+ }
60+ }
61+
62+ // Check entitlements validTill
63+ if ( entitlements . entitlements ) {
64+ for ( const key in entitlements . entitlements ) {
65+ const entitlement = entitlements . entitlements [ key ] ;
66+ if ( ! entitlement ) {
67+ continue ;
68+ }
69+
70+ const validTill = entitlement . validTill ;
71+ const lastValidTill = ( lastRecordedEntitlement &&
72+ lastRecordedEntitlement . entitlements &&
73+ lastRecordedEntitlement . entitlements [ key ] )
74+ ? lastRecordedEntitlement . entitlements [ key ] . validTill
75+ : null ;
76+
77+ if ( isNewlyExpired ( validTill , lastValidTill ) ) {
78+ return key ;
79+ }
80+ }
81+ }
82+
83+ return null ;
84+ }
85+
86+ /**
87+ * Check if entitlements have changed from last recorded state
88+ *
89+ * @param {Object|null } current - Current entitlements object
90+ * @param {Object|null } last - Last recorded entitlements object
91+ * @returns {boolean } - True if entitlements have changed, false otherwise
92+ */
93+ function haveEntitlementsChanged ( current , last ) {
94+ if ( ! last && ! current ) {
95+ return false ;
96+ }
97+ if ( ( ! last && current ) || ( ! current && last ) ) {
98+ return true ;
99+ }
100+ if ( ( ! last . entitlements && current . entitlements ) || ( ! current . entitlements && last . entitlements ) ) {
101+ return true ;
102+ }
103+
104+ // Check paidSubscriber changes
105+ const currentPaidSub = current . plan && current . plan . paidSubscriber ;
106+ const lastPaidSub = last . plan && last . plan . paidSubscriber ;
107+ if ( currentPaidSub !== lastPaidSub ) {
108+ return true ;
109+ }
110+
111+ // Check plan name changes
112+ const currentPlanName = current . plan && current . plan . name ;
113+ const lastPlanName = last . plan && last . plan . name ;
114+ if ( currentPlanName !== lastPlanName ) {
115+ return true ;
116+ }
117+
118+ // Check entitlement activations
119+ if ( current . entitlements && last . entitlements ) {
120+ for ( const key of Object . keys ( current . entitlements ) ) {
121+ const currentActivated = current . entitlements [ key ] && current . entitlements [ key ] . activated ;
122+ const lastActivated = last . entitlements [ key ] && last . entitlements [ key ] . activated ;
123+ if ( currentActivated !== lastActivated ) {
124+ return true ;
125+ }
126+ }
127+ }
128+
129+ return false ;
130+ }
131+
132+ // Export functions
133+ exports . validTillExpired = validTillExpired ;
134+ exports . haveEntitlementsChanged = haveEntitlementsChanged ;
135+ } ) ;
0 commit comments