Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/register2 #2094

Merged
merged 23 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
register2service
  • Loading branch information
leomendoza123 committed Nov 14, 2023
commit 337b4c579e7061f79eb547875ee2a74f0c74ed6b
254 changes: 254 additions & 0 deletions src/app/core/register2/register2.backend-validators.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,254 @@
import { HttpClient } from '@angular/common/http'
import {
AbstractControl,
AsyncValidatorFn,
UntypedFormGroup,
ValidationErrors,
} from '@angular/forms'
import { Observable, of } from 'rxjs'
import { catchError, map, retry } from 'rxjs/operators'
import { Constructor } from 'src/app/types'
import { RegisterForm } from 'src/app/types/register.endpoint'
import { environment } from 'src/environments/environment'
import { ErrorHandlerService } from '../error-handler/error-handler.service'

interface HasHttpClientAndErrorHandler {
_http: HttpClient
_errorHandler: ErrorHandlerService
}

interface HasFormAdapters {
formGroupToEmailRegisterForm(formGroup: UntypedFormGroup): RegisterForm
formGroupToPasswordRegisterForm(formGroup: UntypedFormGroup): RegisterForm
formGroupToFullRegistrationForm(
StepA: UntypedFormGroup,
StepB: UntypedFormGroup,
StepC: UntypedFormGroup,
StepD: UntypedFormGroup,

): RegisterForm
}

export function Register2BackendValidatorMixin<
T extends Constructor<HasHttpClientAndErrorHandler & HasFormAdapters>
>(base: T) {
return class RegisterBackendValidator extends base {
constructor(...args: any[]) {
super(...args)
}
formInputs = {
givenNames: {
validationEndpoint: 'validateGivenNames',
},
familyNames: {
validationEndpoint: 'validateFamilyNames',
},
email: {
validationEndpoint: 'validateEmail',
},
emailsAdditional: {
validationEndpoint: 'validateEmailsAdditional',
},
passwordConfirm: {
validationEndpoint: 'validatePasswordConfirm',
},
password: {
validationEndpoint: 'validatePassword',
},
}

validateRegisterValue(
controlName: string,
value: RegisterForm
): Observable<RegisterForm> {
return this._http
.post<RegisterForm>(
environment.API_WEB +
`oauth/custom/register/${this.formInputs[controlName].validationEndpoint}.json`,
value
)
.pipe(
retry(3),
catchError((error) => this._errorHandler.handleError(error))
)
}

validateAdditionalEmailsReactivation(
value: RegisterForm
): Observable<RegisterForm> {
return this._http
.post(
`${environment.API_WEB}reactivateAdditionalEmailsValidate.json`,
value
)
.pipe(
retry(3),
catchError((error) => this._errorHandler.handleError(error))
)
}

backendValueValidate(
controlName: 'givenNames' | 'familyNames' | 'email' | 'password'
): AsyncValidatorFn {
return (
control: AbstractControl
): Observable<ValidationErrors | null> => {
if (control.value === '') {
return of(null)
}
const value = {}
value[controlName] = { value: control.value }

return this.validateRegisterValue(controlName, value).pipe(
map((res) => {
if (res[controlName].errors && res[controlName].errors.length > 0) {
const error = {
backendError: res[controlName].errors,
}
return error
}
return null
})
)
}
}

backendAdditionalEmailsValidate(reactivate: boolean): AsyncValidatorFn {
return (
formGroup: UntypedFormGroup
): Observable<ValidationErrors | null> => {
const value: RegisterForm = this.formGroupToEmailRegisterForm(formGroup)
if (!value.emailsAdditional || value.emailsAdditional.length === 0) {
return of(null)
}

if (reactivate) {
return this.validateAdditionalEmailsReactivation(value).pipe(
map((response) => {
// Add errors to additional emails controls
return this.setFormGroupEmailErrors(response, 'backendErrors')
})
)
}

return this.validateRegisterValue('emailsAdditional', value).pipe(
map((response) => {
// Add errors to additional emails controls
return this.setFormGroupEmailErrors(response, 'backendErrors')
})
)
}
}

backendPasswordValidate(): AsyncValidatorFn {
return (
formGroup: UntypedFormGroup
): Observable<ValidationErrors | null> => {
const value: RegisterForm =
this.formGroupToPasswordRegisterForm(formGroup)
if (value.password.value === '' || value.passwordConfirm.value === '') {
return of(null)
}
return this.validateRegisterValue('password', value).pipe(
map((response) => {
// Add errors to additional emails controls
return this.setFormGroupPasswordErrors(response, 'backendErrors')
})
)
}
}

backendRegisterFormValidate(
StepA: UntypedFormGroup,
StepB: UntypedFormGroup,
StepC: UntypedFormGroup,
StepD: UntypedFormGroup,

type?: 'shibboleth'
): Observable<RegisterForm> {
const registerForm = this.formGroupToFullRegistrationForm(
StepA,
StepB,
StepC,
StepD
)
return this._http
.post<RegisterForm>(`${environment.API_WEB}register.json`, registerForm)
.pipe(
retry(3),
catchError((error) => this._errorHandler.handleError(error))
)
}

public setFormGroupEmailErrors(
registerForm: RegisterForm,
errorGroup: string
) {
let hasErrors = false
const error = {}
error[errorGroup] = {
additionalEmails: {},
email: [],
}

registerForm.emailsAdditional.forEach((responseControl) => {
if (responseControl.errors && responseControl.errors.length > 0) {
hasErrors = true
error[errorGroup]['additionalEmails'][responseControl.value] =
responseControl.errors
}
})

if (
registerForm.email &&
registerForm.email.errors &&
registerForm.email.errors.length > 0
) {
hasErrors = true
error[errorGroup]['email'].push({
value: registerForm.email.value,
errors: registerForm.email.errors,
})
}

return hasErrors ? error : null
}

public setFormGroupPasswordErrors(
registerForm: RegisterForm,
errorGroup: string
) {
let hasErrors = false
const error = {}
error[errorGroup] = {
password: [],
passwordConfirm: [],
}

if (
registerForm.password &&
registerForm.password.errors &&
registerForm.password.errors.length > 0
) {
hasErrors = true
error[errorGroup]['password'].push({
value: registerForm.email.value,
errors: registerForm.email.errors,
})
}
if (
registerForm.passwordConfirm &&
registerForm.passwordConfirm.errors &&
registerForm.passwordConfirm.errors.length > 0
) {
hasErrors = true
error[errorGroup]['passwordConfirm'].push({
value: registerForm.passwordConfirm.value,
errors: registerForm.passwordConfirm.errors,
})
}

return hasErrors ? error : null
}
}
}
136 changes: 136 additions & 0 deletions src/app/core/register2/register2.form-adapter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
import { UntypedFormGroup } from '@angular/forms'
import { Constructor } from 'src/app/types'
import { Value, Visibility } from 'src/app/types/common.endpoint'
import { RegisterForm } from 'src/app/types/register.endpoint'

export function Register2FormAdapterMixin<T extends Constructor<any>>(base: T) {
return class RegisterFormAdapter extends base {
formGroupToEmailRegisterForm(formGroup: UntypedFormGroup): RegisterForm {
let additionalEmailsValue: Value[]
if (formGroup.controls['additionalEmails']) {
const additionalEmailsControls = (
formGroup.controls['additionalEmails'] as UntypedFormGroup
).controls
additionalEmailsValue = Object.keys(additionalEmailsControls)
.filter((name) => additionalEmailsControls[name].value !== '')
.map((name) => {
if (additionalEmailsControls[name].value) {
return { value: additionalEmailsControls[name].value }
}
})
}
let emailValue
if (formGroup.controls['email']) {
emailValue = formGroup.controls['email'].value
}

const value: RegisterForm = {}

if (emailValue) {
value['email'] = { value: emailValue }
}
if (additionalEmailsValue) {
value['emailsAdditional'] = additionalEmailsValue
}
return value
}

formGroupToNamesRegisterForm(formGroup: UntypedFormGroup): RegisterForm {
return {
givenNames: { value: formGroup.controls['givenNames'].value },
familyNames: { value: formGroup.controls['familyNames'].value },
}
}

formGroupToActivitiesVisibilityForm(
formGroup: UntypedFormGroup
): RegisterForm {
let activitiesVisibilityDefault: Visibility
if (
formGroup &&
formGroup.controls &&
formGroup.controls['activitiesVisibilityDefault']
) {
activitiesVisibilityDefault = {
visibility: formGroup.controls['activitiesVisibilityDefault'].value,
}
}
return { activitiesVisibilityDefault }
}

formGroupToPasswordRegisterForm(formGroup: UntypedFormGroup): RegisterForm {
let password: Value
if (formGroup && formGroup.controls && formGroup.controls['password']) {
password = { value: formGroup.controls['password'].value }
}
let passwordConfirm: Value
if (
formGroup &&
formGroup.controls &&
formGroup.controls['passwordConfirm']
) {
passwordConfirm = { value: formGroup.controls['passwordConfirm'].value }
}
return { password, passwordConfirm }
}

formGroupTermsOfUseAndDataProcessedRegisterForm(
formGroup: UntypedFormGroup
): RegisterForm {
let termsOfUse: Value
let dataProcessed: Value
if (formGroup && formGroup.controls) {
if (formGroup.controls['termsOfUse']) {
termsOfUse = { value: formGroup.controls['termsOfUse'].value }
}
if (formGroup.controls['dataProcessed']) {
dataProcessed = { value: formGroup.controls['dataProcessed'].value }
}
}
return { termsOfUse, dataProcessed }
}

formGroupToSendOrcidNewsForm(formGroup: UntypedFormGroup) {
let sendOrcidNews: Value
if (
formGroup &&
formGroup.controls &&
formGroup.controls['sendOrcidNews']
) {
sendOrcidNews = { value: formGroup.controls['sendOrcidNews'].value }
}
return { sendOrcidNews }
}

formGroupToRecaptchaForm(
formGroup: UntypedFormGroup,
widgetId: number
): RegisterForm {
const value: RegisterForm = {}
value.grecaptchaWidgetId = {
value: widgetId != null ? widgetId.toString() : null,
}
if (formGroup && formGroup.controls && formGroup.controls['captcha']) {
value.grecaptcha = { value: formGroup.controls['captcha'].value }
}
return value
}

formGroupToFullRegistrationForm(
StepA: UntypedFormGroup,
StepB: UntypedFormGroup,
StepC: UntypedFormGroup,
StepD: UntypedFormGroup

): RegisterForm {
return {
...StepA.value.personal,
...StepB.value.password,
...StepB.value.sendOrcidNews,
...StepD.value.activitiesVisibilityDefault,
...StepD.value.termsOfUse,
...StepD.value.captcha,
}
}
}
}
Loading