Skip to content

Commit

Permalink
Merge pull request #53 from marmelab/fix/import-tag-duplicaion
Browse files Browse the repository at this point in the history
Fix(import): tags and companies are no loger duplicated during imports
  • Loading branch information
jonathanarnault authored Aug 9, 2024
2 parents 1ddf538 + ce5c58d commit 9898124
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 26 deletions.
6 changes: 4 additions & 2 deletions src/contacts/useContactImport.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ export function useContactImport() {

const entities = uncachedEntities.length
? await dataProvider.getList(entity, {
filter: { name: uncachedEntities },
pagination: { page: 1, perPage: 1 },
filter: {
'name@in': `(${uncachedEntities.map(entity => `"${entity}"`).join(',')})`,
},
pagination: { page: 1, perPage: trimmedNames.length },
sort: { field: 'id', order: 'ASC' },
})
: { data: [] };
Expand Down
48 changes: 48 additions & 0 deletions src/providers/fakerest/internal/listParser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
export const UNQUOTED_ALLOWED_CHARS = '[A-Za-zÀ-ÖØ-öø-ÿ0-9-]+';
export const QUOTED_ALLOWED_CHARS = '[A-Za-zÀ-ÖØ-öø-ÿ0-9, -]+';
export const LIST_REGEX_BASE = `(${UNQUOTED_ALLOWED_CHARS}|"${QUOTED_ALLOWED_CHARS}")(,(${UNQUOTED_ALLOWED_CHARS}|"${QUOTED_ALLOWED_CHARS}"))*`;

/**
* List represents a list of values, quoted or not.
*
* e.g. 1
* e.g 1,2
* e.g "a","b"
* e.g "a",b
*/
export function parseList(list: string) {
const parsedItems = [];

let currentItem = '';
let currentQuoted = false;
for (const char of list) {
if (char === ',') {
if (currentQuoted) {
currentItem += char;
continue;
}

parsedItems.push(currentItem);
currentItem = '';
continue;
}

if (char === '"') {
currentQuoted = !currentQuoted;
continue;
}

currentItem += char;
}
if (currentItem) {
parsedItems.push(currentItem);
}

return parsedItems.map((v: string) => {
const parsedFloat = Number.parseFloat(v);
if (!Number.isNaN(parsedFloat)) {
return parsedFloat;
}
return v;
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,8 @@ it('should return an array of strings', () => {
expect(transformContainsFilter('{a}')).toEqual(['a']);
expect(transformContainsFilter('{a,B,c-d}')).toEqual(['a', 'B', 'c-d']);
});

it('should return an array of quoted strings', () => {
expect(transformContainsFilter('{"a"}')).toEqual(['a']);
expect(transformContainsFilter('{"a","B, c"}')).toEqual(['a', 'B, c']);
});
16 changes: 4 additions & 12 deletions src/providers/fakerest/internal/transformContainsFilter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export const CONTAINS_FILTER_REGEX =
/^\{[A-Za-zÀ-ÖØ-öø-ÿ0-9-]+(,[A-Za-zÀ-ÖØ-öø-ÿ0-9-]+)*\}$/i;
import { LIST_REGEX_BASE, parseList } from './listParser';

export const CONTAINS_FILTER_REGEX = new RegExp(`^\\{${LIST_REGEX_BASE}\\}$`);

export function transformContainsFilter(value: any) {
if (value === '{}') {
Expand All @@ -12,14 +13,5 @@ export function transformContainsFilter(value: any) {
);
}

return value
.slice(1, -1)
.split(',')
.map((v: string) => {
const parsedFloat = Number.parseFloat(v);
if (!Number.isNaN(parsedFloat)) {
return parsedFloat;
}
return v;
});
return parseList(value.slice(1, -1));
}
5 changes: 5 additions & 0 deletions src/providers/fakerest/internal/transformInFilter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,8 @@ it('should return an array of strings', () => {
expect(transformInFilter('(a)')).toEqual(['a']);
expect(transformInFilter('(a,B,c-d)')).toEqual(['a', 'B', 'c-d']);
});

it('should return an array of quoted strings', () => {
expect(transformInFilter('("a")')).toEqual(['a']);
expect(transformInFilter('("a","B, c")')).toEqual(['a', 'B, c']);
});
16 changes: 4 additions & 12 deletions src/providers/fakerest/internal/transformInFilter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export const IN_FILTER_REGEX =
/^\([A-Za-zÀ-ÖØ-öø-ÿ0-9-]+(,[A-Za-zÀ-ÖØ-öø-ÿ0-9-]+)*\)$/i;
import { LIST_REGEX_BASE, parseList } from './listParser';

export const IN_FILTER_REGEX = new RegExp(`^\\(${LIST_REGEX_BASE}\\)$`);

export function transformInFilter(value: any) {
if (value === '()') {
Expand All @@ -12,14 +13,5 @@ export function transformInFilter(value: any) {
);
}

return value
.slice(1, -1)
.split(',')
.map((v: string) => {
const parsedFloat = Number.parseFloat(v);
if (!Number.isNaN(parsedFloat)) {
return parsedFloat;
}
return v;
});
return parseList(value.slice(1, -1));
}

0 comments on commit 9898124

Please sign in to comment.