Skip to content

Commit

Permalink
Merge branch 'develop' into 350-improve-status-codes-and-messages-for…
Browse files Browse the repository at this point in the history
…-external-integration-responses
  • Loading branch information
janoliver20 authored Dec 12, 2024
2 parents 0371b4a + 5cddc3f commit 678a64a
Showing 1 changed file with 48 additions and 38 deletions.
86 changes: 48 additions & 38 deletions src/main/java/io/redlink/more/data/repository/StudyRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,42 +69,50 @@ INNER JOIN studies s on (s.study_id = pt.study_id)
"DELETE FROM registration_tokens WHERE token = ?";

private static final String SQL_INSERT_CREDENTIALS =
"WITH data as (SELECT :api_secret as api_secret, :study_id as study_id, :participant_id as participant_id) " +
"INSERT INTO api_credentials (api_id, api_secret, study_id, participant_id) " +
"SELECT md5(study_id::text || random()::text), api_secret, study_id, participant_id FROM data " +
"RETURNING api_id";
"""
WITH data as (SELECT :api_secret as api_secret, :study_id as study_id, :participant_id as participant_id)
INSERT INTO api_credentials (api_id, api_secret, study_id, participant_id)
SELECT md5(study_id::text || random()::text), api_secret, study_id, participant_id FROM data
RETURNING api_id""";
private static final String SQL_CLEAR_CREDENTIALS =
"DELETE FROM api_credentials " +
"WHERE api_id = :api_id " +
"RETURNING study_id, participant_id";
"""
DELETE FROM api_credentials
WHERE api_id = :api_id
RETURNING study_id, participant_id""";

private static final String SQL_INSERT_STUDY_CONSENT =
"INSERT INTO participation_consents(study_id, participant_id, accepted, origin, content_md5) VALUES (:study_id, :participant_id, :accepted, :origin, :content_md5) " +
"ON CONFLICT (study_id, participant_id) DO " +
" UPDATE SET accepted = excluded.accepted, origin = excluded.origin, content_md5 = excluded.content_md5, " +
" consent_timestamp = now(), consent_withdrawn = NULL";
"""
INSERT INTO participation_consents(study_id, participant_id, accepted, origin, content_md5) VALUES (:study_id, :participant_id, :accepted, :origin, :content_md5)
ON CONFLICT (study_id, participant_id) DO
UPDATE SET accepted = excluded.accepted, origin = excluded.origin, content_md5 = excluded.content_md5,
consent_timestamp = now(), consent_withdrawn = NULL""";
private static final String SQL_WITHDRAW_STUDY_CONSENT =
"UPDATE participation_consents " +
"SET consent_withdrawn = now() " +
"WHERE study_id = :study_id AND participant_id = :participant_id";
"""
UPDATE participation_consents
SET consent_withdrawn = now()
WHERE study_id = :study_id AND participant_id = :participant_id""";

private static final String SQL_INSERT_OBSERVATION_CONSENT =
"INSERT INTO observation_consents(study_id, participant_id, observation_id) VALUES (:study_id, :participant_id, :observation_id) " +
"ON CONFLICT (study_id, participant_id, observation_id) DO NOTHING";
"""
INSERT INTO observation_consents(study_id, participant_id, observation_id) VALUES (:study_id, :participant_id, :observation_id)
ON CONFLICT (study_id, participant_id, observation_id) DO NOTHING""";
private static final String SQL_SET_PARTICIPANT_STATUS =
"UPDATE participants " +
"SET status = :newStatus::participant_status, start = :start, modified = now() " +
"WHERE study_id = :study_id AND participant_id = :participant_id AND status = :oldStatus::participant_status";
"""
UPDATE participants
SET status = :newStatus::participant_status, start = COALESCE(:start, start), modified = now()
WHERE study_id = :study_id AND participant_id = :participant_id AND status = :oldStatus::participant_status""";

private static final String SQL_LIST_PARTICIPANTS_BY_STUDY =
"SELECT participant_id, alias, status, sg.study_group_id, sg.title as study_group_title, start " +
"FROM participants p LEFT OUTER JOIN study_groups sg ON ( p.study_id = sg.study_id AND p.study_group_id = sg.study_group_id ) " +
"WHERE p.study_id = :study_id " +
"AND (p.study_group_id = :study_group_id OR :study_group_id::INT IS NULL)";
"""
SELECT participant_id, alias, status, sg.study_group_id, sg.title as study_group_title, start
FROM participants p LEFT OUTER JOIN study_groups sg ON ( p.study_id = sg.study_id AND p.study_group_id = sg.study_group_id )
WHERE p.study_id = :study_id
AND (p.study_group_id = :study_group_id OR :study_group_id::INT IS NULL)""";

private static final String GET_OBSERVATION_PROPERTIES_FOR_PARTICIPANT =
"SELECT properties FROM participant_observation_properties " +
"WHERE study_id = ? AND participant_id = ? AND observation_id = ?";
"""
SELECT properties FROM participant_observation_properties
WHERE study_id = ? AND participant_id = ? AND observation_id = ?""";

private static final String GET_API_ROUTING_INFO_BY_API_TOKEN = """
SELECT t.study_id, t.observation_id, o.study_group_id, o.type, t.token,
Expand All @@ -118,15 +126,17 @@ INNER JOIN studies s ON (t.study_id = s.study_id)
private static final String GET_OBSERVATION_SCHEDULE = "SELECT schedule FROM observations WHERE study_id = ? AND observation_id = ?";

private static final String GET_PARTICIPANT_INFO_AND_START_DURATION_END_FOR_STUDY_AND_PARTICIPANT =
"SELECT start, participant_id, alias, COALESCE(sg.duration, s.duration) AS duration, s.planned_end_date FROM participants p " +
"LEFT OUTER JOIN study_groups sg on p.study_id = sg.study_id and p.study_group_id = sg.study_group_id " +
"JOIN studies s on p.study_id = s.study_id " +
"WHERE p.study_id = ? AND participant_id = ?";
"""
SELECT start, participant_id, alias, COALESCE(sg.duration, s.duration) AS duration, s.planned_end_date FROM participants p
LEFT OUTER JOIN study_groups sg on p.study_id = sg.study_id and p.study_group_id = sg.study_group_id
JOIN studies s on p.study_id = s.study_id
WHERE p.study_id = ? AND participant_id = ?""";

private static final String GET_DURATION_INFO_FOR_STUDY =
"SELECT sg.study_group_id as groupid, sg.duration AS groupduration, s.duration AS studyduration, s.planned_end_date AS enddate, s.planned_start_date AS startdate FROM studies s " +
"LEFT OUTER JOIN study_groups sg on s.study_id = sg.study_id " +
"WHERE s.study_id = ?";
"""
SELECT sg.study_group_id as groupid, sg.duration AS groupduration, s.duration AS studyduration, s.planned_end_date AS enddate, s.planned_start_date AS startdate FROM studies s
LEFT OUTER JOIN study_groups sg on s.study_id = sg.study_id
WHERE s.study_id = ?""";

private final JdbcTemplate jdbcTemplate;
private final NamedParameterJdbcTemplate namedTemplate;
Expand Down Expand Up @@ -288,12 +298,12 @@ private void updateParticipantStatus(long studyId, int groupId, int participantI
);
}

namedTemplate.update(SQL_SET_PARTICIPANT_STATUS,
toParameterSource(studyId, participantId)
.addValue("start", start)
.addValue("oldStatus", oldStatus)
.addValue("newStatus", newStatus)
);
var parameterSource = toParameterSource(studyId, participantId)
.addValue("oldStatus", oldStatus)
.addValue("newStatus", newStatus)
.addValue("start", start);

namedTemplate.update(SQL_SET_PARTICIPANT_STATUS, parameterSource);
}

private void storeConsent(long studyId, int participantId, ParticipantConsent consent) {
Expand Down

0 comments on commit 678a64a

Please sign in to comment.