HistoryService: Refactor updateHistoryEntry

The function now expects a `Note` object instead of a noteId to
make it more consistent with `updateHistoryEntryTimestamp`.

Signed-off-by: David Mehren <git@herrmehren.de>
This commit is contained in:
David Mehren 2021-08-29 21:38:10 +02:00
parent 99db4bc336
commit c515569299
No known key found for this signature in database
GPG key ID: 185982BA4C42B7C3
4 changed files with 12 additions and 8 deletions

View file

@ -22,6 +22,8 @@ import { HistoryEntryUpdateDto } from '../../../../history/history-entry-update.
import { HistoryEntryDto } from '../../../../history/history-entry.dto'; import { HistoryEntryDto } from '../../../../history/history-entry.dto';
import { HistoryService } from '../../../../history/history.service'; import { HistoryService } from '../../../../history/history.service';
import { ConsoleLoggerService } from '../../../../logger/console-logger.service'; import { ConsoleLoggerService } from '../../../../logger/console-logger.service';
import { GetNotePipe } from '../../../../notes/get-note.pipe';
import { Note } from '../../../../notes/note.entity';
import { UsersService } from '../../../../users/users.service'; import { UsersService } from '../../../../users/users.service';
@ApiTags('history') @ApiTags('history')
@ -84,14 +86,14 @@ export class HistoryController {
@Put(':note') @Put(':note')
async updateHistoryEntry( async updateHistoryEntry(
@Param('note') noteId: string, @Param('note', GetNotePipe) note: Note,
@Body() entryUpdateDto: HistoryEntryUpdateDto, @Body() entryUpdateDto: HistoryEntryUpdateDto,
): Promise<HistoryEntryDto> { ): Promise<HistoryEntryDto> {
try { try {
// ToDo: use actual user here // ToDo: use actual user here
const user = await this.userService.getUserByUsername('hardcoded'); const user = await this.userService.getUserByUsername('hardcoded');
const newEntry = await this.historyService.updateHistoryEntry( const newEntry = await this.historyService.updateHistoryEntry(
noteId, note,
user, user,
entryUpdateDto, entryUpdateDto,
); );

View file

@ -31,7 +31,9 @@ import { HistoryService } from '../../../history/history.service';
import { ConsoleLoggerService } from '../../../logger/console-logger.service'; import { ConsoleLoggerService } from '../../../logger/console-logger.service';
import { MediaUploadDto } from '../../../media/media-upload.dto'; import { MediaUploadDto } from '../../../media/media-upload.dto';
import { MediaService } from '../../../media/media.service'; import { MediaService } from '../../../media/media.service';
import { GetNotePipe } from '../../../notes/get-note.pipe';
import { NoteMetadataDto } from '../../../notes/note-metadata.dto'; import { NoteMetadataDto } from '../../../notes/note-metadata.dto';
import { Note } from '../../../notes/note.entity';
import { NotesService } from '../../../notes/notes.service'; import { NotesService } from '../../../notes/notes.service';
import { UserInfoDto } from '../../../users/user-info.dto'; import { UserInfoDto } from '../../../users/user-info.dto';
import { User } from '../../../users/user.entity'; import { User } from '../../../users/user.entity';
@ -119,7 +121,7 @@ export class MeController {
@ApiNotFoundResponse({ description: notFoundDescription }) @ApiNotFoundResponse({ description: notFoundDescription })
async updateHistoryEntry( async updateHistoryEntry(
@RequestUser() user: User, @RequestUser() user: User,
@Param('note') note: string, @Param('note', GetNotePipe) note: Note,
@Body() entryUpdateDto: HistoryEntryUpdateDto, @Body() entryUpdateDto: HistoryEntryUpdateDto,
): Promise<HistoryEntryDto> { ): Promise<HistoryEntryDto> {
// ToDo: Check if user is allowed to pin this history entry // ToDo: Check if user is allowed to pin this history entry

View file

@ -218,7 +218,7 @@ describe('HistoryService', () => {
async (entry: HistoryEntry): Promise<HistoryEntry> => entry, async (entry: HistoryEntry): Promise<HistoryEntry> => entry,
); );
const updatedHistoryEntry = await service.updateHistoryEntry( const updatedHistoryEntry = await service.updateHistoryEntry(
alias, note,
user, user,
{ {
pinStatus: true, pinStatus: true,
@ -237,7 +237,7 @@ describe('HistoryService', () => {
jest.spyOn(historyRepo, 'findOne').mockResolvedValueOnce(undefined); jest.spyOn(historyRepo, 'findOne').mockResolvedValueOnce(undefined);
jest.spyOn(noteRepo, 'findOne').mockResolvedValueOnce(note); jest.spyOn(noteRepo, 'findOne').mockResolvedValueOnce(note);
await expect( await expect(
service.updateHistoryEntry(alias, user, { service.updateHistoryEntry(note, user, {
pinStatus: true, pinStatus: true,
}), }),
).rejects.toThrow(NotInDBError); ).rejects.toThrow(NotInDBError);

View file

@ -112,17 +112,17 @@ export class HistoryService {
/** /**
* @async * @async
* Update a history entry identified by the user and a note id or alias * Update a history entry identified by the user and a note id or alias
* @param {string} noteIdOrAlias - the note that the history entry belongs to * @param {Note} note - the note that the history entry belongs to
* @param {User} user - the user that the history entry belongs to * @param {User} user - the user that the history entry belongs to
* @param {HistoryEntryUpdateDto} updateDto - the change that should be applied to the history entry * @param {HistoryEntryUpdateDto} updateDto - the change that should be applied to the history entry
* @return {HistoryEntry} the requested history entry * @return {HistoryEntry} the requested history entry
*/ */
async updateHistoryEntry( async updateHistoryEntry(
noteIdOrAlias: string, note: Note,
user: User, user: User,
updateDto: HistoryEntryUpdateDto, updateDto: HistoryEntryUpdateDto,
): Promise<HistoryEntry> { ): Promise<HistoryEntry> {
const entry = await this.getEntryByNoteIdOrAlias(noteIdOrAlias, user); const entry = await this.getEntryByNote(note, user);
entry.pinStatus = updateDto.pinStatus; entry.pinStatus = updateDto.pinStatus;
return await this.historyEntryRepository.save(entry); return await this.historyEntryRepository.save(entry);
} }