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

Method reSubscribeToPaidPlan

api/server/models/User.ts:1239–1278  ·  view source on GitHub ↗
({ userId })

Source from the content-addressed store, hash-verified

1237
1238 // use it on Billing page
1239 public static async reSubscribeToPaidPlan({ userId }) {
1240 const teamLeader = await this.findById(userId).setOptions({ lean: true });
1241
1242 if (!teamLeader.stripeSubscription) {
1243 throw new Error('No active subscription found for this Account');
1244 }
1245
1246 if (!teamLeader.stripeSubscription.cancel_at) {
1247 throw new Error('Paid subscription is not cancelled');
1248 }
1249
1250 const subscriptionObj = await reSubscribe({ subscriptionId: teamLeader.stripeSubscription.id });
1251
1252 // do it for all teams of TL
1253 for (const team of teamLeader.teamsForTeamLeader) {
1254 for (const idOfTeamMember of team.idsOfTeamMembers) {
1255 await this.updateOne(
1256 { _id: idOfTeamMember, 'teamsForTeamMember.teamId': team.teamId },
1257 {
1258 $set: {
1259 'teamsForTeamMember.$.isSubscriptionActiveForTeam': true,
1260 'teamsForTeamMember.$.isPaymentFailedForTeam': false,
1261 },
1262 },
1263 );
1264 }
1265 }
1266
1267 // TL
1268 return this.findOneAndUpdate(
1269 { _id: userId },
1270 {
1271 $set: {
1272 stripeSubscription: subscriptionObj,
1273 isSubscriptionActiveForAccount: true,
1274 },
1275 },
1276 { runValidators: true, new: true },
1277 ).setOptions({ lean: true });
1278 }
1279
1280 // used in webhook
1281 public static async cancelSubscriptionForAccountAfterFailedPayment({ subscriptionId }) {

Callers

nothing calls this directly

Calls 1

reSubscribeFunction · 0.90

Tested by

no test coverage detected