diff --git a/land_registry/src/land_register.cairo b/land_registry/src/land_register.cairo index 104b0c9c..4cb655b4 100644 --- a/land_registry/src/land_register.cairo +++ b/land_registry/src/land_register.cairo @@ -603,45 +603,21 @@ pub mod LandRegistryContract { history } - - // fn get_inspector_pending_approvals( - // self: @ContractState, inspector: ContractAddress - // ) -> Array { - // // Verify inspector is registered - // assert(self.registered_inspectors.read(inspector), Errors::NOT_REGISTERED_INSP); - - // let mut pending_lands = array![]; - // let total_lands = self.land_count.read(); - // let mut i: u256 = 1; // Starting from 1 since we store in lands_registry from index 1 - - // // Iterate through all lands - // while i < total_lands + 1 { - // let land = self.lands_registry.read(i); - - // // Check if land is assigned to this inspector and is pending - // if land.inspector == inspector && land.status == LandStatus::Pending { - // pending_lands.append(land); - // } - - // i += 1; - // }; - - // pending_lands - // } - fn get_inspector_pending_approvals( self: @ContractState, inspector: ContractAddress, start_time: u64, end_time: u64 ) -> Array { - // Verify inspector is registered assert(self.registered_inspectors.read(inspector), Errors::NOT_REGISTERED_INSP); let mut pending_lands = array![]; - let total_lands = self.land_count.read(); - let mut i: u256 = 1; // Starting from 1 since we store in lands_registry from index 1 + let land_count = self.land_count.read(); + + let mut current_land_id: u256 = 0; + loop { + if current_land_id >= land_count { + break; + } - // Iterate through all lands - while i < total_lands + 1 { - let land = self.lands_registry.read(i); + let land = self.lands.read(current_land_id); if land.inspector == inspector && land.status == LandStatus::Pending @@ -650,7 +626,7 @@ pub mod LandRegistryContract { pending_lands.append(land); } - i += 1; + current_land_id += 1; }; pending_lands diff --git a/land_registry/tests/test_land_register.cairo b/land_registry/tests/test_land_register.cairo index f8f16fbc..d81a0ed4 100644 --- a/land_registry/tests/test_land_register.cairo +++ b/land_registry/tests/test_land_register.cairo @@ -1250,4 +1250,36 @@ fn test_set_land_inspector() { "Inspector address should match the assigned address", ); stop_cheat_caller_address(contract_address); -} \ No newline at end of file +} + +#[test] +#[should_panic(expected: ('Inspector not registered',))] +fn test_get_inspector_pending_approvals_unauthorized() { + let contract_address = deploy("LandRegistryContract"); + let land_register_dispatcher = ILandRegistryDispatcher { contract_address }; + + let unauthorized = starknet::contract_address_const::<0x789>(); + start_cheat_caller_address(contract_address, unauthorized); + land_register_dispatcher.get_inspector_pending_approvals(unauthorized, 1, 2); + stop_cheat_caller_address(contract_address); +} + +#[test] +fn test_get_inspector_pending_approvals_empty_range() { + let contract_address = deploy("LandRegistryContract"); + let land_register_dispatcher = ILandRegistryDispatcher { contract_address }; + + let owner_address = starknet::contract_address_const::<0x123>(); + let inspector_address = starknet::contract_address_const::<0x456>(); + + // Register inspector + start_cheat_caller_address(contract_address, owner_address); + land_register_dispatcher.add_inspector(inspector_address); + stop_cheat_caller_address(contract_address); + + // Check empty time range + start_cheat_caller_address(contract_address, inspector_address); + let pending = land_register_dispatcher.get_inspector_pending_approvals(inspector_address, 2, 1); + assert(pending.len() == 0, 'Should have no approvals'); + stop_cheat_caller_address(contract_address); +}