-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#790 call backend when creating users
+ check if email not exists
- Loading branch information
1 parent
6927b65
commit b31995c
Showing
12 changed files
with
165 additions
and
20 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package ch.puzzle.okr.dto; | ||
|
||
public record NewUserDto(String firstname, String lastname, String email) { | ||
} |
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
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
36 changes: 36 additions & 0 deletions
36
frontend/src/app/team-management/new-user/unique-mail.directive.spec.ts
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,36 @@ | ||
import { UniqueEmailValidatorDirective } from './unique-mail.directive'; | ||
import { users } from '../../shared/testData'; | ||
import { TestBed } from '@angular/core/testing'; | ||
import { of } from 'rxjs'; | ||
import { AbstractControl } from '@angular/forms'; | ||
|
||
describe('UniqueMailDirective', () => { | ||
const userServiceMock = { | ||
getUsers: jest.fn(), | ||
} as any; | ||
|
||
beforeEach(() => { | ||
userServiceMock.getUsers.mockReturnValue(of(users)); | ||
}); | ||
|
||
it('should create an instance', () => { | ||
TestBed.runInInjectionContext(() => { | ||
const directive = new UniqueEmailValidatorDirective(userServiceMock); | ||
expect(directive).toBeTruthy(); | ||
}); | ||
}); | ||
|
||
it('should return validationError if user exists, otherwise null', () => { | ||
TestBed.runInInjectionContext(() => { | ||
const directive = new UniqueEmailValidatorDirective(userServiceMock); | ||
|
||
let control = { value: users[0].email } as AbstractControl; | ||
expect(directive.validate(control)).toStrictEqual({ notUniqueMail: { value: users[0].email } }); | ||
|
||
control = { value: '[email protected]' } as AbstractControl; | ||
expect(directive.validate(control)).toStrictEqual(null); | ||
|
||
expect(directive).toBeTruthy(); | ||
}); | ||
}); | ||
}); |
32 changes: 32 additions & 0 deletions
32
frontend/src/app/team-management/new-user/unique-mail.directive.ts
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,32 @@ | ||
import { Directive } from '@angular/core'; | ||
import { AbstractControl, NG_VALIDATORS, ValidationErrors, Validator } from '@angular/forms'; | ||
import { UserService } from '../../services/user.service'; | ||
import { takeUntilDestroyed } from '@angular/core/rxjs-interop'; | ||
|
||
@Directive({ | ||
selector: '[appUniqueEmail]', | ||
providers: [ | ||
{ | ||
provide: NG_VALIDATORS, | ||
useExisting: UniqueEmailValidatorDirective, | ||
multi: true, | ||
}, | ||
], | ||
}) | ||
export class UniqueEmailValidatorDirective implements Validator { | ||
private existingUserMails: string[] = []; | ||
|
||
constructor(private readonly userService: UserService) { | ||
this.userService | ||
.getUsers() | ||
.pipe(takeUntilDestroyed()) | ||
.subscribe((users) => { | ||
this.existingUserMails = users.map((u) => u.email); | ||
}); | ||
} | ||
|
||
validate(control: AbstractControl): ValidationErrors | null { | ||
const existingUser = this.existingUserMails.includes(control.value); | ||
return existingUser ? { notUniqueMail: { value: control.value } } : null; | ||
} | ||
} |
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