All files / src/operation fetch-account.ts

16.67% Statements 4/24
0% Branches 0/4
0% Functions 0/6
16.67% Lines 4/24

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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  18x 18x   18x                                                                                                                       18x  
import Node from 'type/node';
import SystemId from 'system/enum/id';
import UnauthorizedError from 'http/error/unauthorized';
import BasicAuthCredentials from 'http/type/basic-auth-credentials';
import Operation, { OperationInput } from 'operation';
 
interface Input extends OperationInput {
	readonly credentials: BasicAuthCredentials;
}
 
class FetchAccountOperation extends Operation<Input, Node> {
	protected async performInternal(): Promise<Node> {
		const account_key = await this.fetchAccountId();
 
		return this.fetchAccount(account_key);
	}
 
	private async fetchAccount(account_id: string): Promise<Node> {
		const repository = this.getRepository();
 
		const account = await repository.fetchNode(
			SystemId.ACCOUNT_TYPE,
			account_id
		);
 
		if (account === undefined) {
			throw new UnauthorizedError();
		}
 
		return account;
	}
 
	private async fetchAccountId(): Promise<string> {
		const repository = this.getRepository();
		const username = this.getUsername();
		const password = this.getPassword();
		const account_key = await repository.fetchAccountId(username, password);
 
		if (account_key === undefined) {
			throw new UnauthorizedError();
		}
 
		return account_key;
	}
 
	private getUsername(): string {
		const credentials = this.getCredentials();
 
		return credentials.username;
	}
 
	private getPassword(): string {
		const credentials = this.getCredentials();
 
		return credentials.password;
	}
 
	private getCredentials(): BasicAuthCredentials {
		const input = this.getInput();
 
		return input.credentials;
	}
}
 
export default FetchAccountOperation;