Skip to content

Commit 5931326

Browse files
committed
Fixing generate random email so it's possible when time registration is enabled. If enabled after the user was created the email will be checked and if it's invalid the email is removed, so the user is forced to enter a valid email.
1 parent 0ab1bdc commit 5931326

File tree

4 files changed

+66
-11
lines changed

4 files changed

+66
-11
lines changed

eform-client/src/app/plugins/modules/backend-configuration-pn/modules/property-workers/components/property-worker-create-edit-modal/property-worker-create-edit-modal.component.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ <h3 mat-dialog-title>{{ (edit ? 'Edit employee' : 'New employee') | translate }}
1818
type="text"
1919
id="firstName"
2020
name="userName"
21-
tabindex="1">
21+
tabindex="1"
22+
cdkFocusInitial>
2223
<mat-error *ngIf="form.get('userFirstName').hasError('required')">
2324
Required
2425
</mat-error>
@@ -33,6 +34,7 @@ <h3 mat-dialog-title>{{ (edit ? 'Edit employee' : 'New employee') | translate }}
3334
required
3435
tabindex="3">
3536
<button
37+
*ngIf="shouldShowGenerateEmailButton()"
3638
matSuffix
3739
mat-icon-button
3840
type="button"

eform-client/src/app/plugins/modules/backend-configuration-pn/modules/property-workers/components/property-worker-create-edit-modal/property-worker-create-edit-modal.component.ts

Lines changed: 60 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,15 @@ import {MtxGridColumn} from '@ng-matero/extensions/grid';
1616
import {TranslateService} from '@ngx-translate/core';
1717
import {tap} from 'rxjs/operators';
1818
import {AppSettingsStateService} from 'src/app/modules/application-settings/components/store';
19-
import {FormBuilder, FormControl, FormGroup, Validators} from '@angular/forms';
19+
import {
20+
AbstractControl,
21+
FormBuilder,
22+
FormControl,
23+
FormGroup,
24+
ValidationErrors,
25+
ValidatorFn,
26+
Validators
27+
} from '@angular/forms';
2028
import validator from 'validator';
2129

2230
@AutoUnsubscribe()
@@ -34,12 +42,12 @@ export class PropertyWorkerCreateEditModalComponent implements OnInit, OnDestroy
3442
public dialogRef = inject(MatDialogRef<PropertyWorkerCreateEditModalComponent>);
3543
private appSettingsStateService = inject(AppSettingsStateService);
3644
private model = inject<{
37-
deviceUser: DeviceUserModel,
38-
assignments: PropertyAssignmentWorkerModel[],
39-
availableProperties: CommonDictionaryModel[],
40-
availableTags: CommonDictionaryModel[],
41-
alreadyUsedEmails: string[];
42-
}>(MAT_DIALOG_DATA);
45+
deviceUser: DeviceUserModel,
46+
assignments: PropertyAssignmentWorkerModel[],
47+
availableProperties: CommonDictionaryModel[],
48+
availableTags: CommonDictionaryModel[],
49+
alreadyUsedEmails: string[];
50+
}>(MAT_DIALOG_DATA);
4351

4452
availableProperties: CommonDictionaryModel[] = [];
4553
edit: boolean = false;
@@ -77,15 +85,14 @@ export class PropertyWorkerCreateEditModalComponent implements OnInit, OnDestroy
7785
form: FormGroup;
7886

7987

80-
8188
private updateDisabledFieldsBasedOnResigned() {
8289
const isResigned = this.form.get('resigned')?.value;
8390
Object.keys(this.form.controls).forEach(key => {
8491
if (key !== 'resigned' && key !== 'resignedAtDate') {
8592
if (isResigned) {
86-
this.form.get(key)?.disable({ emitEvent: false });
93+
this.form.get(key)?.disable({emitEvent: false});
8794
} else {
88-
this.form.get(key)?.enable({ emitEvent: false });
95+
this.form.get(key)?.enable({emitEvent: false});
8996
}
9097
}
9198
});
@@ -183,6 +190,20 @@ export class PropertyWorkerCreateEditModalComponent implements OnInit, OnDestroy
183190
Object.assign(this.selectedDeviceUser, formValue);
184191
});
185192

193+
this.form.get('timeRegistrationEnabled')?.valueChanges.subscribe(enabled => {
194+
const emailControl = this.form.get('workerEmail');
195+
const currentEmail = emailControl?.value || '';
196+
197+
if (enabled && currentEmail.includes('invalid')) {
198+
emailControl?.patchValue('');
199+
emailControl?.markAsTouched();
200+
}
201+
202+
this.updateEmailValidation();
203+
});
204+
205+
this.updateEmailValidation();
206+
186207
this.updateDisabledFieldsBasedOnResigned();
187208

188209
this.form.get('resigned')?.valueChanges.subscribe(() => {
@@ -357,4 +378,33 @@ export class PropertyWorkerCreateEditModalComponent implements OnInit, OnDestroy
357378
workerEmail: email
358379
});
359380
}
381+
382+
shouldShowGenerateEmailButton(): boolean {
383+
return !this.form?.get('timeRegistrationEnabled')?.value;
384+
}
385+
386+
private updateEmailValidation(): void {
387+
const emailControl = this.form.get('workerEmail');
388+
const timeRegistrationEnabled = this.form.get('timeRegistrationEnabled')?.value;
389+
390+
if (timeRegistrationEnabled) {
391+
emailControl?.setValidators([Validators.required, Validators.email, this.validEmailValidator()]);
392+
} else {
393+
emailControl?.setValidators([Validators.required, this.validEmailValidator()]);
394+
}
395+
396+
emailControl?.updateValueAndValidity();
397+
}
398+
399+
private validEmailValidator(): ValidatorFn {
400+
return (control: AbstractControl): ValidationErrors | null => {
401+
const email = control.value;
402+
const timeRegistrationEnabled = this.form?.get('timeRegistrationEnabled')?.value;
403+
404+
if (timeRegistrationEnabled && email && email.includes('invalid')) {
405+
return {invalidEmail: true};
406+
}
407+
return null;
408+
};
409+
}
360410
}

eform-client/src/app/plugins/modules/backend-configuration-pn/modules/property-workers/components/property-worker-table/property-worker-table.component.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ export class PropertyWorkerTableComponent implements OnInit, OnDestroy, OnChange
6060
@Input() workersAssignments: PropertyAssignWorkersModel[] = [];
6161
@Input() showResigned: boolean = false;
6262
@Input() availableTags: CommonDictionaryModel[] = [];
63+
@Input() alreadyUsedEmails: string[] = [];
6364
propertyWorkerOtpModalComponentAfterClosedSub$: Subscription;
6465
propertyWorkerEditModalComponentAfterClosedSub$: Subscription;
6566
//availableProperties: CommonDictionaryModel[];
@@ -262,6 +263,7 @@ export class PropertyWorkerTableComponent implements OnInit, OnDestroy, OnChange
262263
//assignments: [],
263264
availableProperties: this.availableProperties,
264265
availableTags: this.availableTags,
266+
alreadyUsedEmails: this.alreadyUsedEmails
265267
}), minWidth: 1024
266268
})
267269

eform-client/src/app/plugins/modules/backend-configuration-pn/modules/property-workers/components/property-workers-page/property-workers-page.component.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
[sitesDto]="sitesDto"
3232
[showResigned]="showResigned"
3333
[availableTags]="availableTags"
34+
[alreadyUsedEmails]="alreadyUsedEmails"
3435
>
3536
</app-property-worker-table>
3637

0 commit comments

Comments
 (0)