refactor: remove cycling dependency between notes and revisions

Signed-off-by: Tilman Vatteroth <git@tilmanvatteroth.de>
This commit is contained in:
Tilman Vatteroth 2022-06-21 16:05:21 +02:00 committed by David Mehren
parent 8cdc90bb8e
commit 8596bed729
7 changed files with 10 additions and 66 deletions

View file

@ -3,7 +3,7 @@
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { forwardRef, Module } from '@nestjs/common';
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { TypeOrmModule } from '@nestjs/typeorm';
@ -30,7 +30,7 @@ import { Tag } from './tag.entity';
User,
Alias,
]),
forwardRef(() => RevisionsModule),
RevisionsModule,
UsersModule,
GroupsModule,
LoggerModule,

View file

@ -359,38 +359,6 @@ describe('NotesService', () => {
});
});
describe('getLatestRevision', () => {
it('works', async () => {
const content = 'testContent';
jest
.spyOn(noteRepo, 'save')
.mockImplementation(async (note: Note): Promise<Note> => note);
const newNote = await service.createNote(content, null);
const revisions = await newNote.revisions;
jest.spyOn(revisionRepo, 'findOne').mockResolvedValueOnce(revisions[0]);
await service.getLatestRevision(newNote).then((result) => {
expect(result).toEqual(revisions[0]);
});
});
});
describe('getFirstRevision', () => {
it('works', async () => {
const user = {} as User;
user.username = 'hardcoded';
const content = 'testContent';
jest
.spyOn(noteRepo, 'save')
.mockImplementation(async (note: Note): Promise<Note> => note);
const newNote = await service.createNote(content, null);
const revisions = await newNote.revisions;
jest.spyOn(revisionRepo, 'findOne').mockResolvedValueOnce(revisions[0]);
await service.getLatestRevision(newNote).then((result) => {
expect(result).toEqual(revisions[0]);
});
});
});
describe('getNoteByIdOrAlias', () => {
it('works', async () => {
const user = User.create('hardcoded', 'Testy') as User;

View file

@ -39,7 +39,6 @@ export class NotesService {
@InjectRepository(User) private userRepository: Repository<User>,
@Inject(UsersService) private usersService: UsersService,
@Inject(GroupsService) private groupsService: GroupsService,
@Inject(forwardRef(() => RevisionsService))
private revisionsService: RevisionsService,
@Inject(noteConfiguration.KEY)
private noteConfig: NoteConfig,
@ -117,27 +116,7 @@ export class NotesService {
* @return {string} the content of the note
*/
async getNoteContent(note: Note): Promise<string> {
return (await this.getLatestRevision(note)).content;
}
/**
* @async
* Get the first revision of the note.
* @param {Note} note - the note to use
* @return {Revision} the first revision of the note
*/
async getLatestRevision(note: Note): Promise<Revision> {
return await this.revisionsService.getLatestRevision(note);
}
/**
* @async
* Get the last revision of the note.
* @param {Note} note - the note to use
* @return {Revision} the last revision of the note
*/
async getFirstRevision(note: Note): Promise<Revision> {
return await this.revisionsService.getFirstRevision(note);
return (await this.revisionsService.getLatestRevision(note)).content;
}
/**
@ -264,7 +243,7 @@ export class NotesService {
* @return {User} user to be used as updateUser in the NoteDto
*/
async calculateUpdateUser(note: Note): Promise<User | null> {
const lastRevision = await this.getLatestRevision(note);
const lastRevision = await this.revisionsService.getLatestRevision(note);
const edits = await lastRevision.edits;
if (edits.length > 0) {
// Sort the last Revisions Edits by their updatedAt Date to get the latest one
@ -333,7 +312,8 @@ export class NotesService {
permissions: await this.toNotePermissionsDto(note),
tags: await this.toTagList(note),
version: note.version,
updatedAt: (await this.getLatestRevision(note)).createdAt,
updatedAt: (await this.revisionsService.getLatestRevision(note))
.createdAt,
updateUsername: updateUser ? updateUser.username : null,
viewCount: note.viewCount,
};

View file

@ -3,13 +3,12 @@
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { forwardRef, Module } from '@nestjs/common';
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { TypeOrmModule } from '@nestjs/typeorm';
import { AuthorsModule } from '../authors/authors.module';
import { LoggerModule } from '../logger/logger.module';
import { NotesModule } from '../notes/notes.module';
import { Edit } from './edit.entity';
import { EditService } from './edit.service';
import { Revision } from './revision.entity';
@ -18,7 +17,6 @@ import { RevisionsService } from './revisions.service';
@Module({
imports: [
TypeOrmModule.forFeature([Revision, Edit]),
forwardRef(() => NotesModule),
LoggerModule,
ConfigModule,
AuthorsModule,

View file

@ -3,14 +3,13 @@
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { forwardRef, Inject, Injectable } from '@nestjs/common';
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Equal, Repository } from 'typeorm';
import { NotInDBError } from '../errors/errors';
import { ConsoleLoggerService } from '../logger/console-logger.service';
import { Note } from '../notes/note.entity';
import { NotesService } from '../notes/notes.service';
import { EditService } from './edit.service';
import { RevisionMetadataDto } from './revision-metadata.dto';
import { RevisionDto } from './revision.dto';
@ -27,7 +26,6 @@ export class RevisionsService {
private readonly logger: ConsoleLoggerService,
@InjectRepository(Revision)
private revisionRepository: Repository<Revision>,
@Inject(forwardRef(() => NotesService)) private notesService: NotesService,
private editService: EditService,
) {
this.logger.setContext(RevisionsService.name);

View file

@ -269,7 +269,7 @@ describe('Notes', () => {
user,
'test5',
);
const revision = await testSetup.notesService.getLatestRevision(note);
const revision = await testSetup.revisionsService.getLatestRevision(note);
const response = await agent
.get(`/api/private/notes/test5/revisions/${revision.id}`)
.expect('Content-Type', /json/)

View file

@ -361,7 +361,7 @@ describe('Notes', () => {
'test7',
);
const revision = await testSetup.notesService.getLatestRevision(note);
const revision = await testSetup.revisionsService.getLatestRevision(note);
const response = await request(testSetup.app.getHttpServer())
.get(`/api/v2/notes/test7/revisions/${revision.id}`)
.expect('Content-Type', /json/)