Skip to content

Commit

Permalink
fix: check for unique invite codes in designer
Browse files Browse the repository at this point in the history
  • Loading branch information
johannesvedder committed Aug 9, 2024
1 parent dac9422 commit 6c3adff
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
2 changes: 1 addition & 1 deletion designer_v2/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Future<void> main() async {
);
}, (error, stackTrace) {
// TODO: top-level error handling
print("Exception: $error");
print("Error: $error");
print("Stack: $stackTrace");
});
}
19 changes: 18 additions & 1 deletion designer_v2/lib/repositories/api_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ abstract class StudyUApi {
Future<void> deleteStudy(Study study);
Future<StudyInvite> saveStudyInvite(StudyInvite invite);
Future<StudyInvite> fetchStudyInvite(String code);
Future<Study> fetchStudyFromInvite(String code);
Future<void> deleteStudyInvite(StudyInvite invite);
Future<List<StudySubject>> deleteParticipants(
Study study,
Expand Down Expand Up @@ -206,6 +207,20 @@ class StudyUApiClient extends SupabaseClientDependant
);
}

@override
Future<Study> fetchStudyFromInvite(String code) async {
await _testDelay();
try {
final request = await executeRpc(
'get_study_record_from_invite',
params: {'invite_code': code},
);
return deserializeObject<Study>(request);
} catch (e) {
throw StudyInviteNotFoundException();
}
}

@override
Future<StudyInvite> saveStudyInvite(StudyInvite invite) async {
await _testDelay();
Expand Down Expand Up @@ -262,7 +277,9 @@ class StudyUApiClient extends SupabaseClientDependant
final result = await future;
return result;
} on SupabaseQueryError catch (e) {
if (onError == null || e.statusCode == null) {
if (onError == null ||
onError[e.statusCode] == null ||
e.statusCode == null) {
throw _apiException(error: e);
}
final errorHandler = onError[e.statusCode]!;
Expand Down
4 changes: 3 additions & 1 deletion designer_v2/lib/repositories/invite_code_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,11 @@ class InviteCodeRepository extends ModelRepository<StudyInvite>
@override
Future<bool> isCodeAlreadyUsed(String code) async {
try {
await apiClient.fetchStudyInvite(code);
await apiClient.fetchStudyFromInvite(code);
} on StudyInviteNotFoundException {
return false;
} catch (e) {
rethrow;
}
return true;
}
Expand Down
15 changes: 15 additions & 0 deletions designer_v2/lib/repositories/supabase_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,21 @@ mixin SupabaseQueryMixin on SupabaseClientDependant {
}
}

Future executeRpc(
String functionName, {
Map<String, dynamic>? params,
}) async {
try {
return await this.supabaseClient.rpc(functionName, params: params);
} on PostgrestException catch (error) {
throw SupabaseQueryError(
statusCode: error.code,
message: error.message,
details: error.details,
);
}
}

// - Deserialization

List<T> deserializeList<T extends SupabaseObject>(dynamic data) {
Expand Down

0 comments on commit 6c3adff

Please sign in to comment.