feat: Add email field to profile edit form
- Add email input field to profile edit form - Display current email or 'No email address set' message - Show helpful hint when adding email for the first time - Update backend to accept email in profile updates - Add email uniqueness validation - Allow users to add/update/remove email address - Support empty string to remove email (converts to null) Users can now add or update their email address from the profile page, enabling them to receive email notifications.
This commit is contained in:
@@ -41,7 +41,7 @@ export class UserController {
|
||||
async updateUserProfile(req: AuthRequest, res: Response) {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
const { firstName, lastName, username } = req.body;
|
||||
const { firstName, lastName, username, email } = req.body;
|
||||
|
||||
// Authorization check - users can only update their own profile
|
||||
if (!req.userId) {
|
||||
@@ -91,6 +91,7 @@ export class UserController {
|
||||
firstName,
|
||||
lastName,
|
||||
username,
|
||||
email,
|
||||
});
|
||||
|
||||
res.json(updatedProfile);
|
||||
|
||||
@@ -5,6 +5,7 @@ export interface UpdateUserProfileData {
|
||||
firstName?: string;
|
||||
lastName?: string;
|
||||
username?: string;
|
||||
email?: string;
|
||||
}
|
||||
|
||||
export interface UpdateNotificationPreferencesData {
|
||||
@@ -146,12 +147,30 @@ export class UserService {
|
||||
}
|
||||
}
|
||||
|
||||
// Check if email is being changed and if it's already taken
|
||||
if (data.email !== undefined) {
|
||||
// Allow setting email to null or empty string
|
||||
if (data.email && data.email.trim() !== '') {
|
||||
const existingUser = await prisma.user.findFirst({
|
||||
where: {
|
||||
email: data.email,
|
||||
id: { not: userId },
|
||||
},
|
||||
});
|
||||
|
||||
if (existingUser) {
|
||||
throw new Error('Email already in use');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const updatedUser = await prisma.user.update({
|
||||
where: { id: userId },
|
||||
data: {
|
||||
firstName: data.firstName,
|
||||
lastName: data.lastName,
|
||||
username: data.username,
|
||||
email: data.email !== undefined ? (data.email.trim() === '' ? null : data.email) : undefined,
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
|
||||
Reference in New Issue
Block a user