PrivateAPI: Add lastVisited to HistoryEntryImportDto

As the DTO is only for importing an existing history the lastVisited 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-11 12:37:47 +02:00 committed by David Mehren
parent eeaa74b385
commit c1d706b350
No known key found for this signature in database
GPG key ID: 185982BA4C42B7C3
5 changed files with 102 additions and 13 deletions

View file

@ -69,6 +69,7 @@ export class HistoryController {
note,
user,
historyEntry.pinStatus,
historyEntry.lastVisited,
);
}
} catch (e) {

View file

@ -4,7 +4,7 @@
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { IsBoolean, IsString } from 'class-validator';
import { IsBoolean, IsDate, IsString } from 'class-validator';
export class HistoryEntryImportDto {
/**
@ -18,4 +18,10 @@ export class HistoryEntryImportDto {
*/
@IsBoolean()
pinStatus: boolean;
/**
* Datestring of the last time this note was updated
* @example "2020-12-01 12:23:34"
*/
@IsDate()
lastVisited: Date;
}

View file

@ -144,7 +144,9 @@ describe('HistoryService', () => {
const user = {} as User;
const alias = 'alias';
const pinStatus = true;
it('without an preexisting entry and without pinStatus', async () => {
const lastVisited = new Date('2020-12-01 12:23:34');
const historyEntry = HistoryEntry.create(user, Note.create(user, alias));
it('without an preexisting entry, without pinStatus and without lastVisited', async () => {
jest.spyOn(historyRepo, 'findOne').mockResolvedValueOnce(undefined);
jest
.spyOn(historyRepo, 'save')
@ -161,7 +163,7 @@ describe('HistoryService', () => {
expect(createHistoryEntry.pinStatus).toEqual(false);
});
it('without an preexisting entry and with pinStatus', async () => {
it('without an preexisting entry, with pinStatus and without lastVisited', async () => {
jest.spyOn(historyRepo, 'findOne').mockResolvedValueOnce(undefined);
jest
.spyOn(historyRepo, 'save')
@ -179,11 +181,47 @@ describe('HistoryService', () => {
expect(createHistoryEntry.pinStatus).toEqual(pinStatus);
});
it('with an preexisting entry and without pinStatus', async () => {
const historyEntry = HistoryEntry.create(
user,
Note.create(user, alias),
it('without an preexisting entry, without pinStatus and with lastVisited', async () => {
jest.spyOn(historyRepo, 'findOne').mockResolvedValueOnce(undefined);
jest
.spyOn(historyRepo, 'save')
.mockImplementation(
async (entry: HistoryEntry): Promise<HistoryEntry> => entry,
);
const createHistoryEntry = await service.createOrUpdateHistoryEntry(
Note.create(user, alias),
user,
undefined,
lastVisited,
);
expect(createHistoryEntry.note.alias).toEqual(alias);
expect(createHistoryEntry.note.owner).toEqual(user);
expect(createHistoryEntry.user).toEqual(user);
expect(createHistoryEntry.pinStatus).toEqual(false);
expect(createHistoryEntry.updatedAt).toEqual(lastVisited);
});
it('without an preexisting entry, with pinStatus and with lastVisited', async () => {
jest.spyOn(historyRepo, 'findOne').mockResolvedValueOnce(undefined);
jest
.spyOn(historyRepo, 'save')
.mockImplementation(
async (entry: HistoryEntry): Promise<HistoryEntry> => entry,
);
const createHistoryEntry = await service.createOrUpdateHistoryEntry(
Note.create(user, alias),
user,
pinStatus,
lastVisited,
);
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).toEqual(lastVisited);
});
it('with an preexisting entry, without pinStatus and without lastVisited', async () => {
jest.spyOn(historyRepo, 'findOne').mockResolvedValueOnce(historyEntry);
jest
.spyOn(historyRepo, 'save')
@ -203,12 +241,7 @@ describe('HistoryService', () => {
);
});
it('with an preexisting entry and with pinStatus', async () => {
const historyEntry = HistoryEntry.create(
user,
Note.create(user, alias),
pinStatus,
);
it('with an preexisting entry, with pinStatus and without lastVisited', async () => {
jest.spyOn(historyRepo, 'findOne').mockResolvedValueOnce(historyEntry);
jest
.spyOn(historyRepo, 'save')
@ -223,11 +256,51 @@ describe('HistoryService', () => {
expect(createHistoryEntry.note.alias).toEqual(alias);
expect(createHistoryEntry.note.owner).toEqual(user);
expect(createHistoryEntry.user).toEqual(user);
expect(createHistoryEntry.pinStatus).toEqual(pinStatus);
expect(createHistoryEntry.pinStatus).not.toEqual(pinStatus);
expect(createHistoryEntry.updatedAt.getTime()).toBeGreaterThanOrEqual(
historyEntry.updatedAt.getTime(),
);
});
it('with an preexisting entry, without pinStatus and with lastVisited', async () => {
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,
undefined,
lastVisited,
);
expect(createHistoryEntry.note.alias).toEqual(alias);
expect(createHistoryEntry.note.owner).toEqual(user);
expect(createHistoryEntry.user).toEqual(user);
expect(createHistoryEntry.pinStatus).toEqual(false);
expect(createHistoryEntry.updatedAt).not.toEqual(lastVisited);
});
it('with an preexisting entry, with pinStatus and with lastVisited', async () => {
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,
lastVisited,
);
expect(createHistoryEntry.note.alias).toEqual(alias);
expect(createHistoryEntry.note.owner).toEqual(user);
expect(createHistoryEntry.user).toEqual(user);
expect(createHistoryEntry.pinStatus).not.toEqual(pinStatus);
expect(createHistoryEntry.updatedAt).not.toEqual(lastVisited);
});
});
});

View file

@ -81,12 +81,14 @@ export class HistoryService {
* @param {Note} note - the note 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
* @param {Date} lastVisited - the last time the associated note was accessed
* @return {HistoryEntry} the requested history entry
*/
async createOrUpdateHistoryEntry(
note: Note,
user: User,
pinStatus?: boolean,
lastVisited?: Date,
): Promise<HistoryEntry> {
let entry = await this.getEntryByNote(note, user);
if (!entry) {
@ -94,6 +96,9 @@ export class HistoryService {
if (pinStatus !== undefined) {
entry.pinStatus = pinStatus;
}
if (lastVisited !== undefined) {
entry.updatedAt = lastVisited;
}
} else {
entry.updatedAt = new Date();
}

View file

@ -100,10 +100,13 @@ describe('History', () => {
});
it('POST /me/history', async () => {
expect((await historyService.getEntriesByUser(user)).length).toEqual(1);
const pinStatus = true;
const lastVisited = new Date('2020-12-01 12:23:34');
const postEntryDto = new HistoryEntryImportDto();
postEntryDto.note = note2.alias;
postEntryDto.pinStatus = pinStatus;
postEntryDto.lastVisited = lastVisited;
await request(app.getHttpServer())
.post('/me/history')
.set('Content-Type', 'application/json')
@ -114,6 +117,7 @@ describe('History', () => {
expect(userEntries[0].note.alias).toEqual(note2.alias);
expect(userEntries[0].user.userName).toEqual(user.userName);
expect(userEntries[0].pinStatus).toEqual(pinStatus);
expect(userEntries[0].updatedAt).toEqual(lastVisited);
});
it('DELETE /me/history', async () => {