const mongoose = require("mongoose"); const ctaButtonSchema = new mongoose.Schema( { label: { type: String, default: "" }, href: { type: String, default: "" }, }, { _id: false }, ); const heroSchema = new mongoose.Schema( { badge: { type: String, default: "" }, title: { type: String, default: "" }, description: { type: String, default: "" }, primaryButton: { type: ctaButtonSchema, default: () => ({}) }, secondaryButton: { type: ctaButtonSchema, default: () => ({}) }, image: { type: String, default: "" }, imageAlt: { type: String, default: "" }, }, { _id: false }, ); const directoryServiceSchema = new mongoose.Schema( { icon: { type: String, default: "" }, title: { type: String, default: "" }, description: { type: String, default: "" }, hours: { type: String, default: "" }, hoursIcon: { type: String, default: "" }, buttonLabel: { type: String, default: "" }, buttonHref: { type: String, default: "" }, }, { _id: false }, ); const directorySchema = new mongoose.Schema( { title: { type: String, default: "" }, subtitle: { type: String, default: "" }, services: { type: [directoryServiceSchema], default: [] }, }, { _id: false }, ); const channelSchema = new mongoose.Schema( { icon: { type: String, default: "" }, title: { type: String, default: "" }, detail: { type: String, default: "" }, }, { _id: false }, ); const labelPlaceholderSchema = new mongoose.Schema( { label: { type: String, default: "" }, placeholder: { type: String, default: "" }, }, { _id: false }, ); const departmentFieldSchema = new mongoose.Schema( { label: { type: String, default: "" }, }, { _id: false }, ); const supportFormFieldsSchema = new mongoose.Schema( { firstName: { type: labelPlaceholderSchema, default: () => ({}) }, lastName: { type: labelPlaceholderSchema, default: () => ({}) }, studentId: { type: labelPlaceholderSchema, default: () => ({}) }, department: { type: departmentFieldSchema, default: () => ({}) }, message: { type: labelPlaceholderSchema, default: () => ({}) }, }, { _id: false }, ); const contactFormSchema = new mongoose.Schema( { heading: { type: String, default: "" }, fields: { type: supportFormFieldsSchema, default: () => ({}) }, departments: { type: [String], default: [] }, submitLabel: { type: String, default: "" }, successMessage: { type: String, default: "" }, }, { _id: false }, ); const contactSectionSchema = new mongoose.Schema( { heading: { type: String, default: "" }, description: { type: String, default: "" }, channels: { type: [channelSchema], default: [] }, form: { type: contactFormSchema, default: () => ({}) }, }, { _id: false }, ); const metadataSchema = new mongoose.Schema( { title: { type: String, default: "" }, description: { type: String, default: "" }, }, { _id: false }, ); const studentSupportSchema = new mongoose.Schema( { slug: { type: String, required: true, unique: true, default: "student-support", }, metadata: { type: metadataSchema, default: () => ({}) }, hero: { type: heroSchema, default: () => ({}) }, directory: { type: directorySchema, default: () => ({}) }, contactSection: { type: contactSectionSchema, default: () => ({}) }, }, { timestamps: true }, ); /** * Seed / migrate từ file JSON (cùng shape với API frontend). */ studentSupportSchema.statics.migrateFromJson = async function (jsonData) { const slug = "student-support"; const payload = { slug, metadata: jsonData.metadata || {}, hero: jsonData.hero || {}, directory: jsonData.directory || { services: [] }, contactSection: jsonData.contactSection || {}, }; const existing = await this.findOne({ slug }); if (existing) { existing.set(payload); await existing.save(); return existing; } return this.create(payload); }; module.exports = mongoose.model("StudentSupport", studentSupportSchema);