Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
| RolePlayPersonaDeletionService | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
3 | |
100.00% |
1 / 1 |
| delete | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Http\Services\RolePlay; |
| 4 | |
| 5 | use App\Http\Models\Auth\User; |
| 6 | use App\Http\Models\RolePlayConversations; |
| 7 | use App\Http\Models\RolePlayProjects; |
| 8 | use App\Http\Models\UserRolePlayProgression; |
| 9 | use Illuminate\Support\Facades\Log; |
| 10 | |
| 11 | /** |
| 12 | * Cascade-deletes a roleplay persona (project) along with all of its |
| 13 | * derived data: |
| 14 | * |
| 15 | * - All conversations (sessions) for the persona — soft-deleted so that |
| 16 | * historical daily usage metadata stays intact. |
| 17 | * - All per-call-type progression entries referencing the persona — |
| 18 | * fully recomputed from the remaining entries via |
| 19 | * UserRolePlayProgression::dropEntriesAndRecompute(). |
| 20 | * - The persona itself (ICPs + custom objections live inline on the |
| 21 | * persona document and go with it). |
| 22 | * |
| 23 | * Does NOT touch fly_msg_user_daily_usage or UserInfo roleplay counters. |
| 24 | * Those are historical metadata and must persist across deletions. |
| 25 | */ |
| 26 | class RolePlayPersonaDeletionService |
| 27 | { |
| 28 | use RolePlayDeletionAuthTrait; |
| 29 | |
| 30 | /** |
| 31 | * Soft-delete the persona, cascade-delete its sessions, and recompute |
| 32 | * the owning user's progression. |
| 33 | * |
| 34 | * @throws \Illuminate\Auth\Access\AuthorizationException |
| 35 | */ |
| 36 | public function delete(User $actor, RolePlayProjects $persona): void |
| 37 | { |
| 38 | $ownerId = (string) $persona->user_id; |
| 39 | $owner = User::find($ownerId); |
| 40 | $ownerCompanyId = $owner?->company_id ? (string) $owner->company_id : null; |
| 41 | |
| 42 | $this->assertCanActOn($actor, $ownerId, $ownerCompanyId); |
| 43 | |
| 44 | $personaId = (string) $persona->id; |
| 45 | |
| 46 | RolePlayConversations::where('project_id', $personaId)->delete(); |
| 47 | |
| 48 | $progressions = UserRolePlayProgression::where('user_id', $ownerId)->get(); |
| 49 | foreach ($progressions as $progression) { |
| 50 | $progression->dropEntriesAndRecompute([], [$personaId]); |
| 51 | } |
| 52 | |
| 53 | $persona->delete(); |
| 54 | |
| 55 | Log::info('Roleplay persona deleted', [ |
| 56 | 'persona_id' => $personaId, |
| 57 | 'owner_id' => $ownerId, |
| 58 | 'actor_id' => (string) $actor->id, |
| 59 | ]); |
| 60 | } |
| 61 | } |