33namespace Fleetbase \Http \Controllers \Internal \v1 ;
44
55use Fleetbase \Http \Controllers \FleetbaseController ;
6+ use Fleetbase \Http \Resources \User as UserResource ;
67use Fleetbase \Models \ChatChannel ;
8+ use Fleetbase \Models \ChatParticipant ;
9+ use Fleetbase \Models \User ;
710use Illuminate \Http \Request ;
811
912class ChatChannelController extends FleetbaseController
@@ -15,6 +18,75 @@ class ChatChannelController extends FleetbaseController
1518 */
1619 public $ resource = 'chat_channel ' ;
1720
21+ /**
22+ * Creates a chat channel and optional initial participants.
23+ *
24+ * @return \Fleetbase\Http\Resources\ChatChannel|\Illuminate\Http\Response
25+ */
26+ public function createRecord (Request $ request )
27+ {
28+ try {
29+ $ name = $ request ->input ('chatChannel.name ' );
30+ $ meta = $ request ->input ('chatChannel.meta ' , []);
31+ $ participants = $ request ->array ('chatChannel.participants ' );
32+
33+ $ chatChannel = ChatChannel::create ([
34+ 'company_uuid ' => session ('company ' ),
35+ 'created_by_uuid ' => session ('user ' ),
36+ 'name ' => $ name ,
37+ 'meta ' => $ meta ,
38+ ]);
39+
40+ foreach ($ participants as $ userId ) {
41+ $ user = User::where ('uuid ' , $ userId )->orWhere ('public_id ' , $ userId )->first ();
42+
43+ if (!$ user || $ user ->uuid === session ('user ' )) {
44+ continue ;
45+ }
46+
47+ ChatParticipant::firstOrCreate ([
48+ 'company_uuid ' => session ('company ' ),
49+ 'user_uuid ' => $ user ->uuid ,
50+ 'chat_channel_uuid ' => $ chatChannel ->uuid ,
51+ ]);
52+ }
53+
54+ $ chatChannel ->load (['participants.user ' , 'lastMessage ' ]);
55+ $ this ->resource ::wrap ($ this ->resourceSingularlName );
56+
57+ return new $ this ->resource ($ chatChannel );
58+ } catch (\Exception $ e ) {
59+ return response ()->error (app ()->hasDebugModeEnabled () ? $ e ->getMessage () : 'Unable to create chat channel. ' );
60+ }
61+ }
62+
63+ /**
64+ * Query users available for a new or existing chat channel.
65+ *
66+ * @return \Fleetbase\Http\Resources\UserCollection
67+ */
68+ public function getAvailableParticipants (Request $ request )
69+ {
70+ $ query = $ request ->input ('query ' );
71+ $ chatChannelId = $ request ->input ('channel ' );
72+ $ chatChannel = $ chatChannelId ? ChatChannel::where ('uuid ' , $ chatChannelId )->orWhere ('public_id ' , $ chatChannelId )->first () : null ;
73+
74+ $ users = User::whereHas ('companyUsers ' , function ($ query ) {
75+ $ query ->where ('company_uuid ' , session ('company ' ));
76+ })
77+ ->where ('uuid ' , '!= ' , session ('user ' ))
78+ ->when ($ query , function ($ builder ) use ($ query ) {
79+ $ builder ->search ($ query );
80+ });
81+
82+ if ($ chatChannel ) {
83+ $ participantUserUuids = $ chatChannel ->participants ()->pluck ('user_uuid ' );
84+ $ users ->whereNotIn ('uuid ' , $ participantUserUuids );
85+ }
86+
87+ return UserResource::collection ($ users ->limit (25 )->get ());
88+ }
89+
1890 /**
1991 * Retrieves the unread message count for a specific chat channel.
2092 *
0 commit comments