import Node from 'type/node';
import ActionType from 'enum/action-type';
import NotFoundError from 'http/error/not-found';
import CheckNodePermissionOperation from 'operation/check-node-permission';
import Operation, { OperationInput } from 'operation';
interface Input extends OperationInput {
readonly id: string;
readonly type_id: string;
readonly version?: number;
}
class FetchNodeOperation extends Operation {
protected async performInternal(): Promise {
const node = await this.fetchNode();
await this.checkNodePermission(node);
return node;
}
private async fetchNode(): Promise {
const { type_id, id } = this.getInput();
const repository = this.getRepository();
const node = await repository.fetchNode({
type_id,
id
});
if (node === undefined) {
throw new NotFoundError(`Unable to find node: ${type_id}/${id}`);
}
return node;
}
private checkNodePermission(node: Node): Promise {
const action_type = ActionType.READ;
const repository = this.getRepository();
const account = this.getAccount();
const input = {
node,
action_type,
repository,
account
};
const operation = new CheckNodePermissionOperation(input);
return operation.perform();
}
}
export default FetchNodeOperation;