import type mongoose from 'mongoose'; import { type Model, type Document, Schema } from 'mongoose'; import { getOrCreateModel } from '~/server/util/mongoose-utils'; const DAYS_UNTIL_EXPIRATION = 30; const generateExpirationDate = (): Date => { const currentDate = new Date(); const expirationDate = new Date(currentDate.setDate(currentDate.getDate() + DAYS_UNTIL_EXPIRATION)); return expirationDate; }; interface ThreadRelation { userId: mongoose.Types.ObjectId; threadId: string; expiredAt: Date; } interface ThreadRelationDocument extends ThreadRelation, Document { updateThreadExpiration(): Promise; } interface ThreadRelationModel extends Model { upsertThreadRelation(userId: string, threadId: string): Promise; getThreadRelation(userId: string, threadId: string): Promise } const schema = new Schema({ userId: { type: Schema.Types.ObjectId, ref: 'User', required: true, }, threadId: { type: String, required: true, unique: true, }, expiredAt: { type: Date, default: generateExpirationDate, required: true, }, }); schema.methods.updateThreadExpiration = async function(): Promise { this.expiredAt = generateExpirationDate(); await this.save(); }; export default getOrCreateModel('ThreadRelation', schema);