2023-10-19 13:38:57 +00:00
|
|
|
import moment from 'moment';
|
|
|
|
import coerce from 'semver/functions/coerce';
|
2023-10-19 19:57:13 +00:00
|
|
|
import satisfies from 'semver/functions/satisfies';
|
2023-10-19 13:38:57 +00:00
|
|
|
|
|
|
|
import { ISupportedVersionsData, TSVDictionary, TSVMessage, TSVStatus } from '../../definitions';
|
|
|
|
import builtInSupportedVersions from '../../../app-supportedversions.json';
|
|
|
|
|
|
|
|
export const getMessage = ({
|
|
|
|
messages,
|
|
|
|
expiration
|
|
|
|
}: {
|
|
|
|
messages?: TSVMessage[];
|
|
|
|
expiration?: string;
|
|
|
|
}): TSVMessage | undefined => {
|
|
|
|
if (!messages?.length || !expiration || moment(expiration).diff(new Date(), 'days') < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const sortedMessages = messages.sort((a, b) => a.remainingDays - b.remainingDays);
|
2023-10-25 18:19:40 +00:00
|
|
|
return sortedMessages.find(({ remainingDays }) => moment(expiration).diff(new Date(), 'hours') <= remainingDays * 24);
|
2023-10-19 13:38:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const getStatus = ({ expiration, message }: { expiration?: string; message?: TSVMessage }): TSVStatus => {
|
|
|
|
if (!(expiration && new Date(expiration) >= new Date())) {
|
|
|
|
return 'expired';
|
|
|
|
}
|
|
|
|
if (message) {
|
|
|
|
return 'warn';
|
|
|
|
}
|
|
|
|
return 'supported';
|
|
|
|
};
|
|
|
|
|
|
|
|
export const checkSupportedVersions = function ({
|
|
|
|
supportedVersions,
|
|
|
|
serverVersion
|
|
|
|
}: {
|
|
|
|
supportedVersions?: ISupportedVersionsData;
|
|
|
|
serverVersion: string;
|
|
|
|
}): {
|
|
|
|
status: TSVStatus;
|
|
|
|
message?: TSVMessage;
|
|
|
|
i18n?: TSVDictionary;
|
|
|
|
expiration?: string;
|
|
|
|
} {
|
2023-10-19 19:57:13 +00:00
|
|
|
const serverVersionTilde = `~${serverVersion.split('.').slice(0, 2).join('.')}`;
|
2023-10-19 13:38:57 +00:00
|
|
|
let sv: ISupportedVersionsData;
|
|
|
|
if (!supportedVersions || supportedVersions.timestamp < builtInSupportedVersions.timestamp) {
|
|
|
|
// Built-in supported versions
|
|
|
|
sv = builtInSupportedVersions as ISupportedVersionsData;
|
|
|
|
} else {
|
|
|
|
// Backend/Cloud
|
|
|
|
sv = supportedVersions;
|
|
|
|
}
|
|
|
|
|
2023-10-19 19:57:13 +00:00
|
|
|
const versionInfo = sv.versions.find(({ version }) => satisfies(coerce(version)?.version ?? '', serverVersionTilde));
|
|
|
|
const exception = sv.exceptions?.versions?.find(({ version }) => satisfies(coerce(version)?.version ?? '', serverVersionTilde));
|
2023-11-27 18:28:19 +00:00
|
|
|
const messages =
|
|
|
|
exception?.messages || (exception ? sv.exceptions?.messages : undefined) || versionInfo?.messages || sv.messages;
|
|
|
|
const expiration = exception?.expiration || versionInfo?.expiration;
|
|
|
|
const message = getMessage({ messages, expiration });
|
|
|
|
const status = getStatus({ message, expiration });
|
2023-10-19 13:38:57 +00:00
|
|
|
|
|
|
|
// TODO: enforcement start date is temp only. Remove after a few releases.
|
|
|
|
if (status === 'expired' && sv?.enforcementStartDate && new Date(sv.enforcementStartDate) > new Date()) {
|
|
|
|
const enforcementMessage = getMessage({
|
|
|
|
messages,
|
|
|
|
expiration: sv.enforcementStartDate
|
|
|
|
});
|
|
|
|
return {
|
|
|
|
status: 'warn',
|
|
|
|
message: enforcementMessage,
|
|
|
|
i18n: enforcementMessage ? sv?.i18n : undefined,
|
|
|
|
expiration: sv.enforcementStartDate
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
status,
|
|
|
|
message,
|
|
|
|
i18n: message ? sv?.i18n : undefined,
|
2023-12-26 13:58:19 +00:00
|
|
|
expiration
|
2023-10-19 13:38:57 +00:00
|
|
|
};
|
|
|
|
};
|