Skip to content

Commit

Permalink
feat(#462): import active medications with genetic data
Browse files Browse the repository at this point in the history
  • Loading branch information
tamslo committed Jun 19, 2023
1 parent 5e740fe commit dba46a4
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
9 changes: 9 additions & 0 deletions app/lib/common/models/userdata/userdata.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import 'dart:convert';

import 'package:collection/collection.dart';
import 'package:hive/hive.dart';
import 'package:http/http.dart';

import '../../../search/module.dart';
import '../../utilities/hive_utils.dart';
Expand Down Expand Up @@ -83,3 +86,9 @@ Future<void> initUserData() async {
final userData = Hive.box<UserData>(_boxName);
UserData._instance = userData.get('data') ?? UserData();
}

// assumes http reponse from lab server
List<String> activeDrugsFromHTTPResponse(Response resp) {
final activeDrugs = jsonDecode(resp.body)['medications'] as List<dynamic>;
return List<String>.from(activeDrugs);
}
9 changes: 6 additions & 3 deletions app/lib/common/utilities/genome_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import '../constants.dart';
import '../models/drug/cached_drugs.dart';
import '../models/module.dart';

Future<void> fetchAndSaveDiplotypes(String token, String url) async {
Future<void> fetchAndSaveDiplotypesAndActiveDrugs(
String token, String url) async {
if (!shouldFetchDiplotypes()) return;
final response = await getDiplotypes(token, url);
if (response.statusCode == 200) {
await _saveDiplotypeResponse(response);
await _saveDiplotypeAndActiveDrugsResponse(response);
} else {
throw Exception();
}
Expand All @@ -21,12 +22,14 @@ Future<Response> getDiplotypes(String? token, String url) async {
return get(Uri.parse(url), headers: {'Authorization': 'Bearer $token'});
}

Future<void> _saveDiplotypeResponse(Response response) async {
Future<void> _saveDiplotypeAndActiveDrugsResponse(Response response) async {
// parse response to list of user's diplotypes
final diplotypes =
diplotypesFromHTTPResponse(response).filterValidDiplotypes();
final activeDrugs = activeDrugsFromHTTPResponse(response);

UserData.instance.diplotypes = {for (var d in diplotypes) d.gene: d};
UserData.instance.activeDrugNames = activeDrugs;
await UserData.save();
// invalidate cached drugs because lookups may have changed and we need to
// refilter the matching guidelines
Expand Down
3 changes: 2 additions & 1 deletion app/lib/login/pages/cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ class LoginPageCubit extends Cubit<LoginPageState> {

try {
// get data
await fetchAndSaveDiplotypes(token, lab.starAllelesUrl.toString());
await fetchAndSaveDiplotypesAndActiveDrugs(
token, lab.starAllelesUrl.toString());
await fetchAndSaveLookups();

// login + fetching of data successful
Expand Down

0 comments on commit dba46a4

Please sign in to comment.