forked from DSpace/dspace-angular
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheperson-registration.service.ts
More file actions
137 lines (118 loc) · 3.97 KB
/
eperson-registration.service.ts
File metadata and controls
137 lines (118 loc) · 3.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import {
HttpHeaders,
HttpParams,
} from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import {
filter,
find,
map,
} from 'rxjs/operators';
import {
hasValue,
isNotEmpty,
} from '../../shared/empty.util';
import { RemoteDataBuildService } from '../cache/builders/remote-data-build.service';
import { HttpOptions } from '../dspace-rest/dspace-rest.service';
import { GenericConstructor } from '../shared/generic-constructor';
import { HALEndpointService } from '../shared/hal-endpoint.service';
import { getFirstCompletedRemoteData } from '../shared/operators';
import { Registration } from '../shared/registration.model';
import { ResponseParsingService } from './parsing.service';
import { RegistrationResponseParsingService } from './registration-response-parsing.service';
import { RemoteData } from './remote-data';
import {
GetRequest,
PostRequest,
} from './request.models';
import { RequestService } from './request.service';
@Injectable({
providedIn: 'root',
})
/**
* Service that will register a new email address and request a token
*/
export class EpersonRegistrationService {
protected linkPath = 'registrations';
protected searchByTokenPath = '/search/findByToken?token=';
constructor(
protected requestService: RequestService,
protected rdbService: RemoteDataBuildService,
protected halService: HALEndpointService,
) {
}
/**
* Retrieves the Registration endpoint
*/
getRegistrationEndpoint(): Observable<string> {
return this.halService.getEndpoint(this.linkPath);
}
/**
* Retrieves the endpoint to search by registration token
*/
getTokenSearchEndpoint(token: string): Observable<string> {
return this.halService.getEndpoint(this.linkPath).pipe(
filter((href: string) => isNotEmpty(href)),
map((href: string) => `${href}${this.searchByTokenPath}${token}`));
}
/**
* Register a new email address
* @param email
* @param captchaToken the value of x-captcha-payload header
*/
registerEmail(email: string, captchaToken: string = null, type?: string): Observable<RemoteData<Registration>> {
const registration = new Registration();
registration.email = email;
const requestId = this.requestService.generateRequestId();
const href$ = this.getRegistrationEndpoint();
const options: HttpOptions = Object.create({});
let headers = new HttpHeaders();
if (captchaToken) {
headers = headers.append('x-captcha-payload', captchaToken);
}
options.headers = headers;
if (hasValue(type)) {
options.params = type ?
new HttpParams({ fromString: 'accountRequestType=' + type }) : new HttpParams();
}
href$.pipe(
find((href: string) => hasValue(href)),
map((href: string) => {
const request = new PostRequest(requestId, href, registration, options);
this.requestService.send(request);
}),
).subscribe();
return this.rdbService.buildFromRequestUUID<Registration>(requestId).pipe(
getFirstCompletedRemoteData(),
);
}
/**
* Search a registration based on the provided token
* @param token
*/
searchByToken(token: string): Observable<RemoteData<Registration>> {
const requestId = this.requestService.generateRequestId();
const href$ = this.getTokenSearchEndpoint(token).pipe(
find((href: string) => hasValue(href)),
);
href$.subscribe((href: string) => {
const request = new GetRequest(requestId, href);
Object.assign(request, {
getResponseParser(): GenericConstructor<ResponseParsingService> {
return RegistrationResponseParsingService;
},
});
this.requestService.send(request, true);
});
return this.rdbService.buildSingle<Registration>(href$).pipe(
map((rd) => {
if (rd.hasSucceeded && hasValue(rd.payload)) {
return Object.assign(rd, { payload: Object.assign(rd.payload, { token }) });
} else {
return rd;
}
}),
);
}
}