userService.ts123 lines, 3.0 KB
| 1 | import { UpdateProfileRequest, UserProfile } from "../../../shared/types"; |
| 2 | import { AuthClient } from "./authClient"; |
| 3 | import prisma from "./database"; |
| 4 | import { createServiceError, sanitizeInput } from "../../../shared/utils"; |
| 5 | |
| 6 | export class UserService { |
| 7 | private authClient: AuthClient; |
| 8 | |
| 9 | constructor() { |
| 10 | this.authClient = new AuthClient(); |
| 11 | } |
| 12 | |
| 13 | async createProfile( |
| 14 | userId: string, |
| 15 | profileData: Partial<UpdateProfileRequest> |
| 16 | ): Promise<UserProfile> { |
| 17 | // check if profile already exists |
| 18 | const existingProfile = await prisma.userProfile.findUnique({ |
| 19 | where: { userId }, |
| 20 | }); |
| 21 | |
| 22 | if (existingProfile) { |
| 23 | throw createServiceError("User profile already exists", 409); |
| 24 | } |
| 25 | |
| 26 | // sanitize input data |
| 27 | const sanitizedData = this.sanitizeProfileData(profileData); |
| 28 | |
| 29 | // create new profile |
| 30 | const profile = await prisma.userProfile.create({ |
| 31 | data: { |
| 32 | userId, |
| 33 | ...sanitizedData, |
| 34 | }, |
| 35 | }); |
| 36 | |
| 37 | return profile; |
| 38 | } |
| 39 | |
| 40 | async getProfile(userId: string): Promise<UserProfile | null> { |
| 41 | const profile = await prisma.userProfile.findUnique({ |
| 42 | where: { userId }, |
| 43 | }); |
| 44 | |
| 45 | if (!profile) { |
| 46 | throw createServiceError("User profile not found", 404); |
| 47 | } |
| 48 | |
| 49 | return profile; |
| 50 | } |
| 51 | |
| 52 | async updateProfile( |
| 53 | userId: string, |
| 54 | profileData: Partial<UpdateProfileRequest> |
| 55 | ): Promise<UserProfile> { |
| 56 | // check if profile exists |
| 57 | const existingProfile = await prisma.userProfile.findUnique({ |
| 58 | where: { userId }, |
| 59 | }); |
| 60 | |
| 61 | if (!existingProfile) { |
| 62 | // if no profile exists, create one |
| 63 | return this.createProfile(userId, profileData); |
| 64 | } |
| 65 | |
| 66 | // sanitize input data |
| 67 | const sanitizedData = this.sanitizeProfileData(profileData); |
| 68 | |
| 69 | // update existing profile |
| 70 | const updatedProfile = await prisma.userProfile.update({ |
| 71 | where: { userId }, |
| 72 | data: sanitizedData, |
| 73 | }); |
| 74 | |
| 75 | return updatedProfile; |
| 76 | } |
| 77 | |
| 78 | async deleteProfile(userId: string): Promise<void> { |
| 79 | const profile = await prisma.userProfile.findUnique({ |
| 80 | where: { userId }, |
| 81 | }); |
| 82 | |
| 83 | if (!profile) { |
| 84 | throw createServiceError("User profile not found", 404); |
| 85 | } |
| 86 | |
| 87 | await prisma.userProfile.delete({ |
| 88 | where: { userId }, |
| 89 | }); |
| 90 | } |
| 91 | |
| 92 | private sanitizeProfileData( |
| 93 | data: Partial<UpdateProfileRequest> |
| 94 | ): Partial<UpdateProfileRequest> { |
| 95 | const sanitized: any = {}; |
| 96 | |
| 97 | if (data.firstName !== undefined) { |
| 98 | sanitized.firstName = data.firstName |
| 99 | ? sanitizeInput(data.firstName) |
| 100 | : null; |
| 101 | } |
| 102 | |
| 103 | if (data.lastName !== undefined) { |
| 104 | sanitized.lastName = data.lastName ? sanitizeInput(data.lastName) : null; |
| 105 | } |
| 106 | |
| 107 | if (data.bio !== undefined) { |
| 108 | sanitized.bio = data.bio ? sanitizeInput(data.bio) : null; |
| 109 | } |
| 110 | |
| 111 | if (data.avatarUrl !== undefined) { |
| 112 | sanitized.avatarUrl = data.avatarUrl |
| 113 | ? sanitizeInput(data.avatarUrl) |
| 114 | : null; |
| 115 | } |
| 116 | |
| 117 | if (data.preferences !== undefined) { |
| 118 | sanitized.preferences = data.preferences ? data.preferences : null; |
| 119 | } |
| 120 | |
| 121 | return sanitized; |
| 122 | } |
| 123 | } |