Merge pull request #87 from witelgroup/bugfix/HarimWebservice

create history for webservice
This commit is contained in:
Amir Ghasempoor
2025-11-09 13:38:25 +03:30
committed by GitHub
12 changed files with 171 additions and 81 deletions

View File

@@ -0,0 +1,9 @@
<?php
namespace App\Enums;
enum PanjarehVahedHistoryActions : int
{
case RECEIVE = 0;
case SEND = 1;
}

View File

@@ -11,7 +11,6 @@ use App\Http\Requests\V3\Dashboard\Harim\PanjareVahed\ReceiveNewRequest;
use App\Http\Traits\ApiResponse;
use App\Models\City;
use App\Models\Harim;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\DB;
@@ -21,64 +20,41 @@ class PanjareVahedController extends Controller
public function receiveNewRequest(ReceiveNewRequest $request): JsonResponse
{
$iv = config('harim_web_services.Harim_Info.iv');
$keyString = config('harim_web_services.Harim_Info.key');
$key = hash('sha256',$keyString, true);
$decrypted = openssl_decrypt($request->data,'aes-256-cbc',$key,false, $iv);
$decodedString = json_decode($decrypted);
$decodedArray = json_decode($decodedString, true);
$wkt = 'POLYGON(('.implode(',', array_map(fn ($c) => "{$c[0]} {$c[1]}", $request->primary_area['coordinates'])).'))';
$result = DB::selectOne('SELECT ST_AsText(ST_PointOnSurface(ST_GeomFromText(?, 4326))) AS point', [$wkt]);
preg_match('/POINT\(([^ ]+) ([^ ]+)\)/', $result->point, $matches);
try {
$city = City::query()
->whereRaw("ST_Contains(
geometry,
ST_GeomFromText('POINT($matches[1] $matches[2])', 4326))"
)
->firstOrFail(['id', 'name_fa', 'province_id']);
} catch (ModelNotFoundException $exception) {
$city = City::query()
->orderByRaw("ST_Distance(
geometry,
ST_GeomFromText('POINT($matches[1] $matches[2])', 4326)) ASC"
)
->first(['id', 'name_fa', 'province_id']);
}
$city = City::findCityWithLatLng(lat:$matches[2], lng: $matches[1]);
$state = HarimStates::BARESI_EDARE_SHAHRESTAN->value;
$harim = Harim::query()->firstOrCreate(
$harim = Harim::query()->firstOrCreate(['panjare_vahed_id' => $request->request_id],
[
'panjare_vahed_id' => $request->request_id
],
[
'state_id' => $state,
'state_name' => HarimStates::name($state),
'panjare_vahed_id' => $request->request_id,
'national_id' => $request->national_id,
'phone_number' => $request->phone_number,
'request_date' => $request->request_date,
'province_id' => $city->province_id,
'province_name' => $city->province()->first()->name_fa,
'city_id' => $city->id,
'city_name' => $city->name_fa,
'edareh_shahri_id' => $city->edarateShahri()->first()->id,
'requested_organization' => $request->requested_organization,
'plan_group' => $request->plan_group,
'plan_title' => $request->plan_title,
'worksheet_id' => $request->worksheet,
'response_options' => json_encode($request->response_options),
'isic' => $request->isic,
'primary_area' => json_encode($request->primary_area),
'data' => $decodedString,
'token' => $decodedArray['token'],
'state_id' => $state,
'state_name' => HarimStates::name($state),
'panjare_vahed_id' => $request->request_id,
'national_id' => $request->national_id,
'phone_number' => $request->phone_number,
'request_date' => $request->request_date,
'province_id' => $city->province_id,
'province_name' => $city->province()->first()->name_fa,
'city_id' => $city->id,
'city_name' => $city->name_fa,
'edareh_shahri_id' => $city->edarateShahri()->first()->id,
'requested_organization' => $request->requested_organization,
'plan_group' => $request->plan_group,
'plan_title' => $request->plan_title,
'worksheet_id' => $request->worksheet,
'response_options' => json_encode($request->response_options),
'isic' => $request->isic,
'primary_area' => json_encode($request->primary_area),
'data' => $request->data,
]
);
$harim->panjarehVahedHistories()->create([
'harim_id' => $harim->id,
'data' => $request->data,
]);
return response()->json([
@@ -96,16 +72,19 @@ class PanjareVahedController extends Controller
$harim = Harim::query()->firstWhere('panjare_vahed_id', '=', $request->request_id);
$harim->panjarehVahedHistories()->create([
'harim_id' => $harim->id,
'data' => $harim->data,
]);
$harim->update([
'state_id' => $state,
'state_name' => HarimStates::name($state),
'access_road' => json_encode($request->access_road),
'data' => $request->data['data'],
'token' => $request->data['token'],
'data' => $request->data,
]);
return response()->json([
'request_id' => $request->request_id,
'id' => $harim->id,
'statusCode' => 200,
'message' => 'successful',
@@ -118,15 +97,18 @@ class PanjareVahedController extends Controller
$harim = Harim::query()->firstWhere('panjare_vahed_id', '=', $request->request_id);
$harim->panjarehVahedHistories()->create([
'harim_id' => $harim->id,
'data' => $harim->data,
]);
$harim->update([
'state_id' => $state,
'state_name' => HarimStates::name($state),
'data' => $request->data['data'],
'token' => $request->data['token'],
'data' => $request->data,
]);
return response()->json([
'request_id' => $request->request_id,
'id' => $harim->id,
'statusCode' => 200,
'message' => 'successful',
@@ -139,17 +121,20 @@ class PanjareVahedController extends Controller
$harim = Harim::query()->firstWhere('panjare_vahed_id', '=', $request->request_id);
$harim->panjarehVahedHistories()->create([
'harim_id' => $harim->id,
'data' => $harim->data,
]);
$harim->update([
'state_id' => $state,
'state_name' => HarimStates::name($state),
'final_area' => json_encode($request->final_area),
'final_plan' => json_encode($request->final_plan),
'data' => $request->data['data'],
'token' => $request->data['token'],
'data' => $request->data,
]);
return response()->json([
'request_id' => $request->request_id,
'id' => $harim->id,
'statusCode' => 200,
'message' => 'successful',

View File

@@ -29,7 +29,7 @@ class GetAccessRoadRequest extends FormRequest
'access_road' => 'required|array',
'access_road.type' => 'required|string',
'access_road.coordinates' => 'required|array',
'data' => 'required|array',
'data' => 'required|string',
];
}

View File

@@ -32,7 +32,7 @@ class GetFinalPolygonsRequest extends FormRequest
'final_plan' => 'required|array',
'final_plan.type' => 'required|string',
'final_plan.coordinates' => 'required|array',
'data' => 'required|array',
'data' => 'required|string',
];
}

View File

@@ -26,7 +26,8 @@ class GetRejectRequest extends FormRequest
{
return [
'request_id' => 'required|exists:harims,panjare_vahed_id',
'data' => 'required|array',
'data' => 'required|string',
'status' => 'required|integer'
];
}

View File

@@ -86,23 +86,18 @@ class City extends Model
{
try {
$city = City::query()
->whereRaw("
ST_Contains(
geometry,
ST_GeomFromText('POINT($lng $lat)', 4326)
)
")
->firstOrFail();
}
catch (ModelNotFoundException $exception) {
->whereRaw("ST_Contains(
geometry,
ST_GeomFromText('POINT($lng $lat)', 4326))"
)
->firstOrFail(['id', 'name_fa', 'province_id']);
} catch (ModelNotFoundException $exception) {
$city = City::query()
->orderByRaw("
ST_Distance(
geometry,
ST_GeomFromText('POINT($lng $lat)', 4326)
) ASC
")
->first();
->orderByRaw("ST_Distance(
geometry,
ST_GeomFromText('POINT($lng $lat)', 4326)) ASC"
)
->first(['id', 'name_fa', 'province_id']);
}
return $city;

View File

@@ -23,4 +23,22 @@ class Harim extends Model
get: fn($value) => json_decode($value)
);
}
public function panjarehVahedHistories(): HasMany
{
return $this->hasMany(PanjarehVahedHistory::class);
}
protected function data(): Attribute
{
return Attribute::make(
set: function ($value) {
$iv = config('harim_web_services.Harim_Info.iv');
$keyString = config('harim_web_services.Harim_Info.keyString');
$key = hash('sha256',$keyString, true);
$decrypted = openssl_decrypt($value,'aes-256-cbc',$key,false, $iv);
return json_decode($decrypted);
},
);
}
}

View File

@@ -0,0 +1,13 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class PanjarehVahedHistory extends Model
{
use HasFactory;
protected $guarded = [];
}