// Copyright 2024 The Nomulus Authors. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. import { HttpClient, HttpErrorResponse } from '@angular/common/http'; import { Injectable } from '@angular/core'; import { Observable, catchError, of, throwError } from 'rxjs'; import { DomainListResult } from 'src/app/domains/domainList.service'; import { DomainLocksResult } from 'src/app/domains/registryLock.service'; import { RegistryLockVerificationResponse } from 'src/app/lock/registryLockVerify.service'; import { OteCreateResponse } from 'src/app/ote/newOte.component'; import { OteStatusResponse } from 'src/app/ote/oteStatus.component'; import { User } from 'src/app/users/users.service'; import { Registrar, SecuritySettingsBackendModel, RdapRegistrarFields, } from '../../registrar/registrar.service'; import { Contact } from '../../settings/contact/contact.service'; import { EppPasswordBackendModel } from '../../settings/security/security.service'; import { UserData } from './userData.service'; import { PasswordResetVerifyResponse } from '../components/passwordReset/passwordResetVerify.component'; import { HistoryRecord } from '../../history/history.service'; @Injectable() export class BackendService { constructor(private http: HttpClient) {} errorCatcher( error: HttpErrorResponse, mockData?: Type ): Observable { // This is a temporary redirect to the old console until the new console // is fully released and enabled if (error.url && new URL(error.url).pathname === '/registrar') { window.location.href = error.url; } if (error.error instanceof Error) { // A client-side or network error occurred. Handle it accordingly. console.error('An error occurred:', error.error.message); } else { // The backend returned an unsuccessful response code. // The response body may contain clues as to what went wrong, console.error( `Backend returned code ${error.status}, body was: ${error.error}` ); } if (mockData) { return of(mockData); } else { return throwError(() => error); } } getContacts(registrarId: string): Observable { return this.http .get( `/console-api/settings/contacts?registrarId=${registrarId}` ) .pipe(catchError((err) => this.errorCatcher(err))); } updateContact(registrarId: string, contact: Contact): Observable { return this.http.put( `/console-api/settings/contacts?registrarId=${registrarId}`, contact ); } createContact(registrarId: string, contact: Contact): Observable { return this.http.post( `/console-api/settings/contacts?registrarId=${registrarId}`, contact ); } deleteContact(registrarId: string, contact: Contact): Observable { return this.http.delete( `/console-api/settings/contacts?registrarId=${registrarId}`, { body: JSON.stringify(contact), } ); } getDomains( registrarId: string, checkpointTime?: string, pageNumber?: number, resultsPerPage?: number, totalResults?: number, searchTerm?: string ): Observable { var url = `/console-api/domain-list?registrarId=${registrarId}`; if (checkpointTime) { url += `&checkpointTime=${checkpointTime}`; } if (pageNumber) { url += `&pageNumber=${pageNumber}`; } if (resultsPerPage) { url += `&resultsPerPage=${resultsPerPage}`; } if (totalResults) { url += `&totalResults=${totalResults}`; } if (searchTerm) { url += `&searchTerm=${searchTerm}`; } return this.http .get(url) .pipe(catchError((err) => this.errorCatcher(err))); } getHistoryLog(registrarId: string, userEmail?: string) { return this.http .get( userEmail ? `/console-api/history?registrarId=${registrarId}&consoleUserEmail=${userEmail}` : `/console-api/history?registrarId=${registrarId}` ) .pipe(catchError((err) => this.errorCatcher(err))); } getRegistrars(): Observable { return this.http .get('/console-api/registrars') .pipe(catchError((err) => this.errorCatcher(err))); } createRegistrar(registrar: Registrar): Observable { return this.http .post('/console-api/registrars', registrar) .pipe(catchError((err) => this.errorCatcher(err))); } updateRegistrar(registrar: Registrar): Observable { return this.http .post('/console-api/registrar', registrar) .pipe(catchError((err) => this.errorCatcher(err))); } getSecuritySettings( registrarId: string ): Observable { return this.http .get( `/console-api/settings/security?registrarId=${registrarId}` ) .pipe( catchError((err) => this.errorCatcher(err) ) ); } postSecuritySettings( registrarId: string, securitySettings: SecuritySettingsBackendModel ): Observable { return this.http.post( `/console-api/settings/security?registrarId=${registrarId}`, securitySettings ); } postEppPasswordUpdate( data: EppPasswordBackendModel ): Observable { return this.http.post( `/console-api/eppPassword`, data ); } getUsers(registrarId: string): Observable { return this.http .get(`/console-api/users?registrarId=${registrarId}`) .pipe(catchError((err) => this.errorCatcher(err))); } createUser(registrarId: string, maybeUser: User | null): Observable { return this.http .post(`/console-api/users?registrarId=${registrarId}`, maybeUser) .pipe(catchError((err) => this.errorCatcher(err))); } deleteUser(registrarId: string, user: User): Observable { return this.http .delete(`/console-api/users?registrarId=${registrarId}`, { body: JSON.stringify(user), }) .pipe(catchError((err) => this.errorCatcher(err))); } bulkDomainAction( domainNames: string[], reason: string, bulkDomainAction: string, registrarId: string ) { return this.http .post( `/console-api/bulk-domain?registrarId=${registrarId}&bulkDomainAction=${bulkDomainAction}`, { domainList: domainNames, reason, } ) .pipe(catchError((err) => this.errorCatcher(err))); } updateUser(registrarId: string, updatedUser: User): Observable { return this.http .put(`/console-api/users?registrarId=${registrarId}`, updatedUser) .pipe(catchError((err) => this.errorCatcher(err))); } getUserData(): Observable { return this.http .get('/console-api/userdata') .pipe(catchError((err) => this.errorCatcher(err))); } postRdapRegistrarFields( rdapRegistrarFields: RdapRegistrarFields ): Observable { return this.http.post( '/console-api/settings/rdap-fields', rdapRegistrarFields ); } registryLockDomain( domainName: string, password: string | undefined, relockDurationMillis: number | undefined, registrarId: string, isLock: boolean ) { return this.http.post( `/console-api/registry-lock?registrarId=${registrarId}`, { domainName, password, isLock, relockDurationMillis, } ); } getLocks(registrarId: string): Observable { return this.http .get( `/console-api/registry-lock?registrarId=${registrarId}` ) .pipe(catchError((err) => this.errorCatcher(err))); } generateOte( oteForm: Object, registrarId: string ): Observable { return this.http.post( `/console-api/ote?registrarId=${registrarId}`, oteForm ); } getOteStatus(registrarId: string) { return this.http .get(`/console-api/ote?registrarId=${registrarId}`) .pipe(catchError((err) => this.errorCatcher(err))); } verifyRegistryLockRequest( lockVerificationCode: string ): Observable { return this.http.get( `/console-api/registry-lock-verify?lockVerificationCode=${lockVerificationCode}` ); } requestRegistryLockPasswordReset( registrarId: string, registryLockEmail: string ) { return this.http.post('/console-api/password-reset-request', { type: 'REGISTRY_LOCK', registrarId, registryLockEmail, }); } requestEppPasswordReset(registrarId: string) { return this.http.post('/console-api/password-reset-request', { type: 'EPP', registrarId, }); } getPasswordResetInformation( verificationCode: string ): Observable { return this.http.get( `/console-api/password-reset-verify?resetRequestVerificationCode=${verificationCode}` ); } finalizePasswordReset(verificationCode: string, newPassword: string) { return this.http.post( `/console-api/password-reset-verify?resetRequestVerificationCode=${verificationCode}`, newPassword ); } }