Skip to content

Commit 3c1cefd

Browse files
berniceuuwituzeb
andauthored
#217 Ft update profile (#157)
* update schema to include bio and manager * setup image uploading --------- Co-authored-by: uwituzeb <[email protected]>
1 parent 8ef534d commit 3c1cefd

File tree

8 files changed

+83
-6
lines changed

8 files changed

+83
-6
lines changed

Diff for: package.json

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"axios": "^1.7.7",
4343
"bcrypt": "^5.1.1",
4444
"bcryptjs": "^2.4.3",
45+
"cloudinary": "^2.5.1",
4546
"dotenv": "^16.0.2",
4647
"google-auth-library": "^8.7.0",
4748
"googleapis": "^108.0.1",

Diff for: src/config/cloudinary.ts

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { v2 as cloudinary } from 'cloudinary';
2+
3+
cloudinary.config({
4+
cloud_name: process.env.CLOUDINARY_CLOUD_NAME,
5+
api_key: process.env.CLOUDINARY_API_KEY,
6+
api_secret: process.env.CLOUDINARY_API_SECRET,
7+
})
8+
9+
export default cloudinary;

Diff for: src/models/AuthUser.ts

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ const userSchema = new Schema(
1313
type: String,
1414
default: process.env.DEFAULT_AVATAR,
1515
},
16+
bio: {
17+
type: String,
18+
default: "",
19+
},
1620
isVerified:{
1721
type:Boolean,
1822
default:false

Diff for: src/models/cohortModel.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ export const cohortModels = mongoose.model('cohortModel',
3232
trainees: [{
3333
type: Schema.Types.ObjectId,
3434
ref: "Trainees",
35-
}]
35+
}],
36+
manager: {
37+
type: String,
38+
}
3639
})
3740
)

Diff for: src/resolvers/loginUserResolver.ts

+28-5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { sessionModel } from '../models/session';
1313
import { cohortModels } from '../models/cohortModel';
1414
import TraineeApplicant from '../models/traineeApplicant';
1515
import { publishNotification } from './adminNotificationsResolver';
16+
import { uploadImage } from '../utils/uploadImage';
1617

1718
const FrontendUrl = process.env.FRONTEND_URL
1819

@@ -26,15 +27,17 @@ export const loggedUserResolvers: any = {
2627

2728
const userWithRole = await LoggedUserModel.findById(
2829
ctx.currentUser._id
29-
).populate("role");
30+
).populate("role")
31+
.populate('cohort')
3032

3133
const userId = userWithRole?._id.toString();
3234

3335
if (userId !== id) {
3436
throw new AuthenticationError("Unauthorized to update user.");
3537
}
36-
const upvalue = await LoggedUserModel.findById(id).populate("role");
37-
return upvalue;
38+
// const upvalue = await LoggedUserModel.findById(id).populate("role");
39+
// return upvalue;
40+
return userWithRole
3841
},
3942
async getUsers_Logged(_: any, args: any, ctx: any, amount: any) {
4043
const users = await LoggedUserModel.find()
@@ -94,6 +97,7 @@ export const loggedUserResolvers: any = {
9497
password,
9598
role,
9699
applicationPhase,
100+
bio,
97101
},
98102
}: any,
99103
ctx: any
@@ -135,6 +139,7 @@ export const loggedUserResolvers: any = {
135139
country,
136140
role,
137141
applicationPhase: applicationPhase || "Applied",
142+
bio,
138143
});
139144
const res: any = await createdUser.save();
140145
await sendEmailTemplate(
@@ -170,6 +175,7 @@ export const loggedUserResolvers: any = {
170175
email,
171176
code,
172177
password,
178+
bio,
173179
});
174180
if (error) {
175181
throw new Error(
@@ -228,6 +234,7 @@ export const loggedUserResolvers: any = {
228234
gender,
229235
country,
230236
role: role._id.toString(),
237+
bio,
231238
});
232239

233240
const res: any = await createdUser.save();
@@ -373,6 +380,7 @@ export const loggedUserResolvers: any = {
373380
picture,
374381
code,
375382
password,
383+
bio,
376384
},
377385
}: any,
378386
ctx: any
@@ -382,9 +390,23 @@ export const loggedUserResolvers: any = {
382390
lastname,
383391
email,
384392
telephone,
385-
picture,
386393
code,
394+
bio,
387395
};
396+
397+
if(picture){
398+
try {
399+
const uploadResult = await uploadImage(
400+
picture,
401+
'profile_pictures',
402+
`user_${ID}_profile`
403+
);
404+
updateData.picture = uploadResult.url;
405+
406+
} catch(err){
407+
throw new Error('Failed to upload picture');
408+
}
409+
}
388410
if (!ctx.currentUser) {
389411
throw new AuthenticationError("You must be logged in");
390412
}
@@ -414,7 +436,7 @@ export const loggedUserResolvers: any = {
414436

415437
async updateUserSelf(
416438
_: any,
417-
{ ID, editUserInput: { firstname, lastname, gender, code, country, telephone, picture } }: any, ctx: any) {
439+
{ ID, editUserInput: { firstname, lastname, gender, code, country, telephone, picture, bio } }: any, ctx: any) {
418440

419441
if (!ctx.currentUser) {
420442
throw new AuthenticationError('You must be logged in');
@@ -431,6 +453,7 @@ export const loggedUserResolvers: any = {
431453
if (country) updateFields.country = country;
432454
if (telephone) updateFields.telephone = telephone;
433455
if (picture) updateFields.picture = picture;
456+
if(bio) updateFields.bio = bio;
434457

435458
const wasEdited = (
436459
await LoggedUserModel.updateOne(

Diff for: src/schema/cohortScheme.ts

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ export const cohortSchema = gql`
1010
end: String
1111
phase: String
1212
trainees:[traineeApplicant]
13+
manager: String
14+
1315
1416
}
1517
@@ -25,6 +27,7 @@ export const cohortSchema = gql`
2527
start: String!
2628
end: String!
2729
phase: String!
30+
manager: String
2831
}
2932
input updateCohortInput {
3033
title: String
@@ -33,6 +36,7 @@ export const cohortSchema = gql`
3336
start: String
3437
end: String
3538
phase: String
39+
manager: String
3640
}
3741
3842
type Mutation {

Diff for: src/schema/loggedUser.ts

+5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export const LoggedUserSchema = gql`
1919
isActive: Boolean,
2020
applicationPhase: String,
2121
cohort: Cohort
22+
bio: String,
2223
2324
isVerified:Boolean
2425
}
@@ -39,6 +40,7 @@ export const LoggedUserSchema = gql`
3940
end: String
4041
phase: Int
4142
trainees:[User_Logged!]
43+
manager: String
4244
}
4345
4446
type CurrentUser{
@@ -59,6 +61,7 @@ export const LoggedUserSchema = gql`
5961
country: String
6062
role: String
6163
applicationPhase: String
64+
bio: String
6265
}
6366
input EditUserSelfInput_Logged {
6467
firstname: String
@@ -68,6 +71,7 @@ export const LoggedUserSchema = gql`
6871
telephone: String
6972
gender: String
7073
country: String
74+
bio: String
7175
}
7276
input EditUserInput_Logged {
7377
firstname: String
@@ -79,6 +83,7 @@ export const LoggedUserSchema = gql`
7983
picture:String
8084
applicationPhase: String
8185
cohortId: ID
86+
bio: String
8287
}
8388
input EmailInput {
8489
email: String

Diff for: src/utils/uploadImage.ts

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import cloudinary from "../config/cloudinary";
2+
3+
export const uploadImage = async (
4+
base64Image: string,
5+
folder: string = 'profile_picture',
6+
public_id?: string
7+
) => {
8+
try{
9+
const options = {
10+
folder,
11+
public_id,
12+
allowed_formats: ['jpg', 'png', 'jpeg', 'gif'],
13+
transformation: [
14+
{width: 500, height: 500, crop: 'limit'},
15+
{quality: 'auto'}
16+
]
17+
};
18+
19+
const result = await cloudinary.uploader.upload(base64Image, options);
20+
return {
21+
url: result.secure_url,
22+
public_id: result.public_id
23+
}
24+
} catch(err){
25+
console.error('Cloudinary upload error:', err);
26+
throw new Error('Failed to upload image to Cloudinary');
27+
}
28+
}

0 commit comments

Comments
 (0)