44
55use App \Event ;
66use App \Helpers \ExcellenceWinnersHelper ;
7+ use App \Excellence as ExcellenceModel ;
78use Illuminate \Console \Command ;
89use Illuminate \Support \Carbon ;
910use Illuminate \Support \Facades \Log ;
@@ -40,9 +41,10 @@ public function __construct()
4041 public function handle (): void
4142 {
4243
43- $ edition = $ this ->argument ('edition ' );
44+ $ edition = ( int ) $ this ->argument ('edition ' );
4445
45- $ codeweek4all_codes = ExcellenceWinnersHelper::query (Carbon::now ()->year ($ edition ), true )->pluck ('codeweek_for_all_participation_code ' );
46+ $ editionDate = Carbon::create ($ edition , 1 , 1 , 0 , 0 , 0 );
47+ $ codeweek4all_codes = ExcellenceWinnersHelper::query ($ editionDate , true )->pluck ('codeweek_for_all_participation_code ' );
4648
4749 //Select the winners from the Database
4850 $ winners = [];
@@ -55,13 +57,33 @@ public function handle(): void
5557 }
5658 }
5759
58- //Create an excellence record for each winner
60+ $ created = 0 ;
61+ $ existing = 0 ;
62+ $ failed = 0 ;
63+
64+ // Create or update one excellence record per winner.
5965 foreach ($ winners as $ user_id ) {
6066 try {
61- create (\App \Excellence::class, ['edition ' => $ edition , 'user_id ' => $ user_id ]);
67+ $ record = ExcellenceModel::updateOrCreate (
68+ [
69+ 'edition ' => $ edition ,
70+ 'user_id ' => (int ) $ user_id ,
71+ 'type ' => 'Excellence ' ,
72+ ],
73+ []
74+ );
75+
76+ if ($ record ->wasRecentlyCreated ) {
77+ $ created ++;
78+ } else {
79+ $ existing ++;
80+ }
6281 } catch (\Exception $ ex ) {
82+ $ failed ++;
6383 Log::info ($ ex ->getMessage ());
6484 }
6585 }
86+
87+ $ this ->info ("Excellence sync completed. Created: {$ created }, Existing: {$ existing }, Failed: {$ failed }" );
6688 }
6789}
0 commit comments