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 getBillingAndUsage from './queries/get-billing-and-usage.ee';
|
||||||
import getInvoices from './queries/get-invoices.ee';
|
import getInvoices from './queries/get-invoices.ee';
|
||||||
import getAutomatischInfo from './queries/get-automatisch-info';
|
import getAutomatischInfo from './queries/get-automatisch-info';
|
||||||
|
import getTrialStatus from './queries/get-trial-status.ee';
|
||||||
import healthcheck from './queries/healthcheck';
|
import healthcheck from './queries/healthcheck';
|
||||||
|
|
||||||
const queryResolvers = {
|
const queryResolvers = {
|
||||||
@@ -39,6 +40,7 @@ const queryResolvers = {
|
|||||||
getBillingAndUsage,
|
getBillingAndUsage,
|
||||||
getInvoices,
|
getInvoices,
|
||||||
getAutomatischInfo,
|
getAutomatischInfo,
|
||||||
|
getTrialStatus,
|
||||||
healthcheck,
|
healthcheck,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@@ -40,6 +40,7 @@ type Query {
|
|||||||
getBillingAndUsage: GetBillingAndUsage
|
getBillingAndUsage: GetBillingAndUsage
|
||||||
getInvoices: [Invoice]
|
getInvoices: [Invoice]
|
||||||
getAutomatischInfo: GetAutomatischInfo
|
getAutomatischInfo: GetAutomatischInfo
|
||||||
|
getTrialStatus: GetTrialStatus
|
||||||
healthcheck: AppHealth
|
healthcheck: AppHealth
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -474,6 +475,10 @@ type GetAutomatischInfo {
|
|||||||
isCloud: Boolean
|
isCloud: Boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type GetTrialStatus {
|
||||||
|
expireAt: String
|
||||||
|
}
|
||||||
|
|
||||||
type GetBillingAndUsage {
|
type GetBillingAndUsage {
|
||||||
subscription: Subscription
|
subscription: Subscription
|
||||||
usage: Usage
|
usage: Usage
|
||||||
|
@@ -76,6 +76,10 @@ class Subscription extends Base {
|
|||||||
get plan() {
|
get plan() {
|
||||||
return getPlanById(this.paddlePlanId);
|
return getPlanById(this.paddlePlanId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get isActive() {
|
||||||
|
return this.status === 'active' || this.status === 'past_due';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default Subscription;
|
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) {
|
login(password: string) {
|
||||||
return bcrypt.compare(password, this.password);
|
return bcrypt.compare(password, this.password);
|
||||||
}
|
}
|
||||||
@@ -178,6 +165,29 @@ class User extends Base {
|
|||||||
this.trialExpiryDate = DateTime.now().plus({ days: 30 }).toISODate();
|
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) {
|
async $beforeInsert(queryContext: QueryContext) {
|
||||||
await super.$beforeInsert(queryContext);
|
await super.$beforeInsert(queryContext);
|
||||||
await this.generateHash();
|
await this.generateHash();
|
||||||
|
Reference in New Issue
Block a user