Skip to content

Commit

Permalink
Merge pull request #88 from atlp-rwanda/fix-user-profile
Browse files Browse the repository at this point in the history
fix: Creating a user profile when a user  has no profile
  • Loading branch information
teerenzo authored Jul 5, 2024
2 parents f5159ff + e44158a commit 04e8e16
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
11 changes: 5 additions & 6 deletions src/controllers/userControllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import { env } from "../utils/env";
import { Emailschema, resetPasswordSchema } from "../schemas/resetPasswordSchema";
import { clearExpiredUserData } from "../jobs/isPasswordExpired";
import { fetchAllsellersService } from "../services/wishlist.service";

import Profile from "../sequelize/models/profiles";
import { error } from "console";

export const fetchAllUsers = async (req: Request, res: Response) => {
try {
Expand Down Expand Up @@ -192,7 +193,7 @@ export const handleSuccess = async (req: Request, res: Response) => {
username: user.name.familyName,
isVerified:true,
//@ts-ignore
password: null
password: null,
});
token = await generateToken(newUser);
foundUser = newUser;
Expand Down Expand Up @@ -271,11 +272,9 @@ export const updateProfileController = async (req: Request, res: Response) => {
status: 200,
message: "You updated your profile sucessfully!",
updatedProfile
});

});
}

catch(error) {
catch(error) {
res.status(500).json({ error: 'Internal server error' });
}
}
Expand Down
13 changes: 8 additions & 5 deletions src/services/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,15 @@ export const updateProfileServices = async (
import("../sequelize/models/profiles")
.ProfileAttributes >) => {
try {
const profile = await Profile.findOne({where: {userId}});
let profile = await Profile.findOne({where: {userId}});

if (!profile) {
throw new Error("No Profile found");
profileData.userId = userId;
//@ts-ignore
profile = await Profile.create(profileData);
}else{
await profile.update(profileData)
}

await profile.update(profileData)
} catch (error) {
throw new Error("Error in update profile");
}
Expand Down Expand Up @@ -260,10 +262,11 @@ export const resetPassword = async (token: string, newPassword: string): Promise
}
const hashPassword = await hashedPassword(newPassword);
await user.update({ password: hashPassword });
user.lastPasswordUpdateTime = new Date();
await user.save();
const subject = 'Password Updated Confirmation';
await sendEmailService(user, subject, generatePasswordUpdateEmailContent(user.name));
await addToBlacklist(token);

return { status: 200, message: 'Password updated successfully.' };
} catch (error) {
return { status: 500, message: 'Internal server error.' };
Expand Down

0 comments on commit 04e8e16

Please sign in to comment.