mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2024-11-25 03:06:31 -05:00
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:
parent
eeaa74b385
commit
c1d706b350
5 changed files with 102 additions and 13 deletions
|
@ -69,6 +69,7 @@ export class HistoryController {
|
||||||
note,
|
note,
|
||||||
user,
|
user,
|
||||||
historyEntry.pinStatus,
|
historyEntry.pinStatus,
|
||||||
|
historyEntry.lastVisited,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
* SPDX-License-Identifier: AGPL-3.0-only
|
* SPDX-License-Identifier: AGPL-3.0-only
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { IsBoolean, IsString } from 'class-validator';
|
import { IsBoolean, IsDate, IsString } from 'class-validator';
|
||||||
|
|
||||||
export class HistoryEntryImportDto {
|
export class HistoryEntryImportDto {
|
||||||
/**
|
/**
|
||||||
|
@ -18,4 +18,10 @@ export class HistoryEntryImportDto {
|
||||||
*/
|
*/
|
||||||
@IsBoolean()
|
@IsBoolean()
|
||||||
pinStatus: boolean;
|
pinStatus: boolean;
|
||||||
|
/**
|
||||||
|
* Datestring of the last time this note was updated
|
||||||
|
* @example "2020-12-01 12:23:34"
|
||||||
|
*/
|
||||||
|
@IsDate()
|
||||||
|
lastVisited: Date;
|
||||||
}
|
}
|
||||||
|
|
|
@ -144,7 +144,9 @@ describe('HistoryService', () => {
|
||||||
const user = {} as User;
|
const user = {} as User;
|
||||||
const alias = 'alias';
|
const alias = 'alias';
|
||||||
const pinStatus = true;
|
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, 'findOne').mockResolvedValueOnce(undefined);
|
||||||
jest
|
jest
|
||||||
.spyOn(historyRepo, 'save')
|
.spyOn(historyRepo, 'save')
|
||||||
|
@ -161,7 +163,7 @@ describe('HistoryService', () => {
|
||||||
expect(createHistoryEntry.pinStatus).toEqual(false);
|
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, 'findOne').mockResolvedValueOnce(undefined);
|
||||||
jest
|
jest
|
||||||
.spyOn(historyRepo, 'save')
|
.spyOn(historyRepo, 'save')
|
||||||
|
@ -179,11 +181,47 @@ describe('HistoryService', () => {
|
||||||
expect(createHistoryEntry.pinStatus).toEqual(pinStatus);
|
expect(createHistoryEntry.pinStatus).toEqual(pinStatus);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('with an preexisting entry and without pinStatus', async () => {
|
it('without an preexisting entry, without pinStatus and with lastVisited', async () => {
|
||||||
const historyEntry = HistoryEntry.create(
|
jest.spyOn(historyRepo, 'findOne').mockResolvedValueOnce(undefined);
|
||||||
user,
|
jest
|
||||||
|
.spyOn(historyRepo, 'save')
|
||||||
|
.mockImplementation(
|
||||||
|
async (entry: HistoryEntry): Promise<HistoryEntry> => entry,
|
||||||
|
);
|
||||||
|
const createHistoryEntry = await service.createOrUpdateHistoryEntry(
|
||||||
Note.create(user, alias),
|
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, 'findOne').mockResolvedValueOnce(historyEntry);
|
||||||
jest
|
jest
|
||||||
.spyOn(historyRepo, 'save')
|
.spyOn(historyRepo, 'save')
|
||||||
|
@ -203,12 +241,7 @@ describe('HistoryService', () => {
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('with an preexisting entry and with pinStatus', async () => {
|
it('with an preexisting entry, with pinStatus and without lastVisited', async () => {
|
||||||
const historyEntry = HistoryEntry.create(
|
|
||||||
user,
|
|
||||||
Note.create(user, alias),
|
|
||||||
pinStatus,
|
|
||||||
);
|
|
||||||
jest.spyOn(historyRepo, 'findOne').mockResolvedValueOnce(historyEntry);
|
jest.spyOn(historyRepo, 'findOne').mockResolvedValueOnce(historyEntry);
|
||||||
jest
|
jest
|
||||||
.spyOn(historyRepo, 'save')
|
.spyOn(historyRepo, 'save')
|
||||||
|
@ -223,11 +256,51 @@ describe('HistoryService', () => {
|
||||||
expect(createHistoryEntry.note.alias).toEqual(alias);
|
expect(createHistoryEntry.note.alias).toEqual(alias);
|
||||||
expect(createHistoryEntry.note.owner).toEqual(user);
|
expect(createHistoryEntry.note.owner).toEqual(user);
|
||||||
expect(createHistoryEntry.user).toEqual(user);
|
expect(createHistoryEntry.user).toEqual(user);
|
||||||
expect(createHistoryEntry.pinStatus).toEqual(pinStatus);
|
expect(createHistoryEntry.pinStatus).not.toEqual(pinStatus);
|
||||||
expect(createHistoryEntry.updatedAt.getTime()).toBeGreaterThanOrEqual(
|
expect(createHistoryEntry.updatedAt.getTime()).toBeGreaterThanOrEqual(
|
||||||
historyEntry.updatedAt.getTime(),
|
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);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -81,12 +81,14 @@ export class HistoryService {
|
||||||
* @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
|
* @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
|
* @return {HistoryEntry} the requested history entry
|
||||||
*/
|
*/
|
||||||
async createOrUpdateHistoryEntry(
|
async createOrUpdateHistoryEntry(
|
||||||
note: Note,
|
note: Note,
|
||||||
user: User,
|
user: User,
|
||||||
pinStatus?: boolean,
|
pinStatus?: boolean,
|
||||||
|
lastVisited?: Date,
|
||||||
): Promise<HistoryEntry> {
|
): Promise<HistoryEntry> {
|
||||||
let entry = await this.getEntryByNote(note, user);
|
let entry = await this.getEntryByNote(note, user);
|
||||||
if (!entry) {
|
if (!entry) {
|
||||||
|
@ -94,6 +96,9 @@ export class HistoryService {
|
||||||
if (pinStatus !== undefined) {
|
if (pinStatus !== undefined) {
|
||||||
entry.pinStatus = pinStatus;
|
entry.pinStatus = pinStatus;
|
||||||
}
|
}
|
||||||
|
if (lastVisited !== undefined) {
|
||||||
|
entry.updatedAt = lastVisited;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
entry.updatedAt = new Date();
|
entry.updatedAt = new Date();
|
||||||
}
|
}
|
||||||
|
|
|
@ -100,10 +100,13 @@ describe('History', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('POST /me/history', async () => {
|
it('POST /me/history', async () => {
|
||||||
|
expect((await historyService.getEntriesByUser(user)).length).toEqual(1);
|
||||||
const pinStatus = true;
|
const pinStatus = true;
|
||||||
|
const lastVisited = new Date('2020-12-01 12:23:34');
|
||||||
const postEntryDto = new HistoryEntryImportDto();
|
const postEntryDto = new HistoryEntryImportDto();
|
||||||
postEntryDto.note = note2.alias;
|
postEntryDto.note = note2.alias;
|
||||||
postEntryDto.pinStatus = pinStatus;
|
postEntryDto.pinStatus = pinStatus;
|
||||||
|
postEntryDto.lastVisited = lastVisited;
|
||||||
await request(app.getHttpServer())
|
await request(app.getHttpServer())
|
||||||
.post('/me/history')
|
.post('/me/history')
|
||||||
.set('Content-Type', 'application/json')
|
.set('Content-Type', 'application/json')
|
||||||
|
@ -114,6 +117,7 @@ describe('History', () => {
|
||||||
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].user.userName).toEqual(user.userName);
|
||||||
expect(userEntries[0].pinStatus).toEqual(pinStatus);
|
expect(userEntries[0].pinStatus).toEqual(pinStatus);
|
||||||
|
expect(userEntries[0].updatedAt).toEqual(lastVisited);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('DELETE /me/history', async () => {
|
it('DELETE /me/history', async () => {
|
||||||
|
|
Loading…
Reference in a new issue