Spaces:
Runtime error
Runtime error
| import { Injectable, BadRequestException } from '@nestjs/common'; | |
| import { User } from '@prisma/client'; | |
| import { UserCoreService } from 'src/core/user-core/user-core.service'; | |
| import { UserInterestCoreService } from 'src/core/user-interest-core/user-interest-core.service'; | |
| import { UpdateProfileDto } from './dto/update-profile.dto'; | |
| () | |
| export class ProfileService { | |
| constructor( | |
| private readonly userCoreService: UserCoreService, | |
| private readonly userInterestCoreService: UserInterestCoreService, | |
| ) {} | |
| async updateProfile( | |
| userId: string, | |
| updateDto: UpdateProfileDto, | |
| ): Promise<User> { | |
| const { | |
| firstName, | |
| lastName, | |
| profileImage, | |
| dateOfBirth, | |
| country, | |
| city, | |
| bio, | |
| genreIds, | |
| } = updateDto; | |
| // Get current user data | |
| const currentUser = await this.userCoreService.findUnique({ | |
| where: { id: userId }, | |
| }); | |
| if (!currentUser) { | |
| throw new BadRequestException('User not found'); | |
| } | |
| // Prepare update data | |
| const updateData: any = {}; | |
| // Update name fields if provided | |
| if (firstName !== undefined) { | |
| updateData.firstName = firstName; | |
| } | |
| if (lastName !== undefined) { | |
| updateData.lastName = lastName; | |
| } | |
| // Update combined name fields if either name changed | |
| if (firstName !== undefined || lastName !== undefined) { | |
| const newFirstName = firstName ?? currentUser.firstName ?? ''; | |
| const newLastName = lastName ?? currentUser.lastName ?? ''; | |
| updateData.firstNameLastName = `${newFirstName} ${newLastName}`.trim(); | |
| updateData.lastNameFirstName = `${newLastName} ${newFirstName}`.trim(); | |
| } | |
| // Update other fields | |
| if (profileImage !== undefined) { | |
| updateData.profileImage = profileImage; | |
| } | |
| if (dateOfBirth !== undefined) { | |
| updateData.dateOfBirth = new Date(dateOfBirth); | |
| } | |
| if (country !== undefined) { | |
| updateData.country = country; | |
| } | |
| if (city !== undefined) { | |
| updateData.city = city; | |
| } | |
| if (bio !== undefined) { | |
| updateData.bio = bio; | |
| } | |
| // Update user profile | |
| const updatedUser = await this.userCoreService.update({ | |
| where: { id: userId }, | |
| data: updateData, | |
| }); | |
| // Update genres if provided | |
| if (genreIds !== undefined && genreIds.length > 0) { | |
| // Hard delete old interests | |
| await this.userInterestCoreService.deleteMany({ | |
| where: { userId }, | |
| }); | |
| // Create new interests | |
| for (const genreId of genreIds) { | |
| await this.userInterestCoreService.create({ | |
| data: { | |
| userId, | |
| genreId, | |
| }, | |
| }); | |
| } | |
| } | |
| return updatedUser; | |
| } | |
| async getProfile(userId: string): Promise<User> { | |
| const user = await this.userCoreService.findUnique({ | |
| where: { id: userId }, | |
| }); | |
| if (!user) { | |
| throw new BadRequestException('User not found'); | |
| } | |
| return user; | |
| } | |
| async getUserInterests(userId: string) { | |
| const interests = await this.userInterestCoreService.findMany({ | |
| where: { userId }, | |
| include: { | |
| genre: true, | |
| }, | |
| }); | |
| return interests; | |
| } | |
| } | |