-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathregister.service.ts
More file actions
194 lines (182 loc) · 5.79 KB
/
Copy pathregister.service.ts
File metadata and controls
194 lines (182 loc) · 5.79 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import { HttpClient } from '@angular/common/http'
import { Injectable } from '@angular/core'
import { UntypedFormGroup } from '@angular/forms'
import { Observable, throwError } from 'rxjs'
import { catchError, first, map, retry, switchMap, tap } from 'rxjs/operators'
import { PlatformInfo, PlatformInfoService } from 'src/app/cdk/platform-info'
import { RequestInfoForm } from 'src/app/types'
import {
DuplicatedName,
RegisterConfirmResponse,
RegisterForm,
} from 'src/app/types/register.endpoint'
import { ERROR_REPORT } from 'src/app/errors'
import { objectToUrlParameters } from '../../constants'
import { ReactivationLocal } from '../../types/reactivation.local'
import { ErrorHandlerService } from '../error-handler/error-handler.service'
import { UserService } from '../user/user.service'
import { RegisterBackendValidatorMixin } from './register.backend-validators'
import { RegisterFormAdapterMixin } from './register.form-adapter'
import { EmailCategoryEndpoint } from 'src/app/types/register.email-category'
// Mixing boiler plate
class RegisterServiceBase {
constructor(
public _http: HttpClient,
public _errorHandler: ErrorHandlerService
) {}
}
const _RegisterServiceMixingBase = RegisterBackendValidatorMixin(
RegisterFormAdapterMixin(RegisterServiceBase)
)
@Injectable({
providedIn: 'root',
})
export class RegisterService extends _RegisterServiceMixingBase {
backendRegistrationForm: RegisterForm
constructor(
_http: HttpClient,
_errorHandler: ErrorHandlerService,
private _userService: UserService,
private _platform: PlatformInfoService
) {
super(_http, _errorHandler)
}
public checkDuplicatedResearcher(names: {
familyNames: string
givenNames: string
}) {
return this._http
.get<DuplicatedName[]>(
runtimeEnvironment.API_WEB + `dupicateResearcher.json`,
{
params: names,
withCredentials: true,
}
)
.pipe(
retry(3),
catchError((error) => this._errorHandler.handleError(error))
)
}
getRegisterForm(): Observable<RegisterForm> {
return this._http
.get<RegisterForm>(`${runtimeEnvironment.API_WEB}register.json`, {
withCredentials: true,
})
.pipe(
retry(3),
catchError((error) => this._errorHandler.handleError(error))
)
.pipe(map((form) => (this.backendRegistrationForm = form)))
}
getEmailCategory(email: string): Observable<EmailCategoryEndpoint> {
return this._http.get<any>(
`${runtimeEnvironment.API_WEB}email-domain/find-category?domain=${email}`
)
}
register(
StepA: UntypedFormGroup,
StepB: UntypedFormGroup,
StepC: UntypedFormGroup,
StepC2: UntypedFormGroup,
StepD: UntypedFormGroup,
reactivation: ReactivationLocal,
requestInfoForm?: RequestInfoForm,
updateUserService = true
): Observable<RegisterConfirmResponse> {
this.backendRegistrationForm.valNumClient =
this.backendRegistrationForm.valNumServer / 2
const registerForm = this.formGroupToFullRegistrationForm(
StepA,
StepB,
StepC,
StepC2,
StepD
)
this.addOauthContext(registerForm, requestInfoForm)
return this._platform.get().pipe(
first(),
switchMap((platform) => {
let url = `${runtimeEnvironment.API_WEB}`
if (
platform.institutional ||
platform.queryParameters.linkType === 'shibboleth'
) {
url += `shibboleth/`
}
if (reactivation.isReactivation) {
url += `reactivationConfirm.json?${objectToUrlParameters(
platform.queryParameters
)}`
registerForm.resetParams = reactivation.reactivationCode
} else {
url += `registerConfirm.json?${objectToUrlParameters(
platform.queryParameters
)}`
}
const registerFormWithTypeContext = this.addCreationTypeContext(
platform,
registerForm
)
return this._http
.post<RegisterConfirmResponse>(
url,
Object.assign(
this.backendRegistrationForm,
registerFormWithTypeContext
)
)
.pipe(
retry(3),
catchError((error) =>
this._errorHandler.handleError(error, ERROR_REPORT.REGISTER)
),
switchMap((value) => {
return this._userService.refreshUserSession(true, true).pipe(
first(),
map((userStatus) => {
if (!userStatus.loggedIn && !value.errors) {
// sanity check the user should be logged
// sanity check the user should be logged
this._errorHandler.handleError(
new Error('registerSanityIssue'),
ERROR_REPORT.REGISTER
)
}
return value
})
)
})
)
})
)
}
addOauthContext(
registerForm: RegisterForm,
requestInfoForm?: RequestInfoForm
): void {
if (requestInfoForm) {
registerForm.referredBy = { value: requestInfoForm.clientId }
}
}
addCreationTypeContext(
platform: PlatformInfo,
registerForm: RegisterForm
): RegisterForm {
/// TODO @leomendoza123 depend only on the user session thirty party login data
/// avoid taking data from the the parameters.
if (
platform.social ||
platform.queryParameters.providerId === 'facebook' ||
platform.queryParameters.providerId === 'google'
) {
registerForm.linkType = 'social'
return registerForm
} else if (platform.institutional || platform.queryParameters.providerId) {
registerForm.linkType = 'shibboleth'
return registerForm
} else {
return registerForm
}
}
}