Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added pre validation #101

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions models/Site.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const sitesSchema = new mongoose.Schema({
address:{
first_line:{
type:String,
required:[true,'Please enter your address'],
required:[true,'Please enter your address']

},
city:{
Expand Down Expand Up @@ -61,11 +61,11 @@ const sitesSchema = new mongoose.Schema({
charges_param:
{
electricity:{
type:Number,
type:Number

},
water:{
type:Number,
type:Number

}

Expand All @@ -75,7 +75,8 @@ const sitesSchema = new mongoose.Schema({
Type:{
enum:['Room','Land','Shops'],
type:String,
required:true
required:true,
trim: true
},

alloted_tenant:{
Expand All @@ -93,4 +94,13 @@ const sitesSchema = new mongoose.Schema({

},{timestamps:true});

sitesSchema.pre("save", (next) => {
if(this.status<2){
return Promise.reject("This site is not empty.")
}
else{
return next();
}
})

module.exports = mongoose.models.Site || mongoose.model("Site",sitesSchema)
18 changes: 18 additions & 0 deletions models/Transaction.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const mongoose = require("mongoose");
import { sendError } from "/helpers/help";
var Charge = require("../models/Charge")
mongoose.Promise = global.Promise;

const tranSchema = new mongoose.Schema({
Expand Down Expand Up @@ -27,4 +29,20 @@ const tranSchema = new mongoose.Schema({
}
},{timestamps:true});

tranSchema.pre('save', (next) => {
var chargeId = this.charge_id;

Charge.findById(chargeId, function(err, chargeData){
if(err) return sendError(res,err.message,500)
else{
if(chargeData.isPaid==true){
return next();
}
else{
return next("Tenant has not completed the transaction");
}
}
})
})

module.exports = mongoose.models.Transaction || mongoose.model("Transaction",tranSchema)
35 changes: 14 additions & 21 deletions models/history.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,26 +38,19 @@ const historySchema = new mongoose.Schema({
},{timestamps:true})

module.exports = mongoose.models.History || mongoose.model("History",historySchema)
/*historySchema.pre("save",(next) => {

if(this.requested('requested_at')){
this.requested_at = Date.now;
next();
}

else if(this.joined('joined_at')){
this.joined_at = Date.now;
next();
}

else if(this.left('leave_at')){
this.leave_at = Date.now;
next();
}

else{
return next();
}
historySchema.pre("save",(next) => {

var tenantId = this.tenant_id;
var siteId = this.site_id;

if(tenantId && siteId){
if(this.status<2){
return Promise.reject('New document cannot be created');
}
else{
return next();
}
}

})
*/

35 changes: 18 additions & 17 deletions models/landlord.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ const landlordSchema = new mongoose.Schema(
verification: {
type: String,
enum: ["Aadhar", "VoterID", "PanCard"],
trim: true
},
account: {
acc_num: {
Expand All @@ -80,28 +81,28 @@ const landlordSchema = new mongoose.Schema(
);

//when landlord updates password
// landlordSchema.pre('save',(next)=>{
landlordSchema.pre('save',(next)=>{

// if(!this.isModified('password')){
// return next();
// }
// const user = this;
// bcrypt.genSalt(10, function(err, salt){
// if (err){ return next(err) }
if(!this.isModified('password')){
return next();
}
const user = this;
bcrypt.genSalt(10, function(err, salt){
if (err){ return next(err) }

// bcrypt.hash(user.password, salt, null, function(err, hash){
// if(err){return next(err)}
bcrypt.hash(user.password, salt, null, function(err, hash){
if(err){return next(err)}

// else if(user.password == hash){
// return next("new password cannot be same as previous password")
// }
else if(user.password == hash){
return next("new password cannot be same as previous password")
}

// user.password = hash;
// next();
// })
// })
user.password = hash;
next();
})
})

// });
});

module.exports =
mongoose.models.Landlord || mongoose.model("Landlord", landlordSchema);
14 changes: 11 additions & 3 deletions models/tenant.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,19 @@ const tenantSchema = new mongoose.Schema({
},{timestamps:true});

//when tenant updates password
/*

tenantSchema.pre('save',(next)=>{

var user = this;
console.log(user)
//console.log(user)

var num = user.contact.toString();
if(num.length>=10){
next();
}
else{
return next("Contact number should have at least 10 digits.")
}

bcrypt.genSalt(10, function(err, salt){
if (err){ return next(err) }
Expand All @@ -110,6 +118,6 @@ tenantSchema.pre('save',(next)=>{
})

});
*/


module.exports = mongoose.models.Tenant || mongoose.model("Tenant",tenantSchema);
2 changes: 1 addition & 1 deletion pages/api/profile/acceptsite.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export default async function handler(req, res) {
})
}
else{
return sendError(res,"can't send as status is not 0 for this history",500)
return sendError(res,"Can't send as status is not 0 for this history",500)
}
}
else{
Expand Down