Skip to content
Merged

Dev #35

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
1 change: 1 addition & 0 deletions functions/src/config/firebase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ admin.initializeApp({
});

const db = admin.firestore();
db.settings({ ignoreUndefinedProperties: true });
const auth = admin.auth();

/**
Expand Down
62 changes: 53 additions & 9 deletions functions/src/controllers/application_controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ async function constructDataToSave(
for (const question of questions) {
if (question.id === undefined || question.id === null) continue;
const fieldValue = req.body[question.id];
if (question.type === QUESTION_TYPE.FILE) {
// rewrite file path
if (question.type === QUESTION_TYPE.FILE && !(fieldValue === undefined || fieldValue === "" || fieldValue === null)) {
dataToSave[
question.id
] = `${STORAGE_BASE_LINK}${USER_UPLOAD_PATH}${UID}_${
Expand Down Expand Up @@ -274,10 +275,19 @@ async function validateFileUploaded(
question: Question,
uid: string
) {
const errors = [];
const errors: { field_id: string; message: string; }[] = [];

const validation = question.validation as FileValidation;


// skip validation if not required and value is empty
if (
validation.required !== true &&
(fieldValue === undefined || fieldValue === "" || fieldValue === null)
) {
return errors;
}

// required
if (
validation.required === true &&
Expand Down Expand Up @@ -324,10 +334,18 @@ async function validateFileUploaded(

// eslint-disable-next-line require-jsdoc
function validateDropdownValue(fieldValue: string | any, question: Question) {
const errors = [];
const errors: { field_id: string; message: string; }[] = [];

const validation = question.validation as DropdownValidation;

// skip validation if not required and value is empty
if (
validation.required !== true &&
(fieldValue === undefined || fieldValue === "" || fieldValue === null)
) {
return errors;
}

// required
if (
validation.required === true &&
Expand All @@ -353,10 +371,18 @@ function validateDropdownValue(fieldValue: string | any, question: Question) {

// eslint-disable-next-line require-jsdoc
function validateDatetimeValue(fieldValue: string, question: Question) {
const errors = [];
const errors: { field_id: string; message: string; }[] = [];

const validation = question.validation as DatetimeValidation;

// skip validation if not required and value is empty
if (
validation.required !== true &&
(fieldValue === undefined || fieldValue === "" || fieldValue === null)
) {
return errors;
}

// required
if (
validation.required === true &&
Expand All @@ -381,10 +407,18 @@ function validateDatetimeValue(fieldValue: string, question: Question) {

// eslint-disable-next-line require-jsdoc
function validateNumberValue(fieldValue: number | any, question: Question) {
const errors = [];
const errors: { field_id: string; message: string; }[] = [];

const validation = question.validation as NumberValidation;

// skip validation if not required and value is empty
if (
validation.required !== true &&
(fieldValue === undefined || fieldValue === "" || fieldValue === null)
) {
return errors;
}

// required
if (
validation.required === true &&
Expand All @@ -410,25 +444,35 @@ function validateNumberValue(fieldValue: number | any, question: Question) {
if (validation.minValue && fieldValue < validation.minValue) {
errors.push({
field_id: `${question.id}`,
message: `Must be more than equals ${validation.minValue}`,
message: `Must be more than or equal to ${validation.minValue}`,
});
} else if (validation.maxValue && fieldValue > validation.maxValue) {
}
if (validation.maxValue && fieldValue > validation.maxValue) {
errors.push({
field_id: `${question.id}`,
message: `Must be less than equals ${validation.maxValue}`,
message: `Must be less than or equal to ${validation.maxValue}`,
});
}

return errors;
}

/**
* Validate string value. Also works for textarea.
*/
function validateStringValue(fieldValue: string | any, question: Question) {
const errors = [];
const errors: { field_id: string; message: string; }[] = [];

const validation = question.validation as StringValidation;

// skip validation if not required and value is empty
if (
validation.required !== true &&
(fieldValue === undefined || fieldValue === "" || fieldValue === null)
) {
return errors;
}

// required
if (
validation.required === true &&
Expand Down
2 changes: 1 addition & 1 deletion functions/src/controllers/auth_controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ export const sessionLogin = async (
user = await auth.getUserByEmail(decodedIdToken.email);

// update user record for first time
const docRef = await db.collection("questions").doc(user.uid).get();
const docRef = await db.collection("users").doc(user.uid).get();
if (!docRef.exists) {
const userData: User = formatUser({
email: user.email ?? "",
Expand Down