Skip to content
Snippets Groups Projects
Unverified Commit 94c6e897 authored by Guilherme Gazzo's avatar Guilherme Gazzo Committed by GitHub
Browse files

chore: replace endpoint to licenses.info (#30697)

parent 832df7f4
No related branches found
No related tags found
No related merge requests found
......@@ -3,8 +3,8 @@ import { useEndpoint, usePermission } from '@rocket.chat/ui-contexts';
import type { UseQueryResult } from '@tanstack/react-query';
import { useQuery } from '@tanstack/react-query';
export const useLicense = (): UseQueryResult<OperationResult<'GET', '/v1/licenses.get'>> => {
const getLicenses = useEndpoint('GET', '/v1/licenses.get');
export const useLicense = (): UseQueryResult<OperationResult<'GET', '/v1/licenses.info'>> => {
const getLicenses = useEndpoint('GET', '/v1/licenses.info');
const canViewLicense = usePermission('view-privileged-setting');
return useQuery(
......@@ -13,7 +13,7 @@ export const useLicense = (): UseQueryResult<OperationResult<'GET', '/v1/license
if (!canViewLicense) {
throw new Error('unauthorized api call');
}
return getLicenses();
return getLicenses({});
},
{
staleTime: Infinity,
......
......@@ -6,13 +6,12 @@ import { useAdministrationItems } from './useAdministrationItems';
it('should not show upgrade item if has license and not have trial', async () => {
const { result, waitFor } = renderHook(() => useAdministrationItems(), {
wrapper: mockAppRoot()
.withEndpoint('GET', '/v1/licenses.get', () => ({
licenses: [
{
modules: ['testModule'],
meta: { trial: false },
} as any,
],
.withEndpoint('GET', '/v1/licenses.info', () => ({
license: {
// @ts-expect-error this is a mock
license: { activeModules: ['testModule'] },
trial: false,
},
}))
.withEndpoint('GET', '/v1/cloud.registrationStatus', () => ({
registrationStatus: {
......@@ -25,19 +24,21 @@ it('should not show upgrade item if has license and not have trial', async () =>
});
await waitFor(() => !!(result.all.length > 1));
expect(result.current.length).toEqual(1);
expect(result.current[0]).toEqual(
expect.objectContaining({
id: 'workspace',
}),
);
});
it('should return an upgrade item if not have license or if have a trial', async () => {
const { result, waitFor } = renderHook(() => useAdministrationItems(), {
wrapper: mockAppRoot()
.withEndpoint('GET', '/v1/licenses.get', () => ({
licenses: [
{
modules: [],
} as any,
],
.withEndpoint('GET', '/v1/licenses.info', () => ({
// @ts-expect-error this is a mock
license: {},
}))
.withEndpoint('GET', '/v1/cloud.registrationStatus', () => ({
registrationStatus: {
......@@ -62,12 +63,9 @@ it('should return an upgrade item if not have license or if have a trial', async
it('should return omnichannel item if has `view-livechat-manager` permission ', async () => {
const { result, waitFor } = renderHook(() => useAdministrationItems(), {
wrapper: mockAppRoot()
.withEndpoint('GET', '/v1/licenses.get', () => ({
licenses: [
{
modules: [],
} as any,
],
.withEndpoint('GET', '/v1/licenses.info', () => ({
// @ts-expect-error this is a mock
license: {},
}))
.withEndpoint('GET', '/v1/cloud.registrationStatus', () => ({
registrationStatus: {
......@@ -90,12 +88,9 @@ it('should return omnichannel item if has `view-livechat-manager` permission ',
it('should show administration item if has at least one admin permission', async () => {
const { result, waitFor } = renderHook(() => useAdministrationItems(), {
wrapper: mockAppRoot()
.withEndpoint('GET', '/v1/licenses.get', () => ({
licenses: [
{
modules: [],
} as any,
],
.withEndpoint('GET', '/v1/licenses.info', () => ({
// @ts-expect-error this is a mock
license: {},
}))
.withEndpoint('GET', '/v1/cloud.registrationStatus', () => ({
registrationStatus: {
......
......@@ -19,15 +19,7 @@ const LicenseCard = (): ReactElement => {
const isAirGapped = true;
const { data, isError, isLoading } = useLicense();
const { modules = [] } = isLoading || isError || !data?.licenses?.length ? {} : data?.licenses[0];
const hasEngagement = modules.includes('engagement-dashboard');
const hasOmnichannel = modules.includes('livechat-enterprise');
const hasAuditing = modules.includes('auditing');
const hasCannedResponses = modules.includes('canned-responses');
const hasReadReceipts = modules.includes('message-read-receipt');
const request = useLicense();
const handleApplyLicense = useMutableCallback(() =>
setModal(
......@@ -41,6 +33,37 @@ const LicenseCard = (): ReactElement => {
),
);
if (request.isLoading || request.isError) {
return (
<Card data-qa-id='license-card'>
<CardTitle>{t('License')}</CardTitle>
<CardBody>
<CardCol>
<CardColSection>
<PlanTag />
</CardColSection>
<CardColSection>
<CardColTitle>{t('Features')}</CardColTitle>
<Skeleton width='40x' />
<Skeleton width='40x' />
<Skeleton width='40x' />
<Skeleton width='40x' />
</CardColSection>
</CardCol>
</CardBody>
</Card>
);
}
const { activeModules } = request.data.license;
const hasEngagement = activeModules.includes('engagement-dashboard');
const hasOmnichannel = activeModules.includes('livechat-enterprise');
const hasAuditing = activeModules.includes('auditing');
const hasCannedResponses = activeModules.includes('canned-responses');
const hasReadReceipts = activeModules.includes('message-read-receipt');
return (
<Card data-qa-id='license-card'>
<CardTitle>{t('License')}</CardTitle>
......@@ -51,22 +74,12 @@ const LicenseCard = (): ReactElement => {
</CardColSection>
<CardColSection>
<CardColTitle>{t('Features')}</CardColTitle>
{isLoading ? (
<>
<Skeleton width='40x' />
<Skeleton width='40x' />
<Skeleton width='40x' />
<Skeleton width='40x' />
</>
) : (
<>
<Feature label={t('Omnichannel')} enabled={hasOmnichannel} />
<Feature label={t('Auditing')} enabled={hasAuditing} />
<Feature label={t('Canned_Responses')} enabled={hasCannedResponses} />
<Feature label={t('Engagement_Dashboard')} enabled={hasEngagement} />
<Feature label={t('Read_Receipts')} enabled={hasReadReceipts} />
</>
)}
<Feature label={t('Omnichannel')} enabled={hasOmnichannel} />
<Feature label={t('Auditing')} enabled={hasAuditing} />
<Feature label={t('Canned_Responses')} enabled={hasCannedResponses} />
<Feature label={t('Engagement_Dashboard')} enabled={hasEngagement} />
<Feature label={t('Read_Receipts')} enabled={hasReadReceipts} />
</CardColSection>
</CardCol>
</CardBody>
......
import type { ILicenseV2, ILicenseV3 } from '@rocket.chat/license';
import { useSetting } from '@rocket.chat/ui-contexts';
import { format } from 'date-fns';
......@@ -14,14 +13,11 @@ export const useUpgradeTabParams = (): { tabType: UpgradeTabVariant | false; tri
const { data: registrationStatusData, isSuccess: isSuccessRegistrationStatus } = useRegistrationStatus();
const registered = registrationStatusData?.registrationStatus?.workspaceRegistered ?? false;
const hasValidLicense = licensesData?.licenses.some((license) => license.modules.length > 0) ?? false;
const hasValidLicense = Boolean(licensesData?.license?.license ?? false);
const hadExpiredTrials = cloudWorkspaceHadTrial ?? false;
const licenses = (licensesData?.licenses || []) as (Partial<ILicenseV2 & ILicenseV3> & { modules: string[] })[];
const trialLicense = licenses.find(({ meta, information }) => information?.trial ?? meta?.trial);
const isTrial = Boolean(trialLicense);
const trialEndDateStr = trialLicense?.information?.visualExpiration || trialLicense?.meta?.trialEnd || trialLicense?.cloudMeta?.trialEnd;
const isTrial = Boolean(licensesData?.license?.trial);
const trialEndDateStr = licensesData?.license?.license?.information?.visualExpiration;
const trialEndDate = trialEndDateStr ? format(new Date(trialEndDateStr), 'yyyy-MM-dd') : undefined;
const upgradeTabType = getUpgradeTabType({
......
......@@ -5,12 +5,15 @@ import { check } from 'meteor/check';
import { API } from '../../../app/api/server/api';
import { hasPermissionAsync } from '../../../app/authorization/server/functions/hasPermission';
import { apiDeprecationLogger } from '../../../app/lib/server/lib/deprecationWarningLogger';
API.v1.addRoute(
'licenses.get',
{ authRequired: true },
{
async get() {
apiDeprecationLogger.endpoint(this.request.route, '7.0.0', this.response, ' Use licenses.info instead.');
if (!(await hasPermissionAsync(this.userId, 'view-privileged-setting'))) {
return API.v1.unauthorized();
}
......@@ -31,9 +34,9 @@ API.v1.addRoute(
const unrestrictedAccess = await hasPermissionAsync(this.userId, 'view-privileged-setting');
const loadCurrentValues = unrestrictedAccess && Boolean(this.queryParams.loadValues);
const data = await License.getInfo({ limits: unrestrictedAccess, license: unrestrictedAccess, currentValues: loadCurrentValues });
const license = await License.getInfo({ limits: unrestrictedAccess, license: unrestrictedAccess, currentValues: loadCurrentValues });
return API.v1.success({ data });
return API.v1.success({ license });
},
},
);
......@@ -81,8 +84,9 @@ API.v1.addRoute(
{ authOrAnonRequired: true },
{
get() {
const isEnterpriseEdtion = License.hasValidLicense();
return API.v1.success({ isEnterprise: isEnterpriseEdtion });
apiDeprecationLogger.endpoint(this.request.route, '7.0.0', this.response, ' Use licenses.info instead.');
const isEnterpriseEdition = License.hasValidLicense();
return API.v1.success({ isEnterprise: isEnterpriseEdition });
},
},
);
......@@ -126,9 +126,9 @@ describe('licenses', function () {
.expect(200)
.expect((res) => {
expect(res.body).to.have.property('success', true);
expect(res.body).to.have.property('data').and.to.be.an('object');
expect(res.body.data).to.not.have.property('license');
expect(res.body.data).to.have.property('tags').and.to.be.an('array');
expect(res.body).to.have.property('license').and.to.be.an('object');
expect(res.body.license).to.not.have.property('license');
expect(res.body.license).to.have.property('tags').and.to.be.an('array');
})
.end(done);
});
......@@ -140,11 +140,11 @@ describe('licenses', function () {
.expect(200)
.expect((res) => {
expect(res.body).to.have.property('success', true);
expect(res.body).to.have.property('data').and.to.be.an('object');
expect(res.body).to.have.property('license').and.to.be.an('object');
if (process.env.IS_EE) {
expect(res.body.data).to.have.property('license').and.to.be.an('object');
expect(res.body.license).to.have.property('license').and.to.be.an('object');
}
expect(res.body.data).to.have.property('tags').and.to.be.an('array');
expect(res.body.license).to.have.property('tags').and.to.be.an('array');
})
.end(done);
......
......@@ -45,7 +45,7 @@ export type LicensesEndpoints = {
};
'/v1/licenses.info': {
GET: (params: licensesInfoProps) => {
data: LicenseInfo;
license: LicenseInfo;
};
};
'/v1/licenses.add': {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment