dimanche 10 janvier 2021

Determine status of multiple collection objects

I essentially have a report model. A Report has one ChartGroup and a ChartGroup can have many Charts. I essentially need to know when a report is ready to be viewed, and this can occur when at least one chart has been processed.

I then need to know when the report is ready to download, and this is when all charts for a report have been processed. This is my basic schema structure with some columns removed to reduce code.

Schema::create('reports', function (Blueprint $table) {
    $table->id();

    $table->boolean('isSubmitted')->nullable()->default(false);
    $table->boolean('isViewable')->nullable()->default(false);
    $table->boolean('reportReady')->nullable()->default(false);
});

Schema::create('chart_group', function (Blueprint $table) {
    $table->id();
    $table->foreignId('report_id');

    $table->foreign('report_id')
        ->references('id')
        ->on('reports')
        ->onDelete('cascade');
});

Schema::create('charts', function (Blueprint $table) {
    $table->id();
    $table->string('chart_name')->nullable();
    $table->boolean('chart_processed')->default(false);
    $table->foreignId('chart_group_id');

    $table->foreign('chart_group_id')
        ->references('id')
        ->on('chart_group')
        ->onDelete('cascade');
});

And then within a Laravel job, I am getting all reports and looping them. I then check the charts for a report and if it has not been processed I process it. If processed successgully, I update the chart and mark it as processed (other code).

foreach ($reports as $report) {
    foreach ($report->chartGroup->chart as $chart) {
        if (!$chart->report_processed) {
            if ($this->queryStatus($chart)) {
                if (!$report->isViewable) {
                    $report->isViewable = true;
                    $report->saveOrFail();
                }
            }
        }
    }
}

So this works fine so far, if a report has a chart that needs processing, its processed and that chart is marked as such. The problem I am having is this. A report may have 5 charts. I need a way to identify when all of the charts have been processed so I can mark the report as being ready.

How could I go about doing this?

Thanks



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire