notesService.ts200 lines, 4.2 KB
| 1 | import { createServiceError, sanitizeInput } from "../../../shared/utils"; |
| 2 | import { CreateNoteRequest, Note } from "../../../shared/types"; |
| 3 | import prisma from "./database"; |
| 4 | import { TagsServiceClient } from "./tagsServiceClient"; |
| 5 | |
| 6 | export class NotesService { |
| 7 | private tagsServiceClient: TagsServiceClient; |
| 8 | constructor() { |
| 9 | this.tagsServiceClient = new TagsServiceClient(); |
| 10 | } |
| 11 | |
| 12 | async createNote( |
| 13 | userId: string, |
| 14 | noteData: CreateNoteRequest, |
| 15 | authToken?: string |
| 16 | ): Promise<Note> { |
| 17 | // sanitize input data |
| 18 | const sanitizedTitl = sanitizeInput(noteData.title); |
| 19 | const sanitizedContent = sanitizeInput(noteData.content); |
| 20 | |
| 21 | //Create note |
| 22 | const note = await prisma.note.create({ |
| 23 | data: { |
| 24 | userId, |
| 25 | title: sanitizedTitl, |
| 26 | content: sanitizedContent, |
| 27 | }, |
| 28 | include: { |
| 29 | noteTags: true, |
| 30 | }, |
| 31 | }); |
| 32 | |
| 33 | //TODO: add tags to note if provided |
| 34 | if (noteData.tagIds && note.noteTags.length === 0) { |
| 35 | // Validate tags exists and belongs to user |
| 36 | if (authToken) { |
| 37 | await this.tagsServiceClient.validateTags(noteData.tagIds, authToken); |
| 38 | } |
| 39 | await this.addTagsToNote(note.id, noteData.tagIds); |
| 40 | |
| 41 | // fetch the note again with tags |
| 42 | return this.getNoteById(note.id, userId); |
| 43 | } |
| 44 | |
| 45 | return note as Note; |
| 46 | } |
| 47 | |
| 48 | async getNoteById(noteId: string, userId: string): Promise<Note> { |
| 49 | const note = await prisma.note.findFirst({ |
| 50 | where: { |
| 51 | id: noteId, |
| 52 | userId, |
| 53 | isDeleted: false, |
| 54 | }, |
| 55 | include: { |
| 56 | noteTags: true, |
| 57 | }, |
| 58 | }); |
| 59 | |
| 60 | if (!note) { |
| 61 | throw createServiceError("Note not found", 404); |
| 62 | } |
| 63 | |
| 64 | return note as Note; |
| 65 | } |
| 66 | |
| 67 | async getNotesByUser( |
| 68 | userId: string, |
| 69 | page: number = 1, |
| 70 | limit: number = 50, |
| 71 | search?: string |
| 72 | ): Promise<{ |
| 73 | notes: Note[]; |
| 74 | total: number; |
| 75 | page: number; |
| 76 | totalPages: number; |
| 77 | }> { |
| 78 | const skip = (page - 1) * limit; |
| 79 | |
| 80 | // build where clause |
| 81 | const whereClause: any = { |
| 82 | userId, |
| 83 | isDeleted: false, |
| 84 | }; |
| 85 | |
| 86 | // add search functionality |
| 87 | if (search) { |
| 88 | const sanitizedSearch = sanitizeInput(search); |
| 89 | whereClause.OR = [ |
| 90 | { |
| 91 | title: { |
| 92 | contains: sanitizedSearch, |
| 93 | mode: "insensitive", |
| 94 | }, |
| 95 | }, |
| 96 | { |
| 97 | content: { |
| 98 | contains: sanitizedSearch, |
| 99 | mode: "insensitive", |
| 100 | }, |
| 101 | }, |
| 102 | ]; |
| 103 | } |
| 104 | |
| 105 | const [notes, total] = await Promise.all([ |
| 106 | prisma.note.findMany({ |
| 107 | where: whereClause, |
| 108 | include: { |
| 109 | noteTags: true, |
| 110 | }, |
| 111 | skip, |
| 112 | take: limit, |
| 113 | orderBy: { updatedAt: "desc" }, |
| 114 | }), |
| 115 | prisma.note.count({ |
| 116 | where: whereClause, |
| 117 | }), |
| 118 | ]); |
| 119 | |
| 120 | const totalPages = Math.ceil(total / limit); |
| 121 | |
| 122 | return { |
| 123 | notes, |
| 124 | total, |
| 125 | page, |
| 126 | totalPages, |
| 127 | }; |
| 128 | } |
| 129 | |
| 130 | private async addTagsToNote(noteId: string, tagIds: string[]): Promise<void> { |
| 131 | const noteTagData = tagIds.map((tagId) => ({ |
| 132 | noteId, |
| 133 | tagId, |
| 134 | })); |
| 135 | |
| 136 | await prisma.noteTag.createMany({ |
| 137 | data: noteTagData, |
| 138 | skipDuplicates: true, // avoid errors if tag is already associated |
| 139 | }); |
| 140 | } |
| 141 | |
| 142 | async getNotesByTag( |
| 143 | userId: string, |
| 144 | tagId: string, |
| 145 | page: number = 1, |
| 146 | limit: number = 50, |
| 147 | authToken?: string |
| 148 | ): Promise<{ |
| 149 | notes: Note[]; |
| 150 | total: number; |
| 151 | page: number; |
| 152 | totalPages: number; |
| 153 | }> { |
| 154 | if (authToken) { |
| 155 | await this.tagsServiceClient.validateTags([tagId], authToken); |
| 156 | } |
| 157 | |
| 158 | const skip = (page - 1) * limit; |
| 159 | |
| 160 | const [notes, total] = await Promise.all([ |
| 161 | prisma.note.findMany({ |
| 162 | where: { |
| 163 | userId, |
| 164 | isDeleted: false, |
| 165 | noteTags: { |
| 166 | some: { |
| 167 | tagId, |
| 168 | }, |
| 169 | }, |
| 170 | }, |
| 171 | include: { |
| 172 | noteTags: true, |
| 173 | }, |
| 174 | skip, |
| 175 | take: limit, |
| 176 | orderBy: { updatedAt: "desc" }, |
| 177 | }), |
| 178 | prisma.note.count({ |
| 179 | where: { |
| 180 | userId, |
| 181 | isDeleted: false, |
| 182 | noteTags: { |
| 183 | some: { |
| 184 | tagId, |
| 185 | }, |
| 186 | }, |
| 187 | }, |
| 188 | }), |
| 189 | ]); |
| 190 | |
| 191 | const totalPages = Math.ceil(total / limit); |
| 192 | |
| 193 | return { |
| 194 | notes, |
| 195 | total, |
| 196 | page, |
| 197 | totalPages, |
| 198 | }; |
| 199 | } |
| 200 | } |