Untitled

mail@pastecode.io avatar
unknown
php
2 years ago
2.5 kB
1
Indexable
<?php

namespace App\Imports;

use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
use Maatwebsite\Excel\Concerns\SkipsEmptyRows;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\WithChunkReading;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Maatwebsite\Excel\Concerns\WithStartRow;
use Maatwebsite\Excel\Concerns\WithValidation;

class OrganizationImport implements ToCollection,
    WithStartRow,
    WithChunkReading,
    ShouldQueue,
    WithHeadingRow,
    WithValidation,
    SkipsEmptyRows
{

    public function collection(Collection $collection)
    {
        $method = 'post';
        $url =
            config('apiendpoint.base_url_emeeting') .
            config('apiendpoint.eventmgmt.service').'api/organization_unit' ;


        $token = get_jwt_token();

        foreach ($collection as $row) {
            $req_data = [
                'code' => Arr::get($row, 'org_code'),
                'parent_org_code' => Arr::get($row, 'parent_org_code'),
                'stext' => Arr::get($row, 'stext'),
                'ltext' => Arr::get($row, 'ltext'),
                'personnel_area_code' => Arr::get($row, 'personnel_area'),
                'personnel_sub_area_code' => Arr::get($row, 'personnel_sub_area'),
                'show_in_dropdown' => Arr::get($row, 'show_in_dropdown')
            ];
            $api = api_call(
                $method,
                [
                    'Authorization' => 'Bearer ' . $token,
                    'Content-Type' => 'application/json',
                ],
                $url,
                ['form_params' => $req_data]
            );
            $results = (array) json_decode((string) $api->getBody(), true);

            Log::info('result import organization', ['data' => $results]);
        }
    }

    public function chunkSize(): int
    {
        return 500;
    }

    public function startRow(): int
    {
        return 2;
    }

    public function headingRow(): int
    {
        return 1;
    }

    public function rules(): array
    {
        return [
            'org_code' => 'required',
            'stext' => 'required',
            'ltext' => 'required',
            'personnel_area' => ['required'],
            'personnel_sub_area' => ['required'],
            'parent_org_code' => 'nullable',
            'show_in_dropdown' => ['required', 'boolean']
        ];
    }
}