mongoose#ObjectId TypeScript Examples
The following examples show how to use
mongoose#ObjectId.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: certification.model.ts From frontend.ro with MIT License | 6 votes |
CertificationSchema = new mongoose.Schema<CertificationI>({
user: { type: mongoose.Schema.Types.ObjectId, ref: 'User', required: true },
timestamp: { type: Number, required: true },
tutorial: { type: mongoose.Schema.Types.ObjectId, ref: 'Tutorial' },
lesson_exercises: [{ type: mongoose.Schema.Types.ObjectId, ref: 'LessonExercise' }],
og_image: { type: String, required: false },
pdf: { type: String, required: false }
}, {
timestamps: true
})
Example #2
Source File: certification.model.ts From frontend.ro with MIT License | 5 votes |
/**
* Store a certification into the Database and trigger a Lambda Function
* to generate it's corresponding assets: PDF and JPEG.
* > NOTE: this function is somewhat idempotent - in the sense that
* > multiple calls to this function don't create multiple Certifications,
* > instead they update the same one.
* @param userId string
* @param tutorial_id ObjectId
* @param dryRun boolean - whether to actually perform the tasks, or simply to log the result, without any side effects
* @returns
*/
async function createCertification(
userId: string,
tutorial_id: ObjectId,
dryRun = false
): Promise<mongoose.Document<any, any, CertificationI> & CertificationI> {
const SPAN = `[createCertification, userId=${userId}, tutorial_id=${tutorial_id}, dryRun=${dryRun}]`;
const tutorial = await Tutorial
.findById(tutorial_id)
.populate<{ lessons: LessonI[] }>("lessons");
if (tutorial === null) {
console.error(`${SPAN} couldn't find tutorial. Exiting.`);
return;
}
let certification: mongoose.Document<any, any, CertificationI> & CertificationI;
// Step 1: store certification into the DB
try {
certification = await storeCertificationData(tutorial, userId, dryRun);
} catch (err) {
console.error(`${SPAN} Certification couldn't be saved in DB`, err);
return;
}
try {
certification = await refreshCertificationAssets(certification, dryRun);
} catch (err) {
console.error(`${SPAN} Certification assets couldn't be generated.`, err);
return;
}
console.info(`${SPAN} successfully created certification`);
return certification;
}
Example #3
Source File: certification.model.ts From frontend.ro with MIT License | 5 votes |
/**
* Create or Update (if already exists) certification information into the Database.
* @param userId string
* @param tutorial_id ObjectId
* @param dryRun boolean - whether to actually perform the tasks, or simply to log the result, without any side effects
* @returns Persisted Certification
*/
async function storeCertificationData(
tutorial: WIPPopulatedTutorialI,
userId: string,
dryRun = false
) {
const SPAN = `[storeCertificationData, userId=${userId}, tutorial_id=${tutorial._id}, dryRun=${dryRun}]`;
let certification = await Certification.findOne({ tutorial: tutorial._id });
if (certification !== null) {
console.info(`${SPAN} Certification already exists. We'll update it!`)
certification.timestamp = Date.now();
} else {
certification = new Certification({
tutorial: tutorial._id,
user: userId,
timestamp: Date.now(),
lesson_exercises: [],
_id: new mongoose.Types.ObjectId(),
});
}
const lessonIds = (tutorial.lessons as LessonI[]).map((lesson) => lesson.lessonId);
const userSubmissions: WIPPopulatedSubmissionI[] = await SubmissionModel.getAllUserSubmissions(userId);
certification.lesson_exercises = userSubmissions
.filter(submission => submission.status === SubmissionStatus.DONE)
.filter(submission => lessonIds.includes(submission.exercise.lesson))
.map(submission => submission.exercise._id.toString());
if (!dryRun) {
certification = await certification.save();
}
console.info(`${SPAN} successfully persisted certification.`);
return certification;
}
Example #4
Source File: auth.service.ts From uniauth-backend with MIT License | 5 votes |
async generateJwt(jwtData: { id: ObjectId; email: string }): Promise<string> {
const token = await this.jwtService.signAsync(jwtData, newJWTConstants);
return token;
}