Skip to content

Commit

Permalink
chore: replace endpoint to licenses.info (#30697)
Browse files Browse the repository at this point in the history
  • Loading branch information
ggazzo authored Oct 20, 2023
1 parent 832df7f commit 94c6e89
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 72 deletions.
6 changes: 3 additions & 3 deletions apps/meteor/client/hooks/useLicense.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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: {
Expand All @@ -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: {
Expand All @@ -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: {
Expand Down
63 changes: 38 additions & 25 deletions apps/meteor/client/views/admin/info/LicenseCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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>
Expand All @@ -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>
Expand Down
10 changes: 3 additions & 7 deletions apps/meteor/client/views/hooks/useUpgradeTabParams.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { ILicenseV2, ILicenseV3 } from '@rocket.chat/license';
import { useSetting } from '@rocket.chat/ui-contexts';
import { format } from 'date-fns';

Expand All @@ -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({
Expand Down
12 changes: 8 additions & 4 deletions apps/meteor/ee/server/api/licenses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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 });
},
},
);
Expand Down Expand Up @@ -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 });
},
},
);
12 changes: 6 additions & 6 deletions apps/meteor/tests/end-to-end/api/20-licenses.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion packages/rest-typings/src/v1/licenses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export type LicensesEndpoints = {
};
'/v1/licenses.info': {
GET: (params: licensesInfoProps) => {
data: LicenseInfo;
license: LicenseInfo;
};
};
'/v1/licenses.add': {
Expand Down

0 comments on commit 94c6e89

Please sign in to comment.