Skip to content

Commit

Permalink
api created for instructor profile
Browse files Browse the repository at this point in the history
  • Loading branch information
abinth11 committed Jul 22, 2023
1 parent 354e4d8 commit 3144502
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 6 deletions.
2 changes: 2 additions & 0 deletions server/src/adapters/controllers/instructorController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ const instructorController = (

})



return {
getInstructorRequests,
verifyInstructor,
Expand Down
11 changes: 9 additions & 2 deletions server/src/app/repositories/instructorDbRepository.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { InstructorRepositoryMongoDb } from '@src/frameworks/database/mongodb/repositories/instructorRepoMongoDb';
import { InstructorInterface } from '@src/types/instructorInterface';
import { SavedInstructorInterface } from '@src/types/instructorInterface';

export const instructorDbRepository = (
repository: ReturnType<InstructorRepositoryMongoDb>
Expand Down Expand Up @@ -38,7 +39,11 @@ export const instructorDbRepository = (
const getInstructorById = async (instructorId: string) =>
await repository.getInstructorById(instructorId);

const getTotalNumberOfInstructors = async ()=> repository.getTotalNumberOfInstructors()
const getTotalNumberOfInstructors = async ()=> await repository.getTotalNumberOfInstructors()

const changePassword = async (id:string,password:string)=> await repository.changePassword(id,password)

const updateProfile = async (id:string,instructorInfo:SavedInstructorInterface)=> await repository.updateProfile(id,instructorInfo)

return {
addInstructor,
Expand All @@ -52,7 +57,9 @@ export const instructorDbRepository = (
unblockInstructors,
getBlockedInstructors,
getInstructorById,
getTotalNumberOfInstructors
getTotalNumberOfInstructors,
changePassword,
updateProfile
};
};

Expand Down
2 changes: 1 addition & 1 deletion server/src/app/usecases/auth/instructorAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const instructorRegister = async (
if (files) {
files.map((file) => {
if (file.originalname === 'profilePic') {
instructor.profilePic = file.path;
instructor.profilePic.url = file.path;
} else {
const certificate = {
name:file.originalname,
Expand Down
72 changes: 72 additions & 0 deletions server/src/app/usecases/instructor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { InstructorDbInterface } from '../repositories/instructorDbRepository';
import AppError from '../../utils/appError';
import HttpStatusCodes from '../../constants/HttpStatusCodes';
import { AuthServiceInterface } from '../services/authServicesInterface';
import { SavedInstructorInterface } from '@src/types/instructorInterface';
import { CloudServiceInterface } from '../services/cloudServiceInterface';

export const changePasswordU = async (
id: string | undefined,
password: { currentPassword: string; newPassword: string },
authService: ReturnType<AuthServiceInterface>,
instructorDbRepository: ReturnType<InstructorDbInterface>
) => {
if (!id) {
throw new AppError('Invalid student', HttpStatusCodes.BAD_REQUEST);
}
if (!password.currentPassword) {
throw new AppError(
'Please provide current password',
HttpStatusCodes.BAD_REQUEST
);
}
const instructor:SavedInstructorInterface|null = await instructorDbRepository.getInstructorById(
id
);
if (!instructor) {
throw new AppError('Unauthorized user', HttpStatusCodes.NOT_FOUND);
}
const isPasswordCorrect = await authService.comparePassword(
password.currentPassword,
instructor?.password
);
if (!isPasswordCorrect) {
throw new AppError(
'Sorry, your current password is incorrect.',
HttpStatusCodes.BAD_REQUEST
);
}
if (!password.newPassword) {
throw new AppError(
'new password cannot be empty',
HttpStatusCodes.UNAUTHORIZED
);
}
const hashedPassword = await authService.hashPassword(password.newPassword);
await instructorDbRepository.changePassword(id, hashedPassword);
};

export const updateProfileU = async (
id: string | undefined,
instructorInfo: SavedInstructorInterface,
profilePic: Express.Multer.File,
cloudService: ReturnType<CloudServiceInterface>,
instructorDbRepository: ReturnType<InstructorDbInterface>
) => {
if (!id) {
throw new AppError('Invalid instructor', HttpStatusCodes.BAD_REQUEST);
}
if (Object.keys(instructorInfo).length === 0) {
throw new AppError(
'At least update a single field',
HttpStatusCodes.BAD_REQUEST
);
}
if (profilePic) {
const response = await cloudService.upload(profilePic);
instructorInfo.profilePic = response;
}
await instructorDbRepository.updateProfile(id, instructorInfo);
};


3 changes: 3 additions & 0 deletions server/src/app/usecases/management/instructorManagement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,5 +117,8 @@ export const getInstructorByIdUseCase = async (
throw new AppError('Invalid instructor id', HttpStatusCodes.BAD_REQUEST);
}
const instructor = await instructorRepository.getInstructorById(instructorId);
if (instructor) {
instructor.password = 'no password';
}
return instructor;
};
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export const instructorRepoMongoDb = () => {
};

const getInstructorById = async (instructorId: string) => {
const instructor = await Instructor.findOne({
const instructor:SavedInstructorInterface|null = await Instructor.findOne({
_id: new mongoose.Types.ObjectId(instructorId)
});
return instructor;
Expand All @@ -107,6 +107,19 @@ export const instructorRepoMongoDb = () => {
return total;
};

const changePassword = async (id: string, password: string) => {
await Instructor.updateOne(
{ _id: new mongoose.Types.ObjectId(id) },
{ password }
);
};

const updateProfile = async (id: string, instructorInfo: SavedInstructorInterface) => {
await Instructor.updateOne(
{ _id: new mongoose.Types.ObjectId(id) },
{ ...instructorInfo }
);
};
return {
addInstructor,
getInstructorByEmail,
Expand All @@ -119,7 +132,9 @@ export const instructorRepoMongoDb = () => {
unblockInstructors,
getBlockedInstructors,
getInstructorById,
getTotalNumberOfInstructors
getTotalNumberOfInstructors,
changePassword,
updateProfile
};
};

Expand Down
6 changes: 5 additions & 1 deletion server/src/types/instructorInterface.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
export interface InstructorInterface {
firstName:string;
lastName:string;
profilePic:string;
profilePic:{
name:string;
key?:string;
url?:string;
};
email: string;
mobile:number;
qualifications:string;
Expand Down

0 comments on commit 3144502

Please sign in to comment.