Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 48 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| CancelUserPlanAction | |
0.00% |
0 / 48 |
|
0.00% |
0 / 2 |
210 | |
0.00% |
0 / 1 |
| execute | |
0.00% |
0 / 46 |
|
0.00% |
0 / 1 |
182 | |||
| cancelInstancySubscription | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Actions\Users; |
| 4 | |
| 5 | use App\Http\Models\Auth\User; |
| 6 | use App\Http\Models\Plans; |
| 7 | use App\Http\Models\Subscription; |
| 8 | use App\Http\Models\UserInfo; |
| 9 | use App\Http\Services\HubspotServiceV2; |
| 10 | use App\Http\Services\InstancyServiceV2; |
| 11 | use App\Http\Services\StatisticsService; |
| 12 | use App\Services\UserInfo\SubscriptionService; |
| 13 | use App\Services\UserInfo\UserInfoService; |
| 14 | use Carbon\Carbon; |
| 15 | use MongoDB\BSON\UTCDateTime; |
| 16 | |
| 17 | class CancelUserPlanAction |
| 18 | { |
| 19 | public function execute(User $user, ?bool $onlyDeactivate = false, $cancellation_date = null) |
| 20 | { |
| 21 | if (empty($cancellation_date)) { |
| 22 | $cancellation_date = Carbon::now(); |
| 23 | } |
| 24 | |
| 25 | $deleted = filled($user->deleted_at) && $user->deleted_at != null && !$onlyDeactivate; |
| 26 | |
| 27 | $userInfo = UserInfo::where('user_id', $user->id)->first(); |
| 28 | |
| 29 | $data = [ |
| 30 | 'subscription_type' => 'Cancelled Account', |
| 31 | 'status' => 'Deactivated', |
| 32 | ]; |
| 33 | |
| 34 | if ($deleted) { |
| 35 | // update the property opt out for the email the api |
| 36 | (new HubspotServiceV2())->updateHubspotOptOutProperty($user->email); |
| 37 | } |
| 38 | |
| 39 | if ($deleted) { |
| 40 | $delete_from_flymsg_email = "delete_from_flymsg_$user->email"; |
| 41 | $data = [ |
| 42 | 'deleted_at' => new UTCDateTime($cancellation_date->getTimestamp() * 1000), |
| 43 | 'status' => 'Deleted', |
| 44 | 'subscription_type' => 'Deleted Profile from FlyMSG', |
| 45 | 'email' => $delete_from_flymsg_email, |
| 46 | 'first_name' => "", |
| 47 | 'last_name' => "", |
| 48 | 'full_name' => "", |
| 49 | 'email_used_for_login' => "", |
| 50 | 'avatar' => "", |
| 51 | 'job_role' => "", |
| 52 | 'department' => "", |
| 53 | 'company' => "" |
| 54 | ]; |
| 55 | } |
| 56 | |
| 57 | $subscriptionService = new SubscriptionService(); |
| 58 | |
| 59 | $userSub = $user->subscription("main"); |
| 60 | if ($userSub) { |
| 61 | $subscription = Subscription::find($userSub->id); |
| 62 | $subData = $subscriptionService->endSubscription($userSub->toArray(), $subscription, true, $cancellation_date); |
| 63 | $data = array_merge($data, $subData); |
| 64 | |
| 65 | $plan = $userSub->plan; |
| 66 | |
| 67 | if (isset($plan->identifier) && $plan->identifier != Plans::FREEMIUM_IDENTIFIER && $userSub->valid()) { |
| 68 | if ($user->company_id) { |
| 69 | $userSub->markAsCancelledOnlyInDB(); |
| 70 | } else { |
| 71 | $userSub->markAsCanceled(); |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | $endFreemiumData = $subscriptionService->endFreemiumSubscription($user->id, $cancellation_date); |
| 77 | |
| 78 | $data = array_merge($data, $endFreemiumData); |
| 79 | |
| 80 | $userInfo->update($data); |
| 81 | |
| 82 | $userInfoService = new UserInfoService(new StatisticsService()); |
| 83 | |
| 84 | $userInfoService->pushItToHubspot($user->id, false); |
| 85 | |
| 86 | if ($deleted) { |
| 87 | // update the property opt out for the email the api |
| 88 | (new HubspotServiceV2())->updateHubspotOptOutProperty($delete_from_flymsg_email); |
| 89 | } |
| 90 | |
| 91 | if ($user->instancy_id) { |
| 92 | $this->cancelInstancySubscription($user, $cancellation_date); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | private function cancelInstancySubscription(User $user, Carbon $cancellation_date) |
| 97 | { |
| 98 | $instancyService = new InstancyServiceV2(); |
| 99 | $instancyService->updateMembership($user->email, $cancellation_date->subDay()->toDateString()); |
| 100 | } |
| 101 | } |