Skip to content

Commit

Permalink
Fix: Change the base strucutre how plans permissions are sent
Browse files Browse the repository at this point in the history
  • Loading branch information
umakantp committed Feb 14, 2025
1 parent b05fb00 commit ec558d0
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 62 deletions.
16 changes: 10 additions & 6 deletions src/data/plans.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Plan, PlanPermission, SubscriptionPlan } from 'src/types'
import { Plan, PlanPermission } from 'src/types'
import { SubscriptionPlan } from 'src/types/graphql'

export const plans: Plan[] = [
{
Expand Down Expand Up @@ -30,8 +31,9 @@ export const plans: Plan[] = [
},
]

export const planPermissions: Record<SubscriptionPlan, PlanPermission> = {
[SubscriptionPlan.BASIC]: {
export const planPermissions: PlanPermission[] = [
{
id: SubscriptionPlan.BASIC,
portfolios: 1,
funds: 5,
stocks: 30,
Expand All @@ -40,7 +42,8 @@ export const planPermissions: Record<SubscriptionPlan, PlanPermission> = {
familyMembers: 0,
uploadFiles: false,
},
[SubscriptionPlan.PRO]: {
{
id: SubscriptionPlan.PRO,
portfolios: 5,
funds: 15,
stocks: 100,
Expand All @@ -49,7 +52,8 @@ export const planPermissions: Record<SubscriptionPlan, PlanPermission> = {
familyMembers: 2,
uploadFiles: false,
},
[SubscriptionPlan.ADVANCED]: {
{
id: SubscriptionPlan.ADVANCED,
portfolios: 10,
funds: 30,
stocks: 200,
Expand All @@ -58,4 +62,4 @@ export const planPermissions: Record<SubscriptionPlan, PlanPermission> = {
familyMembers: 5,
uploadFiles: true,
},
}
]
2 changes: 1 addition & 1 deletion src/graphql/schema/core/base/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const resolvers: Resolvers = {
plans: (_, _args, _ctx) => {
return {
plans,
planPermissions
planPermissions,
}
},
},
Expand Down
11 changes: 3 additions & 8 deletions src/graphql/schema/core/base/types.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ type SuccessPayload {
}

type Plan {
id: NonEmptyString!
id: SubscriptionPlan!
title: NonEmptyString!
monthlyPrice: Int!
yearlyPrice: Int!
Expand All @@ -62,6 +62,7 @@ type Plan {
}

type PlanPermission {
id: SubscriptionPlan!
portfolios: Int!
funds: Int!
stocks: Int!
Expand All @@ -70,15 +71,9 @@ type PlanPermission {
uploadFiles: Boolean!
}

type PlanPermissions {
BASIC: PlanPermission
PRO: PlanPermission
ADVANCED: PlanPermission
}

type PlansPayload {
plans: [Plan!]!
planPermissions: PlanPermissions!
planPermissions: [PlanPermission!]!
}

type Query {
Expand Down
2 changes: 1 addition & 1 deletion src/graphql/schema/core/enums/types.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ enum FundCategory {
EQUITY_ELSS
EQUITY_FOCUSED
EQUITY_DIVIDEND_YIELD
EQUITY_BANKING_AND_FINANCIAL_SERVICES
EQUITY_SECTORAL_BANKING_AND_FINANCIAL_SERVICES
EQUITY_SECTORAL_TECHNOLOGY
EQUITY_SECTORAL_INFRASTRUCTURE
EQUITY_SECTORAL_PHARMA_AND_HEALTHCARE
Expand Down
8 changes: 5 additions & 3 deletions src/graphql/schema/core/scalar/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ export const resolvers: Resolvers = {
['ID' as any]: NonEmptyStringResolver,
Json: JSONResolver,
JsonObject: JSONObjectResolver,
};
}

// Otherwise it uses the name of the scalar `NonEmptyStringResolver`
(resolvers as any)['ID'].name = 'ID'
// Otherwise it uses the name of the scalar e.g. `NonEmptyStringResolver`
for (const name in resolvers) {
(resolvers as any)[name].name = name
}

export default resolvers
3 changes: 2 additions & 1 deletion src/graphql/schema/core/scalar/types.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ scalar PhoneNumber
scalar EmailAddress
scalar JWT
scalar URL
""" Can take valid scalar values also """
scalar Json
""" Same as Json but validates it's a plain object """
""" Same as Json but validates it's a object """
scalar JsonObject
5 changes: 4 additions & 1 deletion src/models/portfolio.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { keyBy } from 'lodash'
import { planPermissions } from 'src/data/plans'
import db from 'src/db'
import { whereId } from 'src/models/core'
Expand All @@ -8,6 +9,8 @@ export const get = (entity: string) => {
return db.portfolio.findUniqueOrThrow(whereId(entity))
}


const planPermissionsMap = keyBy(planPermissions, p => p.id)
export const activeSubscription = async (portfolioId: string) => {
// If portfolio exists.
await get(portfolioId)
Expand All @@ -19,5 +22,5 @@ export const activeSubscription = async (portfolioId: string) => {
const user = members[0].userId
return await activeUserSubscription(user)
}
return planPermissions[SubscriptionPlan.BASIC]
return planPermissionsMap[SubscriptionPlan.BASIC]
}
4 changes: 3 additions & 1 deletion src/models/user.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { keyBy } from 'lodash'
import { planPermissions } from 'src/data/plans'
import db from 'src/db'
import { whereId } from 'src/models/core'
Expand All @@ -22,11 +23,12 @@ export const verifyUser = async (userId: string) => {
await db.user.update({ where: { id: userId }, data: { isVerified: true } })
}

const planPermissionsMap = keyBy(planPermissions, p => p.id)
export const activeSubscription = async (userId: string) => {
const subscription = await db.subscription.findFirst({
where: { userId, endDate: { lt: new Date() } },
})
return planPermissions[subscription?.plan || SubscriptionPlan.BASIC]
return planPermissionsMap[subscription?.plan || SubscriptionPlan.BASIC]
}

export const fromJwt = async (claims?: jwt.Jwt): Promise<User | undefined> => {
Expand Down
11 changes: 3 additions & 8 deletions src/types/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ export type PagePayload = {
export type Plan = {
description: Scalars['NonEmptyString']['output'];
features: Array<Scalars['NonEmptyString']['output']>;
id: Scalars['NonEmptyString']['output'];
id: SubscriptionPlan;
monthlyPrice: Scalars['Int']['output'];
popular: Scalars['Boolean']['output'];
title: Scalars['NonEmptyString']['output'];
Expand All @@ -470,20 +470,15 @@ export type Plan = {
export type PlanPermission = {
familyMembers: Scalars['Int']['output'];
funds: Scalars['Int']['output'];
id: SubscriptionPlan;
portfolios: Scalars['Int']['output'];
schemes: Scalars['Int']['output'];
stocks: Scalars['Int']['output'];
uploadFiles: Scalars['Boolean']['output'];
};

export type PlanPermissions = {
ADVANCED?: Maybe<PlanPermission>;
BASIC?: Maybe<PlanPermission>;
PRO?: Maybe<PlanPermission>;
};

export type PlansPayload = {
planPermissions: PlanPermissions;
planPermissions: Array<PlanPermission>;
plans: Array<Plan>;
};

Expand Down
20 changes: 0 additions & 20 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,3 @@ export interface PagePayload<T extends Node> {
pageInfo: PageInfo,
total: number,
}

export type Plan = {
// TODO: Should have been SubscriptionPlan
id: string,
title: string,
monthlyPrice: number,
yearlyPrice: number,
description: string,
features: string[],
popular: boolean,
}

export type PlanPermission = {
portfolios: number,
funds: number,
stocks: number,
schemes: number,
familyMembers: number,
uploadFiles: boolean,
}
15 changes: 3 additions & 12 deletions src/types/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ export type ResolversTypes = {
PhoneNumber: ResolverTypeWrapper<t.Scalars['PhoneNumber']['output']>;
Plan: ResolverTypeWrapper<t.Plan>;
PlanPermission: ResolverTypeWrapper<t.PlanPermission>;
PlanPermissions: ResolverTypeWrapper<t.PlanPermissions>;
PlansPayload: ResolverTypeWrapper<t.PlansPayload>;
Portfolio: ResolverTypeWrapper<t.Portfolio>;
PortfolioFund: ResolverTypeWrapper<t.PortfolioFund>;
Expand Down Expand Up @@ -197,7 +196,6 @@ export type ResolversParentTypes = {
PhoneNumber: t.Scalars['PhoneNumber']['output'];
Plan: t.Plan;
PlanPermission: t.PlanPermission;
PlanPermissions: t.PlanPermissions;
PlansPayload: t.PlansPayload;
Portfolio: t.Portfolio;
PortfolioFund: t.PortfolioFund;
Expand Down Expand Up @@ -368,7 +366,7 @@ export interface PhoneNumberScalarConfig extends GraphQLScalarTypeConfig<Resolve
export type PlanResolvers<ContextType = GraphQLContext, ParentType extends ResolversParentTypes['Plan'] = ResolversParentTypes['Plan']> = {
description?: Resolver<ResolversTypes['NonEmptyString'], ParentType, ContextType>;
features?: Resolver<ReadonlyArray<ResolversTypes['NonEmptyString']>, ParentType, ContextType>;
id?: Resolver<ResolversTypes['NonEmptyString'], ParentType, ContextType>;
id?: Resolver<ResolversTypes['SubscriptionPlan'], ParentType, ContextType>;
monthlyPrice?: Resolver<ResolversTypes['Int'], ParentType, ContextType>;
popular?: Resolver<ResolversTypes['Boolean'], ParentType, ContextType>;
title?: Resolver<ResolversTypes['NonEmptyString'], ParentType, ContextType>;
Expand All @@ -379,22 +377,16 @@ export type PlanResolvers<ContextType = GraphQLContext, ParentType extends Resol
export type PlanPermissionResolvers<ContextType = GraphQLContext, ParentType extends ResolversParentTypes['PlanPermission'] = ResolversParentTypes['PlanPermission']> = {
familyMembers?: Resolver<ResolversTypes['Int'], ParentType, ContextType>;
funds?: Resolver<ResolversTypes['Int'], ParentType, ContextType>;
id?: Resolver<ResolversTypes['SubscriptionPlan'], ParentType, ContextType>;
portfolios?: Resolver<ResolversTypes['Int'], ParentType, ContextType>;
schemes?: Resolver<ResolversTypes['Int'], ParentType, ContextType>;
stocks?: Resolver<ResolversTypes['Int'], ParentType, ContextType>;
uploadFiles?: Resolver<ResolversTypes['Boolean'], ParentType, ContextType>;
__isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
};

export type PlanPermissionsResolvers<ContextType = GraphQLContext, ParentType extends ResolversParentTypes['PlanPermissions'] = ResolversParentTypes['PlanPermissions']> = {
ADVANCED?: Resolver<t.Maybe<ResolversTypes['PlanPermission']>, ParentType, ContextType>;
BASIC?: Resolver<t.Maybe<ResolversTypes['PlanPermission']>, ParentType, ContextType>;
PRO?: Resolver<t.Maybe<ResolversTypes['PlanPermission']>, ParentType, ContextType>;
__isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
};

export type PlansPayloadResolvers<ContextType = GraphQLContext, ParentType extends ResolversParentTypes['PlansPayload'] = ResolversParentTypes['PlansPayload']> = {
planPermissions?: Resolver<ResolversTypes['PlanPermissions'], ParentType, ContextType>;
planPermissions?: Resolver<ReadonlyArray<ResolversTypes['PlanPermission']>, ParentType, ContextType>;
plans?: Resolver<ReadonlyArray<ResolversTypes['Plan']>, ParentType, ContextType>;
__isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
};
Expand Down Expand Up @@ -534,7 +526,6 @@ export type Resolvers<ContextType = GraphQLContext> = {
PhoneNumber?: GraphQLScalarType;
Plan?: PlanResolvers<ContextType>;
PlanPermission?: PlanPermissionResolvers<ContextType>;
PlanPermissions?: PlanPermissionsResolvers<ContextType>;
PlansPayload?: PlansPayloadResolvers<ContextType>;
Portfolio?: PortfolioResolvers<ContextType>;
PortfolioFund?: PortfolioFundResolvers<ContextType>;
Expand Down

0 comments on commit ec558d0

Please sign in to comment.