-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathnote.ts
More file actions
231 lines (195 loc) · 6.75 KB
/
note.ts
File metadata and controls
231 lines (195 loc) · 6.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import type { Note, NoteInternalId, NotePublicId } from '@domain/entities/note.js';
import type NoteRepository from '@repository/note.repository.js';
import type NoteVisitsRepository from '@repository/noteVisits.repository.js';
import { createPublicId } from '@infrastructure/utils/id.js';
import { DomainError } from '@domain/entities/DomainError.js';
import type NoteRelationsRepository from '@repository/noteRelations.repository.js';
import type User from '@domain/entities/user.js';
import type { NoteList } from '@domain/entities/noteList.js';
import EventBus from '@domain/event-bus/index.js';
import { NoteAddedEvent } from '@domain/event-bus/events/noteAddedEvent.js';
/**
* Note service
*/
export default class NoteService {
/**
* Note repository
*/
public noteRepository: NoteRepository;
/**
* Note relationship repository
*/
public noteRelationsRepository: NoteRelationsRepository;
/**
* Note visits repository
*/
public noteVisitsRepository: NoteVisitsRepository;
/**
* Number of the notes to be displayed on one page
* it is used to calculate offset and limit for getting notes that the user has recently opened
*/
private readonly noteListPortionSize = 30;
/**
* Note service constructor
*
* @param noteRepository - note repository
* @param noteRelationsRepository - note relationship repository
* @param noteVisitsRepository - note visits repository
*/
constructor(noteRepository: NoteRepository, noteRelationsRepository: NoteRelationsRepository, noteVisitsRepository: NoteVisitsRepository) {
this.noteRepository = noteRepository;
this.noteRelationsRepository = noteRelationsRepository;
this.noteVisitsRepository = noteVisitsRepository;
}
/**
* Adds note
*
* @param content - note content
* @param creatorId - note creator
* @param parentPublicId - parent note if exist
* @returns { Note } added note object
*/
public async addNote(content: JSON, creatorId: Note['creatorId'], parentPublicId: Note['publicId'] | undefined): Promise<Note> {
const note = await this.noteRepository.addNote({
publicId: createPublicId(),
content,
creatorId,
});
if (parentPublicId !== undefined) {
const parentNote = await this.getNoteByPublicId(parentPublicId);
if (parentNote === null) {
throw new DomainError(`Incorrect parent note`);
}
await this.noteRelationsRepository.addNoteRelation(note.id, parentNote.id);
}
/**
* Dispatches an event when a note is added
*/
EventBus.getInstance().dispatch(new NoteAddedEvent(note.id, creatorId));
return note;
}
/**
* @todo Build a note tree and delete all descendants of a deleted note
*/
/**
* Deletes note by id
*
* @param id - note internal id
*/
public async deleteNoteById(id: NoteInternalId): Promise<boolean> {
/**
* @todo If the note has not been deleted,
* we must reset the note_relations database to its original state
*/
const hasRelation = await this.noteRelationsRepository.hasRelation(id);
if (hasRelation) {
const isNoteRelationsDeleted = await this.noteRelationsRepository.deleteNoteRelationsByNoteId(id);
if (isNoteRelationsDeleted === false) {
throw new DomainError(`Relation with noteId ${id} was not deleted`);
}
}
const isNoteDeleted = await this.noteRepository.deleteNoteById(id);
if (isNoteDeleted === false) {
throw new DomainError(`Note with id ${id} was not deleted`);
}
return isNoteDeleted;
}
/**
* Updates a note
*
* @param id - note internal id
* @param content - new content
*/
public async updateNoteContentById(id: NoteInternalId, content: Note['content']): Promise<Note> {
const updatedNote = await this.noteRepository.updateNoteContentById(id, content);
if (updatedNote === null) {
throw new DomainError(`Note with id ${id} was not updated`);
}
return updatedNote;
}
/**
* Unlink parent note from the current note
*
* @param noteId - id of note to unlink parent
*/
public async unlinkParent(noteId: NoteInternalId): Promise<boolean> {
return this.noteRelationsRepository.unlinkParent(noteId);
}
/**
* Returns note by id
*
* @param id - note internal id
*/
public async getNoteById(id: NoteInternalId): Promise<Note> {
const note = await this.noteRepository.getNoteById(id);
if (note === null) {
throw new DomainError(`Note with id ${id} was not found`);
}
return note;
}
/**
* Returns note by public id
*
* @param publicId - note public id
*/
public async getNoteByPublicId(publicId: NotePublicId): Promise<Note> {
const note = await this.noteRepository.getNoteByPublicId(publicId);
if (note === null) {
throw new DomainError(`Note with public id ${publicId} was not found`);
}
return note;
}
/**
* Gets note by custom hostname
*
* @param hostname - hostname
* @returns { Promise<Note | null> } note
*/
public async getNoteByHostname(hostname: string): Promise<Note | null> {
return await this.noteRepository.getNoteByHostname(hostname);
}
/**
* Get parent note id by note id
*
* @param noteId - id of the current note
*/
public async getParentNoteIdByNoteId(noteId: NoteInternalId): Promise<NoteInternalId | null> {
return await this.noteRelationsRepository.getParentNoteIdByNoteId(noteId);
}
/**
* Returns note list by creator id
*
* @param userId - id of the user
* @param page - number of current page
* @returns list of the notes ordered by time of last visit
*/
public async getNoteListByUserId(userId: User['id'], page: number): Promise<NoteList> {
const offset = (page - 1) * this.noteListPortionSize;
return {
items: await this.noteRepository.getNoteListByUserId(userId, offset, this.noteListPortionSize),
};
}
/**
* Update note relation
*
* @param noteId - id of the current note
* @param parentPublicId - id of the new parent note
*/
public async updateNoteRelation(noteId: NoteInternalId, parentPublicId: NotePublicId): Promise<boolean> {
const parentNote = await this.noteRepository.getNoteByPublicId(parentPublicId);
if (parentNote === null) {
throw new DomainError(`Incorrect parent note`);
}
let parentNoteId: number | null = parentNote.id;
/**
* This loop checks for cyclic reference when updating a note's parent.
*/
while (parentNoteId !== null) {
if (parentNoteId === noteId) {
throw new DomainError(`Forbidden relation. Note can't be a child of own child`);
}
parentNoteId = await this.noteRelationsRepository.getParentNoteIdByNoteId(parentNoteId);
}
return await this.noteRelationsRepository.updateNoteRelationById(noteId, parentNote.id);
};
}