@@ -495,7 +495,7 @@ public function sanctionedList(Request $request): JsonResponse
495495 $ search = strtolower ($ request ->input ('search ' , '' ));
496496 $ limit = min ((int ) $ request ->input ('limit ' , 100 ), 500 );
497497
498- $ sanctioned = [];
498+ $ sanctionedData = [];
499499 $ count = 0 ;
500500
501501 foreach ($ vessels as $ vessel ) {
@@ -505,6 +505,34 @@ public function sanctionedList(Request $request): JsonResponse
505505 continue ;
506506 }
507507
508+ $ sanctionedData [] = $ vessel ;
509+
510+ $ count ++;
511+ if ($ count >= $ limit ) {
512+ break ;
513+ }
514+ }
515+
516+ if (empty ($ sanctionedData )) {
517+ return response ()->json ([
518+ 'message ' => 'No sanctioned vessels found in the current records. ' ,
519+ ], 404 );
520+ }
521+
522+ // Pre-fetch local vessel data to cross-reference ONLY for the limited result set
523+ $ mmsis = collect ($ sanctionedData )->pluck ('mmsi ' )->filter ()->toArray ();
524+ $ imos = collect ($ sanctionedData )->pluck ('imo ' )->filter ()->toArray ();
525+
526+ $ localVessels = Vessel::whereIn ('mmsi ' , $ mmsis )
527+ ->orWhereIn ('imo ' , $ imos )
528+ ->get (['mmsi ' , 'imo ' , 'last_seen_at ' ]);
529+
530+ $ localByMmsi = $ localVessels ->whereNotNull ('mmsi ' )->keyBy ('mmsi ' );
531+ $ localByImo = $ localVessels ->whereNotNull ('imo ' )->keyBy ('imo ' );
532+
533+ $ sanctioned = [];
534+ foreach ($ sanctionedData as $ vessel ) {
535+ $ name = $ vessel ['name ' ] ?? '' ;
508536 $ sanctioners = $ vessel ['sanctioners ' ] ?? [];
509537 $ sanctions_count = is_array ($ sanctioners ) ? count ($ sanctioners ) : 0 ;
510538
@@ -515,32 +543,33 @@ public function sanctionedList(Request $request): JsonResponse
515543 $ risk_level = 'low ' ;
516544 }
517545
546+ $ mmsi = isset ($ vessel ['mmsi ' ]) ? (int ) $ vessel ['mmsi ' ] : null ;
547+ $ imo = isset ($ vessel ['imo ' ]) ? (int ) $ vessel ['imo ' ] : null ;
548+
549+ $ localMatch = null ;
550+ if ($ mmsi && isset ($ localByMmsi [$ mmsi ])) {
551+ $ localMatch = $ localByMmsi [$ mmsi ];
552+ } elseif ($ imo && isset ($ localByImo [$ imo ])) {
553+ $ localMatch = $ localByImo [$ imo ];
554+ }
555+
518556 // Using fleetleaks data to construct standardized response
519557 $ sanctioned [] = [
520558 'id ' => $ vessel ['id ' ] ?? null ,
521- 'mmsi ' => isset ( $ vessel [ ' mmsi ' ]) ? ( int ) $ vessel [ ' mmsi ' ] : (int ) ($ vessel ['imo ' ] ?? 0 ), // fallback for matching
522- 'imo ' => isset ( $ vessel [ ' imo ' ]) ? ( int ) $ vessel [ ' imo ' ] : null ,
559+ 'mmsi ' => $ mmsi ? : (int ) ($ vessel ['imo ' ] ?? 0 ), // fallback for matching
560+ 'imo ' => $ imo ,
523561 'name ' => $ name ,
524562 'lat ' => (float ) ($ vessel ['latitude ' ] ?? 0 ),
525563 'lng ' => (float ) ($ vessel ['longitude ' ] ?? 0 ),
526564 'course ' => (float ) ($ vessel ['course_degrees ' ] ?? 0 ),
527- 'last_seen_at ' => $ vessel ['location_last_updated ' ] ?? now ()->toIso8601String (),
565+ 'last_seen_at ' => $ vessel ['location_last_updated ' ] ?? null ,
566+ 'sist_last_seen_at ' => $ localMatch ? $ localMatch ->last_seen_at ?->toIso8601String() : null ,
567+ 'in_sist ' => ! is_null ($ localMatch ),
528568 'is_sanctioned ' => true ,
529569 'risk_level ' => $ risk_level ,
530570 'sanctions_count ' => $ sanctions_count ,
531571 'sanctioners ' => $ sanctioners ,
532572 ];
533-
534- $ count ++;
535- if ($ count >= $ limit ) {
536- break ;
537- }
538- }
539-
540- if (empty ($ sanctioned )) {
541- return response ()->json ([
542- 'message ' => 'No sanctioned vessels found in the current records. ' ,
543- ], 404 );
544573 }
545574
546575 return response ()->json (['data ' => $ sanctioned ]);
0 commit comments