2022-04-05 16:50:35 +00:00
|
|
|
import mongoose from "mongoose";
|
2022-04-05 17:00:55 +00:00
|
|
|
import modelNameRegister from "./modelNameRegister.js";
|
2022-04-05 16:50:35 +00:00
|
|
|
|
|
|
|
const Types = mongoose.Schema.Types;
|
|
|
|
|
|
|
|
const rentalSchema = new mongoose.Schema({
|
|
|
|
title: { type: String, required: true, trim: true },
|
|
|
|
rate: { type: String, required: true, trim: true },
|
|
|
|
description: { type: String, required: true },
|
|
|
|
contact: { type: String, required: true },
|
|
|
|
createDate: { type: Date, required: true, default: Date.now },
|
|
|
|
creator: { type: Types.ObjectId, ref: modelNameRegister.User }
|
|
|
|
});
|
|
|
|
|
|
|
|
rentalSchema.pre("remove", async function (next) {
|
|
|
|
const rental = this;
|
|
|
|
const rentalInd = rental.creator.createdRentals.indexOf(rental._id);
|
|
|
|
rental.creator.createdRentals.splice(rentalInd, 1);
|
|
|
|
await rental.save();
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
|
|
|
|
export default mongoose.model(modelNameRegister.Rental, rentalSchema);
|