diff --git a/app/Enums/PanjarehVahedHistoryActions.php b/app/Enums/PanjarehVahedHistoryActions.php new file mode 100644 index 00000000..ca6f5180 --- /dev/null +++ b/app/Enums/PanjarehVahedHistoryActions.php @@ -0,0 +1,9 @@ +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]); - $harim = Harim::query()->firstOrCreate( + $state = HarimStates::BARESI_EDARE_SHAHRESTAN->value; + + $harim = Harim::query()->firstOrCreate(['panjare_vahed_id' => $request->request_id], [ - 'panjare_vahed_id' => $request->request_id, - ], - [ - 'state_id' => HarimStates::BARESI_EDARE_SHAHRESTAN->value, - 'state_name' => HarimStates::BARESI_EDARE_SHAHRESTAN->label(), + 'state_id' => $state, + 'state_name' => HarimStates::name($state), 'panjare_vahed_id' => $request->request_id, 'national_id' => $request->national_id, 'phone_number' => $request->phone_number, @@ -74,9 +48,14 @@ class PanjareVahedController extends Controller 'response_options' => json_encode($request->response_options), 'isic' => $request->isic, 'primary_area' => json_encode($request->primary_area), - 'data' => $decodedString, - 'token' => $decodedArray['token'], - ]); + 'data' => $request->data, + ] + ); + + $harim->panjarehVahedHistories()->create([ + 'harim_id' => $harim->id, + 'data' => $request->data, + ]); return response()->json([ 'id' => $harim->id, @@ -91,16 +70,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' => HarimStates::BARESI_RAHE_DASTRASI_APLODE_SHODE_TAVASOTE_DAFTAR_HARIM->value, 'state_name' => HarimStates::BARESI_RAHE_DASTRASI_APLODE_SHODE_TAVASOTE_DAFTAR_HARIM->label(), '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', @@ -113,15 +95,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', @@ -133,17 +118,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' => HarimStates::BARESI_ARSE_VA_AYAN_ERSAL_SHODE_TAVASOT_DAFTAR_HARIM->value, 'state_name' => HarimStates::BARESI_ARSE_VA_AYAN_ERSAL_SHODE_TAVASOT_DAFTAR_HARIM->label(), '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', diff --git a/app/Http/Requests/V3/Dashboard/Harim/PanjareVahed/GetAccessRoadRequest.php b/app/Http/Requests/V3/Dashboard/Harim/PanjareVahed/GetAccessRoadRequest.php index bcd4d85d..733a03b6 100644 --- a/app/Http/Requests/V3/Dashboard/Harim/PanjareVahed/GetAccessRoadRequest.php +++ b/app/Http/Requests/V3/Dashboard/Harim/PanjareVahed/GetAccessRoadRequest.php @@ -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', ]; } diff --git a/app/Http/Requests/V3/Dashboard/Harim/PanjareVahed/GetFinalPolygonsRequest.php b/app/Http/Requests/V3/Dashboard/Harim/PanjareVahed/GetFinalPolygonsRequest.php index 1aa06f93..480bc4cf 100644 --- a/app/Http/Requests/V3/Dashboard/Harim/PanjareVahed/GetFinalPolygonsRequest.php +++ b/app/Http/Requests/V3/Dashboard/Harim/PanjareVahed/GetFinalPolygonsRequest.php @@ -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', ]; } diff --git a/app/Http/Requests/V3/Dashboard/Harim/PanjareVahed/GetRejectRequest.php b/app/Http/Requests/V3/Dashboard/Harim/PanjareVahed/GetRejectRequest.php index f270b3a0..b0f34405 100644 --- a/app/Http/Requests/V3/Dashboard/Harim/PanjareVahed/GetRejectRequest.php +++ b/app/Http/Requests/V3/Dashboard/Harim/PanjareVahed/GetRejectRequest.php @@ -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' ]; } diff --git a/app/Models/City.php b/app/Models/City.php index 43dde894..614cc699 100644 --- a/app/Models/City.php +++ b/app/Models/City.php @@ -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; diff --git a/app/Models/Harim.php b/app/Models/Harim.php index e63421ae..6f622b0e 100644 --- a/app/Models/Harim.php +++ b/app/Models/Harim.php @@ -44,5 +44,21 @@ class Harim extends Model $this->update($data); }); + 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); + }, + ); } } diff --git a/app/Models/PanjarehVahedHistory.php b/app/Models/PanjarehVahedHistory.php new file mode 100644 index 00000000..b47736cf --- /dev/null +++ b/app/Models/PanjarehVahedHistory.php @@ -0,0 +1,13 @@ + + */ +class PanjarehVahedHistoryFactory extends Factory +{ + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + return [ + // + ]; + } +} diff --git a/database/migrations/2025_10_15_085149_add_column_to_harims_table.php b/database/migrations/2025_10_15_085149_add_column_to_harims_table.php index 71037b4c..175ff217 100644 --- a/database/migrations/2025_10_15_085149_add_column_to_harims_table.php +++ b/database/migrations/2025_10_15_085149_add_column_to_harims_table.php @@ -13,7 +13,6 @@ return new class extends Migration { Schema::table('harims', function (Blueprint $table) { $table->longText('data')->nullable(); - $table->string('token', 255)->nullable(); }); } @@ -23,7 +22,7 @@ return new class extends Migration public function down(): void { Schema::table('harims', function (Blueprint $table) { - $table->dropColumn(['data', 'token']); + $table->dropColumn('data'); }); } }; diff --git a/database/migrations/2025_11_04_104903_create_panjareh_vahed_histories_table.php b/database/migrations/2025_11_04_104903_create_panjareh_vahed_histories_table.php new file mode 100644 index 00000000..904d2a59 --- /dev/null +++ b/database/migrations/2025_11_04_104903_create_panjareh_vahed_histories_table.php @@ -0,0 +1,30 @@ +id(); + $table->foreignId('harim_id')->constrained('harims'); + $table->longText('data'); + $table->tinyInteger('action')->default(0); // 0 => receive, 1 => send + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('panjareh_vahed_histories'); + } +}; diff --git a/database/seeders/PanjarehVahedHistorySeeder.php b/database/seeders/PanjarehVahedHistorySeeder.php new file mode 100644 index 00000000..3b0930a6 --- /dev/null +++ b/database/seeders/PanjarehVahedHistorySeeder.php @@ -0,0 +1,17 @@ +