-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
assign unique survey name when importing survey from Collect (#1622)
Co-authored-by: Stefano Ricci <SteRiccio@users.noreply.github.com> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
- Loading branch information
1 parent
a62dcda
commit 86d113e
Showing
12 changed files
with
115 additions
and
36 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
33 changes: 33 additions & 0 deletions
33
...r/modules/collectImport/service/collectImport/metaImportJobs/surveyUniqueNameGenerator.js
This file contains 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,33 @@ | ||
import { db } from '@server/db/db' | ||
import * as SurveyManager from '@server/modules/survey/manager/surveyManager' | ||
|
||
const nameWithCountPattern = /^(.*)_(\d{1,2})$/ // everything ending with _ followed by 1 or 2 decimals | ||
|
||
const parseName = (name) => { | ||
const match = name.match(nameWithCountPattern) | ||
return match | ||
? { | ||
nameWithoutCount: match[1], | ||
count: Number(match[2]), | ||
} | ||
: { | ||
nameWithoutCount: name, | ||
count: 0, | ||
} | ||
} | ||
|
||
export const findUniqueSurveyName = async ({ startingName, client = db }) => { | ||
const surveyIdsAndNames = await SurveyManager.fetchSurveyIdsAndNames(client) | ||
|
||
const isDuplicate = (name) => surveyIdsAndNames.some(({ name: surveyName }) => surveyName === name) | ||
|
||
// extract count from startingName (suffix _NN added to the survey name, if any) | ||
let currentName = startingName | ||
let { count, nameWithoutCount } = parseName(startingName) | ||
|
||
while (isDuplicate(currentName)) { | ||
count += 1 | ||
currentName = `${nameWithoutCount}_${count}` | ||
} | ||
return currentName | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
|
@@ -60,4 +60,5 @@ export const { | |
deleteTemporarySurveys, | ||
// UTILS | ||
validateNewSurvey, | ||
validateSurveyClone, | ||
} = SurveyManager |
This file contains 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
This file contains 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