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>
</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>
</div>
</mat-card>

View File

@ -6,3 +6,7 @@
.pbl-ngrid-row {
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 { User } from 'app/shared/models/users/user';
import { BaseImportListComponentDirective } from 'app/site/base/base-import-list';
import { ImportCreateUser } from '../../models/import-create-user';
import { UserImportService } from '../../services/user-import.service';
/**
@ -44,15 +45,19 @@ export class UserImportListComponent extends BaseImportListComponentDirective<Us
private statusImportColumn: PblColumnDefinition = {
label: this.translate.instant('Status'),
prop: `status`
prop: `status`,
maxWidth: 50
};
private get generateImportColumns(): PblColumnDefinition[] {
return this.importer.headerMap.map((property, index: number) => {
const columnwidth = this.getPropertyWidth(property);
const singleColumnDef: PblColumnDefinition = {
label: this.translate.instant(this.headerRowDefinition[index]),
prop: `newEntry.${property}`,
type: this.guessType(property as keyof User)
type: this.guessType(property as keyof User),
width: columnwidth
};
return singleColumnDef;
@ -60,7 +65,7 @@ export class UserImportListComponent extends BaseImportListComponentDirective<Us
}
public columnSet = columnFactory()
.default({ minWidth: 150 })
.default({ minWidth: 90 })
.table(this.statusImportColumn, ...this.generateImportColumns)
.build();
@ -86,6 +91,27 @@ export class UserImportListComponent extends BaseImportListComponentDirective<Us
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
*/

View File

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

View File

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