MCPcopy Create free account
hub / github.com/async-labs/async / subscribeToPaidPlan

Method subscribeToPaidPlan

api/server/models/User.ts:1105–1165  ·  view source on GitHub ↗
({
    session,
    teamLeader,
  }: {
    session: Stripe.Checkout.Session;
    teamLeader: IUserDocument;
  })

Source from the content-addressed store, hash-verified

1103
1104 // used in Checkout
1105 public static async subscribeToPaidPlan({
1106 session,
1107 teamLeader,
1108 }: {
1109 session: Stripe.Checkout.Session;
1110 teamLeader: IUserDocument;
1111 }): Promise<void> {
1112 if (!session.subscription) {
1113 throw new Error('Not paid subscription inside session');
1114 }
1115
1116 if (!teamLeader) {
1117 throw new Error('Team Leader is not found.');
1118 }
1119
1120 if (teamLeader.isSubscriptionActiveForAccount) {
1121 throw new Error('Your account is already subscribed to paid plan.');
1122 }
1123
1124 const stripeSubscription = await retrieveSubscription(session.subscription as string);
1125 if (stripeSubscription.cancel_at || stripeSubscription.canceled_at) {
1126 throw new Error('Canceled');
1127 }
1128
1129 const stripeCard =
1130 (stripeSubscription.default_payment_method &&
1131 (stripeSubscription.default_payment_method as Stripe.PaymentMethod).card) ||
1132 undefined;
1133
1134 const hasCardInformation = !!stripeCard;
1135
1136 // Team Leader
1137 await this.updateOne(
1138 { _id: teamLeader._id.toString() },
1139 {
1140 $set: {
1141 stripeCustomer: stripeSubscription.customer,
1142 stripeCard,
1143 hasCardInformation,
1144 stripeSubscription: stripeSubscription,
1145 isSubscriptionActiveForAccount: true,
1146 isPaymentFailedForAccount: false,
1147 },
1148 },
1149 );
1150
1151 // do it for all teams of TL
1152 for (const team of teamLeader.teamsForTeamLeader) {
1153 for (const idOfTeamMember of team.idsOfTeamMembers) {
1154 await this.updateOne(
1155 { _id: idOfTeamMember, 'teamsForTeamMember.teamId': team.teamId },
1156 {
1157 $set: {
1158 'teamsForTeamMember.$.isSubscriptionActiveForTeam': true,
1159 'teamsForTeamMember.$.isPaymentFailedForTeam': false,
1160 },
1161 },
1162 );

Callers

nothing calls this directly

Calls 1

retrieveSubscriptionFunction · 0.90

Tested by

no test coverage detected