-
Notifications
You must be signed in to change notification settings - Fork 12
fix: view-mapped plugin restrict to mapped sheet #720
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@flatfile/plugin-view-mapped': patch | ||
| --- | ||
|
|
||
| This release fixes a bug in the view-mapped plugin when multiple sheets have fields with the same key by limiting the view-mapped update to the sheet being mapped. Additionally, the view-mapped plugin now only runs when the `trackChanges` setting in the workbook is enabled to prevent a race condition where the view-mapped plugin runs before hooks have completed. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,12 @@ | ||
| import { FlatfileClient } from '@flatfile/api' | ||
| import { type Flatfile, FlatfileClient } from '@flatfile/api' | ||
| import type { FlatfileListener } from '@flatfile/listener' | ||
| import { jobHandler } from '@flatfile/plugin-job-handler' | ||
| import { logError } from '@flatfile/util-common' | ||
|
|
||
| const api = new FlatfileClient() | ||
|
|
||
| const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)) | ||
|
|
||
| /** | ||
| * This plugin allows you to make the post-mapping sheet only display mapped data | ||
| */ | ||
|
|
@@ -48,6 +50,10 @@ export function viewMappedPlugin() { | |
| // Obtaining the mapping job's execution plan to later extract "fieldMapping" out of it, which tells us which fields were mapped in the Matching step | ||
| const jobPlan = await api.jobs.getExecutionPlan(mappingJobId) | ||
|
|
||
| const destinationSheetId = ( | ||
| jobPlan.data.job.config as Flatfile.MappingProgramJobConfig | ||
| ).destinationSheetId | ||
|
|
||
| // Initializing an empty array to store the keys of the mapped fields | ||
| const mappedFields = [] | ||
|
|
||
|
|
@@ -65,13 +71,23 @@ export function viewMappedPlugin() { | |
| // We need to make this API call and cannot just use what's inside of "workbookOne" because we need data in a specific format | ||
| const { data: workbook } = await api.workbooks.get(workbookId) | ||
|
|
||
| // If trackChanges is not enabled, we skip the rest of the job. This configuration is required to provide the plugins with the | ||
| // awareness that all hooks have run. Without it, we run into a race condition between the hook and the Workbook update. If the | ||
| // Workbook update runs before the hook, the hook will not be able to update the Workbook. | ||
| if (!workbook.settings.trackChanges) { | ||
| console.log('Skipping because trackChanges is not enabled') | ||
| return | ||
| } | ||
|
|
||
| // Looping through all sheets of the Workbook One. For all fields that are mapped, updating those fields' metadata to "{mapped: true}" | ||
| workbook.sheets.forEach((sheet) => { | ||
| sheet.config.fields.forEach((field) => { | ||
| if (mappedFields.includes(field.key)) { | ||
| field.metadata = { mapped: true } | ||
| } | ||
| }) | ||
| if (sheet.id === destinationSheetId) { | ||
| sheet.config.fields.forEach((field) => { | ||
| if (mappedFields.includes(field.key)) { | ||
| field.metadata = { mapped: true } | ||
| } | ||
| }) | ||
| } | ||
| }) | ||
|
|
||
| // Looping over each sheet in "workbook" and filtering for fields with metadata "mapped: true". Saving mapped fields per each sheet inside of "filteredWorkbookFields" varibable | ||
|
|
@@ -104,6 +120,23 @@ export function viewMappedPlugin() { | |
|
|
||
| await tick(80, 'plugins.viewMapped.almostDone') | ||
|
|
||
| // Check that all commits are completed before running this job | ||
| let hasUncompletedCommits = true | ||
| do { | ||
| const { data: commits } = await api.sheets.getSheetCommits( | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there any other way to trigger this other than polling? Can we listen for completed commits or do we need to make another mechanism to control the order of these event responses? |
||
| destinationSheetId, | ||
| { | ||
| completed: false, | ||
| } | ||
| ) | ||
| console.log(`Waiting on ${commits.length} commits to complete...`) | ||
| hasUncompletedCommits = commits.length > 0 | ||
| if (hasUncompletedCommits) { | ||
| // We wait for 200ms between each check to avoid making too many requests to the API | ||
| await sleep(200) | ||
| } | ||
| } while (hasUncompletedCommits) | ||
|
|
||
|
carlbrugger marked this conversation as resolved.
|
||
| // Updating each sheet in a workbook to only contain fields that a user mapped. This ensures that when the table with data loads, only mapped fields will be displayed | ||
| await api.workbooks.update(workbookId, { | ||
| // Keeping other non-sheet elements of the workbook untouched (Workbook name, its Submit action, etc) | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.