/*
 * SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
 *
 * SPDX-License-Identifier: AGPL-3.0-only
 */
import {
  CallHandler,
  ExecutionContext,
  Injectable,
  NestInterceptor,
} from '@nestjs/common';
import { Observable } from 'rxjs';

import { NotesService } from '../../notes/notes.service';
import { extractNoteFromRequest } from './extract-note-from-request';
import { CompleteRequest } from './request.type';

/**
 * Saves the note identified by the `noteIdOrAlias` URL parameter
 * under the `note` property of the request object.
 */
@Injectable()
export class GetNoteInterceptor implements NestInterceptor {
  constructor(private noteService: NotesService) {}

  async intercept<T>(
    context: ExecutionContext,
    next: CallHandler,
  ): Promise<Observable<T>> {
    const request: CompleteRequest = context.switchToHttp().getRequest();
    const note = await extractNoteFromRequest(request, this.noteService);
    if (note !== undefined) {
      request.note = note;
    }
    return next.handle();
  }
}
