Skip to content

Commit

Permalink
fix bcr projects
Browse files Browse the repository at this point in the history
  • Loading branch information
gheorghelupu17 committed Nov 27, 2023
1 parent 8e35cae commit edae666
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 83 deletions.
84 changes: 42 additions & 42 deletions app/Console/Commands/Import/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,48 +139,48 @@ public function parseDate(?string $input): ?Carbon
public function mapCounty(int $oldCountyId): int
{
$countiesMap = [
1 => 1,//Alba
2 => 2,//Arad
3 => 3,//Arges
4 => 4,//Bacau
5 => 5,//Bihor
6 => 6,//Bistrita-Nasaud
7 => 7,//Botosani
8 => 9,//Braila
9 => 8,//Brasov
10 => 40,//Bucuresti
11 => 10,//Buzau
12 => 51,//Calarasi
13 => 11,//Caras-Severin
14 => 12,//Cluj
15 => 13,//Constanta
16 => 14,//Covasna
17 => 15,//Dambovita
18 => 16,//Dolj
19 => 17,//Galati
20 => 52,//Giurgiu
21 => 18,//Gorj
22 => 19,//Harghita
23 => 20,//Hunedoara
24 => 21,//Ialomita
25 => 22,//Iasi
26 => 23,//Ilfov
27 => 24,//Maramures
28 => 25,//Mehedinti
29 => 26,//Mures
30 => 27,//Neamt
31 => 28,//Olt
32 => 29,//Prahova
33 => 31,//Salaj
34 => 30,//Satu Mare
35 => 32,//Sibiu
36 => 33,//Suceava
37 => 34,//Teleorman
38 => 35,//Timis
39 => 36,//Tulcea
40 => 38,//Valcea
41 => 37,//Vaslui
42 => 39,//Vrancea
1 => 1, //Alba
2 => 2, //Arad
3 => 3, //Arges
4 => 4, //Bacau
5 => 5, //Bihor
6 => 6, //Bistrita-Nasaud
7 => 7, //Botosani
8 => 9, //Braila
9 => 8, //Brasov
10 => 40, //Bucuresti
11 => 10, //Buzau
12 => 51, //Calarasi
13 => 11, //Caras-Severin
14 => 12, //Cluj
15 => 13, //Constanta
16 => 14, //Covasna
17 => 15, //Dambovita
18 => 16, //Dolj
19 => 17, //Galati
20 => 52, //Giurgiu
21 => 18, //Gorj
22 => 19, //Harghita
23 => 20, //Hunedoara
24 => 21, //Ialomita
25 => 22, //Iasi
26 => 23, //Ilfov
27 => 24, //Maramures
28 => 25, //Mehedinti
29 => 26, //Mures
30 => 27, //Neamt
31 => 28, //Olt
32 => 29, //Prahova
33 => 31, //Salaj
34 => 30, //Satu Mare
35 => 32, //Sibiu
36 => 33, //Suceava
37 => 34, //Teleorman
38 => 35, //Timis
39 => 36, //Tulcea
40 => 38, //Valcea
41 => 37, //Vaslui
42 => 39, //Vrancea

];

Expand Down
118 changes: 82 additions & 36 deletions app/Console/Commands/Import/ImportProjectsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
namespace App\Console\Commands\Import;

use App\Enums\ProjectStatus;
use App\Models\BCRProject;
use App\Models\Project;
use App\Models\ProjectCategory;
use App\Services\Sanitize;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Throwable;

Expand Down Expand Up @@ -89,7 +91,7 @@ protected function importProjects(): void
->selectRaw("STRING_AGG(CountyId,',')")
->whereColumn('dbo.ProjectCounties.ProjectId', 'dbo.ONGProjects.Id'),
])
->join('dbo.Projects', 'dbo.Projects.Id', 'dbo.ONGProjects.Id')
->rightJoin('dbo.Projects', 'dbo.Projects.Id', 'dbo.ONGProjects.Id')
->orderBy('dbo.ONGProjects.Id');

$this->createProgressBar(
Expand All @@ -105,41 +107,9 @@ protected function importProjects(): void
->each(function (object $row) {
$created_at = Carbon::parse($row->CreationDate);
try {
$project = Project::forceCreate([
'id' => (int) $row->Id,
'organization_id' => (int) $row->ONGId,
'name' => Sanitize::text($row->Name),
'slug' => Sanitize::text($row->DynamicUrl),
'description' => Sanitize::text($row->Description),
'target_budget' => (int) $row->TargetAmmount,
'start' => $this->parseDate($row->StartDate),
'end' => $this->parseDate($row->EndDate),
'accepting_volunteers' => (bool) $row->HasVolunteering,
'accepting_comments' => (bool) $row->AcceptComments,
'reason_to_donate' => Sanitize::text($row->WhyDonate),
'scope' => Sanitize::text($row->ProjectPurpose),
'beneficiaries' => Sanitize::text($row->ToWhomDoIDonate),

'created_at' => $created_at,
'updated_at' => $created_at,
'archived_at' => $row->ProjectStatusTypeId === 3 ? $created_at : null,

'status' => match ($row->ProjectStatusTypeId) {
1 => ProjectStatus::pending,
2 => ProjectStatus::approved,
3 => ProjectStatus::approved,//set arhivat
4 => ProjectStatus::approved,
default => ProjectStatus::draft,
},
]);

$project->categories()->attach($row->ProjectCategoryId);

$mappedCounties = collect(explode(',', $row->Counties ?? ''))
->filter(fn (string $county) => $county !== '')
->map(fn (int $county) => $this->mapCounty($county))
->all();
$project->counties()->attach($mappedCounties);
$project = empty($row->ONGId) ?
$this->createBcrProject($row, $created_at) :
$this->createProject($row, $created_at);

if (! $this->option('skip-files')) {
// Add main image
Expand All @@ -164,4 +134,80 @@ protected function importProjects(): void
: 'Imported projects'
);
}

private function createProject(object $row, Carbon $created_at): Model|Project
{
$project = Project::forceCreate([
'id' => (int) $row->Id,
'organization_id' => (int) $row->ONGId,
'name' => Sanitize::text($row->Name),
'slug' => Sanitize::text($row->DynamicUrl),
'description' => Sanitize::text($row->Description),
'target_budget' => (int) $row->TargetAmmount,
'start' => $this->parseDate($row->StartDate),
'end' => $this->parseDate($row->EndDate),
'accepting_volunteers' => (bool) $row->HasVolunteering,
'accepting_comments' => (bool) $row->AcceptComments,
'reason_to_donate' => Sanitize::text($row->WhyDonate),
'scope' => Sanitize::text($row->ProjectPurpose),
'beneficiaries' => Sanitize::text($row->ToWhomDoIDonate),

'created_at' => $created_at,
'updated_at' => $created_at,
'archived_at' => $row->ProjectStatusTypeId === 3 ? $created_at : null,

'status' => match ($row->ProjectStatusTypeId) {
1 => ProjectStatus::pending,
2 => ProjectStatus::approved,
3 => ProjectStatus::approved,//set arhivat
4 => ProjectStatus::approved,
default => ProjectStatus::draft,
},
]);

$project->categories()->attach($row->ProjectCategoryId);

$mappedCounties = collect(explode(',', $row->Counties ?? ''))
->filter(fn (string $county) => $county !== '')
->map(fn (int $county) => $this->mapCounty($county))
->all();
$project->counties()->attach($mappedCounties);

return $project;
}

private function createBcrProject(object $row, Carbon $created_at): Model|BCRProject
{
$mappedCounties = collect(explode(',', $row->Counties ?? ''))
->filter(fn (string $county) => $county !== '')
->map(fn (int $county) => $this->mapCounty($county));

$countyID = $mappedCounties?->first() ?? null;


return BCRProject::forceCreate(
[
'id' => (int) $row->Id,
'name' => Sanitize::text($row->Name),
'slug' => Sanitize::text($row->DynamicUrl),
'description' => Sanitize::text($row->Description),
'project_category_id' => $row->ProjectCategoryId,
'county_id' => $countyID,
'start_date' => $this->parseDate($row->StartDate),
'end_date' => $this->parseDate($row->EndDate),
'is_national' => (bool) $row->AllCounties,
'facebook_link' => $row->FacebookPageLink,
'created_at' => $created_at,
'updated_at' => $created_at,

'status' => match ($row->ProjectStatusTypeId) {
1 => ProjectStatus::pending,
2 => ProjectStatus::approved,
3 => ProjectStatus::approved,//set arhivat
4 => ProjectStatus::approved,
default => ProjectStatus::draft,
},
]
);
}
}
8 changes: 6 additions & 2 deletions app/Filament/Resources/BCRProjectResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@

namespace App\Filament\Resources;

use App\Enums\ProjectStatus;
use App\Filament\Resources\BCRProjectResource\Pages;
use App\Models\Article;
use App\Models\BCRProject;
use Camya\Filament\Forms\Components\TitleWithSlugInput;
use Filament\Forms\Components\DatePicker;
use Filament\Forms\Components\Grid;
use Filament\Forms\Components\Repeater;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\SpatieMediaLibraryFileUpload;
Expand Down Expand Up @@ -78,6 +77,11 @@ public static function form(Form $form): Form
->inlineLabel()
->columnSpanFull()
->required(),
Select::make('status')
->label('bcr-project.labels.status')
->inlineLabel()
->options(ProjectStatus::options())
->required(),

Textarea::make('description')
->label(__('bcr-project.labels.description'))
Expand Down
5 changes: 3 additions & 2 deletions app/Http/Resources/BcrProjectResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ class BcrProjectResource extends JsonResource

public function toArray(Request $request): array
{

return [
'id' => $this->id,
'name' => $this->name,
'slug' => $this->slug,
'county' => $this->county->name,
'county' => $this?->county?->name,
'image' => $this->getFirstMediaUrl('preview'),
'gallery' => $this->getMedia('gallery')->map(function ($media) {
return [
Expand All @@ -26,7 +27,7 @@ public function toArray(Request $request): array
];
})->toArray(),
'is_national' => \boolval($this->is_national),
'area' => $this->is_national ? __('bcr-project.labels.national') : $this->county->name,
'area' => $this->is_national ? __('bcr-project.labels.national') : $this?->county?->name,
'start' => $this->start_date?->format('d.m.Y'),
'end' => $this->end_date?->format('d.m.Y'),
'description' => $this->description,
Expand Down
2 changes: 2 additions & 0 deletions app/Models/BCRProject.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class BCRProject extends Model implements HasMedia
use InteractsWithMedia;
use HasSlug;

protected $table='bcr_projects';

protected $fillable = [
'name',
'description',
Expand Down
2 changes: 2 additions & 0 deletions database/factories/BCRProjectFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Database\Factories;

use App\Enums\ProjectStatus;
use App\Models\City;
use App\Models\ProjectCategory;
use Illuminate\Database\Eloquent\Factories\Factory;
Expand Down Expand Up @@ -37,6 +38,7 @@ public function definition(): array
'description' => $this->faker->paragraph(),
'start_date' => $this->faker->dateTimeBetween('-1 day', 'now'),
'end_date' => $this->faker->dateTimeBetween('now', '+1 day'),
'status' => $this->faker->randomElement(ProjectStatus::options()),
'external_links' => [
[
'title' => $this->faker->sentence(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
public function up(): void
{
Schema::create('b_c_r_projects', function (Blueprint $table) {
Schema::create('bcr_projects', function (Blueprint $table) {
$table->id();
$table->foreignIdFor(ProjectCategory::class)
->nullable()
Expand All @@ -32,6 +32,7 @@ public function up(): void
->cascadeOnDelete();
$table->string('name');
$table->string('slug')->unique();
$table->string('status');
$table->text('description');
$table->date('start_date');
$table->date('end_date');
Expand Down

0 comments on commit edae666

Please sign in to comment.