Skip to content

Commit

Permalink
Merge pull request #94 from atlp-rwanda/fix-profile
Browse files Browse the repository at this point in the history
fix:user profile
  • Loading branch information
teerenzo authored Jul 22, 2024
2 parents 0df8206 + e446bf5 commit e2bab33
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
25 changes: 22 additions & 3 deletions src/controllers/userControllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import User from "../sequelize/models/users";
import { verifyOtpTemplate } from "../email-templates/verifyotp";
import { getProfileServices, updateProfileServices } from "../services/user.service";
import uploadFile from "../utils/handleUpload";
import { updateUserRoleService } from "../services/user.service";
import { updateUserRoleService,updateUserInfo } from "../services/user.service";
import { generateRandomNumber } from "../utils/generateRandomNumber";
import { env } from "../utils/env";
import { Emailschema, resetPasswordSchema } from "../schemas/resetPasswordSchema";
Expand Down Expand Up @@ -78,7 +78,7 @@ export const userLogin = async (req: Request, res: Response) => {
if (!match) {
res.status(401).json({
status: 401,
message: " User email or password is incorrect!",
message: " Invalid credentials!",
});
} else {
// @ts-ignore
Expand Down Expand Up @@ -214,9 +214,26 @@ export const handleSuccess = async (req: Request, res: Response) => {
isVerified:true,
//@ts-ignore
password: null,
lastPasswordUpdateTime: new Date(),
});
token = await generateToken(newUser);
foundUser = newUser;
await Profile.create({
//@ts-ignore
userId: newUser.id,
profileImage:"",
fullName:newUser.name,
email:newUser.email,
gender: "",
birthdate: "",
preferredLanguage: "",
preferredCurrency: "",
street: "",
city: "",
state: "",
postalCode:"",
country: "",
})
} else {
token = await generateToken(foundUser);
}
Expand Down Expand Up @@ -256,7 +273,7 @@ export const getProfileController = async (req: Request, res: Response) => {
res.status(404).json({ status: 404, message: "profile not found!" });
} else {
const { dataValues } = profile;
const { id, userId, email, ...filteredProfile } = dataValues;
const { id, userId,...filteredProfile } = dataValues;
res.status(200).json(filteredProfile);
}
} catch (error) {
Expand All @@ -267,6 +284,7 @@ export const getProfileController = async (req: Request, res: Response) => {
export const updateProfileController = async (req: Request, res: Response) => {
try{
const userId = (req as any).user.id;
const user = (req as any).user;
const profileData = req.body;
const file = req.file
let profileImage;
Expand All @@ -288,6 +306,7 @@ export const updateProfileController = async (req: Request, res: Response) => {
userId,
{ ...profileData, profileImage }
);
await updateUserInfo(user,profileData.fullName)
res.status(200).json({
status: 200,
message: "You updated your profile sucessfully!",
Expand Down
9 changes: 7 additions & 2 deletions src/services/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ export const getUserById = async (id: number) => {
}
};

export const updateUserInfo = async(user: User,name:string) => {
const updatedInfo = await User.update({ name: name}, { where: { id: user.id}})
return updatedInfo;
;}

export const loggedInUser = async (email: string) => {
try {
const user: any = await User.findOne({
Expand Down Expand Up @@ -95,8 +100,8 @@ export const createUserService = async (name: string, email: string, username: s
// @ts-ignore
userId: user.id,
profileImage:"",
fullName: "",
email: "",
fullName:user.name,
email: user.email,
gender: "",
birthdate: "",
preferredLanguage: "",
Expand Down

0 comments on commit e2bab33

Please sign in to comment.