feat: Implement getTrialStatus graphQL query
This commit is contained in:
19
packages/backend/src/graphql/queries/get-trial-status.ee.ts
Normal file
19
packages/backend/src/graphql/queries/get-trial-status.ee.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import appConfig from '../../config/app';
|
||||
import Context from '../../types/express/context';
|
||||
|
||||
const getTrialStatus = async (
|
||||
_parent: unknown,
|
||||
_params: unknown,
|
||||
context: Context
|
||||
) => {
|
||||
if (!appConfig.isCloud) return;
|
||||
|
||||
const inTrial = await context.currentUser.inTrial();
|
||||
if (!inTrial) return;
|
||||
|
||||
return {
|
||||
expireAt: context.currentUser.trialExpiryDate,
|
||||
};
|
||||
};
|
||||
|
||||
export default getTrialStatus;
|
@@ -17,6 +17,7 @@ import getPaddleInfo from './queries/get-paddle-info.ee';
|
||||
import getBillingAndUsage from './queries/get-billing-and-usage.ee';
|
||||
import getInvoices from './queries/get-invoices.ee';
|
||||
import getAutomatischInfo from './queries/get-automatisch-info';
|
||||
import getTrialStatus from './queries/get-trial-status.ee';
|
||||
import healthcheck from './queries/healthcheck';
|
||||
|
||||
const queryResolvers = {
|
||||
@@ -39,6 +40,7 @@ const queryResolvers = {
|
||||
getBillingAndUsage,
|
||||
getInvoices,
|
||||
getAutomatischInfo,
|
||||
getTrialStatus,
|
||||
healthcheck,
|
||||
};
|
||||
|
||||
|
@@ -40,6 +40,7 @@ type Query {
|
||||
getBillingAndUsage: GetBillingAndUsage
|
||||
getInvoices: [Invoice]
|
||||
getAutomatischInfo: GetAutomatischInfo
|
||||
getTrialStatus: GetTrialStatus
|
||||
healthcheck: AppHealth
|
||||
}
|
||||
|
||||
@@ -474,6 +475,10 @@ type GetAutomatischInfo {
|
||||
isCloud: Boolean
|
||||
}
|
||||
|
||||
type GetTrialStatus {
|
||||
expireAt: String
|
||||
}
|
||||
|
||||
type GetBillingAndUsage {
|
||||
subscription: Subscription
|
||||
usage: Usage
|
||||
|
@@ -76,6 +76,10 @@ class Subscription extends Base {
|
||||
get plan() {
|
||||
return getPlanById(this.paddlePlanId);
|
||||
}
|
||||
|
||||
get isActive() {
|
||||
return this.status === 'active' || this.status === 'past_due';
|
||||
}
|
||||
}
|
||||
|
||||
export default Subscription;
|
||||
|
@@ -126,19 +126,6 @@ class User extends Base {
|
||||
},
|
||||
});
|
||||
|
||||
get inTrial() {
|
||||
if (!this.trialExpiryDate) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const expiryDate = DateTime.fromJSDate(
|
||||
this.trialExpiryDate as unknown as Date
|
||||
);
|
||||
const now = DateTime.now();
|
||||
|
||||
return now < expiryDate;
|
||||
}
|
||||
|
||||
login(password: string) {
|
||||
return bcrypt.compare(password, this.password);
|
||||
}
|
||||
@@ -178,6 +165,29 @@ class User extends Base {
|
||||
this.trialExpiryDate = DateTime.now().plus({ days: 30 }).toISODate();
|
||||
}
|
||||
|
||||
async inTrial() {
|
||||
if (!appConfig.isCloud) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!this.trialExpiryDate) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const subscription = await this.$relatedQuery('currentSubscription');
|
||||
|
||||
if (subscription?.isActive) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const expiryDate = DateTime.fromJSDate(
|
||||
this.trialExpiryDate as unknown as Date
|
||||
);
|
||||
const now = DateTime.now();
|
||||
|
||||
return now < expiryDate;
|
||||
}
|
||||
|
||||
async $beforeInsert(queryContext: QueryContext) {
|
||||
await super.$beforeInsert(queryContext);
|
||||
await this.generateHash();
|
||||
|
Reference in New Issue
Block a user