feat: add sending user requests to process (#1908)

* feat: add sending user requests to process

* fix(snjs): yarn lock

* fix(snjs): imports

* fix: specs
This commit is contained in:
Karol Sójko
2022-11-02 11:33:02 +01:00
committed by GitHub
parent f687334d7d
commit b2faa815e9
81 changed files with 766 additions and 325 deletions

View File

@@ -1,17 +1,57 @@
import { RootKeyParamsInterface } from '@standardnotes/models'
import { UserRequestType } from '@standardnotes/common'
import { ErrorMessage } from '../../Error/ErrorMessage'
import { ApiCallError } from '../../Error/ApiCallError'
import { UserRegistrationResponse } from '../../Response/User/UserRegistrationResponse'
import { UserServerInterface } from '../../Server/User/UserServerInterface'
import { ApiVersion } from '../../Api/ApiVersion'
import { ApiEndpointParam } from '../../Request/ApiEndpointParam'
import { UserRequestResponse } from '../../Response/UserRequest/UserRequestResponse'
import { UserDeletionResponse } from '../../Response/User/UserDeletionResponse'
import { UserRequestServerInterface } from '../../Server/UserRequest/UserRequestServerInterface'
import { UserApiOperations } from './UserApiOperations'
import { UserApiServiceInterface } from './UserApiServiceInterface'
export class UserApiService implements UserApiServiceInterface {
private registering: boolean
private operationsInProgress: Map<UserApiOperations, boolean>
constructor(private userServer: UserServerInterface) {
this.registering = false
constructor(private userServer: UserServerInterface, private userRequestServer: UserRequestServerInterface) {
this.operationsInProgress = new Map()
}
async deleteAccount(userUuid: string): Promise<UserDeletionResponse> {
this.lockOperation(UserApiOperations.DeletingAccount)
try {
const response = await this.userServer.deleteAccount({
userUuid: userUuid,
})
this.unlockOperation(UserApiOperations.DeletingAccount)
return response
} catch (error) {
throw new ApiCallError(ErrorMessage.GenericRegistrationFail)
}
}
async submitUserRequest(dto: { userUuid: string; requestType: UserRequestType }): Promise<UserRequestResponse> {
this.lockOperation(UserApiOperations.SubmittingRequest)
try {
const response = await this.userRequestServer.submitUserRequest({
userUuid: dto.userUuid,
requestType: dto.requestType,
})
this.unlockOperation(UserApiOperations.SubmittingRequest)
return response
} catch (error) {
throw new ApiCallError(ErrorMessage.GenericRegistrationFail)
}
}
async register(registerDTO: {
@@ -20,10 +60,7 @@ export class UserApiService implements UserApiServiceInterface {
keyParams: RootKeyParamsInterface
ephemeral: boolean
}): Promise<UserRegistrationResponse> {
if (this.registering) {
throw new ApiCallError(ErrorMessage.RegistrationInProgress)
}
this.registering = true
this.lockOperation(UserApiOperations.Registering)
try {
const response = await this.userServer.register({
@@ -34,11 +71,23 @@ export class UserApiService implements UserApiServiceInterface {
...registerDTO.keyParams.getPortableValue(),
})
this.registering = false
this.unlockOperation(UserApiOperations.Registering)
return response
} catch (error) {
throw new ApiCallError(ErrorMessage.GenericRegistrationFail)
}
}
private lockOperation(operation: UserApiOperations): void {
if (this.operationsInProgress.get(operation)) {
throw new ApiCallError(ErrorMessage.GenericInProgress)
}
this.operationsInProgress.set(operation, true)
}
private unlockOperation(operation: UserApiOperations): void {
this.operationsInProgress.set(operation, false)
}
}