Skip to content

Commit

Permalink
ref: pulled out validateStudy and validateParticipant from prompting …
Browse files Browse the repository at this point in the history
…functions to use to check if arguments passed into CLI directly when running delete and download are valid
  • Loading branch information
YUUU23 committed Jul 27, 2024
1 parent 37fcb29 commit aaf6969
Showing 1 changed file with 28 additions and 13 deletions.
41 changes: 28 additions & 13 deletions cli.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,24 @@ async function main() {
// TODO @brown-ccv #291: Enable downloading all study data at once
if (STUDY_ID == undefined) {
STUDY_ID = await studyIDPrompt();
} else {
// when args directly passed in through CLI, check if study is valid
const hasStudy = await validateStudyFirebase(STUDY_ID);
if (hasStudy != true) {
console.error(hasStudy);
return;
}
}
// TODO @brown-ccv #291: Enable downloading all participant data at once
if (PARTICIPANT_ID == undefined) {
PARTICIPANT_ID = await participantIDPrompt();
} else {
// when args directly passed in through CLI, check if participant is valid
const hasParticipant = await validateParticipantFirebase(STUDY_ID);
if (hasParticipant != true) {
console.error(hasParticipant);
return;
}
}
EXPERIMENT_IDS = await experimentIDPrompt();

Expand Down Expand Up @@ -228,19 +242,19 @@ async function deploymentPrompt() {
}

/** Prompt the user to enter the ID of a study */
async function studyIDPrompt() {
// helper to check if the given study (input) is in firestore
const validateStudyFirebase = async (input) => {
const invalidMessage = "Please enter a valid study from your Firestore database";
const validateStudyFirebase = async (input) => {
// subcollection is programmatically generated, if it doesn't exist then input must not be a valid studyID
const studyIDCollections = await getStudyRef(input).listCollections();
return studyIDCollections.find((c) => c.id === PARTICIPANTS_COL) ? true : invalidMessage;
};
// subcollection is programmatically generated, if it doesn't exist then input must not be a valid studyID
const studyIDCollections = await getStudyRef(input).listCollections();
return studyIDCollections.find((c) => c.id === PARTICIPANTS_COL) ? true : invalidMessage;
};

async function studyIDPrompt() {
return await input({
message: "Select a study:",
validate: async (input) => {
if (!input) return invalidMessage;

switch (DEPLOYMENT) {
case "firebase":
return validateStudyFirebase(input);
Expand All @@ -252,14 +266,15 @@ async function studyIDPrompt() {
}

/** Prompt the user to enter the ID of a participant on the STUDY_ID study */
async function participantIDPrompt() {
// helper to check if the given participant (input) is in firestore under study
const validateParticipantFirebase = async (input) => {
const invalidMessage = `Please enter a valid participant on the study "${STUDY_ID}"`;
const validateParticipantFirebase = async (input) => {
// subcollection is programmatically generated, if it doesn't exist then input must not be a valid participantID
const studyIDCollections = await getParticipantRef(STUDY_ID, input).listCollections();
return studyIDCollections.find((c) => c.id === DATA_COL) ? true : invalidMessage;
};
// subcollection is programmatically generated, if it doesn't exist then input must not be a valid participantID
const studyIDCollections = await getParticipantRef(STUDY_ID, input).listCollections();
return studyIDCollections.find((c) => c.id === DATA_COL) ? true : invalidMessage;
};

async function participantIDPrompt() {
return await input({
message: "Select a participant:",
validate: async (input) => {
Expand Down

0 comments on commit aaf6969

Please sign in to comment.