Skip to content
Closed
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
32 changes: 30 additions & 2 deletions plugins/view-mapped/src/view-mapped.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms))
/**
* This plugin allows you to make the post-mapping sheet only display mapped data
*/
export function viewMappedPlugin() {
export function viewMappedPlugin(options?: { includeRequired?: boolean }) {
const { includeRequired = false } = options
return (listener: FlatfileListener) => {
// Defining what needs to be done when Flatfile is done mapping columns based on user input during the Mapping stage of the import process
listener.on('job:completed', { job: 'workbook:map' }, async (event) => {
Expand Down Expand Up @@ -71,6 +72,33 @@ export function viewMappedPlugin() {
mappedFields.push(destinationFieldKey)
}

//If the includeRequired bool value is set to true, we will add all required fields to the mappedFields array
if (includeRequired) {
for (
let i = 0;
i < jobPlan.data.plan.unmappedDestinationFields.length;
i++
) {
for (
let j = 0;
j <
jobPlan.data.plan.unmappedDestinationFields[i].destinationField
.constraints?.length;
j++
) {
if (
jobPlan.data.plan.unmappedDestinationFields[i]
.destinationField.constraints[j].type === 'required'
) {
const requiredFieldKey =
jobPlan.data.plan.unmappedDestinationFields[i]
.destinationField.key
mappedFields.push(requiredFieldKey)
}
Comment on lines +96 to +97

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Prevent duplicate field keys in mappedFields array.

If a field is both mapped and required, it could be added to the mappedFields array twice, which might lead to unexpected behavior.

Add a check to prevent duplicates:

-  mappedFields.push(requiredFieldKey)
+  if (!mappedFields.includes(requiredFieldKey)) {
+    mappedFields.push(requiredFieldKey)
+  }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
mappedFields.push(requiredFieldKey)
}
if (!mappedFields.includes(requiredFieldKey)) {
mappedFields.push(requiredFieldKey)
}
}

}
}
}
Comment on lines +75 to +100

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Add null-safety checks for constraints property.

The current implementation might cause runtime errors if constraints is undefined for any field.

Add proper null-safety checks with optional chaining:

  if (includeRequired) {
    for (
      let i = 0;
      i < jobPlan.data.plan.unmappedDestinationFields.length;
      i++
    ) {
+     const constraints = jobPlan.data.plan.unmappedDestinationFields[i]?.destinationField?.constraints;
+     if (!constraints || constraints.length === 0) continue;
+     
      for (
        let j = 0;
-        j <
-        jobPlan.data.plan.unmappedDestinationFields[i].destinationField
-          .constraints?.length;
+        j < constraints.length;
        j++
      ) {
        if (
-          jobPlan.data.plan.unmappedDestinationFields[i]
-            .destinationField.constraints[j].type === 'required'
+          constraints[j].type === 'required'
        ) {
          const requiredFieldKey =
            jobPlan.data.plan.unmappedDestinationFields[i]
              .destinationField.key
          mappedFields.push(requiredFieldKey)
        }
      }
    }
  }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
//If the includeRequired bool value is set to true, we will add all required fields to the mappedFields array
if (includeRequired) {
for (
let i = 0;
i < jobPlan.data.plan.unmappedDestinationFields.length;
i++
) {
for (
let j = 0;
j <
jobPlan.data.plan.unmappedDestinationFields[i].destinationField
.constraints?.length;
j++
) {
if (
jobPlan.data.plan.unmappedDestinationFields[i]
.destinationField.constraints[j].type === 'required'
) {
const requiredFieldKey =
jobPlan.data.plan.unmappedDestinationFields[i]
.destinationField.key
mappedFields.push(requiredFieldKey)
}
}
}
}
//If the includeRequired bool value is set to true, we will add all required fields to the mappedFields array
if (includeRequired) {
for (
let i = 0;
i < jobPlan.data.plan.unmappedDestinationFields.length;
i++
) {
const constraints = jobPlan.data.plan.unmappedDestinationFields[i]?.destinationField?.constraints;
if (!constraints || constraints.length === 0) continue;
for (
let j = 0;
j < constraints.length;
j++
) {
if (constraints[j].type === 'required') {
const requiredFieldKey =
jobPlan.data.plan.unmappedDestinationFields[i]
.destinationField.key
mappedFields.push(requiredFieldKey)
}
}
}
}


await tick(30, 'plugins.viewMapped.updatingTable')

// Making an API call to only get the "data" property out of the response, and saving it as its own "fetchedWorkbook" variable
Expand All @@ -85,7 +113,7 @@ export function viewMappedPlugin() {
return
}

// Looping through all sheets of the Workbook One. For all fields that are mapped, updating those fields' metadata to "{mapped: true}"
// Looping through all sheets of the Workbook One. For all fields that are mapped or are required (if the includeRequired bool value is set to true), updating those fields' metadata to "{mapped: true}"
workbook.sheets.forEach((sheet) => {
if (sheet.id === destinationSheetId) {
sheet.config.fields.forEach((field) => {
Expand Down