fix user counter and optimise profile picture getting

This commit is contained in:
Yasien Mac Mini 2026-07-02 11:01:59 +02:00
parent 62eafaf766
commit 1438dd6b5a
8 changed files with 39 additions and 85 deletions

View file

@ -8,15 +8,7 @@ minioSecret = os.getenv("MINIO_SECRET_KEY")
minioEndpoint = os.getenv("MINIO_ENDPOINT", "mih-minio:9000") minioEndpoint = os.getenv("MINIO_ENDPOINT", "mih-minio:9000")
minioSecure = os.getenv("MINIO_SECURE", "False") in ("True") minioSecure = os.getenv("MINIO_SECURE", "False") in ("True")
def minioConnect(env): def minioConnect():
if(env == "Dev"):
return Minio(
endpoint=minioEndpoint,
access_key=minioAccess,
secret_key=minioSecret,
secure=minioSecure,
)
else:
return Minio( return Minio(
endpoint=minioEndpoint, endpoint=minioEndpoint,
access_key=minioAccess, access_key=minioAccess,

View file

@ -108,11 +108,11 @@ class claimStatementUploud(BaseModel):
logo_path: str logo_path: str
sig_path: str sig_path: str
@router.get("/v2/minio/pull/file/{env}/{app_id}/{folder}/{file_name}", tags=["Minio"]) @router.get("/v2/minio/pull/file/{app_id}/{folder}/{file_name}", tags=["Minio"])
async def pullFileFromMinioV2(app_id: str, folder: str, file_name: str, env: str): async def pullFileFromMinioV2(app_id: str, folder: str, file_name: str,):
object_path = f"{app_id}/{folder}/{file_name}" object_path = f"{app_id}/{folder}/{file_name}"
try: try:
client = Minio_Storage.minioConnection.minioConnect(env) client = Minio_Storage.minioConnection.minioConnect()
response = client.get_object(bucket_name="mih", object_name=object_path) response = client.get_object(bucket_name="mih", object_name=object_path)
@ -136,7 +136,7 @@ async def pull_File_from_user(app_id: str, folder: str, file_name: str, env: str
# print(f"env: {env}") # print(f"env: {env}")
# uploudFile(app_id, file.filename, extension[1], content) # uploudFile(app_id, file.filename, extension[1], content)
client = Minio_Storage.minioConnection.minioConnect(env) client = Minio_Storage.minioConnection.minioConnect()
# buckets = client.list_buckets() # buckets = client.list_buckets()
# print("Connected to MinIO successfully!") # print("Connected to MinIO successfully!")
# print("Available buckets:", [bucket.name for bucket in buckets]) # print("Available buckets:", [bucket.name for bucket in buckets])
@ -188,7 +188,7 @@ async def delete_File_of_user(requestItem: minioDeleteRequest, session: SessionC
path = requestItem.file_path path = requestItem.file_path
try: try:
# uploudFile(app_id, file.filename, extension[1], content) # uploudFile(app_id, file.filename, extension[1], content)
client = Minio_Storage.minioConnection.minioConnect(requestItem.env) client = Minio_Storage.minioConnection.minioConnect()
minioError = client.remove_object( minioError = client.remove_object(
bucket_name="mih", bucket_name="mih",
@ -222,7 +222,7 @@ async def upload_perscription_to_user(requestItem: claimStatementUploud, session
return {"message": "Successfully Generated File"} return {"message": "Successfully Generated File"}
def uploudFile(app_id, env, folder, fileName, extension, content): def uploudFile(app_id, env, folder, fileName, extension, content):
client = Minio_Storage.minioConnection.minioConnect(env) client = Minio_Storage.minioConnection.minioConnect()
found = client.bucket_exists(bucket_name="mih") found = client.bucket_exists(bucket_name="mih")
if not found: if not found:
client.make_bucket(bucket_name="mih") client.make_bucket(bucket_name="mih")
@ -238,7 +238,7 @@ def uploudFile(app_id, env, folder, fileName, extension, content):
content_type=f"application/{extension}") content_type=f"application/{extension}")
def uploudMedCert(requestItem: medCertUploud): def uploudMedCert(requestItem: medCertUploud):
client = Minio_Storage.minioConnection.minioConnect(requestItem.env) client = Minio_Storage.minioConnection.minioConnect()
generateMedCertPDF(requestItem) generateMedCertPDF(requestItem)
today = datetime.today().strftime('%Y-%m-%d') today = datetime.today().strftime('%Y-%m-%d')
found = client.bucket_exists(bucket_name="mih") found = client.bucket_exists(bucket_name="mih")
@ -250,7 +250,7 @@ def uploudMedCert(requestItem: medCertUploud):
client.fput_object("mih", fileName, "temp-med-cert.pdf") client.fput_object("mih", fileName, "temp-med-cert.pdf")
def generateMedCertPDF(requestItem: medCertUploud): def generateMedCertPDF(requestItem: medCertUploud):
client = Minio_Storage.minioConnection.minioConnect(requestItem.env) client = Minio_Storage.minioConnection.minioConnect()
new_logo_path = requestItem.logo_path.replace(" ","-") new_logo_path = requestItem.logo_path.replace(" ","-")
new_sig_path = requestItem.sig_path.replace(" ","-") new_sig_path = requestItem.sig_path.replace(" ","-")
minioLogo = client.get_object("mih", new_logo_path).read() minioLogo = client.get_object("mih", new_logo_path).read()
@ -315,7 +315,7 @@ def generateMedCertPDF(requestItem: medCertUploud):
myCanvas.save() myCanvas.save()
def uploudPerscription(requestItem: perscriptionList): def uploudPerscription(requestItem: perscriptionList):
client = Minio_Storage.minioConnection.minioConnect(requestItem.env) client = Minio_Storage.minioConnection.minioConnect()
generatePerscriptionPDF(requestItem) generatePerscriptionPDF(requestItem)
today = datetime.today().strftime('%Y-%m-%d') today = datetime.today().strftime('%Y-%m-%d')
found = client.bucket_exists(bucket_name="mih") found = client.bucket_exists(bucket_name="mih")
@ -327,7 +327,7 @@ def uploudPerscription(requestItem: perscriptionList):
client.fput_object("mih", fileName, "temp-perscription.pdf") client.fput_object("mih", fileName, "temp-perscription.pdf")
def generatePerscriptionPDF(requestItem: perscriptionList): def generatePerscriptionPDF(requestItem: perscriptionList):
client = Minio_Storage.minioConnection.minioConnect(requestItem.env) client = Minio_Storage.minioConnection.minioConnect()
new_logo_path = requestItem.logo_path.replace(" ","-") new_logo_path = requestItem.logo_path.replace(" ","-")
new_sig_path = requestItem.sig_path.replace(" ","-") new_sig_path = requestItem.sig_path.replace(" ","-")
minioLogo = client.get_object("mih", new_logo_path).read() minioLogo = client.get_object("mih", new_logo_path).read()
@ -417,7 +417,7 @@ def generatePerscriptionPDF(requestItem: perscriptionList):
def uploudClaimStatement(requestItem: claimStatementUploud): def uploudClaimStatement(requestItem: claimStatementUploud):
try: try:
client = Minio_Storage.minioConnection.minioConnect(requestItem.env) client = Minio_Storage.minioConnection.minioConnect()
print("connected") print("connected")
except Exception: except Exception:
print("error") print("error")
@ -433,7 +433,7 @@ def uploudClaimStatement(requestItem: claimStatementUploud):
client.fput_object("mih", fileName, "temp-claim-statement.pdf") client.fput_object("mih", fileName, "temp-claim-statement.pdf")
def generateClaimStatementPDF(requestItem: claimStatementUploud): def generateClaimStatementPDF(requestItem: claimStatementUploud):
client = Minio_Storage.minioConnection.minioConnect(requestItem.env) client = Minio_Storage.minioConnection.minioConnect()
# print("buckets: " + client.list_buckets) # print("buckets: " + client.list_buckets)
new_logo_path = requestItem.logo_path.replace(" ","-") new_logo_path = requestItem.logo_path.replace(" ","-")
new_sig_path = requestItem.sig_path.replace(" ","-") new_sig_path = requestItem.sig_path.replace(" ","-")

View file

@ -39,7 +39,6 @@ void main() async {
await Hive.openBox<Business>('business_box'); await Hive.openBox<Business>('business_box');
await Hive.openBox<BusinessUser>('business_user_box'); await Hive.openBox<BusinessUser>('business_user_box');
await Hive.openBox<UserConsent>('user_consent_box'); await Hive.openBox<UserConsent>('user_consent_box');
await Hive.openBox<String>('image_urls_box');
await Hive.openBox<ProfileLink>('personal_profile_links_box'); await Hive.openBox<ProfileLink>('personal_profile_links_box');
await Hive.openBox<ProfileLink>('business_profile_links_box'); await Hive.openBox<ProfileLink>('business_profile_links_box');
await Hive.openBox<BusinessEmployee>('business_employees_box'); await Hive.openBox<BusinessEmployee>('business_employees_box');

View file

@ -39,7 +39,6 @@ void main() async {
await Hive.openBox<Business>('business_box'); await Hive.openBox<Business>('business_box');
await Hive.openBox<BusinessUser>('business_user_box'); await Hive.openBox<BusinessUser>('business_user_box');
await Hive.openBox<UserConsent>('user_consent_box'); await Hive.openBox<UserConsent>('user_consent_box');
await Hive.openBox<String>('image_urls_box');
await Hive.openBox<ProfileLink>('personal_profile_links_box'); await Hive.openBox<ProfileLink>('personal_profile_links_box');
await Hive.openBox<ProfileLink>('business_profile_links_box'); await Hive.openBox<ProfileLink>('business_profile_links_box');
await Hive.openBox<BusinessEmployee>('business_employees_box'); await Hive.openBox<BusinessEmployee>('business_employees_box');

View file

@ -7,7 +7,7 @@ class AboutMihHiveData {
final Box<int> _mihUserBusinessCountBox = Hive.box<int>('about_mih_box'); final Box<int> _mihUserBusinessCountBox = Hive.box<int>('about_mih_box');
static const String kUserCountKey = 'current_user_count'; static const String kUserCountKey = 'current_user_count';
static const String kBusinessCountKey = 'current_user_count'; static const String kBusinessCountKey = 'current_business_count';
// Get Data from local storage // Get Data from local storage
int? getcachedUserCount() => _mihUserBusinessCountBox.get(kUserCountKey); int? getcachedUserCount() => _mihUserBusinessCountBox.get(kUserCountKey);
@ -21,7 +21,7 @@ class AboutMihHiveData {
} }
Future<void> cacheBusinessCount(int remoteBusinessCount) async { Future<void> cacheBusinessCount(int remoteBusinessCount) async {
await _mihUserBusinessCountBox.put(kUserCountKey, remoteBusinessCount); await _mihUserBusinessCountBox.put(kBusinessCountKey, remoteBusinessCount);
KenLogger.success("MIH Business Count Cached"); KenLogger.success("MIH Business Count Cached");
} }

View file

@ -8,7 +8,6 @@ import 'package:mzansi_innovation_hub/mih_objects/profile_link.dart';
import 'package:mzansi_innovation_hub/mih_objects/user_consent.dart'; import 'package:mzansi_innovation_hub/mih_objects/user_consent.dart';
import 'package:mzansi_innovation_hub/mih_services/mih_business_details_services.dart'; import 'package:mzansi_innovation_hub/mih_services/mih_business_details_services.dart';
import 'package:mzansi_innovation_hub/mih_services/mih_business_employee_services.dart'; import 'package:mzansi_innovation_hub/mih_services/mih_business_employee_services.dart';
import 'package:mzansi_innovation_hub/mih_services/mih_file_services.dart';
import 'package:mzansi_innovation_hub/mih_services/mih_my_business_user_services.dart'; import 'package:mzansi_innovation_hub/mih_services/mih_my_business_user_services.dart';
import 'package:mzansi_innovation_hub/mih_services/mih_profile_links_service.dart'; import 'package:mzansi_innovation_hub/mih_services/mih_profile_links_service.dart';
import 'package:mzansi_innovation_hub/mih_services/mih_user_consent_services.dart'; import 'package:mzansi_innovation_hub/mih_services/mih_user_consent_services.dart';
@ -27,7 +26,6 @@ class MzansiProfileHiveData {
Hive.box<ProfileLink>('business_profile_links_box'); Hive.box<ProfileLink>('business_profile_links_box');
final Box<BusinessEmployee> _businessEmployeesBox = final Box<BusinessEmployee> _businessEmployeesBox =
Hive.box<BusinessEmployee>('business_Employees_box'); Hive.box<BusinessEmployee>('business_Employees_box');
final Box<String> _resolvedUrlsBox = Hive.box<String>('image_urls_box');
static const String kUserKey = 'current_user'; static const String kUserKey = 'current_user';
static const String kBusinessKey = 'current_business'; static const String kBusinessKey = 'current_business';
@ -43,9 +41,6 @@ class MzansiProfileHiveData {
BusinessUser? getCachedBusinessUser() => BusinessUser? getCachedBusinessUser() =>
_businessUserBox.get(kBusinessUserKey); _businessUserBox.get(kBusinessUserKey);
UserConsent? getCachedConsent() => _userConsentBox.get(kConsentKey); UserConsent? getCachedConsent() => _userConsentBox.get(kConsentKey);
String? getCachedUserPicUrl() => _resolvedUrlsBox.get(kUserPicUrlKey);
String? getCachedBusinessPicUrl() => _resolvedUrlsBox.get(kBusinessPicUrlKey);
String? getCachedSignatureUrl() => _resolvedUrlsBox.get(kSignatureUrlKey);
List<ProfileLink> getCachedPersonalProfileLinks() { List<ProfileLink> getCachedPersonalProfileLinks() {
final links = _personalProfileLinksBox.values.toList(); final links = _personalProfileLinksBox.values.toList();
@ -68,11 +63,6 @@ class MzansiProfileHiveData {
// Caching Data to local storage // Caching Data to local storage
Future<void> cacheUserData(AppUser remoteUser) async { Future<void> cacheUserData(AppUser remoteUser) async {
await _userBox.put(kUserKey, remoteUser); await _userBox.put(kUserKey, remoteUser);
if (remoteUser.pro_pic_path.isNotEmpty) {
String userPicUrl =
await MihFileApi.getMinioFileUrl(remoteUser.pro_pic_path);
await _resolvedUrlsBox.put(kUserPicUrlKey, userPicUrl);
}
KenLogger.success("User Profile Cached"); KenLogger.success("User Profile Cached");
} }
@ -83,26 +73,11 @@ class MzansiProfileHiveData {
Future<void> cacheBusinessData(Business remoteBusiness) async { Future<void> cacheBusinessData(Business remoteBusiness) async {
await _businessBox.put(kBusinessKey, remoteBusiness); await _businessBox.put(kBusinessKey, remoteBusiness);
if (remoteBusiness.logo_path.isNotEmpty) {
String logoUrl =
await MihFileApi.getMinioFileUrl(remoteBusiness.logo_path);
await _resolvedUrlsBox.put(kBusinessPicUrlKey, logoUrl);
}
BusinessUser? remoteBizUser =
await MihMyBusinessUserServices().getBusinessUserV2();
if (remoteBizUser != null) {
cacheBusinessUserData(remoteBizUser);
}
KenLogger.success("Busines Profile Cached"); KenLogger.success("Busines Profile Cached");
} }
Future<void> cacheBusinessUserData(BusinessUser remoteBizUser) async { Future<void> cacheBusinessUserData(BusinessUser remoteBizUser) async {
await _businessUserBox.put(kBusinessUserKey, remoteBizUser); await _businessUserBox.put(kBusinessUserKey, remoteBizUser);
if (remoteBizUser.sig_path.isNotEmpty) {
String signatureUrl =
await MihFileApi.getMinioFileUrl(remoteBizUser.sig_path);
await _resolvedUrlsBox.put(kSignatureUrlKey, signatureUrl);
}
KenLogger.success("Busines User Profile Cached"); KenLogger.success("Busines User Profile Cached");
} }
@ -146,8 +121,13 @@ class MzansiProfileHiveData {
Business? remoteBusiness = Business? remoteBusiness =
await MihBusinessDetailsServices().getBusinessDetailsByUserV2(); await MihBusinessDetailsServices().getBusinessDetailsByUserV2();
if (remoteBusiness != null) { if (remoteBusiness != null) {
cacheBusinessData(remoteBusiness); await cacheBusinessData(remoteBusiness);
BusinessUser? remoteBizUser =
await MihMyBusinessUserServices().getBusinessUserV2();
if (remoteBizUser != null) {
cacheBusinessUserData(remoteBizUser);
}
final remoteBusinessEmployeeList = await MihBusinessEmployeeServices() final remoteBusinessEmployeeList = await MihBusinessEmployeeServices()
.fetchEmployeesV2(remoteBusiness.business_id); .fetchEmployeesV2(remoteBusiness.business_id);
cacheBusinessEmployeesData(remoteBusinessEmployeeList); cacheBusinessEmployeesData(remoteBusinessEmployeeList);

View file

@ -8,6 +8,7 @@ import 'package:mzansi_innovation_hub/mih_objects/business_employee.dart';
import 'package:mzansi_innovation_hub/mih_objects/business_user.dart'; import 'package:mzansi_innovation_hub/mih_objects/business_user.dart';
import 'package:mzansi_innovation_hub/mih_objects/profile_link.dart'; import 'package:mzansi_innovation_hub/mih_objects/profile_link.dart';
import 'package:mzansi_innovation_hub/mih_objects/user_consent.dart'; import 'package:mzansi_innovation_hub/mih_objects/user_consent.dart';
import 'package:mzansi_innovation_hub/mih_services/mih_file_services.dart';
class MzansiProfileProvider extends ChangeNotifier { class MzansiProfileProvider extends ChangeNotifier {
final MzansiProfileHiveData _hiveData; final MzansiProfileHiveData _hiveData;
@ -46,20 +47,21 @@ class MzansiProfileProvider extends ChangeNotifier {
userConsent = _hiveData.getCachedConsent(); userConsent = _hiveData.getCachedConsent();
business = _hiveData.getCachedBusiness(); business = _hiveData.getCachedBusiness();
businessUser = _hiveData.getCachedBusinessUser(); businessUser = _hiveData.getCachedBusinessUser();
String? cachedUserUrl = _hiveData.getCachedUserPicUrl(); if (user != null && user!.pro_pic_path.isNotEmpty) {
if (cachedUserUrl != null && cachedUserUrl.isNotEmpty) { KenLogger.success("proPicPath: ${user!.pro_pic_path}| THis is it");
userProfilePicUrl = cachedUserUrl; userProfilePicUrl = MihFileApi.getMinioFileUrlV2(user!.pro_pic_path);
userProfilePicture = CachedNetworkImageProvider(cachedUserUrl); userProfilePicture = CachedNetworkImageProvider(userProfilePicUrl!);
} }
String? cachedBizUrl = _hiveData.getCachedBusinessPicUrl(); if (business != null && business!.logo_path.isNotEmpty) {
if (cachedBizUrl != null && cachedBizUrl.isNotEmpty) { businessProfilePicUrl = MihFileApi.getMinioFileUrlV2(business!.logo_path);
businessProfilePicUrl = cachedBizUrl; businessProfilePicture =
businessProfilePicture = CachedNetworkImageProvider(cachedBizUrl); CachedNetworkImageProvider(businessProfilePicUrl!);
} }
String? cachedSigUrl = _hiveData.getCachedSignatureUrl(); if (businessUser != null && businessUser!.sig_path.isNotEmpty) {
if (cachedSigUrl != null && cachedSigUrl.isNotEmpty) { businessUserSignatureUrl =
businessUserSignatureUrl = cachedSigUrl; MihFileApi.getMinioFileUrlV2(businessUser!.sig_path);
businessUserSignature = CachedNetworkImageProvider(cachedSigUrl); businessUserSignature =
CachedNetworkImageProvider(businessUserSignatureUrl!);
} }
employeeList = _hiveData.getCachedBusinessEmployees(); employeeList = _hiveData.getCachedBusinessEmployees();

View file

@ -39,29 +39,11 @@ class MihFileApi {
return fileUrl; return fileUrl;
} }
static Future<String> getMinioFileUrlV2( static String getMinioFileUrlV2(
String filePath, String filePath,
) async { ) {
String fileUrl = ""; if (filePath.isEmpty) return "";
try { return "${AppEnviroment.baseApiUrl}/v2/minio/pull/file/$filePath";
var url =
"${AppEnviroment.baseApiUrl}/minio/pull/file/${AppEnviroment.getEnv()}/$filePath";
var response = await http.get(Uri.parse(url));
if (response.statusCode == 200) {
var decodedData = jsonDecode(response.body);
fileUrl = decodedData['minioURL'];
} else {}
} catch (e) {
KenLogger.error("Error getting url");
} finally {}
if (AppEnviroment.getEnv() == "Dev" && kIsWeb) {
fileUrl = fileUrl.replaceAll("10.0.2.2", "127.0.0.1");
} else if (AppEnviroment.getEnv() == "Dev" && Platform.isIOS) {
fileUrl = fileUrl.replaceAll("10.0.2.2", "127.0.0.1");
} else if (AppEnviroment.getEnv() == "Dev" && Platform.isLinux) {
fileUrl = fileUrl.replaceAll("10.0.2.2", "127.0.0.1");
}
return fileUrl;
} }
static Future<int> uploadFile( static Future<int> uploadFile(