aboutsummaryrefslogtreecommitdiffstats
path: root/source/app/Http/Controllers/MessageController.php
blob: e92c5206eed87e01ad91d603ee12ab12984ab527 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
<?php
//Link AI: https://chatgpt.com/share/684fd09e-f0c0-8005-90e4-9f3e6d9cbdee
namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use Illuminate\Validation\ValidationException;

class MessageController extends Controller
{
    /**
     * Haal het gesprek op tussen een student en een bedrijf
     */
    public function getConversation(Request $request)
    {
        try {
            $validated = $request->validate([
                'user1_id' => 'required|integer',
                'user1_type' => 'required|string',
                'user2_id' => 'required|integer',
                'user2_type' => 'required|string',
            ]);
        } catch (ValidationException $e) {
            if ($request->expectsJson()) {
                return response()->json(['errors' => $e->errors()], 422);
            }
            return redirect()->back()->withErrors($e->errors());
        }

        // API call to get conversation
        $response = Http::post("{$this->messagesApiUrl}/conversation", $validated);

        if (!$response->successful()) {
            if ($request->expectsJson()) {
                return response()->json(['error' => 'Failed to fetch conversation'], 500);
            }
            return redirect()->back()->withErrors(['error' => 'Failed to fetch conversation']);
        }

        $responseData = $response->json();
        $messages = $responseData['conversation'] ?? [];

        if ($request->expectsJson()) {
            return response()->json([
                'success' => true,
                'data' => $messages
            ]);
        }

        return redirect()->back()->with('messages', $messages);
    }

    /**
     * Verzend een nieuw bericht
     */
    public function sendMessage(Request $request)
    {
        try {
            $validated = $request->validate([
                'sender_id' => 'required|integer',
                'sender_type' => 'required|string',
                'receiver_id' => 'required|integer',
                'receiver_type' => 'required|string',
                'content' => 'required|string|max:1000',
            ]);
        } catch (ValidationException $e) {
            if ($request->expectsJson()) {
                return response()->json(['errors' => $e->errors()], 422);
            }
            return redirect()->back()->withErrors($e->errors());
        }

        // API call to send message
        $response = Http::post("{$this->messagesApiUrl}/send", $validated);

        if (!$response->successful()) {
            $errorMessage = 'Failed to send message';
            if ($response->status() === 422) {
                $errorData = $response->json();
                $errorMessage = $errorData['message'] ?? 'Validation error';
            }

            if ($request->expectsJson()) {
                return response()->json(['error' => $errorMessage], 500);
            }
            return redirect()->back()->withErrors(['error' => $errorMessage]);
        }

        $message = $response->json('data');

        if ($request->expectsJson()) {
            return response()->json([
                'success' => true,
                'message' => 'Bericht succesvol verzonden!',
                'data' => $message
            ]);
        }

        return redirect()->back()->with('messageRes', 'Bericht succesvol verzonden!');
    }

    /**
     * Haal alle berichten op voor een specifieke student
     */
    public function getAllMessagesForStudent($studentId)
    {
        // Get all companies instead of just connected ones
        $response = Http::get($this->companiesApiUrl);

        if (!$response->successful()) {
            return [];
        }

        $companiesData = $response->json('data');
        $companies = $companiesData ?? [];

        if (empty($companies)) {
            return [];
        }

        $groupedMessages = [];

        // For each company, fetch the conversation with the student
        foreach ($companies as $company) {
            $companyId = $company['id'];

            // Prepare data for conversation API call
            $conversationData = [
                'user1_id' => (int)$studentId,
                'user1_type' => 'App\\Models\\Student',
                'user2_id' => (int)$companyId,
                'user2_type' => 'App\\Models\\Company',
            ];

            // API call to get conversation between student and this company
            $response = Http::post("{$this->messagesApiUrl}/conversation", $conversationData);

            if ($response->successful()) {
                $responseData = $response->json();
                $messages = $responseData['conversation'] ?? []; // Use 'conversation' key from API response

                if (!empty($messages)) {
                    $groupedMessages[$companyId] = $messages;
                }
            }
        }

        return $groupedMessages;
    }

    /**
     * Haal alle berichten op voor een specifiek bedrijf
     */
    public function getAllMessagesForCompany($companyId)
    {
        Log::info('getAllMessagesForCompany called', ['companyId' => $companyId]);

        // Get all students instead of just connected ones
        $response = Http::get($this->studentsApiUrl);

        if (!$response->successful()) {
            Log::error('Failed to fetch students', [
                'status' => $response->status(),
                'url' => $this->studentsApiUrl
            ]);
            return [];
        }

        $studentsData = $response->json('data');
        $students = $studentsData ?? [];

        Log::info('Students fetched', ['count' => count($students)]);

        if (empty($students)) {
            Log::warning('No students found');
            return [];
        }

        $groupedMessages = [];

        // For each student, fetch the conversation with the company
        foreach ($students as $student) {
            $studentId = $student['id'];

            // Prepare data for conversation API call
            $conversationData = [
                'user1_id' => (int)$companyId,
                'user1_type' => 'App\\Models\\Company',
                'user2_id' => (int)$studentId,
                'user2_type' => 'App\\Models\\Student',
            ];

            Log::info('Making conversation API call', [
                'url' => "{$this->messagesApiUrl}/conversation",
                'data' => $conversationData
            ]);

            // API call to get conversation between company and this student
            $response = Http::post("{$this->messagesApiUrl}/conversation", $conversationData);

            Log::info('Conversation API response', [
                'student_id' => $studentId,
                'status' => $response->status(),
                'successful' => $response->successful(),
                'body' => $response->body()
            ]);

            if ($response->successful()) {
                $responseData = $response->json();
                $messages = $responseData['conversation'] ?? []; // Use 'conversation' key from API response

                if (!empty($messages)) {
                    $groupedMessages[$studentId] = [
                        'user_id' => $studentId,
                        'messages' => $messages
                    ];
                    Log::info('Messages found for student', [
                        'student_id' => $studentId,
                        'message_count' => count($messages)
                    ]);
                } else {
                    Log::info('No messages found for student', ['student_id' => $studentId]);
                }
            } else {
                Log::error('Conversation API call failed', [
                    'student_id' => $studentId,
                    'status' => $response->status(),
                    'response' => $response->body()
                ]);
            }
        }

        Log::info('getAllMessagesForCompany result', [
            'total_conversations' => count($groupedMessages)
        ]);

        return $groupedMessages;
    }
}