tagsService.ts144 lines, 3.2 KB
| 1 | import { |
| 2 | createServiceError, |
| 3 | isValidUUID, |
| 4 | sanitizeInput, |
| 5 | } from "../../../shared/utils"; |
| 6 | import { CreateTagRequest, Tag } from "../../../shared/types"; |
| 7 | import prisma from "./database"; |
| 8 | |
| 9 | export class TagsService { |
| 10 | async createTag(userId: string, tagData: CreateTagRequest): Promise<Tag> { |
| 11 | // Sanitize and validate the tag data |
| 12 | const sanitizedName = sanitizeInput(tagData.name); |
| 13 | const sanitizedColor = tagData.color |
| 14 | ? sanitizeInput(tagData.color) |
| 15 | : undefined; |
| 16 | |
| 17 | // Validate color format if provided ( hex color format ) |
| 18 | if (sanitizedColor && !this.isValidHexColor(sanitizedColor)) { |
| 19 | throw createServiceError( |
| 20 | "Invalid color format, Use hex color format (eg: #FF5733 or #F73)", |
| 21 | 400 |
| 22 | ); |
| 23 | } |
| 24 | |
| 25 | try { |
| 26 | // create tag |
| 27 | const tag = await prisma.tag.create({ |
| 28 | data: { |
| 29 | userId, |
| 30 | name: sanitizedName, |
| 31 | color: sanitizedColor, |
| 32 | }, |
| 33 | }); |
| 34 | |
| 35 | return tag as Tag; |
| 36 | } catch (error) { |
| 37 | // handle unique constaraint violation error |
| 38 | if (error.code === "P2002") { |
| 39 | throw createServiceError("Tag name already exists", 409); |
| 40 | } |
| 41 | throw createServiceError("Failed to create tag", 500); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | async getTagById(tagId: string, userId: string): Promise<Tag> { |
| 46 | if (!isValidUUID(tagId)) { |
| 47 | throw createServiceError("Invalid tag id", 400); |
| 48 | } |
| 49 | |
| 50 | const tag = await prisma.tag.findFirst({ |
| 51 | where: { |
| 52 | id: tagId, |
| 53 | userId, |
| 54 | }, |
| 55 | }); |
| 56 | |
| 57 | if (!tag) { |
| 58 | throw createServiceError("Tag not found", 404); |
| 59 | } |
| 60 | |
| 61 | return tag as Tag; |
| 62 | } |
| 63 | |
| 64 | async getTagsByUser( |
| 65 | page: number = 1, |
| 66 | limit: number = 50, |
| 67 | search?: string, |
| 68 | userId?: string |
| 69 | ): Promise<{ |
| 70 | tags: Tag[]; |
| 71 | total: number; |
| 72 | page: number; |
| 73 | totalPages: number; |
| 74 | }> { |
| 75 | const skip = (page - 1) * limit; |
| 76 | |
| 77 | // build where clause |
| 78 | const whereClause: any = { |
| 79 | userId, |
| 80 | }; |
| 81 | |
| 82 | // add search functionality |
| 83 | if (search) { |
| 84 | const sanitizedSearch = sanitizeInput(search); |
| 85 | whereClause.name = { |
| 86 | contains: sanitizedSearch, |
| 87 | mode: "insensitive", |
| 88 | }; |
| 89 | } |
| 90 | |
| 91 | const [tags, total] = await Promise.all([ |
| 92 | prisma.tag.findMany({ |
| 93 | where: whereClause, |
| 94 | skip, |
| 95 | take: limit, |
| 96 | orderBy: { name: "asc" }, // sorct tags alphebetically |
| 97 | }), |
| 98 | prisma.tag.count({ |
| 99 | where: whereClause, |
| 100 | }), |
| 101 | ]); |
| 102 | |
| 103 | const totalPages = Math.ceil(total / limit); |
| 104 | |
| 105 | return { |
| 106 | tags: tags as Tag[], |
| 107 | total, |
| 108 | page, |
| 109 | totalPages, |
| 110 | }; |
| 111 | } |
| 112 | |
| 113 | async validateTags( |
| 114 | tagIds: string[], |
| 115 | userId: string |
| 116 | ): Promise<{ |
| 117 | validTags: Tag[]; |
| 118 | invalidTagIds: string[]; |
| 119 | }> { |
| 120 | const validTags: Tag[] = []; |
| 121 | const invalidTagIds: string[] = []; |
| 122 | |
| 123 | for (const tagId of tagIds) { |
| 124 | if (!isValidUUID(tagId)) { |
| 125 | invalidTagIds.push(tagId); |
| 126 | continue; |
| 127 | } |
| 128 | |
| 129 | try { |
| 130 | const tag = await this.getTagById(tagId, userId); |
| 131 | validTags.push(tag); |
| 132 | } catch (error) { |
| 133 | invalidTagIds.push(tagId); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | return { validTags, invalidTagIds }; |
| 138 | } |
| 139 | |
| 140 | private isValidHexColor(color: string): boolean { |
| 141 | const hexColorRegex = /^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/; |
| 142 | return hexColorRegex.test(color); |
| 143 | } |
| 144 | } |