import mongoose from "mongoose" ; import Counter from "./counter" ; export interface AnnouncementModel extends mongoose . Document { id : number ; contestId : number ; title : string ; content : string ; priority : number ; createdAt : Date ; createdBy : number ; updatedAt : Date ; updatedBy : number ; } const announcementSchema = new mongoose . Schema < AnnouncementModel > ( { id : { type : Number , unique : true } , contestId : { type : Number , required : true } , title : { type : String , required : true } , content : { type : String , required : true } , priority : { type : Number , required : true , default : 1 } , createdAt : { type : Date , default : Date . now } , createdBy : Number , updatedAt : { type : Date , default : Date . now } , updatedBy : Number , } , { collection : "announcements" , } ) ; announcementSchema . pre < AnnouncementModel > ( "save" , function ( next ) { Counter . findByIdAndUpdate ( "announcement" , { $inc : { count : 1 } } , { new : true , upsert : true } , ( err , counter ) => { if ( err ) { return next ( err ) ; } this . id = counter . count ; next ( ) ; } ) ; } ) ; export default mongoose . model < AnnouncementModel > ( "Announcement" , announcementSchema ) ;