const mongoose = require("mongoose"); const Schema = mongoose.Schema; const ObjectId = Schema.Types.ObjectId; const moment = require('moment'); const code = Math.floor(100000 + Math.random() * 900000); const { User,Counter, generateBookingId,resetCounter,generateCustomerId,ProfilePicture} = require('../models/User') const generateSupplierId = async () => { var result = await Counter.findOneAndUpdate( { _id: 'supplier_id' }, { $inc: { seq: 1 } }, { upsert: true, new: true } ); return result.seq; }; const supplierSchema = new mongoose.Schema( { suppliername: { type: String }, phone: { type: String, unique: true, trim: true }, bussinessname: {type : String, default: null}, registration_number: {type : String, default: null}, years_in_business: {type : String, default: null}, supplierId: {type : String, default: null}, phoneVerified: { type: Boolean, default: false }, phoneVerificationCode: { type: Number, default: 11111 }, passwordResetCode: { type: Number, default: code }, oneTimePasswordSetFlag: { type: Boolean, default: false }, emails: [{ email: String, verified: { type: Boolean, default: false } }], services: { password: { bcrypt: String } }, description: {type : String, default: null}, startingPrice : { type : String, default: 0.0}, status: { type : String, default:"under_verification"}, profile: { role: [{ type: String, default: "supplier" }], firstName: { type: String, default: null }, lastName: { type: String, default: null }, contactNumber: { type: String, default: null }, alternativeContactNumber : { type: String, default: null }, office_address: { type: String, default: null }, city: { type: String, default: null }, state: { type: String, default: null }, country: { type: String, default: null }, zip: { type: String, default: null }, }, status: { type: String, enum: ['inactive', 'active'], default: 'active' }, // currentGPS: { // // It's important to define type within type field, because // // mongoose use "type" to identify field's object type. // gpsType: { type: String, default: "Point" }, // // Default value is needed. Mongoose pass an empty array to // // array type by default, but it will fail MongoDB's pre-save // // validation. // coordinates: { type: [Number], default: [0, 0] }, // }, // location: { // type: { type: String }, // coordinates: [Number] // }, longitude: { type : Number,default: 0.0}, latitude: {type: Number,default: 0.0}, isActive: Boolean, tenantId: ObjectId, fcmId: { type: String, default: null }, createdAt: { type: Date, default: function () { return Date.now(); }, }, createdBy: ObjectId, updatedAt: { type: Date, default: function () { return Date.now(); }, }, updatedBy: ObjectId, }, { versionKey: false } ); // const deliveryAgent = new mongoose.Schema({ // deliveryboyId : { type: String, default: null }, // deliveryboyname : { type: String }, // vechilenumber : { type: String, default: null }, // bookingid : { type: String, default: null }, // status: { // type: String, // enum: ['complete', 'pending'], // default: 'pending' // }, // }) const friendRequestSchema = new mongoose.Schema({ customerId: { type: String, default: null }, supplierId: { type: String, default: null }, status: { type: String, default: "pending" }, // timestamp: { type: Date, default: Date.now } timestamp: { type: String, default: moment().format('DD-MM-YY HH:mm:ss') }, }); const deliveryBoySchema = new mongoose.Schema({ supplierId:{ type: String, default: null }, suppliername:{ type: String, default: null }, name: { type: String, default: null }, phone: { type: String, default: null,unique:true }, alternativeContactNumber : { type : String,default: null }, phoneVerified: { type: Boolean, default: false }, phoneVerificationCode: { type: Number, default: 11111 }, passwordResetCode: { type: Number, default: code }, oneTimePasswordSetFlag: { type: Boolean, default: false }, address: { type: String, default: null }, city: { type: String, default: null }, state: { type: String, default: null }, zip: { type: String, default: null }, timestamp: { type: Date, default: Date.now }, status: { type: String, default: "Inactive" }, longitude: { type : Number,default: 0.0}, latitude: {type: Number,default: 0.0}, fcmId: { type: String, default: null }, }); const profilePictureSupplierSchema = new Schema({ supplierId: { type: String, unique: true, required: true }, picture: { type: String, // Change the type to String required: true, validate: { validator: function (value) { const supportedFormats = ['jpg', 'jpeg', 'png']; const fileExtension = value.split('.').pop().toLowerCase(); return supportedFormats.includes(fileExtension); }, message: 'Picture must be a JPEG, PNG, or JPG image' } } }); const requestedSupplierSchema = new mongoose.Schema({ supplierId: String, quoted_amount: Number, time: {type:String,default:null}, // ✅ New field added here status:{type:String,default: "pending" }, }, { _id: false }); const requestedBookingSchema = new mongoose.Schema({ customerId: { type: String, required: true }, type_of_water: String, capacity: String, quantity: String, total_required_capacity: Number, date: String, time: String, requested_suppliers: [requestedSupplierSchema], status: { type: String, default: "pending" }, }, { timestamps: true }); // models/RecurringRequestedBooking.js // const requestedSupplier1Schema = new mongoose.Schema({ // supplierId: String, // quoted_amount: Number, // time: { type: String, default: null }, // status: { type: String, default: "pending" }, // }, { _id: false }); // const recurringRequestedBookingSchema = new mongoose.Schema({ // customerId: { type: String, required: true }, // type_of_water: String, // capacity: String, // quantity: String, // total_required_capacity: Number, // frequency: { type: String, enum: ["daily","weekly_once","weekly_twice","weekly_thrice","weekly"], required: true }, // weekly_count: { type: Number, enum: [1,2,3] }, // start_date: { type: String, required: true }, // end_date: { type: String, required: true }, // time: String, // dates: [String], // requested_suppliers: [requestedSupplier1Schema], // status: { type: String, default: "pending" }, // }, { timestamps: true }); const requestedSupplier1Schema = new mongoose.Schema({ supplierId: { type: String, required: true }, quoted_amount: { type: Number, default: 0 }, time: { type: String, default: null }, // keep as string to match current payloads status: { type: String, enum: ["pending", "accepted", "rejected"], default: "pending" }, }, { _id: false }); const recurringRequestedBookingSchema = new mongoose.Schema({ customerId: { type: String, required: true }, type_of_water: { type: String, required: true }, capacity: { type: String, required: true }, // keep as sent by UI, we also store parsed number below if you want quantity: { type: String, required: true }, total_required_capacity: { type: Number, required: true }, // capacity * quantity (numeric) frequency: { type: String, enum: ["daily", "weekly_once", "weekly_twice", "weekly_thrice", "weekly"], required: true }, weekly_count: { type: Number, default: 1 }, start_date: { type: String, required: true }, // storing original string for audit end_date: { type: String, required: true }, time: { type: String, required: true }, dates: { type: [String], default: [] }, // ISO "YYYY-MM-DD" strings requested_suppliers: { type: [requestedSupplier1Schema], default: [] }, status: { type: String, default: "pending" }, }, { timestamps: true }); const RequestedBooking = mongoose.model('RequestedBooking', requestedBookingSchema); const RecurringRequestedBooking = mongoose.model("RecurringRequestedBooking", recurringRequestedBookingSchema); const Supplier = mongoose.model("Supplier", supplierSchema); //const DeliveryAgent = mongoose.model("DeliveryAgent", deliveryAgent); const FriendRequest = mongoose.model('FriendRequest', friendRequestSchema); const DeliveryBoy = mongoose.model('DeliveryBoy', deliveryBoySchema); const profilePictureSupplier = mongoose.model('ProfilePictureSupplier', profilePictureSupplierSchema); module.exports = { Supplier, generateSupplierId, FriendRequest,DeliveryBoy, profilePictureSupplier,RequestedBooking,RecurringRequestedBooking}