-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from SprintCV-S24/sr-jg-models
Models and skills route
- Loading branch information
Showing
44 changed files
with
3,551 additions
and
38 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
import { | ||
ActivitiesModel, | ||
type ActivitiesType, | ||
} from "../models/activities.model"; | ||
import { ResumeModel } from "../models/resume.model"; | ||
import mongoose from "mongoose"; | ||
import { HttpError, HttpStatus, checkMongooseErrors } from "../utils/errors"; | ||
import { checkDuplicateItemName } from "../utils/checkDuplicates"; | ||
|
||
export const createActivity = async (activitiesFields: ActivitiesType) => { | ||
try { | ||
if (await checkDuplicateItemName(activitiesFields.itemName)) { | ||
throw new HttpError(HttpStatus.BAD_REQUEST, "Duplicate item name"); | ||
} | ||
|
||
const newActivity = new ActivitiesModel(activitiesFields); | ||
await newActivity.save(); | ||
return newActivity; | ||
} catch (err: unknown) { | ||
if (err instanceof HttpError) { | ||
throw err; | ||
} | ||
|
||
checkMongooseErrors(err); | ||
|
||
throw new HttpError( | ||
HttpStatus.INTERNAL_SERVER_ERROR, | ||
"Activity creation failed", | ||
{ cause: err }, | ||
); | ||
} | ||
}; | ||
|
||
export const getAllActivities = async (user: string) => { | ||
try { | ||
const activities = await ActivitiesModel.find({ user: user }); | ||
return activities; | ||
} catch (err: unknown) { | ||
//rethrow any errors as HttpErrors | ||
if (err instanceof HttpError) { | ||
throw err; | ||
} | ||
//checks if mongoose threw and will rethrow with appropriate status code and message | ||
checkMongooseErrors(err); | ||
|
||
throw new HttpError( | ||
HttpStatus.INTERNAL_SERVER_ERROR, | ||
"Activities retrieval failed", | ||
{ cause: err }, | ||
); | ||
} | ||
}; | ||
|
||
export const getActivityById = async (user: string, activityId: string) => { | ||
try { | ||
const activity = await ActivitiesModel.findOne({ | ||
user: user, | ||
_id: activityId, | ||
}); | ||
return activity; | ||
} catch (err: unknown) { | ||
//rethrow any errors as HttpErrors | ||
if (err instanceof HttpError) { | ||
throw err; | ||
} | ||
//checks if mongoose threw and will rethrow with appropriate status code and message | ||
checkMongooseErrors(err); | ||
|
||
throw new HttpError( | ||
HttpStatus.INTERNAL_SERVER_ERROR, | ||
"Activity retrieval failed", | ||
{ cause: err }, | ||
); | ||
} | ||
}; | ||
|
||
export const updateActivity = async ( | ||
user: string, | ||
activityId: string, | ||
activitiesFields: ActivitiesType, | ||
) => { | ||
try { | ||
if (!activityId) { | ||
throw new HttpError( | ||
HttpStatus.BAD_REQUEST, | ||
"Missing activity ID for update", | ||
); | ||
} | ||
|
||
if (await checkDuplicateItemName(activitiesFields.itemName, activityId)) { | ||
throw new HttpError(HttpStatus.BAD_REQUEST, "Duplicate item name"); | ||
} | ||
|
||
const updatedActivity = await ActivitiesModel.findOneAndUpdate( | ||
{ _id: activityId, user: user }, // Query to match the document by _id and user | ||
{ $set: activitiesFields }, // Update operation | ||
{ new: true, runValidators: true }, // Options: return the updated document and run schema validators | ||
); | ||
return updatedActivity; | ||
} catch (err: unknown) { | ||
//rethrow any errors as HttpErrors | ||
if (err instanceof HttpError) { | ||
throw err; | ||
} | ||
//checks if mongoose threw and will rethrow with appropriate status code and message | ||
checkMongooseErrors(err); | ||
|
||
throw new HttpError( | ||
HttpStatus.INTERNAL_SERVER_ERROR, | ||
"Activity update failed", | ||
{ cause: err }, | ||
); | ||
} | ||
}; | ||
|
||
export const deleteActivity = async (user: string, activityId: string) => { | ||
try { | ||
await ResumeModel.updateMany( | ||
{ itemIds: new mongoose.Types.ObjectId(activityId) }, | ||
{ $pull: { itemIds: new mongoose.Types.ObjectId(activityId) } } | ||
); | ||
|
||
const deletedActivity = await ActivitiesModel.findOneAndDelete({ | ||
_id: activityId, | ||
user: user, | ||
}); | ||
if (!deletedActivity) { | ||
throw new HttpError( | ||
HttpStatus.NOT_FOUND, | ||
"Activity not found or already deleted", | ||
); | ||
} | ||
return { message: "Activity deleted successfully" }; | ||
} catch (err: unknown) { | ||
//rethrow any errors as HttpErrors | ||
if (err instanceof HttpError) { | ||
throw err; | ||
} | ||
//checks if mongoose threw and will rethrow with appropriate status code and message | ||
checkMongooseErrors(err); | ||
|
||
throw new HttpError( | ||
HttpStatus.INTERNAL_SERVER_ERROR, | ||
"Activity deletion failed", | ||
{ cause: err }, | ||
); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
import { EducationModel, type EducationType } from "../models/education.model"; | ||
import { ResumeModel } from "../models/resume.model"; | ||
import mongoose from "mongoose"; | ||
import { HttpError, HttpStatus, checkMongooseErrors } from "../utils/errors"; | ||
import { checkDuplicateItemName } from "../utils/checkDuplicates"; | ||
|
||
export const createEducation = async (educationFields: EducationType) => { | ||
try { | ||
if (await checkDuplicateItemName(educationFields.itemName)) { | ||
throw new HttpError(HttpStatus.BAD_REQUEST, "Duplicate item name"); | ||
} | ||
|
||
const newEducation = new EducationModel(educationFields); | ||
await newEducation.save(); | ||
return newEducation; | ||
} catch (err: unknown) { | ||
if (err instanceof HttpError) { | ||
throw err; | ||
} | ||
|
||
checkMongooseErrors(err); | ||
|
||
throw new HttpError( | ||
HttpStatus.INTERNAL_SERVER_ERROR, | ||
"Education creation failed", | ||
{ cause: err }, | ||
); | ||
} | ||
}; | ||
|
||
export const getAllEducation = async (user: string) => { | ||
try { | ||
const education = await EducationModel.find({ user: user }); | ||
return education; | ||
} catch (err: unknown) { | ||
//rethrow any errors as HttpErrors | ||
if (err instanceof HttpError) { | ||
throw err; | ||
} | ||
//checks if mongoose threw and will rethrow with appropriate status code and message | ||
checkMongooseErrors(err); | ||
|
||
throw new HttpError( | ||
HttpStatus.INTERNAL_SERVER_ERROR, | ||
"Education retrieval failed", | ||
{ cause: err }, | ||
); | ||
} | ||
}; | ||
|
||
export const getEducationById = async (user: string, educationId: string) => { | ||
try { | ||
const education = await EducationModel.findOne({ | ||
user: user, | ||
_id: educationId, | ||
}); | ||
return education; | ||
} catch (err: unknown) { | ||
//rethrow any errors as HttpErrors | ||
if (err instanceof HttpError) { | ||
throw err; | ||
} | ||
//checks if mongoose threw and will rethrow with appropriate status code and message | ||
checkMongooseErrors(err); | ||
|
||
throw new HttpError( | ||
HttpStatus.INTERNAL_SERVER_ERROR, | ||
"Education retrieval failed", | ||
{ cause: err }, | ||
); | ||
} | ||
}; | ||
|
||
export const updateEducation = async ( | ||
user: string, | ||
educationId: string, | ||
educationFields: EducationType, | ||
) => { | ||
try { | ||
if (!educationId) { | ||
throw new HttpError( | ||
HttpStatus.BAD_REQUEST, | ||
"Missing education ID for update", | ||
); | ||
} | ||
|
||
if (await checkDuplicateItemName(educationFields.itemName, educationId)) { | ||
throw new HttpError(HttpStatus.BAD_REQUEST, "Duplicate item name"); | ||
} | ||
|
||
const updatedEducation = await EducationModel.findOneAndUpdate( | ||
{ _id: educationId, user: user }, // Query to match the document by _id and user | ||
{ $set: educationFields }, // Update operation | ||
{ new: true, runValidators: true }, // Options: return the updated document and run schema validators | ||
); | ||
return updatedEducation; | ||
} catch (err: unknown) { | ||
//rethrow any errors as HttpErrors | ||
if (err instanceof HttpError) { | ||
throw err; | ||
} | ||
//checks if mongoose threw and will rethrow with appropriate status code and message | ||
checkMongooseErrors(err); | ||
|
||
throw new HttpError( | ||
HttpStatus.INTERNAL_SERVER_ERROR, | ||
"Education update failed", | ||
{ cause: err }, | ||
); | ||
} | ||
}; | ||
|
||
export const deleteEducation = async (user: string, educationId: string) => { | ||
try { | ||
await ResumeModel.updateMany( | ||
{ itemIds: new mongoose.Types.ObjectId(educationId) }, | ||
{ $pull: { itemIds: new mongoose.Types.ObjectId(educationId) } } | ||
); | ||
|
||
const deletedEducation = await EducationModel.findOneAndDelete({ | ||
_id: educationId, | ||
user: user, | ||
}); | ||
if (!deletedEducation) { | ||
throw new HttpError( | ||
HttpStatus.NOT_FOUND, | ||
"Education not found or already deleted", | ||
); | ||
} | ||
return { message: "Education deleted successfully" }; | ||
} catch (err: unknown) { | ||
//rethrow any errors as HttpErrors | ||
if (err instanceof HttpError) { | ||
throw err; | ||
} | ||
//checks if mongoose threw and will rethrow with appropriate status code and message | ||
checkMongooseErrors(err); | ||
|
||
throw new HttpError( | ||
HttpStatus.INTERNAL_SERVER_ERROR, | ||
"Education deletion failed", | ||
{ cause: err }, | ||
); | ||
} | ||
}; |
Oops, something went wrong.