Merge pull request #1722 from automatisch/AUT-832
refactor: rewrite useUserTrial with RQ
This commit is contained in:
@@ -1,17 +0,0 @@
|
|||||||
import appConfig from '../../config/app.js';
|
|
||||||
|
|
||||||
const getTrialStatus = async (_parent, _params, context) => {
|
|
||||||
if (!appConfig.isCloud) return;
|
|
||||||
|
|
||||||
const inTrial = await context.currentUser.inTrial();
|
|
||||||
const hasActiveSubscription =
|
|
||||||
await context.currentUser.hasActiveSubscription();
|
|
||||||
|
|
||||||
if (!inTrial && hasActiveSubscription) return;
|
|
||||||
|
|
||||||
return {
|
|
||||||
expireAt: context.currentUser.trialExpiryDate,
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
export default getTrialStatus;
|
|
@@ -1,100 +0,0 @@
|
|||||||
import { vi, describe, it, expect, beforeEach } from 'vitest';
|
|
||||||
import request from 'supertest';
|
|
||||||
import app from '../../app';
|
|
||||||
import User from '../../models/user';
|
|
||||||
import { createUser } from '../../../test/factories/user';
|
|
||||||
import createAuthTokenByUserId from '../../helpers/create-auth-token-by-user-id';
|
|
||||||
import { DateTime } from 'luxon';
|
|
||||||
import appConfig from '../../config/app';
|
|
||||||
|
|
||||||
describe('graphQL getTrialStatus query', () => {
|
|
||||||
const query = `
|
|
||||||
query GetTrialStatus {
|
|
||||||
getTrialStatus {
|
|
||||||
expireAt
|
|
||||||
}
|
|
||||||
}
|
|
||||||
`;
|
|
||||||
|
|
||||||
let user, userToken;
|
|
||||||
|
|
||||||
beforeEach(async () => {
|
|
||||||
const trialExpiryDate = DateTime.now().plus({ days: 30 }).toISODate();
|
|
||||||
|
|
||||||
user = await createUser({ trialExpiryDate });
|
|
||||||
userToken = createAuthTokenByUserId(user.id);
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('and with cloud flag disabled', () => {
|
|
||||||
beforeEach(async () => {
|
|
||||||
vi.spyOn(appConfig, 'isCloud', 'get').mockReturnValue(false);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should return null', async () => {
|
|
||||||
const response = await request(app)
|
|
||||||
.post('/graphql')
|
|
||||||
.set('Authorization', userToken)
|
|
||||||
.send({ query })
|
|
||||||
.expect(200);
|
|
||||||
|
|
||||||
const expectedResponsePayload = {
|
|
||||||
data: { getTrialStatus: null },
|
|
||||||
};
|
|
||||||
|
|
||||||
expect(response.body).toEqual(expectedResponsePayload);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('and with cloud flag enabled', () => {
|
|
||||||
beforeEach(async () => {
|
|
||||||
vi.spyOn(appConfig, 'isCloud', 'get').mockReturnValue(true);
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('and not in trial and has active subscription', () => {
|
|
||||||
beforeEach(async () => {
|
|
||||||
vi.spyOn(User.prototype, 'inTrial').mockResolvedValue(false);
|
|
||||||
vi.spyOn(User.prototype, 'hasActiveSubscription').mockResolvedValue(
|
|
||||||
true
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should return null', async () => {
|
|
||||||
const response = await request(app)
|
|
||||||
.post('/graphql')
|
|
||||||
.set('Authorization', userToken)
|
|
||||||
.send({ query })
|
|
||||||
.expect(200);
|
|
||||||
|
|
||||||
const expectedResponsePayload = {
|
|
||||||
data: { getTrialStatus: null },
|
|
||||||
};
|
|
||||||
|
|
||||||
expect(response.body).toEqual(expectedResponsePayload);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('and in trial period', () => {
|
|
||||||
beforeEach(async () => {
|
|
||||||
vi.spyOn(User.prototype, 'inTrial').mockResolvedValue(true);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should return null', async () => {
|
|
||||||
const response = await request(app)
|
|
||||||
.post('/graphql')
|
|
||||||
.set('Authorization', userToken)
|
|
||||||
.send({ query })
|
|
||||||
.expect(200);
|
|
||||||
|
|
||||||
const expectedResponsePayload = {
|
|
||||||
data: {
|
|
||||||
getTrialStatus: {
|
|
||||||
expireAt: new Date(user.trialExpiryDate).getTime().toString(),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
expect(response.body).toEqual(expectedResponsePayload);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
@@ -12,7 +12,6 @@ import getNotifications from './queries/get-notifications.js';
|
|||||||
import getPermissionCatalog from './queries/get-permission-catalog.ee.js';
|
import getPermissionCatalog from './queries/get-permission-catalog.ee.js';
|
||||||
import getSamlAuthProviderRoleMappings from './queries/get-saml-auth-provider-role-mappings.ee.js';
|
import getSamlAuthProviderRoleMappings from './queries/get-saml-auth-provider-role-mappings.ee.js';
|
||||||
import getStepWithTestExecutions from './queries/get-step-with-test-executions.js';
|
import getStepWithTestExecutions from './queries/get-step-with-test-executions.js';
|
||||||
import getTrialStatus from './queries/get-trial-status.ee.js';
|
|
||||||
import getUsers from './queries/get-users.js';
|
import getUsers from './queries/get-users.js';
|
||||||
import listSamlAuthProviders from './queries/list-saml-auth-providers.ee.js';
|
import listSamlAuthProviders from './queries/list-saml-auth-providers.ee.js';
|
||||||
import testConnection from './queries/test-connection.js';
|
import testConnection from './queries/test-connection.js';
|
||||||
@@ -32,7 +31,6 @@ const queryResolvers = {
|
|||||||
getPermissionCatalog,
|
getPermissionCatalog,
|
||||||
getSamlAuthProviderRoleMappings,
|
getSamlAuthProviderRoleMappings,
|
||||||
getStepWithTestExecutions,
|
getStepWithTestExecutions,
|
||||||
getTrialStatus,
|
|
||||||
getUsers,
|
getUsers,
|
||||||
listSamlAuthProviders,
|
listSamlAuthProviders,
|
||||||
testConnection,
|
testConnection,
|
||||||
|
@@ -28,7 +28,6 @@ type Query {
|
|||||||
getPermissionCatalog: PermissionCatalog
|
getPermissionCatalog: PermissionCatalog
|
||||||
getNotifications: [Notification]
|
getNotifications: [Notification]
|
||||||
getSamlAuthProviderRoleMappings(id: String!): [SamlAuthProvidersRoleMapping]
|
getSamlAuthProviderRoleMappings(id: String!): [SamlAuthProvidersRoleMapping]
|
||||||
getTrialStatus: GetTrialStatus
|
|
||||||
getUsers(limit: Int!, offset: Int!): UserConnection
|
getUsers(limit: Int!, offset: Int!): UserConnection
|
||||||
listSamlAuthProviders: [ListSamlAuthProvider]
|
listSamlAuthProviders: [ListSamlAuthProvider]
|
||||||
}
|
}
|
||||||
@@ -601,10 +600,6 @@ type License {
|
|||||||
verified: Boolean
|
verified: Boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
type GetTrialStatus {
|
|
||||||
expireAt: String
|
|
||||||
}
|
|
||||||
|
|
||||||
type GetBillingAndUsage {
|
type GetBillingAndUsage {
|
||||||
subscription: Subscription
|
subscription: Subscription
|
||||||
usage: Usage
|
usage: Usage
|
||||||
|
@@ -1,14 +1,18 @@
|
|||||||
import * as React from 'react';
|
import * as React from 'react';
|
||||||
import Alert from '@mui/material/Alert';
|
import Alert from '@mui/material/Alert';
|
||||||
import Typography from '@mui/material/Typography';
|
import Typography from '@mui/material/Typography';
|
||||||
|
|
||||||
import * as URLS from 'config/urls';
|
import * as URLS from 'config/urls';
|
||||||
import { generateInternalLink } from 'helpers/translationValues';
|
import { generateInternalLink } from 'helpers/translationValues';
|
||||||
import useTrialStatus from 'hooks/useTrialStatus.ee';
|
import useUserTrial from 'hooks/useUserTrial.ee';
|
||||||
import useFormatMessage from 'hooks/useFormatMessage';
|
import useFormatMessage from 'hooks/useFormatMessage';
|
||||||
|
|
||||||
export default function TrialOverAlert() {
|
export default function TrialOverAlert() {
|
||||||
const formatMessage = useFormatMessage();
|
const formatMessage = useFormatMessage();
|
||||||
const trialStatus = useTrialStatus();
|
const trialStatus = useUserTrial();
|
||||||
|
|
||||||
if (!trialStatus || !trialStatus.over) return <React.Fragment />;
|
if (!trialStatus || !trialStatus.over) return <React.Fragment />;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Alert
|
<Alert
|
||||||
severity="error"
|
severity="error"
|
||||||
|
@@ -1,12 +1,17 @@
|
|||||||
import * as React from 'react';
|
import * as React from 'react';
|
||||||
import { Link } from 'react-router-dom';
|
import { Link } from 'react-router-dom';
|
||||||
|
|
||||||
import { Chip } from './style.ee';
|
import { Chip } from './style.ee';
|
||||||
import * as URLS from 'config/urls';
|
import * as URLS from 'config/urls';
|
||||||
import useTrialStatus from 'hooks/useTrialStatus.ee';
|
import useUserTrial from 'hooks/useUserTrial.ee';
|
||||||
|
|
||||||
export default function TrialStatusBadge() {
|
export default function TrialStatusBadge() {
|
||||||
const data = useTrialStatus();
|
const data = useUserTrial();
|
||||||
|
|
||||||
if (!data) return <React.Fragment />;
|
if (!data) return <React.Fragment />;
|
||||||
|
|
||||||
const { message, status } = data;
|
const { message, status } = data;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Chip
|
<Chip
|
||||||
component={Link}
|
component={Link}
|
||||||
|
@@ -1,8 +0,0 @@
|
|||||||
import { gql } from '@apollo/client';
|
|
||||||
export const GET_TRIAL_STATUS = gql`
|
|
||||||
query GetTrialStatus {
|
|
||||||
getTrialStatus {
|
|
||||||
expireAt
|
|
||||||
}
|
|
||||||
}
|
|
||||||
`;
|
|
@@ -1,17 +1,21 @@
|
|||||||
import * as React from 'react';
|
import * as React from 'react';
|
||||||
import { useQuery } from '@apollo/client';
|
|
||||||
import { useLocation } from 'react-router-dom';
|
import { useLocation } from 'react-router-dom';
|
||||||
import { DateTime } from 'luxon';
|
import { DateTime } from 'luxon';
|
||||||
import { GET_TRIAL_STATUS } from 'graphql/queries/get-trial-status.ee';
|
|
||||||
import useFormatMessage from './useFormatMessage';
|
import useFormatMessage from './useFormatMessage';
|
||||||
|
import api from 'helpers/api';
|
||||||
|
import { useQuery } from '@tanstack/react-query';
|
||||||
|
|
||||||
function getDiffInDays(date) {
|
function getDiffInDays(date) {
|
||||||
const today = DateTime.now().startOf('day');
|
const today = DateTime.now().startOf('day');
|
||||||
const diffInDays = date.diff(today, 'days').days;
|
const diffInDays = date.diff(today, 'days').days;
|
||||||
const roundedDiffInDays = Math.round(diffInDays);
|
const roundedDiffInDays = Math.round(diffInDays);
|
||||||
|
|
||||||
return roundedDiffInDays;
|
return roundedDiffInDays;
|
||||||
}
|
}
|
||||||
function getFeedbackPayload(date) {
|
function getFeedbackPayload(date) {
|
||||||
const diffInDays = getDiffInDays(date);
|
const diffInDays = getDiffInDays(date);
|
||||||
|
|
||||||
if (diffInDays <= -1) {
|
if (diffInDays <= -1) {
|
||||||
return {
|
return {
|
||||||
translationEntryId: 'trialBadge.over',
|
translationEntryId: 'trialBadge.over',
|
||||||
@@ -35,36 +39,53 @@ function getFeedbackPayload(date) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
export default function useTrialStatus() {
|
export default function useUserTrial() {
|
||||||
const formatMessage = useFormatMessage();
|
const formatMessage = useFormatMessage();
|
||||||
const location = useLocation();
|
const location = useLocation();
|
||||||
const state = location.state;
|
const state = location.state;
|
||||||
const checkoutCompleted = state?.checkoutCompleted;
|
const checkoutCompleted = state?.checkoutCompleted;
|
||||||
const { data, loading, startPolling, stopPolling } =
|
const [isPolling, setIsPolling] = React.useState(false);
|
||||||
useQuery(GET_TRIAL_STATUS);
|
|
||||||
const hasTrial = !!data?.getTrialStatus?.expireAt;
|
const { data, isLoading: isUserTrialLoading } = useQuery({
|
||||||
|
queryKey: ['userTrial'],
|
||||||
|
queryFn: async ({ signal }) => {
|
||||||
|
const { data } = await api.get('/v1/users/me/trial', {
|
||||||
|
signal,
|
||||||
|
});
|
||||||
|
|
||||||
|
return data;
|
||||||
|
},
|
||||||
|
refetchInterval: isPolling ? 1000 : false,
|
||||||
|
});
|
||||||
|
const userTrial = data?.data;
|
||||||
|
|
||||||
|
const hasTrial = userTrial?.inTrial;
|
||||||
|
|
||||||
React.useEffect(
|
React.useEffect(
|
||||||
function pollDataUntilTrialEnds() {
|
function pollDataUntilTrialEnds() {
|
||||||
if (checkoutCompleted && hasTrial) {
|
if (checkoutCompleted && hasTrial) {
|
||||||
startPolling(1000);
|
setIsPolling(true);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[checkoutCompleted, hasTrial, startPolling],
|
[checkoutCompleted, hasTrial, setIsPolling],
|
||||||
);
|
);
|
||||||
|
|
||||||
React.useEffect(
|
React.useEffect(
|
||||||
function stopPollingWhenTrialEnds() {
|
function stopPollingWhenTrialEnds() {
|
||||||
if (checkoutCompleted && !hasTrial) {
|
if (checkoutCompleted && !hasTrial) {
|
||||||
stopPolling();
|
setIsPolling(false);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[checkoutCompleted, hasTrial, stopPolling],
|
[checkoutCompleted, hasTrial, setIsPolling],
|
||||||
);
|
);
|
||||||
if (loading || !data?.getTrialStatus) return null;
|
|
||||||
const expireAt = DateTime.fromMillis(
|
if (isUserTrialLoading || !userTrial) return null;
|
||||||
Number(data.getTrialStatus.expireAt),
|
|
||||||
).startOf('day');
|
const expireAt = DateTime.fromISO(userTrial?.expireAt).startOf('day');
|
||||||
|
|
||||||
const { translationEntryId, translationEntryValues, status, over } =
|
const { translationEntryId, translationEntryValues, status, over } =
|
||||||
getFeedbackPayload(expireAt);
|
getFeedbackPayload(expireAt);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
message: formatMessage(translationEntryId, translationEntryValues),
|
message: formatMessage(translationEntryId, translationEntryValues),
|
||||||
expireAt,
|
expireAt,
|
Reference in New Issue
Block a user