diff --git a/well-api.py b/well-api.py index 9be7f48..8389119 100644 --- a/well-api.py +++ b/well-api.py @@ -1707,8 +1707,8 @@ def StoreBeneficiary2DB(parameters, editing_user_id, user_id): field_mapping = { 'role_ids': 'role_ids', 'access_to_deployments': 'access_to_deployments', - 'email': 'email', - 'new_user_name': 'user_name', + 'beneficiary_email': 'email', + 'beneficiary_user_name': 'user_name', 'first_name': 'first_name', 'last_name': 'last_name', 'address_street': 'address_street', @@ -1716,9 +1716,7 @@ def StoreBeneficiary2DB(parameters, editing_user_id, user_id): 'address_zip': 'address_zip', 'address_state': 'address_state', 'address_country': 'address_country', - 'phone_number': 'phone_number', - 'picture': 'picture', - 'key': 'key' + 'beneficiary_password': 'key' } for param_key, db_field in field_mapping.items(): @@ -1742,8 +1740,8 @@ def StoreBeneficiary2DB(parameters, editing_user_id, user_id): field_mapping = { 'role_ids': 'role_ids', 'access_to_deployments': 'access_to_deployments', - 'email': 'email', - 'new_user_name': 'user_name', + 'beneficiary_email': 'email', + 'beneficiary_user_name': 'user_name', 'first_name': 'first_name', 'last_name': 'last_name', 'address_street': 'address_street', @@ -1751,9 +1749,7 @@ def StoreBeneficiary2DB(parameters, editing_user_id, user_id): 'address_zip': 'address_zip', 'address_state': 'address_state', 'address_country': 'address_country', - 'phone_number': 'phone_number', - 'picture': 'picture', - 'key': 'key' + 'beneficiary_password': 'key' } # Prepare values, using None for empty strings @@ -1768,8 +1764,8 @@ def StoreBeneficiary2DB(parameters, editing_user_id, user_id): INSERT INTO public.person_details (role_ids, access_to_deployments, email, user_name, first_name, last_name, address_street, address_city, address_zip, address_state, address_country, - time_edit, user_edit, phone_number, picture, key) - VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) + key, time_edit, user_edit) + VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) RETURNING user_id """ @@ -13503,7 +13499,7 @@ class RequestParser: # Store in context req.context.form_data = form_data - logger.debug(f"RequestParser: Parsed form data: {form_data}") + #logger.debug(f"RequestParser: Parsed form data: {form_data}") # Reset the stream with the original content req.stream = io.BytesIO(raw_body) @@ -17002,20 +16998,22 @@ class WellApi: try: # Use your existing get_form_data function for URL-encoded form_data = get_form_data(req) - logger.debug(f"Form data: {form_data}") + #logger.debug(f"Form data: {form_data}") # Add the Base64 photo fix - if 'beneficiary_photo' in form_data: - photo_fixed = quick_fix_base64_photo(form_data) + if 'beneficiary_photo_data' in form_data: + photo_fixed = quick_fix_base64_photo(form_data, form_data["beneficiary_photo"]) if photo_fixed: + form_data["beneficiary_photo_data"] = "" logger.debug("Base64 photo processed successfully") + logger.debug(f"Form data: {form_data}") # Your existing processing logic continues here with form_data... # Get form data using our helper function - but don't read stream again #form_data = get_form_data(req) - logger.debug(f"Form data: {form_data}") + #logger.debug(f"Form data: {form_data}") try: @@ -18356,7 +18354,7 @@ class WellApi: deployment = form_data.get('deployment') beneficiary_name = form_data.get('beneficiary_name') - image_file_name = beneficiary_name.replace(" ","_") + ".jpg" + image_file_name = form_data["beneficiary_photo"] gps_lat = form_data.get('lat') gps_lng = form_data.get('lng') time_zone_s = GetTZFromGPS(gps_lat, gps_lng) @@ -18364,19 +18362,19 @@ class WellApi: #debug_received_data(form_data) # Or better yet, update to handle both cases for backward compatibility: - if form_data.get("file_sent", "") != "": - # Old Base64 method - StoreFile2Blob(form_data["file_sent"], image_file_name, "user-pictures") - elif "beneficiary_photo" in files: - # New multipart method - try: - with open('beneficiary.jpg', 'rb') as f: - image_data = f.read() - image_base64 = base64.b64encode(image_data).decode('utf-8') - StoreFile2Blob(image_base64, image_file_name, "user-pictures") - os.remove('beneficiary.jpg') - except Exception as e: - logger.error(f"Failed to process beneficiary photo: {e}") + #if form_data.get("beneficiary_photo", "") != "": + # # Old Base64 method + # StoreFile2Blob(form_data["beneficiary_photo"], image_file_name, "user-pictures") + #elif "beneficiary_photo" in files: + ## New multipart method + #try: + #with open('beneficiary.jpg', 'rb') as f: + #image_data = f.read() + #image_base64 = base64.b64encode(image_data).decode('utf-8') + #StoreFile2Blob(image_base64, image_file_name, "user-pictures") + #os.remove('beneficiary.jpg') + #except Exception as e: + #logger.error(f"Failed to process beneficiary photo: {e}") devices_list = json.loads(devices) deployments_id = [] @@ -18402,7 +18400,7 @@ class WellApi: if deployment == "NEW": if in_db_user_name != None: #this email exists in DB, so cannot be used for NEW deployment! - error_string = f"This email cannot be used for new account!" + error_string = f"This email already has associated account!" print(error_string) payload = {'ok': 0, 'error': error_string} resp.media = package_response(payload) @@ -18455,14 +18453,14 @@ class WellApi: form_data['picture'] = image_file_name form_data['key'] = password #update person_details + #if in_db_user_name == None, than we really need to create new person. beneficiary_id, if_new = StoreBeneficiary2DB(form_data, editing_user_id, user_id) #this will update or create beneficiary #lets check if devices listed are not part of existing deployment if reuse_existing_devices == "0": #do re-use success, result = DevicesNotUsed(devices, user_name) else: success = True - result["deployed"] = [] - result["not_found"] = [] + result = {"deployed": [], "not_found": []} if success: #this should always be true! if result["deployed"]: @@ -18515,7 +18513,7 @@ class WellApi: form_data_temp['wifis'] = form_data.get('wifis') form_data_temp['lat'] = form_data.get('lat') form_data_temp['lng'] = form_data.get('lng') - form_data_temp['gps_age'] = form_data.get('gps_age') + form_data_temp['gps_age'] = str(int(float(form_data.get('gps_age')))) form_data_temp['time_zone_s'] = time_zone_s #["64B7088909FC", "64B7088909B8", "901506CA3DA0", "142B2F81A020", "64B7088905BC", "64B708890898", "64B70888FAD4","64B7088904BC"] form_data_temp['devices'] = WellIDs2MAC(form_data.get('devices')) @@ -18525,7 +18523,7 @@ class WellApi: if is_new_deployment == 1 and if_new: #This is new email, therefore person, so we need to give him access to only this deployment form_data['access_to_deployments'] = str(editing_deployment_id) - beneficiary_id, if_new = StoreBeneficiary2DB(form_data, editing_user_id, user_id) + beneficiary_id, if_new = StoreBeneficiary2DB(form_data, beneficiary_id, user_id) #deployment_id = deployment_details[0] #we need to update deployment_history table if devices changed @@ -18548,14 +18546,24 @@ class WellApi: print(devices_in_history_last) ok = 1 + + #at this step, we know deployment_id + if form_data.get("beneficiary_photo", "") != "": + image_file_name = form_data.get("beneficiary_photo", "") + if image_file_name == "NEW.jpg": + image_file_name = f"{editing_deployment_id}_{beneficiary_name}" + image_file_name = image_file_name.replace(" ", "_") + StoreFile2Blob(form_data["beneficiary_photo"], image_file_name, "user-pictures") + + if credentials_changed: if if_new == 1: #we need to call cd ~/mqtt-auth-service/acl_manager.py #we created new beneficiary and he needs welcome email #if deployment == "NEW": - SendWelcomeBeneficiaryEmail(form_data['email'], form_data['first_name'], form_data['last_name'], devices, form_data['phone_number'], form_data['new_user_name'], form_data['key']) + SendWelcomeBeneficiaryEmail(form_data['beneficiary_email'], form_data['first_name'], form_data['last_name'], devices, form_data['phone_number'], form_data['new_user_name'], form_data['key'], signature) else: - SendCredentialsChangedEmail(form_data['email'], form_data['first_name'], form_data['last_name'], devices, form_data['phone_number'], form_data['new_user_name'], form_data['key']) + SendCredentialsChangedEmail(form_data['beneficiary_email'], form_data['first_name'], form_data['last_name'], devices, form_data['phone_number'], form_data['new_user_name'], form_data['key'], signature) else: if devices_changed: CallICLUpdate() @@ -20063,7 +20071,7 @@ class WellApi: device_ids, device_list = MACsToWellIds(cur, macs_list_clean) - devices_details[deployment_id] = deployment + devices_details[deployment_id] = (deployment, device_ids, device_list, users_formatted) elif deployment_id in privileges_lst: deployment = DeploymentDetails(deployment_id) @@ -20078,29 +20086,34 @@ class WellApi: macs_list_clean.append(mac) device_ids, device_list = MACsToWellIds(cur, macs_list_clean) - devices_details[deployment_id] = deployment + devices_details[deployment_id] = (deployment, device_ids, device_list, users_formatted) + + - device_list_to_report = [] - for device in device_list: - #we need well_id, location and decription only - device_list_to_report.append((device[0], device[2], device[3])) - sql = f"SELECT first_name, last_name, email, picture FROM public.person_details WHERE user_id IN ({users_formatted});" - print(sql) - cur.execute(sql) - result1 = cur.fetchall() - counter = 0 for entry in result: deployment_id = str(entry[0]) - deployment = devices_details[deployment_id] + device_list_to_report = [] + device_list = devices_details[deployment_id][2] + users_formatted = devices_details[deployment_id][3] + for device in device_list: + #we need well_id, location and decription only + device_list_to_report.append((device[0], device[2], device[3])) + + sql = f"SELECT first_name, last_name, email, picture FROM public.person_details WHERE user_id IN ({users_formatted});" + print(sql) + cur.execute(sql) + result1 = cur.fetchall() + counter = 0 + deployment_id = str(entry[0]) + deployment_t = devices_details[deployment_id][0] first_name, last_name, email, photo = result1[counter] - deployment["beneficiary_first_name"] = first_name - deployment["beneficiary_last_name"] = last_name - deployment["beneficiary_email"] = email - deployment["photo"] = photo - devices_details[deployment_id] = deployment - to_report.append((entry, device_list_to_report, devices_details[deployment_id])) + deployment_t["beneficiary_first_name"] = first_name + deployment_t["beneficiary_last_name"] = last_name + deployment_t["beneficiary_email"] = email + deployment_t["photo"] = photo + to_report.append((entry, device_list_to_report, deployment_t)) print(to_report) @@ -21689,9 +21702,9 @@ def parse_multipart_data(request_body, content_type): return form_data, files -def quick_fix_base64_photo(form_data): +def quick_fix_base64_photo(form_data, file_name): """Fix corrupted Base64 photo from URL-encoded form""" - photo_b64 = form_data.get('beneficiary_photo') + photo_b64 = form_data.get('beneficiary_photo_data') if not photo_b64: return False @@ -21710,10 +21723,10 @@ def quick_fix_base64_photo(form_data): image_data = base64.b64decode(clean) if image_data[:3] == b'\xff\xd8\xff': - with open('beneficiary.jpg', 'wb') as f: + with open(file_name, 'wb') as f: f.write(image_data) - print(f"Base64 photo beneficiary.jpg fixed and saved: {len(image_data)} bytes") + print(f"Base64 photo {file_name} fixed and saved: {len(image_data)} bytes") return True else: logger.error("Invalid JPEG header in Base64 data") @@ -21987,7 +22000,7 @@ def SendWelcomeCaretakerEmail(email, first_name, last_name, devices, phone_numbe redis_conn.xadd('messaging_requests_stream', queue_data) -def SendWelcomeBeneficiaryEmail(email, first_name, last_name, devices, phone_number, user_name, password): +def SendWelcomeBeneficiaryEmail(email, first_name, last_name, devices, phone_number, user_name, password, signature): logger.error(f"Requesting welcome beneficiary email to {email}") queue_data = { 'function': "new_beneficiary", @@ -22004,7 +22017,7 @@ def SendWelcomeBeneficiaryEmail(email, first_name, last_name, devices, phone_num } redis_conn.xadd('messaging_requests_stream', queue_data) -def SendCredentialsChangedEmail(email, first_name, last_name, devices, phone_number, user_name, password): +def SendCredentialsChangedEmail(email, first_name, last_name, devices, phone_number, user_name, password, signature): logger.error(f"Requesting credentials changed of beneficiary email to {email}") queue_data = { 'function': "credentials_updated",