Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
39 / 39
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
CompanyRolePlayProjectResource
100.00% covered (success)
100.00%
39 / 39
100.00% covered (success)
100.00%
1 / 1
4
100.00% covered (success)
100.00%
1 / 1
 toArray
100.00% covered (success)
100.00%
39 / 39
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2
3namespace App\Http\Resources\v2;
4
5use App\Http\Services\CorporatePersonaEditAuthorizationService;
6use Illuminate\Http\Resources\Json\JsonResource;
7
8/**
9 * Resource for transforming CompanyRolePlayProject models for API responses.
10 *
11 * Transforms company-level roleplay projects including all configuration
12 * fields and computed creator information.
13 *
14 * @property string $_id The project ID
15 * @property string $company_id The owning company ID
16 * @property string $created_by The creator user ID
17 * @property string $name The project name
18 * @property string $type The call type (e.g., 'cold-call', 'discovery-call')
19 * @property string|null $description The project description
20 * @property string $industry The target industry
21 * @property array $target_job_titles Array of targeted job titles
22 * @property array $key_features Array of key product/service features
23 * @property string|null $product_description Product description
24 * @property int $difficulty_level Difficulty level (1-5)
25 * @property array $customer_profiles Array of customer profile objects
26 * @property array $training_personalities Array of training personality configurations
27 * @property array $scorecard_config Array of scorecard configuration objects
28 * @property array $objections Array of objection objects
29 * @property bool $allow_user_customization Whether users can customize when cloning (legacy)
30 * @property bool|null $allow_clone Whether the persona can be cloned (defaults to allow_user_customization when null)
31 * @property bool|null $allow_direct_calls Whether direct-call sessions are permitted (defaults to true when null)
32 * @property array $assigned_groups Array of assigned group IDs
33 * @property string $status Project status ('active', 'inactive', 'archived')
34 * @property \App\Http\Models\Auth\User|null $creator The user who created this project
35 * @property \Carbon\Carbon|null $created_at
36 * @property \Carbon\Carbon|null $updated_at
37 */
38class CompanyRolePlayProjectResource extends JsonResource
39{
40    /**
41     * Transform the resource into an array.
42     *
43     * @param \Illuminate\Http\Request $request
44     * @return array<string, mixed>
45     */
46    public function toArray($request): array
47    {
48        $editAuthorization = app(CorporatePersonaEditAuthorizationService::class);
49        $authUser = $request->user();
50        $canEdit = $editAuthorization->canEdit($authUser, $this->resource);
51        $editUrl = $canEdit
52            ? rtrim((string) config('romeo.admin-app-url'), '/')
53                .'/roleplay/personas/'.(string) $this->_id
54            : null;
55
56        return [
57            'id' => (string) $this->_id,
58            'company_id' => $this->company_id,
59            'created_by' => $this->created_by,
60            'creator_name' => $this->whenLoaded('creator', function () {
61                return $this->creator?->first_name . ' ' . $this->creator?->last_name;
62            }),
63            'name' => $this->name,
64            'type' => $this->type,
65            'description' => $this->description,
66            'industry' => $this->industry,
67            'target_job_titles' => $this->target_job_titles ?? [],
68            'company_sizes' => $this->company_sizes ?? [],
69            'key_features' => $this->key_features ?? [],
70            'product_description' => $this->product_description,
71            'difficulty_level' => (int) $this->difficulty_level,
72            'customer_profiles' => $this->customer_profiles ?? [],
73            'scorecard_config' => $this->scorecard_config ?? [],
74            'objections' => $this->objections ?? [],
75            'allow_user_customization' => (bool) $this->allow_user_customization,
76            'allow_clone' => $this->allow_clone === null ? (bool) $this->allow_user_customization : (bool) $this->allow_clone,
77            'allow_direct_calls' => $this->allow_direct_calls === null ? true : (bool) $this->allow_direct_calls,
78            'assigned_groups' => $this->assigned_groups ?? [],
79            'status' => $this->status,
80            'created_at' => $this->created_at?->timestamp,
81            'updated_at' => $this->updated_at?->timestamp,
82            // Per-user aggregates, present when the controller pre-loads them
83            // (currently the user-side `companyProjects` endpoint). Defaults
84            // are zero / null so consumers don't need to special-case absence.
85            'my_sessions_count' => (int) (($this->my_aggregates['sessions_count'] ?? 0)),
86            'my_last_practiced_at' => $this->my_aggregates['last_practiced_at'] ?? null,
87            'my_avg_score' => $this->my_aggregates['avg_score'] ?? null,
88            // Edit-button gating for the end-user roleplay app. The button
89            // is rendered only when `editable_by_current_user` is true; the
90            // FE opens `edit_url` in a new tab. Authorization rule lives in
91            // CorporatePersonaEditAuthorizationService — never duplicate it
92            // on the FE.
93            'editable_by_current_user' => $canEdit,
94            'edit_url' => $editUrl,
95        ];
96    }
97}