PrivateAPI: Add pinStatus to HistoryEntryImportDto

As the DTO is only for importing an existing history the pinStatus of those entries should also be posted.

Signed-off-by: Philip Molares <philip.molares@udo.edu>
This commit is contained in:
Philip Molares 2021-04-10 23:31:18 +02:00 committed by David Mehren
parent 1154894876
commit eeaa74b385
No known key found for this signature in database
GPG key ID: 185982BA4C42B7C3
5 changed files with 70 additions and 8 deletions

View file

@ -65,7 +65,11 @@ export class HistoryController {
const note = await this.noteService.getNoteByIdOrAlias( const note = await this.noteService.getNoteByIdOrAlias(
historyEntry.note, historyEntry.note,
); );
await this.historyService.createOrUpdateHistoryEntry(note, user); await this.historyService.createOrUpdateHistoryEntry(
note,
user,
historyEntry.pinStatus,
);
} }
} catch (e) { } catch (e) {
if (e instanceof NotInDBError) { if (e instanceof NotInDBError) {

View file

@ -4,7 +4,7 @@
* SPDX-License-Identifier: AGPL-3.0-only * SPDX-License-Identifier: AGPL-3.0-only
*/ */
import { IsString } from 'class-validator'; import { IsBoolean, IsString } from 'class-validator';
export class HistoryEntryImportDto { export class HistoryEntryImportDto {
/** /**
@ -12,4 +12,10 @@ export class HistoryEntryImportDto {
*/ */
@IsString() @IsString()
note: string; note: string;
/**
* True if the note should be pinned
* @example true
*/
@IsBoolean()
pinStatus: boolean;
} }

View file

@ -141,9 +141,10 @@ describe('HistoryService', () => {
describe('createOrUpdateHistoryEntry', () => { describe('createOrUpdateHistoryEntry', () => {
describe('works', () => { describe('works', () => {
it('without an preexisting entry', async () => {
const user = {} as User; const user = {} as User;
const alias = 'alias'; const alias = 'alias';
const pinStatus = true;
it('without an preexisting entry and without pinStatus', async () => {
jest.spyOn(historyRepo, 'findOne').mockResolvedValueOnce(undefined); jest.spyOn(historyRepo, 'findOne').mockResolvedValueOnce(undefined);
jest jest
.spyOn(historyRepo, 'save') .spyOn(historyRepo, 'save')
@ -160,9 +161,25 @@ describe('HistoryService', () => {
expect(createHistoryEntry.pinStatus).toEqual(false); expect(createHistoryEntry.pinStatus).toEqual(false);
}); });
it('with an preexisting entry', async () => { it('without an preexisting entry and with pinStatus', async () => {
const user = {} as User; jest.spyOn(historyRepo, 'findOne').mockResolvedValueOnce(undefined);
const alias = 'alias'; jest
.spyOn(historyRepo, 'save')
.mockImplementation(
async (entry: HistoryEntry): Promise<HistoryEntry> => entry,
);
const createHistoryEntry = await service.createOrUpdateHistoryEntry(
Note.create(user, alias),
user,
pinStatus,
);
expect(createHistoryEntry.note.alias).toEqual(alias);
expect(createHistoryEntry.note.owner).toEqual(user);
expect(createHistoryEntry.user).toEqual(user);
expect(createHistoryEntry.pinStatus).toEqual(pinStatus);
});
it('with an preexisting entry and without pinStatus', async () => {
const historyEntry = HistoryEntry.create( const historyEntry = HistoryEntry.create(
user, user,
Note.create(user, alias), Note.create(user, alias),
@ -185,6 +202,32 @@ describe('HistoryService', () => {
historyEntry.updatedAt.getTime(), historyEntry.updatedAt.getTime(),
); );
}); });
it('with an preexisting entry and with pinStatus', async () => {
const historyEntry = HistoryEntry.create(
user,
Note.create(user, alias),
pinStatus,
);
jest.spyOn(historyRepo, 'findOne').mockResolvedValueOnce(historyEntry);
jest
.spyOn(historyRepo, 'save')
.mockImplementation(
async (entry: HistoryEntry): Promise<HistoryEntry> => entry,
);
const createHistoryEntry = await service.createOrUpdateHistoryEntry(
Note.create(user, alias),
user,
pinStatus,
);
expect(createHistoryEntry.note.alias).toEqual(alias);
expect(createHistoryEntry.note.owner).toEqual(user);
expect(createHistoryEntry.user).toEqual(user);
expect(createHistoryEntry.pinStatus).toEqual(pinStatus);
expect(createHistoryEntry.updatedAt.getTime()).toBeGreaterThanOrEqual(
historyEntry.updatedAt.getTime(),
);
});
}); });
}); });

View file

@ -80,15 +80,20 @@ export class HistoryService {
* Create or update a history entry by the user and note. If the entry is merely updated the updatedAt date is set to the current date. * Create or update a history entry by the user and note. If the entry is merely updated the updatedAt date is set to the current date.
* @param {Note} note - 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 {boolean} pinStatus - if the pinStatus of the history entry should be set
* @return {HistoryEntry} the requested history entry * @return {HistoryEntry} the requested history entry
*/ */
async createOrUpdateHistoryEntry( async createOrUpdateHistoryEntry(
note: Note, note: Note,
user: User, user: User,
pinStatus?: boolean,
): Promise<HistoryEntry> { ): Promise<HistoryEntry> {
let entry = await this.getEntryByNote(note, user); let entry = await this.getEntryByNote(note, user);
if (!entry) { if (!entry) {
entry = HistoryEntry.create(user, note); entry = HistoryEntry.create(user, note);
if (pinStatus !== undefined) {
entry.pinStatus = pinStatus;
}
} else { } else {
entry.updatedAt = new Date(); entry.updatedAt = new Date();
} }

View file

@ -100,8 +100,10 @@ describe('History', () => {
}); });
it('POST /me/history', async () => { it('POST /me/history', async () => {
const pinStatus = true;
const postEntryDto = new HistoryEntryImportDto(); const postEntryDto = new HistoryEntryImportDto();
postEntryDto.note = note2.alias; postEntryDto.note = note2.alias;
postEntryDto.pinStatus = pinStatus;
await request(app.getHttpServer()) await request(app.getHttpServer())
.post('/me/history') .post('/me/history')
.set('Content-Type', 'application/json') .set('Content-Type', 'application/json')
@ -110,6 +112,8 @@ describe('History', () => {
const userEntries = await historyService.getEntriesByUser(user); const userEntries = await historyService.getEntriesByUser(user);
expect(userEntries.length).toEqual(1); expect(userEntries.length).toEqual(1);
expect(userEntries[0].note.alias).toEqual(note2.alias); expect(userEntries[0].note.alias).toEqual(note2.alias);
expect(userEntries[0].user.userName).toEqual(user.userName);
expect(userEntries[0].pinStatus).toEqual(pinStatus);
}); });
it('DELETE /me/history', async () => { it('DELETE /me/history', async () => {