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

Method cancelSubscriptionForAccount

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

Source from the content-addressed store, hash-verified

1198 }
1199
1200 public static async cancelSubscriptionForAccount({ userId }) {
1201 const teamLeader = await this.findById(userId).setOptions({ lean: true });
1202
1203 if (!teamLeader.isSubscriptionActiveForAccount) {
1204 throw new Error('This account is not subscribed to paid plan.');
1205 }
1206
1207 const cancelledSubscriptionObj = await cancelSubscription({
1208 subscriptionId: teamLeader.stripeSubscription.id,
1209 });
1210
1211 // do it for all teams of TL
1212 for (const team of teamLeader.teamsForTeamLeader) {
1213 for (const idOfTeamMember of team.idsOfTeamMembers) {
1214 await this.updateOne(
1215 { _id: idOfTeamMember, 'teamsForTeamMember.teamId': team.teamId },
1216 {
1217 $set: {
1218 'teamsForTeamMember.$.isSubscriptionActiveForTeam': false,
1219 },
1220 },
1221 );
1222 }
1223 }
1224
1225 // Team Leader
1226 return this.findOneAndUpdate(
1227 { _id: userId },
1228 {
1229 $set: {
1230 stripeSubscription: cancelledSubscriptionObj,
1231 isSubscriptionActiveForAccount: false,
1232 },
1233 },
1234 { runValidators: true, new: true },
1235 ).setOptions({ lean: true });
1236 }
1237
1238 // use it on Billing page
1239 public static async reSubscribeToPaidPlan({ userId }) {

Callers

nothing calls this directly

Calls 1

cancelSubscriptionFunction · 0.90

Tested by

no test coverage detected