Hi,
I need to build a db handling book rentals, if a book is not returned on time (3 days) a monetary penalty needs to be applied to the document and the book should be marked as “delayed” so it’s easy to query and find what books have not been returned on time.
I have this mongoose schema:
const rentSchema = new schema(
{
movieID: { type: Number, required: true },
customerID: { type: Number, required: true },
amount: { type: Number, required: true },
totalCost: { type: Number, required: true },
returnDate: { type: Date, default: Date.now() + 3 * 24 * 60 * 60 * 1000 },
delayed: { type: Boolean, default: false },
penalty: { type: Number },
},
{ timestamps: true }
);
But I’m unsure how I can automize this process?
Is there a way to insert data conditionally based on the returnDate somehow?
I found a similiar thread on Stackoverflow discussing this theme and the author was recommended to use virtual properties with a condition in my case it could be currentDate >= returnDate, but when I checked the documentation it says that virtual properties can’t be used for queries or to insert data into the db? So how could this be useful in this case then?
If not virtual properties, is there some other way I could deal with this?