params = $params; $this->task = $task; $this->vdb_env = config('services.vdb.env'); if ($this->vdb_env == 'PROD') { $this->apiToken = config('services.vdb.prod.auth_token'); $this->apiKey = config('services.vdb.prod.api_key'); } else { $this->apiToken = config('services.vdb.dev.auth_token'); $this->apiKey = config('services.vdb.dev.api_key'); } } /** * Execute the job. * * @return void */ public function handle() { $this->task->update(['status' => 'in_progress']); $allData = []; // $totalItems = 6463; $totalItems = 10107; $itemsPerRequest = 50; $totalPages = ceil($totalItems / $itemsPerRequest); $headers = [ 'Authorization' => "Token token={$this->apiToken}, api_key={$this->apiKey}", ]; for ($page = 1; $page <= $totalPages; $page++) { $response = Http::withHeaders($headers) ->get(config('services.vdb.url') . 'diamonds', array_merge($this->params, [ 'page_number' => $page, 'page_size' => $itemsPerRequest, ])); if ($response->successful()) { $allData = array_merge($allData, $response->json()['response']['body']['diamonds']); } else { ## Handle errors here throw new \Exception("Error fetching data for page $page"); } ## Optional: Add a delay to avoid API rate limits sleep(2); } ## Now process and store the CSV file $this->storeCSV($allData); } public function storeCSV($data) { $csvData = []; try { foreach ($data as $item) { $csvData[] = [ 'Id' => $item['id'], 'Stock_Number' => $item['stock_num'], 'Vendor_Id' => $item['vendor_id'], 'Total_Sales_Price' => $item['total_sales_price'], 'Carat' => $item['size'], 'Country' => $item['country'], 'Shape' => $item['shape'], 'Color' => $item['color'], 'Clarity' => $item['clarity'], 'Cut' => $item['cut'], 'Price_Per_Carat' => $item['price_per_carat'], ]; } // Generate filename with timestamp $filename = 'lab_grown_diamond_data_' . now()->format('Y_m_d_H_i_s') . '.csv'; // Open a temporary file for writing $file = fopen('php://temp', 'w+'); // Write the CSV headers fputcsv($file, array_keys($csvData[0])); // Write the data foreach ($csvData as $row) { fputcsv($file, $row); } // Rewind the file pointer to the beginning rewind($file); // Store the file on S3 (this uploads the file to S3) $path = 'exported_diamonds/' . $filename; // Storage::disk('s3')->put($path, stream_get_contents($file)); Storage::disk('s3_storage')->put($path, stream_get_contents($file)); // Close the file fclose($file); // Update task status to completed $this->task->update(['status' => 'completed']); // Prepare the notification for task completion $notification = [ 'type' => 'task_completed', 'msg_short_title' => 'Task Completed', 'message' => 'lab grown diamomd sheet has been generated.', 'context_id' => $this->task->id, 'context_type' => 'task', 'recipient_id' => 1, 'status' => 'true', ]; } catch (\Throwable $th) { // Update task status to failed $this->task->update(['status' => 'failed']); // Prepare the notification for task failure $notification = [ 'type' => 'task_failed', 'msg_short_title' => 'Task Failed', 'message' => 'Faild to download the lab grown diamond sheet.', 'context_id' => $this->task->id, 'context_type' => 'task', 'recipient_id' => 1, 'status' => 'true', ]; } // Save notification Notifications::create($notification); // Log the saved file information $savedData = [ 'type' => 'diamond', 'source' => 'vdb', 'filename' => $filename, ]; SaveExternalLog::create($savedData); } }