Merge pull request #5823 from tsiegleauq/fix-group-import

Fix csv importing users with groups
This commit is contained in:
Sean 2021-02-05 10:38:36 +01:00 committed by GitHub
commit 058a7f71ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 60 additions and 33 deletions

View File

@ -199,6 +199,20 @@
</mat-icon> </mat-icon>
</div> </div>
</div> </div>
<!-- handle groups -->
<div *pblNgridCellDef="'newEntry.csvGroups'; value as groups">
<span *ngFor="let group of groups; let last = last">
<span>{{ group.name | translate }}</span>
<span *ngIf="!group.id"> <mat-icon class="new-icon" color="accent">add</mat-icon></span>
<span *ngIf="!last">, </span>
</span>
</div>
<!-- handle gender, needs translation -->
<div *pblNgridCellDef="'newEntry.gender'; value as gender">
{{ gender | translate }}
</div>
</pbl-ngrid> </pbl-ngrid>
</div> </div>
</mat-card> </mat-card>

View File

@ -6,3 +6,7 @@
.pbl-ngrid-row { .pbl-ngrid-row {
height: 50px; height: 50px;
} }
.new-icon {
vertical-align: sub;
}

View File

@ -10,6 +10,7 @@ import { NewEntry } from 'app/core/ui-services/base-import.service';
import { CsvExportService } from 'app/core/ui-services/csv-export.service'; import { CsvExportService } from 'app/core/ui-services/csv-export.service';
import { User } from 'app/shared/models/users/user'; import { User } from 'app/shared/models/users/user';
import { BaseImportListComponentDirective } from 'app/site/base/base-import-list'; import { BaseImportListComponentDirective } from 'app/site/base/base-import-list';
import { ImportCreateUser } from '../../models/import-create-user';
import { UserImportService } from '../../services/user-import.service'; import { UserImportService } from '../../services/user-import.service';
/** /**
@ -44,15 +45,19 @@ export class UserImportListComponent extends BaseImportListComponentDirective<Us
private statusImportColumn: PblColumnDefinition = { private statusImportColumn: PblColumnDefinition = {
label: this.translate.instant('Status'), label: this.translate.instant('Status'),
prop: `status` prop: `status`,
maxWidth: 50
}; };
private get generateImportColumns(): PblColumnDefinition[] { private get generateImportColumns(): PblColumnDefinition[] {
return this.importer.headerMap.map((property, index: number) => { return this.importer.headerMap.map((property, index: number) => {
const columnwidth = this.getPropertyWidth(property);
const singleColumnDef: PblColumnDefinition = { const singleColumnDef: PblColumnDefinition = {
label: this.translate.instant(this.headerRowDefinition[index]), label: this.translate.instant(this.headerRowDefinition[index]),
prop: `newEntry.${property}`, prop: `newEntry.${property}`,
type: this.guessType(property as keyof User) type: this.guessType(property as keyof User),
width: columnwidth
}; };
return singleColumnDef; return singleColumnDef;
@ -60,7 +65,7 @@ export class UserImportListComponent extends BaseImportListComponentDirective<Us
} }
public columnSet = columnFactory() public columnSet = columnFactory()
.default({ minWidth: 150 }) .default({ minWidth: 90 })
.table(this.statusImportColumn, ...this.generateImportColumns) .table(this.statusImportColumn, ...this.generateImportColumns)
.build(); .build();
@ -86,6 +91,27 @@ export class UserImportListComponent extends BaseImportListComponentDirective<Us
this.textAreaForm = formBuilder.group({ inputtext: [''] }); this.textAreaForm = formBuilder.group({ inputtext: [''] });
} }
private getPropertyWidth(property: keyof ImportCreateUser): string {
switch (property) {
case 'username':
case 'gender':
case 'vote_weight':
case 'default_password':
return '120px';
case 'first_name':
case 'last_name':
case 'structure_level':
case 'number':
case 'email':
case 'comment':
return '300px';
case 'csvGroups':
return '400px';
default:
return '';
}
}
/** /**
* Triggers an example csv download * Triggers an example csv download
*/ */

View File

@ -39,27 +39,13 @@ export class ImportCreateUser extends User {
* *
* @param groups * @param groups
*/ */
public solveGroups(groups: CsvMapping[]): number { public solveGroups(recentlyCreatedGroups: CsvMapping[]): void {
let open = 0; const groups = this.csvGroups;
const ids: number[] = []; const directIds = groups.filter(directGroup => directGroup.id).map(directGroup => directGroup.id);
this.csvGroups.forEach(group => { const groupsWithoutId = groups.filter(noIdGroup => !noIdGroup.id);
if (group.id) { const transferedIds = recentlyCreatedGroups
ids.push(group.id); .filter(newGroup => groupsWithoutId.find(noId => noId.name === newGroup.name))
return; .map(newGroup => newGroup.id);
} this.groups_id = directIds.concat(transferedIds);
if (!groups.length) {
open += 1;
return;
}
const mapped = groups.find(newGroup => newGroup.name === group.name);
if (mapped) {
group.id = mapped.id;
ids.push(mapped.id);
} else {
open += 1;
}
});
this.groups_id = ids;
return open;
} }
} }

View File

@ -25,7 +25,7 @@ export class UserImportService extends BaseImportService<User> {
'last_name', 'last_name',
'structure_level', 'structure_level',
'number', 'number',
'groups_id', 'csvGroups',
'comment', 'comment',
'is_active', 'is_active',
'is_present', 'is_present',
@ -97,10 +97,11 @@ export class UserImportService extends BaseImportService<User> {
public mapData(line: string): NewEntry<User> { public mapData(line: string): NewEntry<User> {
const user = new ImportCreateUser(); const user = new ImportCreateUser();
const headerLength = Math.min(this.expectedHeader.length, line.length); const headerLength = Math.min(this.expectedHeader.length, line.length);
let hasErrors = false; let hasErrors = false;
for (let idx = 0; idx < headerLength; idx++) { for (let idx = 0; idx < headerLength; idx++) {
switch (this.expectedHeader[idx]) { switch (this.expectedHeader[idx]) {
case 'groups_id': case 'csvGroups':
user.csvGroups = this.getGroups(line[idx]); user.csvGroups = this.getGroups(line[idx]);
break; break;
case 'is_active': case 'is_active':
@ -152,16 +153,12 @@ export class UserImportService extends BaseImportService<User> {
if (entry.status !== 'new') { if (entry.status !== 'new') {
continue; continue;
} }
const openGroups = (entry.newEntry as ImportCreateUser).solveGroups(this.newGroups); (entry.newEntry as ImportCreateUser).solveGroups(this.newGroups);
if (openGroups) {
this.setError(entry, 'Group');
this.updatePreview();
continue;
}
entry.importTrackId = trackId; entry.importTrackId = trackId;
trackId += 1; trackId += 1;
importUsers.push(entry); importUsers.push(entry);
} }
while (importUsers.length) { while (importUsers.length) {
const subSet = importUsers.splice(0, 100); // don't send bulks too large const subSet = importUsers.splice(0, 100); // don't send bulks too large
const result = await this.repo.bulkCreate(subSet); const result = await this.repo.bulkCreate(subSet);