fix: some details on supported versions (#5284)

* Ignore patches

* Fix null versions find

* Fix cloud request

* Fix inverted date comparison to trigger action sheet
This commit is contained in:
Diego Mello 2023-10-19 16:57:13 -03:00 committed by GitHub
parent 18b77ab4d6
commit f1b7cfe510
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 35 additions and 8 deletions

View File

@ -120,6 +120,27 @@ jest.useFakeTimers('modern');
jest.setSystemTime(new Date(TODAY));
describe('checkSupportedVersions', () => {
describe('General', () => {
test('ignore the patch and compare as minor', () => {
expect(
checkSupportedVersions({
supportedVersions: MOCK,
serverVersion: '1.5.1'
})
).toMatchObject({
status: 'supported'
});
expect(
checkSupportedVersions({
supportedVersions: MOCK,
serverVersion: '1.2.1'
})
).toMatchObject({
status: 'expired'
});
});
});
describe('Built-in supported versions', () => {
test('no supported versions', () => {
expect(checkSupportedVersions({ supportedVersions: undefined, serverVersion: '1.5.0' })).toMatchObject({

View File

@ -1,5 +1,6 @@
import moment from 'moment';
import coerce from 'semver/functions/coerce';
import satisfies from 'semver/functions/satisfies';
import { ISupportedVersionsData, TSVDictionary, TSVMessage, TSVStatus } from '../../definitions';
import builtInSupportedVersions from '../../../app-supportedversions.json';
@ -40,6 +41,7 @@ export const checkSupportedVersions = function ({
i18n?: TSVDictionary;
expiration?: string;
} {
const serverVersionTilde = `~${serverVersion.split('.').slice(0, 2).join('.')}`;
let sv: ISupportedVersionsData;
if (!supportedVersions || supportedVersions.timestamp < builtInSupportedVersions.timestamp) {
// Built-in supported versions
@ -49,7 +51,7 @@ export const checkSupportedVersions = function ({
sv = supportedVersions;
}
const versionInfo = sv.versions.find(({ version }) => coerce(version)?.version === serverVersion);
const versionInfo = sv.versions.find(({ version }) => satisfies(coerce(version)?.version ?? '', serverVersionTilde));
if (versionInfo && new Date(versionInfo.expiration) >= new Date()) {
const messages = versionInfo?.messages || sv?.messages;
const message = getMessage({ messages, expiration: versionInfo.expiration });
@ -62,7 +64,7 @@ export const checkSupportedVersions = function ({
}
// Exceptions
const exception = sv.exceptions?.versions.find(({ version }) => coerce(version)?.version === serverVersion);
const exception = sv.exceptions?.versions?.find(({ version }) => satisfies(coerce(version)?.version ?? '', serverVersionTilde));
const messages = exception?.messages || sv.exceptions?.messages || versionInfo?.messages || sv.messages;
const message = getMessage({ messages, expiration: exception?.expiration });
const status = getStatus({ expiration: exception?.expiration, message });

View File

@ -57,7 +57,7 @@ export async function getServerInfo(server: string): Promise<TServerInfoResult>
// if backend doesn't have supported versions or JWT is invalid, request from cloud
if (!supportedVersions) {
const cloudInfo = await getCloudInfo();
const cloudInfo = await getCloudInfo(server);
// Makes use of signed JWT to get supported versions
const supportedVersionsCloud = verifyJWT(cloudInfo?.signed);
@ -96,9 +96,14 @@ export async function getServerInfo(server: string): Promise<TServerInfoResult>
};
}
export const getCloudInfo = async (): Promise<TCloudInfo | null> => {
const uniqueId = store.getState().settings.uniqueID as string;
const domain = store.getState().server.server;
const getUniqueId = async (server: string): Promise<string> => {
const response = await fetch(`${server}/api/v1/settings.public?query={"_id": "uniqueID"}`);
const result = await response.json();
return result?.settings?.[0]?.value;
};
export const getCloudInfo = async (domain: string): Promise<TCloudInfo | null> => {
const uniqueId = await getUniqueId(domain);
const response = await getSupportedVersionsCloud(uniqueId, domain);
return response.json() as unknown as TCloudInfo;
};

View File

@ -56,8 +56,7 @@ const showSupportedVersionsWarning = function* showSupportedVersionsWarning(serv
}
const serverRecord = yield getServerById(server);
const isMasterDetail = yield select(state => state.app.isMasterDetail);
if (!serverRecord || moment(serverRecord?.supportedVersionsWarningAt).diff(new Date(), 'hours') <= 12) {
if (!serverRecord || moment(new Date()).diff(serverRecord?.supportedVersionsWarningAt, 'hours') <= 12) {
return;
}