def mutate(cls, _, info, geo_api_data, company_id, alias=None): company_known_address = CompanyKnownAddress( alias=alias, address=Address.get_or_create(geo_api_data), company_id=company_id, ) db.session.add(company_known_address) db.session.commit() return company_known_address
def mutate( cls, _, info, mission_id, type, company_known_address_id=None, geo_api_data=None, manual_address=None, kilometer_reading=None, ): with atomic_transaction(commit_at_end=True): if (int(company_known_address_id is not None) + int(geo_api_data is not None) + int(manual_address is not None) != 1): raise InvalidParamsError( "Exactly one of companyKnownAddressId or geoApiData or manualAddress should be set" ) mission = Mission.query.get(mission_id) company_known_address = None if company_known_address_id: company_known_address = CompanyKnownAddress.query.get( company_known_address_id) if (not company_known_address or company_known_address.company_id != mission.company_id): raise InvalidParamsError("Invalid companyKnownAddressId") address = company_known_address.address elif geo_api_data: address = Address.get_or_create(geo_api_data) else: address = Address(manual=True, name=manual_address) db.session.add(address) existing_location_entry = [ l for l in mission.location_entries if l.type == type ] existing_location_entry = (existing_location_entry[0] if existing_location_entry else None) if existing_location_entry: are_addresses_equal = ( address.name == existing_location_entry.address.name if address.manual and existing_location_entry.address.manual else address == existing_location_entry.address) if are_addresses_equal: if kilometer_reading: existing_location_entry.register_kilometer_reading( kilometer_reading) return existing_location_entry raise MissionLocationAlreadySetError() now = datetime.now() location_entry = LocationEntry( _address=address, mission=mission, reception_time=now, submitter=current_user, _company_known_address=company_known_address, type=type, ) location_entry.register_kilometer_reading(kilometer_reading, now) db.session.add(location_entry) return location_entry