({ userId })
| 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 }) { |
nothing calls this directly
no test coverage detected