@@ -20,73 +20,110 @@ export class UmbSysinfoRepository extends UmbRepositoryBase {
20
20
return data ;
21
21
}
22
22
23
- async serverUpgradeCheck ( ) : Promise < UmbServerUpgradeCheck | null > {
23
+ /**
24
+ * Check if the server has an upgrade available and return the result.
25
+ * If the server has an upgrade available, the result will be stored in local storage.
26
+ * If the server does not have an upgrade available, the result will be stored in local storage as well.
27
+ * @param {string } currentVersion The current version of the server.
28
+ * @returns {Promise<UmbServerUpgradeCheck | null> } The server upgrade check result or null if the check is not allowed or if the check failed.
29
+ */
30
+ async serverUpgradeCheck ( currentVersion : string ) : Promise < UmbServerUpgradeCheck | null > {
24
31
// Check if we are allowed to check again
25
32
const appContext = await this . getContext ( UMB_APP_CONTEXT ) ;
26
33
const versionCheckPeriod = await this . observe ( appContext . getServerConnection ( ) . versionCheckPeriod ) . asPromise ( ) ;
27
34
28
35
if ( versionCheckPeriod <= 0 ) {
36
+ // We do not need to check the server for an upgrade
29
37
return null ;
30
38
}
31
39
32
- let shouldCheck = true ;
33
-
34
- const lastCheck = localStorage . getItem ( 'umb:lastUpgradeCheck' ) ;
35
- const now = new Date ( ) ;
36
- if ( lastCheck ) {
37
- const lastCheckDate = new Date ( lastCheck ) ;
38
- const diff = now . getTime ( ) - lastCheckDate . getTime ( ) ;
39
- const diffDays = diff / ( 1000 * 3600 * 24 ) ;
40
-
41
- if ( diffDays < versionCheckPeriod ) {
42
- shouldCheck = false ;
40
+ const lastUpgradeCheck = this . #getStoredServerUpgradeCheck( currentVersion ) ;
41
+
42
+ // If we have a stored check, then return it if it is still valid
43
+ if ( lastUpgradeCheck !== null ) {
44
+ // If we have a stored check, then check if we should check again based on the period
45
+ if ( lastUpgradeCheck . createdAt ) {
46
+ const lastCheckDate = new Date ( lastUpgradeCheck . createdAt ) ;
47
+ const diff = new Date ( ) . getTime ( ) - lastCheckDate . getTime ( ) ;
48
+ const diffDays = diff / ( 1000 * 3600 * 24 ) ;
49
+
50
+ if ( diffDays < versionCheckPeriod ) {
51
+ // If we should not check, then return what we have stored if it is still valid
52
+ if ( lastUpgradeCheck . type . toLowerCase ( ) !== 'none' ) {
53
+ return lastUpgradeCheck ;
54
+ }
55
+ return null ; // no upgrade available
56
+ }
43
57
}
58
+ }
44
59
45
- // If we should not check, then return what we have stored if it is still valid
46
- if ( ! shouldCheck ) {
47
- return this . #getStoredServerUpgradeCheck( lastCheckDate ) ;
48
- }
60
+ // Check the server for an upgrade because we have no stored check or the stored check is invalid
61
+ return this . #fetchServerUpgradeCheck( versionCheckPeriod , currentVersion ) ;
62
+ }
63
+
64
+ /**
65
+ * Get the stored server upgrade check if it is still valid, otherwise return null and remove the stored check.
66
+ * @param {string } currentVersion The current version of the server.
67
+ * @returns {UmbServerUpgradeCheck | null } The stored server upgrade check or null if it is not valid.
68
+ */
69
+ #getStoredServerUpgradeCheck( currentVersion : string ) : UmbServerUpgradeCheck | null {
70
+ const storedCheck = localStorage . getItem ( 'umb:serverUpgradeCheck' ) ;
71
+ if ( ! storedCheck ) {
72
+ return null ;
49
73
}
50
74
51
- if ( ! shouldCheck ) {
75
+ const upgradeCheck : UmbServerUpgradeCheck = JSON . parse ( storedCheck ) ;
76
+
77
+ // Check that the stored check is for the same version
78
+ if ( upgradeCheck . version !== currentVersion ) {
79
+ localStorage . removeItem ( 'umb:serverUpgradeCheck' ) ;
52
80
return null ;
53
81
}
54
82
55
- // Check the server
83
+ // Check that the stored check is not older than the last check
84
+ if ( upgradeCheck . createdAt ) {
85
+ const createdAt = new Date ( upgradeCheck . createdAt ) ;
86
+ const expiresAt = new Date ( upgradeCheck . expires ) ;
87
+ if ( expiresAt . getTime ( ) <= createdAt . getTime ( ) ) {
88
+ localStorage . removeItem ( 'umb:serverUpgradeCheck' ) ;
89
+ return null ;
90
+ }
91
+ }
92
+
93
+ return upgradeCheck ;
94
+ }
95
+
96
+ /**
97
+ * Fetch the server upgrade check from the server and store the result in local storage.
98
+ * @param {number } versionCheckPeriod A period in days to wait before checking the server again.
99
+ * @param {string } currentVersion The current version of the server.
100
+ * @returns {Promise<UmbServerUpgradeCheck | null> } The server upgrade check result or null if the check failed.
101
+ */
102
+ async #fetchServerUpgradeCheck(
103
+ versionCheckPeriod : number ,
104
+ currentVersion : string ,
105
+ ) : Promise < UmbServerUpgradeCheck | null > {
106
+ // Check the server for an upgrade because we have no stored check or the stored check is invalid
56
107
const { data } = await tryExecute ( ServerService . getServerUpgradeCheck ( ) ) ;
57
108
58
109
if ( data ) {
59
110
// Save the last check date including the data received
60
111
const expiresAt = new Date ( ) ;
61
112
expiresAt . setDate ( expiresAt . getDate ( ) + versionCheckPeriod ) ;
62
- const upgradeCheck = { ...data , expires : expiresAt . toISOString ( ) } satisfies UmbServerUpgradeCheck ;
113
+ const upgradeCheck = {
114
+ ...data ,
115
+ expires : expiresAt . toISOString ( ) ,
116
+ version : currentVersion ,
117
+ createdAt : new Date ( ) . toISOString ( ) ,
118
+ } satisfies UmbServerUpgradeCheck ;
63
119
localStorage . setItem ( 'umb:serverUpgradeCheck' , JSON . stringify ( upgradeCheck ) ) ;
64
- localStorage . setItem ( 'umb:lastUpgradeCheck' , now . toISOString ( ) ) ;
65
120
66
121
// Only return if we have a valid type
67
122
if ( data . type . toLowerCase ( ) !== 'none' ) {
68
123
return upgradeCheck ;
69
124
}
70
125
}
71
126
72
- return null ;
73
- }
74
-
75
- #getStoredServerUpgradeCheck( lastCheck : Date ) : UmbServerUpgradeCheck | null {
76
- const storedCheck = localStorage . getItem ( 'umb:serverUpgradeCheck' ) ;
77
- if ( storedCheck ) {
78
- const upgradeCheck : UmbServerUpgradeCheck = JSON . parse ( storedCheck ) ;
79
- // Check that the stored check is not older than the last check
80
- const expiresAt = new Date ( upgradeCheck . expires ) ;
81
- if ( expiresAt . getTime ( ) > lastCheck . getTime ( ) ) {
82
- if ( upgradeCheck . type . toLowerCase ( ) !== 'none' ) {
83
- return upgradeCheck ;
84
- }
85
- } else {
86
- localStorage . removeItem ( 'umb:serverUpgradeCheck' ) ;
87
- }
88
- }
89
-
90
- return null ;
127
+ return null ; // no upgrade available
91
128
}
92
129
}
0 commit comments