-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1080 from ORCID/refactorNgServices
Refactor ng services
- Loading branch information
Showing
9 changed files
with
101 additions
and
55 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
ui/src/app/account/model/password-reset-init-result.model.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,17 @@ | ||
export interface IPasswordResetInitResult { | ||
success: boolean | ||
emailNotFound: boolean | ||
generalError: boolean | ||
} | ||
|
||
export class PasswordResetInitResult implements IPasswordResetInitResult { | ||
success: boolean | ||
emailNotFound: boolean | ||
generalError: boolean | ||
|
||
constructor(success: boolean, emailNotFound: boolean, generalError: boolean) { | ||
this.emailNotFound = emailNotFound | ||
this.generalError = generalError | ||
this.success = success | ||
} | ||
} |
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 |
---|---|---|
|
@@ -6,6 +6,7 @@ import { PasswordResetInitComponent } from './password-reset-init.component' | |
import { EMAIL_NOT_FOUND_TYPE } from 'src/app/app.constants' | ||
import { HttpClientTestingModule } from '@angular/common/http/testing' | ||
import { By } from '@angular/platform-browser' | ||
import { PasswordResetInitResult } from '../model/password-reset-init-result.model' | ||
|
||
describe('Component Tests', () => { | ||
describe('PasswordResetInitComponent', () => { | ||
|
@@ -28,7 +29,7 @@ describe('Component Tests', () => { | |
it('notifies of success upon successful requestReset', inject( | ||
[PasswordResetInitService], | ||
(service: PasswordResetInitService) => { | ||
spyOn(service, 'initPasswordReset').and.returnValue(of({})) | ||
spyOn(service, 'initPasswordReset').and.returnValue(of(new PasswordResetInitResult(true, false, false))) | ||
comp.resetRequestForm.patchValue({ | ||
email: '[email protected]', | ||
}) | ||
|
@@ -51,12 +52,7 @@ describe('Component Tests', () => { | |
it('notifies of unknown email upon email address not registered/400', inject( | ||
[PasswordResetInitService], | ||
(service: PasswordResetInitService) => { | ||
spyOn(service, 'initPasswordReset').and.returnValue( | ||
throwError({ | ||
status: 400, | ||
error: { type: EMAIL_NOT_FOUND_TYPE }, | ||
}) | ||
) | ||
spyOn(service, 'initPasswordReset').and.returnValue(of(new PasswordResetInitResult(false, true, false))) | ||
comp.resetRequestForm.patchValue({ | ||
email: '[email protected]', | ||
}) | ||
|
@@ -72,12 +68,7 @@ describe('Component Tests', () => { | |
it('notifies of error upon error response', inject( | ||
[PasswordResetInitService], | ||
(service: PasswordResetInitService) => { | ||
spyOn(service, 'initPasswordReset').and.returnValue( | ||
throwError({ | ||
status: 503, | ||
data: 'something else', | ||
}) | ||
) | ||
spyOn(service, 'initPasswordReset').and.returnValue(of(new PasswordResetInitResult(false, false, true))) | ||
comp.resetRequestForm.patchValue({ | ||
email: '[email protected]', | ||
}) | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,31 @@ | ||
import { Injectable } from '@angular/core' | ||
import { HttpClient } from '@angular/common/http' | ||
import { Observable } from 'rxjs' | ||
import { HttpClient, HttpResponse } from '@angular/common/http' | ||
import { Observable, map, catchError, of } from 'rxjs' | ||
import { PasswordResetInitResult } from '../model/password-reset-init-result.model' | ||
import { EMAIL_NOT_FOUND_TYPE } from 'src/app/app.constants' | ||
|
||
@Injectable({ providedIn: 'root' }) | ||
export class PasswordResetInitService { | ||
constructor(private http: HttpClient) {} | ||
|
||
initPasswordReset(mail: string): Observable<any> { | ||
return this.http.post('/services/userservice/api/account/reset-password/init', mail) | ||
initPasswordReset(mail: string): Observable<PasswordResetInitResult | null> { | ||
return this.http.post('/services/userservice/api/account/reset-password/init', mail, { observe: 'response' }).pipe( | ||
map((res: HttpResponse<any>) => this.getResult(res)), | ||
catchError((err) => { | ||
return of(null) | ||
}) | ||
) | ||
} | ||
|
||
getResult(res: HttpResponse<any>): PasswordResetInitResult { | ||
if (res.status == 200) { | ||
return new PasswordResetInitResult(true, false, false) | ||
} | ||
|
||
if (res.status === 400 && res.body.error.type === EMAIL_NOT_FOUND_TYPE) { | ||
return new PasswordResetInitResult(false, false, true) | ||
} | ||
|
||
return new PasswordResetInitResult(false, true, false) | ||
} | ||
} |
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