Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

await response from 'submit' endpoint #692

Merged
merged 2 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions utils/connectApp.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { getResponseJSON, setHeadersDomainRestricted, getUserProfile } = require('./shared');
const { recruitSubmit, submitSocial, getUserSurveys, getUserCollections } = require('./submission');
const { submit, submitSocial, getUserSurveys, getUserCollections } = require('./submission');
const { retrieveNotifications, sendEmailLink } = require('./notifications');
const { validateToken, generateToken, updateParticipantFirebaseAuthentication, validateUsersEmailPhone } = require('./validation');

Expand Down Expand Up @@ -37,9 +37,23 @@ const connectApp = async (req, res) => {
console.log(`PWA API: ${api}, called from uid: ${uid}`);

try {
if (api === 'submit') return await recruitSubmit(req, res, uid);
if (api === 'submit') {

if (api === 'submitSocial') return submitSocial(req, res, uid);
if (req.method !== 'POST') {
return res.status(405).json(getResponseJSON('Only POST requests are accepted!', 405));
}

const body = req.body;

if (!body || Object.keys(body).length === 0) {
return res.status(400).json(getResponseJSON('Bad request!', 400));
}

// all 'submit' paths return res.status(code).json({ message, code });
return await submit(res, body, uid);
}

else if (api === 'submitSocial') return submitSocial(req, res, uid);

else if (api === 'getUserProfile') return getUserProfile(req, res, uid);

Expand Down
15 changes: 8 additions & 7 deletions utils/firestore.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,17 @@ const updateResponse = async (data, uid) => {
try{
const snapshot = await db.collection('participants').where('state.uid', '==', uid).get();
printDocsCount(snapshot, "updateResponse");
if(snapshot.size === 1) {
for(let doc of snapshot.docs){
await db.collection('participants').doc(doc.id).update(data);
return true;
}

if (snapshot.size !== 1) {
throw new Error(`updateResponse expected 1 document, found ${snapshot.size}. uid: ${uid}`);
}

await snapshot.docs[0].ref.update(data);
return true;
}
catch(error){
console.error(error);
return new Error(error)
console.error(`Error in updateResponse: ${error}`);
return new Error(`Error in updateResponse: ${error}`);
}
}

Expand Down
2 changes: 1 addition & 1 deletion utils/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -784,7 +784,7 @@ const cleanSurveyData = (data) => {
const admin = require('firebase-admin');

Object.keys(data).forEach(key => {
if(data[key] === null) {
if (data[key] === null || data[key] === undefined) {
data[key] = admin.firestore.FieldValue.delete();
}
});
Expand Down
141 changes: 61 additions & 80 deletions utils/submission.js
Original file line number Diff line number Diff line change
@@ -1,96 +1,90 @@
const { getResponseJSON, setHeaders, logIPAddress } = require('./shared');
const { cleanSurveyData, getResponseJSON, lockedAttributes, setHeaders, logIPAddress, moduleConceptsToCollections, moduleStatusConcepts } = require('./shared');
const { updateResponse } = require('./firestore');
const fieldMapping = require('./fieldToConceptIdMapping');

const submit = async (res, data, uid) => {

// Remove locked attributes.
const { lockedAttributes } = require('./shared');
lockedAttributes.forEach(atr => delete data[atr]);

// generate Connect_ID if Consent form submitted
if(data[919254129] !== undefined && data[919254129] === 353358909) {
const { generateConnectID } = require('./shared');
const { sanityCheckConnectID } = require('./firestore');
let boo = false;
let Connect_ID;
while(boo === false){
const ID = generateConnectID();
const response = await sanityCheckConnectID(ID);
if(response === true) {
Connect_ID = ID;
boo = true;
try {
// generate Connect_ID if Consent form submitted
if(data[919254129] !== undefined && data[919254129] === 353358909) {
const { generateConnectID } = require('./shared');
const { sanityCheckConnectID } = require('./firestore');
JoeArmani marked this conversation as resolved.
Show resolved Hide resolved
let boo = false;
let Connect_ID;
while(boo === false){
const ID = generateConnectID();
const response = await sanityCheckConnectID(ID);
if(response === true) {
Connect_ID = ID;
boo = true;
}
}
data = {...data, Connect_ID}
}
data = {...data, Connect_ID}
}

let keys = Object.keys(data);

// check if submitting survey
if (keys.length === 1) {
let keys = Object.keys(data);

const { moduleConceptsToCollections } = require('./shared');
// check if submitting survey
if (keys.length === 1) {
let key = keys[0];
let collection = moduleConceptsToCollections[key];

let key = keys[0];
let collection = moduleConceptsToCollections[key];

if(collection) {
let moduleData = data[key];
return await submitSurvey(res, moduleData, collection, uid)
if(collection) {
let moduleData = data[key];
return await submitSurvey(res, moduleData, collection, uid)
}
}
}

const { cleanSurveyData } = require('./shared');
data = cleanSurveyData(data);

const { updateResponse } = require('./firestore');
const response = await updateResponse(data, uid);

if (response) {
let moduleComplete = false;
let calculateScores = false;
data = cleanSurveyData(data);

keys.forEach(key => {
const { moduleStatusConcepts } = require('./shared');
// response is either true or an Error object
const response = await updateResponse(data, uid);
if (response) {
let moduleComplete = false;
let calculateScores = false;

if (moduleStatusConcepts[key] && data[key] === 231311385) {
moduleComplete = true;
keys.forEach(key => {
if (moduleStatusConcepts[key] && data[key] === 231311385) {
moduleComplete = true;

if (key === '320303124') {
calculateScores = true;
if (key === '320303124') {
calculateScores = true;
}
}
}
})

if(moduleComplete) {
})

const { checkDerivedVariables } = require('./validation');
const { getTokenForParticipant, retrieveUserProfile } = require('./firestore');
if (moduleComplete) {
const { checkDerivedVariables } = require('./validation');
const { getTokenForParticipant, retrieveUserProfile } = require('./firestore');

const participant = await retrieveUserProfile(uid);
const siteCode = participant['827220437'];
const token = await getTokenForParticipant(uid);
const participant = await retrieveUserProfile(uid);
const siteCode = participant['827220437'];
const token = await getTokenForParticipant(uid);

await checkDerivedVariables(token, siteCode);
await checkDerivedVariables(token, siteCode);

if (calculateScores) {
if (calculateScores) {

//remove condition once implemented in dev tier
if (process.env.GCLOUD_PROJECT === 'nih-nci-dceg-connect-stg-5519' || process.env.GCLOUD_PROJECT === 'nih-nci-dceg-connect-prod-6d04') {
const { processPromisResults } = require('./promis');
processPromisResults(uid);
//remove condition once implemented in dev tier
if (process.env.GCLOUD_PROJECT === 'nih-nci-dceg-connect-stg-5519' || process.env.GCLOUD_PROJECT === 'nih-nci-dceg-connect-prod-6d04') {
const { processPromisResults } = require('./promis');
processPromisResults(uid);
}
}
}
}

if (response instanceof Error) {
return res.status(500).json(getResponseJSON(response.message, 500));
}
return res.status(200).json(getResponseJSON('Data stored successfully!', 200));

} catch (error) {
console.error('Error in submit:', error);
return res.status(500).json(getResponseJSON(error.message, 500));
}

if(response instanceof Error){
return res.status(500).json(getResponseJSON(response.message, 500));
}
if(!response) {
return res.status(500).json(getResponseJSON("Can't add/update data!", 500));
}
return res.status(200).json(getResponseJSON('Data stored successfully!', 200));
};

const submitSurvey = async (res, data, collection, uid) => {
Expand Down Expand Up @@ -129,18 +123,6 @@ const submitSurvey = async (res, data, collection, uid) => {
return res.status(200).json(getResponseJSON('Survey data stored successfully!', 200));
}

const recruitSubmit = async (req, res, uid) => {
if(req.method !== 'POST') {
return res.status(405).json(getResponseJSON('Only POST requests are accepted!', 405));
}

const data = req.body;
if(Object.keys(data).length <= 0){
return res.status(400).json(getResponseJSON('Bad request!', 400));
}
return await submit(res, data, uid);
}

const submitSocial = async (req, res, uid) => {

if (req.method !== 'POST') {
Expand Down Expand Up @@ -744,7 +726,6 @@ const getTextAndVersionNumberFromGitHubCommit = async (sha, path, token) => {

module.exports = {
submit,
recruitSubmit,
submitSocial,
getFilteredParticipants,
getParticipants,
Expand Down