const mongoose = require("mongoose"); // Clear cache if (mongoose.models.Pricing) { delete mongoose.models.Pricing; } if (mongoose.connection.models.Pricing) { delete mongoose.connection.models.Pricing; } // Schema for breadcrumb item const breadcrumbItemSchema = new mongoose.Schema( { text: { type: String, trim: true, default: "", maxlength: 40, }, link: { type: String, trim: true, default: "", maxlength: 255, }, }, { _id: false } ); // Schema for hero section const heroSchema = new mongoose.Schema( { title: { type: String, trim: true, default: "Pricing Plan", maxlength: 60, }, backgroundImage: { type: String, trim: true, default: "/assets/img/inner-page/breadcrumb.jpg", maxlength: 255, }, shapeImage: { type: String, trim: true, default: "/assets/img/inner-page/shape.png", maxlength: 255, }, breadcrumb: { type: [breadcrumbItemSchema], default: [], }, }, { _id: false } ); // Schema for pricing section header const pricingSectionSchema = new mongoose.Schema( { subtitle: { type: String, trim: true, default: "pricing plan", maxlength: 64, }, heading: { type: String, trim: true, default: "Flexible Plans to Suit Every Traveler", maxlength: 120, }, description: { type: String, trim: true, default: "", maxlength: 500, }, }, { _id: false } ); // Schema for individual plan const planSchema = new mongoose.Schema( { name: { type: String, trim: true, required: true, maxlength: 64, }, price: { type: String, trim: true, default: "0", maxlength: 32, }, period: { type: String, trim: true, default: "mo", maxlength: 8, }, currency: { type: String, trim: true, default: "$", maxlength: 8, }, buttonText: { type: String, trim: true, default: "Get Started Today", maxlength: 64, }, buttonLink: { type: String, trim: true, default: "/pricing", maxlength: 255, }, buttonIcon: { type: String, trim: true, default: "fa-solid fa-arrow-right", maxlength: 64, }, style: { type: String, trim: true, enum: ["default", "style-2"], default: "default", }, features: { type: [{ type: String, maxlength: 96 }], default: [], }, }, { _id: false } ); // Schema for plans container const plansSchema = new mongoose.Schema( { monthly: { type: [planSchema], default: [], }, yearly: { type: [planSchema], default: [], }, }, { _id: false } ); // Schema for testimonial item const testimonialItemSchema = new mongoose.Schema( { name: { type: String, trim: true, default: "", maxlength: 64, }, role: { type: String, trim: true, default: "", maxlength: 64, }, rating: { type: Number, min: 1, max: 5, default: 5, }, content: { type: String, trim: true, default: "", maxlength: 400, }, }, { _id: false } ); // Schema for testimonials section const testimonialsSchema = new mongoose.Schema( { subtitle: { type: String, trim: true, default: "What Our Clients Say", maxlength: 64, }, heading: { type: String, trim: true, default: "Immigration Success Stories", maxlength: 120, }, buttonText: { type: String, trim: true, default: "View All Review", maxlength: 64, }, buttonLink: { type: String, trim: true, default: "/contact", maxlength: 255, }, buttonIcon: { type: String, trim: true, default: "fa-solid fa-arrow-right", maxlength: 64, }, image: { type: String, trim: true, default: "", maxlength: 255, }, items: { type: [testimonialItemSchema], default: [], }, }, { _id: false } ); // Main Pricing Schema const pricingSchema = new mongoose.Schema( { name: { type: String, default: "default", unique: true, }, hero: { type: heroSchema, default: () => ({}), }, pricingSection: { type: pricingSectionSchema, default: () => ({}), }, plans: { type: plansSchema, default: () => ({}), }, testimonials: { type: testimonialsSchema, default: () => ({}), }, }, { timestamps: true, } ); // Migration method to import data from JSON pricingSchema.statics.migrateFromJson = async function (jsonData) { try { // Check if default pricing exists const existingPricing = await this.findOne({ name: "default" }); // Process data from JSON const processedData = { hero: { title: jsonData.hero?.title || "Pricing Plan", backgroundImage: jsonData.hero?.backgroundImage || "/assets/img/inner-page/breadcrumb.jpg", shapeImage: jsonData.hero?.shapeImage || "/assets/img/inner-page/shape.png", breadcrumb: (jsonData.hero?.breadcrumb || []).map((item) => ({ text: item.text || "", link: item.link || "", })), }, pricingSection: { subtitle: jsonData.pricingSection?.subtitle || "pricing plan", heading: jsonData.pricingSection?.heading || "Flexible Plans to Suit Every Traveler", description: jsonData.pricingSection?.description || "", }, plans: { monthly: (jsonData.plans?.monthly || []).map((plan) => ({ name: plan.name || "", price: plan.price || "0", period: plan.period || "mo", currency: plan.currency || "$", buttonText: plan.buttonText || "Get Started Today", buttonLink: plan.buttonLink || "/pricing", buttonIcon: plan.buttonIcon || "fa-solid fa-arrow-right", style: plan.style || "default", features: plan.features || [], })), yearly: (jsonData.plans?.yearly || []).map((plan) => ({ name: plan.name || "", price: plan.price || "0", period: plan.period || "mo", currency: plan.currency || "$", buttonText: plan.buttonText || "Get Started Today", buttonLink: plan.buttonLink || "/pricing", buttonIcon: plan.buttonIcon || "fa-solid fa-arrow-right", style: plan.style || "default", features: plan.features || [], })), }, testimonials: { subtitle: jsonData.testimonials?.subtitle || "What Our Clients Say", heading: jsonData.testimonials?.heading || "Immigration Success Stories", buttonText: jsonData.testimonials?.buttonText || "View All Review", buttonLink: jsonData.testimonials?.buttonLink || "/contact", buttonIcon: jsonData.testimonials?.buttonIcon || "fa-solid fa-arrow-right", image: jsonData.testimonials?.image || "", items: (jsonData.testimonials?.items || []).map((item) => ({ name: item.name || "", role: item.role || "", rating: item.rating || 5, content: item.content || "", })), }, }; if (existingPricing) { // Update existing pricing existingPricing.hero = processedData.hero; existingPricing.pricingSection = processedData.pricingSection; existingPricing.plans = processedData.plans; existingPricing.testimonials = processedData.testimonials; await existingPricing.save(); console.log("Pricing data updated successfully"); return existingPricing; } else { // Create new pricing const newPricing = await this.create({ name: "default", ...processedData, }); console.log("Pricing data imported successfully"); return newPricing; } } catch (error) { console.error("Error migrating pricing data:", error); throw error; } }; module.exports = mongoose.model("Pricing", pricingSchema);