Skip to content
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
35 changes: 31 additions & 4 deletions functions/src/controllers/application_controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,14 @@ async function saveData(
uid: string
) {
try {
functions.logger.info(
`Starting saveData for user ${uid} in state ${state}`
);
functions.logger.info(`Data to save: ${JSON.stringify(dataToSave)}`);

// if currently in PROFILE state, then upsert data to `users` collection.
if (state === APPLICATION_STATES.PROFILE) {
functions.logger.info(`Saving to users collection for state ${state}`);
const userRef = db.collection("users").doc(uid);
const userDoc = await userRef.get();

Expand All @@ -121,14 +127,23 @@ async function saveData(
};

if (!userDoc.exists) {
functions.logger.info(`Creating new user document for ${uid}`);
data.createdAt = new Date().toISOString();
} else {
functions.logger.info(`Updating existing user document for ${uid}`);
}

await userRef.set(data, { merge: true });
functions.logger.info(
`Successfully saved to users collection for ${uid}`
);
}

// upsert other data in `application` section.
else {
functions.logger.info(
`Saving to applications collection for state ${state}`
);
const docRef = db.collection("applications").doc(uid);
const doc = await docRef.get();

Expand All @@ -139,14 +154,23 @@ async function saveData(
};

if (!doc.exists) {
functions.logger.info(`Creating new application document for ${uid}`);
data.createdAt = new Date().toISOString();
} else {
functions.logger.info(
`Updating existing application document for ${uid}`
);
}

await docRef.set(data, { merge: true });
functions.logger.info(
`Successfully saved to applications collection for ${uid}`
);
}
} catch (error) {
console.error("Error saving application:", error);
throw new Error("Failed to save application");
const err = error as Error;
functions.logger.error(`Error saving application for user ${uid}:`, err);
throw new Error(`Failed to save application: ${err.message}`);
}
}

Expand Down Expand Up @@ -526,7 +550,10 @@ function validateStringValue(fieldValue: string | any, question: Question) {
field_id: `${question.id}`,
message: `Must be at least ${validation.minLength} word(s)`,
});
} else if (validation.maxLength && countWords(fieldValue) > validation.maxLength) {
} else if (
validation.maxLength &&
countWords(fieldValue) > validation.maxLength
) {
errors.push({
field_id: `${question.id}`,
message: `Must be less than ${validation.maxLength} word(s)`,
Expand All @@ -550,7 +577,7 @@ function validateStringValue(fieldValue: string | any, question: Question) {
// });
// }
// }

return errors;
}

Expand Down
3 changes: 0 additions & 3 deletions functions/src/middlewares/auth_middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ export const validateSessionCookie = async (
const sessionCookie = extractSessionCookieFromCookie(req);
// Check for session cookie
if (!sessionCookie) {
functions.logger.error(
"No session cookie found. Login for session cookies."
);
res.status(401).json({
status: 401,
error: "No session cookie found",
Expand Down
1 change: 0 additions & 1 deletion functions/src/utils/jwt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ export function extractSessionCookieFromCookie(req: Request) {
sessionCookie = req.cookies.__session;
return sessionCookie;
}
functions.logger.warn("Cannot find __session cookie");
return;
}

Expand Down