Skip to content

Dev release#2

Merged
Shubham4026 merged 5 commits into
mainfrom
dev-release
May 7, 2026
Merged

Dev release#2
Shubham4026 merged 5 commits into
mainfrom
dev-release

Conversation

@Shubham4026
Copy link
Copy Markdown
Collaborator

No description provided.

@Shubham4026 Shubham4026 merged commit 761c7a1 into main May 7, 2026
1 check passed
Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a contextType filter for surveys and refactors the APIResponse utility to pass result objects directly rather than spreading them. It also implements conditional logic for field validation in survey responses. Feedback focuses on ensuring surveyId remains mandatory in the response DTO for better error reporting and optimizing the validation logic by using a Map for field lookups to improve performance from O(N^2) to O(N).

Comment on lines +11 to 13
@IsOptional()
@IsUUID()
surveyId: string;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The surveyId field should be mandatory when creating a response. Marking it as @IsOptional() allows requests with a missing surveyId to pass validation, which will subsequently cause a NotFoundException in the service layer when it fails to fetch the survey. It is better to enforce this requirement at the DTO level to provide a clearer error response (400 Bad Request).

Suggested change
@IsOptional()
@IsUUID()
surveyId: string;
@IsUUID()
surveyId: string;

Comment on lines +535 to +549
private isConditionMet(field: any, allFields: any[], responseData: Record<string, any>): boolean {
const logic = field.conditionalLogic;
if (!logic || !logic.depends_on || !logic.show_if) return true;

const parentField = allFields.find(f => f.fieldName === logic.depends_on);
if (!parentField) return true;

const parentValue = responseData[parentField.fieldId];
const showIf: string[] = logic.show_if;

if (Array.isArray(parentValue)) {
return parentValue.some(v => showIf.includes(v));
}
return showIf.includes(parentValue);
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The current implementation of isConditionMet performs an O(N) search using allFields.find(). Since this method is called inside a loop in validateRequiredFields, the overall complexity becomes O(N^2). Changing the parameter to a Map will allow for O(1) lookups, which is significantly more efficient for surveys with a large number of fields.

Suggested change
private isConditionMet(field: any, allFields: any[], responseData: Record<string, any>): boolean {
const logic = field.conditionalLogic;
if (!logic || !logic.depends_on || !logic.show_if) return true;
const parentField = allFields.find(f => f.fieldName === logic.depends_on);
if (!parentField) return true;
const parentValue = responseData[parentField.fieldId];
const showIf: string[] = logic.show_if;
if (Array.isArray(parentValue)) {
return parentValue.some(v => showIf.includes(v));
}
return showIf.includes(parentValue);
}
private isConditionMet(field: any, fieldsMap: Map<string, any>, responseData: Record<string, any>): boolean {
const logic = field.conditionalLogic;
if (!logic || !logic.depends_on || !logic.show_if) return true;
const parentField = fieldsMap.get(logic.depends_on);
if (!parentField) return true;
const parentValue = responseData[parentField.fieldId];
const showIf: string[] = logic.show_if;
if (Array.isArray(parentValue)) {
return parentValue.some(v => showIf.includes(v));
}
return showIf.includes(parentValue);
}

Comment on lines +554 to +560
const allFields = (survey.sections || []).flatMap((s: any) => s.fields || []);

for (const section of survey.sections || []) {
for (const field of section.fields || []) {
if (field.isRequired) {
const value = surveyResponse.responseData[field.fieldId];
const fileIds = surveyResponse.fileUploadIds[field.fieldId];
if (field.isRequired && this.isConditionMet(field, allFields, surveyResponse.responseData ?? {})) {
const value = (surveyResponse.responseData ?? {})[field.fieldId];
const fileIds = (surveyResponse.fileUploadIds ?? {})[field.fieldId];
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To optimize the validation process and improve code readability, create a Map for field lookups once and extract responseData and fileUploadIds into local variables. This avoids repeated O(N) searches and redundant nullish coalescing operations inside the nested loops.

    const allFields = (survey.sections || []).flatMap((s: any) => s.fields || []);
    const fieldsMap = new Map(allFields.map(f => [f.fieldName, f]));
    const responseData = surveyResponse.responseData ?? {};
    const fileUploadIds = surveyResponse.fileUploadIds ?? {};

    for (const section of survey.sections || []) {
      for (const field of section.fields || []) {
        if (field.isRequired && this.isConditionMet(field, fieldsMap, responseData)) {
          const value = responseData[field.fieldId];
          const fileIds = fileUploadIds[field.fieldId];

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants