Spaces:
Sleeping
Sleeping
Download backend/src/models/Document.js from edichat/edi.chat.test: direct link, hf CLI and curl.
- Browser
- Download file 5.33 kB
-
https://huggingface.co/spaces/edichat/edi.chat.test/resolve/main/backend/src/models/Document.js
- Command line
-
hf download hf://spaces/edichat/edi.chat.test/backend/src/models/Document.js
-
curl -L -o Document.js https://huggingface.co/spaces/edichat/edi.chat.test/resolve/main/backend/src/models/Document.js
5.33 kB
| const mongoose = require('mongoose'); | |
| const dueDateSchema = new mongoose.Schema({ | |
| title: { | |
| type: String, | |
| required: true, | |
| trim: true | |
| }, | |
| dueDate: { | |
| type: Date, | |
| required: true | |
| }, | |
| description: { | |
| type: String, | |
| trim: true | |
| }, | |
| type: { | |
| type: String, | |
| enum: ['assignment', 'exam', 'project', 'quiz', 'presentation', 'reading', 'other'], | |
| default: 'assignment' | |
| }, | |
| course: { | |
| type: String, | |
| trim: true | |
| }, | |
| extractedText: { | |
| type: String, | |
| trim: true | |
| }, | |
| confidence: { | |
| type: Number, | |
| min: 0, | |
| max: 1, | |
| default: 0.5 | |
| } | |
| }, { | |
| timestamps: true | |
| }); | |
| const documentSchema = new mongoose.Schema({ | |
| // Document metadata | |
| filename: { | |
| type: String, | |
| required: true, | |
| trim: true | |
| }, | |
| originalName: { | |
| type: String, | |
| required: true, | |
| trim: true | |
| }, | |
| mimeType: { | |
| type: String, | |
| required: true | |
| }, | |
| size: { | |
| type: Number, | |
| required: true | |
| }, | |
| // File storage | |
| filePath: { | |
| type: String, | |
| required: true | |
| }, | |
| // Document content | |
| extractedText: { | |
| type: String, | |
| required: true | |
| }, | |
| // Document classification | |
| type: { | |
| type: String, | |
| enum: ['syllabus', 'assignment', 'reading', 'schedule', 'other'], | |
| default: 'other' | |
| }, | |
| // Course information | |
| courseCode: { | |
| type: String, | |
| trim: true | |
| }, | |
| courseName: { | |
| type: String, | |
| trim: true | |
| }, | |
| semester: { | |
| type: String, | |
| trim: true | |
| }, | |
| year: { | |
| type: Number | |
| }, | |
| professor: { | |
| type: String, | |
| trim: true | |
| }, | |
| // Extracted due dates | |
| dueDates: [dueDateSchema], | |
| // Processing status | |
| processingStatus: { | |
| type: String, | |
| enum: ['pending', 'processing', 'completed', 'failed'], | |
| default: 'pending' | |
| }, | |
| processingErrors: [String], | |
| // AI analysis | |
| aiSummary: { | |
| type: String, | |
| trim: true | |
| }, | |
| aiTags: [String], | |
| // User association | |
| userId: { | |
| type: mongoose.Schema.Types.ObjectId, | |
| ref: 'User', | |
| required: true | |
| }, | |
| // Visibility and access | |
| isPublic: { | |
| type: Boolean, | |
| default: false | |
| }, | |
| sharedWith: [{ | |
| type: mongoose.Schema.Types.ObjectId, | |
| ref: 'User' | |
| }], | |
| // Usage tracking | |
| viewCount: { | |
| type: Number, | |
| default: 0 | |
| }, | |
| lastAccessed: { | |
| type: Date, | |
| default: Date.now | |
| } | |
| }, { | |
| timestamps: true, | |
| toJSON: { | |
| transform: function(doc, ret) { | |
| // Don't expose file path in API responses for security | |
| delete ret.filePath; | |
| return ret; | |
| } | |
| } | |
| }); | |
| // Indexes for efficient querying | |
| documentSchema.index({ userId: 1, createdAt: -1 }); | |
| documentSchema.index({ type: 1 }); | |
| documentSchema.index({ courseCode: 1, semester: 1, year: 1 }); | |
| documentSchema.index({ 'dueDates.dueDate': 1 }); | |
| documentSchema.index({ processingStatus: 1 }); | |
| // Text search index | |
| documentSchema.index({ | |
| extractedText: 'text', | |
| aiSummary: 'text', | |
| courseName: 'text', | |
| originalName: 'text' | |
| }); | |
| // Virtual for upcoming due dates | |
| documentSchema.virtual('upcomingDueDates').get(function() { | |
| const now = new Date(); | |
| return this.dueDates | |
| .filter(dd => dd.dueDate > now) | |
| .sort((a, b) => a.dueDate - b.dueDate); | |
| }); | |
| // Method to add a due date | |
| documentSchema.methods.addDueDate = function(dueDateData) { | |
| this.dueDates.push(dueDateData); | |
| return this.save(); | |
| }; | |
| // Method to update processing status | |
| documentSchema.methods.updateProcessingStatus = function(status, errors = []) { | |
| this.processingStatus = status; | |
| if (errors.length > 0) { | |
| this.processingErrors = errors; | |
| } | |
| return this.save(); | |
| }; | |
| // Method to increment view count | |
| documentSchema.methods.incrementViewCount = function() { | |
| this.viewCount += 1; | |
| this.lastAccessed = new Date(); | |
| return this.save(); | |
| }; | |
| // Static method to find documents by user | |
| documentSchema.statics.findByUser = function(userId, options = {}) { | |
| const query = { userId }; | |
| if (options.type) { | |
| query.type = options.type; | |
| } | |
| if (options.courseCode) { | |
| query.courseCode = options.courseCode; | |
| } | |
| if (options.semester && options.year) { | |
| query.semester = options.semester; | |
| query.year = options.year; | |
| } | |
| return this.find(query).sort({ createdAt: -1 }); | |
| }; | |
| // Static method to find upcoming due dates for a user | |
| documentSchema.statics.findUpcomingDueDates = function(userId, days = 30) { | |
| // Validate ObjectId format | |
| if (!mongoose.Types.ObjectId.isValid(userId)) { | |
| throw new Error('Invalid user ID format'); | |
| } | |
| const now = new Date(); | |
| const futureDate = new Date(); | |
| futureDate.setDate(now.getDate() + days); | |
| return this.aggregate([ | |
| { $match: { userId: new mongoose.Types.ObjectId(userId) } }, | |
| { $unwind: '$dueDates' }, | |
| { | |
| $match: { | |
| 'dueDates.dueDate': { | |
| $gte: now, | |
| $lte: futureDate | |
| } | |
| } | |
| }, | |
| { $sort: { 'dueDates.dueDate': 1 } }, | |
| { | |
| $project: { | |
| _id: '$dueDates._id', | |
| title: '$dueDates.title', | |
| dueDate: '$dueDates.dueDate', | |
| description: '$dueDates.description', | |
| type: '$dueDates.type', | |
| course: '$dueDates.course', | |
| confidence: '$dueDates.confidence', | |
| documentId: '$_id', | |
| documentName: '$originalName', | |
| courseCode: '$courseCode', | |
| courseName: '$courseName' | |
| } | |
| } | |
| ]); | |
| }; | |
| module.exports = mongoose.model('Document', documentSchema); |