Skip to content

Commit

Permalink
Merge pull request #676 from hpi-studyu/fix/invite-code
Browse files Browse the repository at this point in the history
fix: Make invite codes unique
  • Loading branch information
johannesvedder authored Aug 9, 2024
2 parents f35888c + 6c3adff commit 7558c7b
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 3 deletions.
4 changes: 4 additions & 0 deletions database/migration/20240801_invite_code_unique.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ALTER TABLE
public.study_invite
ADD
CONSTRAINT study_invite_code_unique UNIQUE (code);
6 changes: 6 additions & 0 deletions database/studyu-schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1065,4 +1065,10 @@ ALTER TABLE public."user" ENABLE ROW LEVEL SECURITY;

ALTER VIEW public.study_progress_export SET (security_invoker = on);

--
-- Name: study_invite; Type: TABLE; Schema: public; Owner: postgres
--

ALTER TABLE public.study_invite ADD CONSTRAINT study_invite_code_unique UNIQUE (code);

COMMIT;
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 7558c7b

Please sign in to comment.