johnhamelink
6/20/2012 - 8:27 AM

model.js

        // The contact embedded doc allows multiple
        // contact details
        var contact = new Schema({
            key:   { type: String, required: true }, // Form of contact
            value: { type: String, required: true }  // Contact detail
        },{strict: true});

        contact.plugin(useTimestamps);

        // The assistant embedded doc allows multiple
        // assistants and multiple contact details
        // per assistant
        var assistant = new Schema({
            name:           { type: String, required: true }, // Name of assistant
            email:          { type: String, required: true }, // Form of contact
            telephone:      { type: String, required: true },  // Contact detail
            address:        { type: String, required: true },  // Contact detail
            addressLine2:   { type: String, required: true },  // Contact detail
            town:           { type: String, required: true },  // Contact detail
            postcode:       { type: String, required: true },  // Contact detail
            country:        { type: String, required: true }  // Contact detail
        },{strict: true});

        assistant.plugin(useTimestamps);

        var aum = new Schema({
            idStaff:            { type: ObjectId, required: true, ref: "staff" },
            idAccount:      { type: ObjectId, required: true,ref: "account" },
            numValue:           { type: Number, required: true }
        },{strict: true});

        aum.plugin(useTimestamps);

        var search = new Schema({
            URL:            { type: String, required: true },
            type:           { type: String, required: true },
            idCountry:      { type: ObjectId, required: true, ref: "country" },
            idSector:       { type: ObjectId, required: true, ref: "sector" },
            idCompany:      { type: ObjectId, required: true, ref: "company" }
        },{strict: true});

        search.plugin(useTimestamps);

        var analytics = new Schema({
            URL:            { type: String, required: true },
            action:         { type: String, required: true }
        },{strict: true});

        analytics.plugin(useTimestamps);

        var IPAddress = new Schema({
            ip:             { type: String, required: true },
            tag:            { type: String, required: true },
            floAum:         { type: Number, required: true }
        },{strict: true});

        IPAddress.plugin(useTimestamps);

        var preference = new Schema({
            key:   { type: String, required: true },
            value: { type: String, required: true }
        },{strict: true});

        preference.plugin(useTimestamps);

        var institutionRevenues = new Schema({
            key:   { type: String, required: true },
            value: { type: String, required: true }
        },{strict: true});

        institutionRevenues.plugin(useTimestamps);

        var researchViewed = new Schema({
            type:       { type: String, required: true },
            activity:   { type: String, required: true },
            idResearch: { type: ObjectId, required: true,ref: "research"},
            idAccount:  { type: ObjectId, required: true,ref: "account" },
            email:      { type: String, required: true }
        },{strict: true});

        researchViewed.plugin(useTimestamps);

        var clientSchema = new Schema({
            firstName:              { type: String, required: true, trim: true },                   // The first name of the client
            lastName:               { type: String, required: true, trim: true },                   // The second name of the client
            password:               { type: String, trim: true, index: true, required: true },      // The password of the client
            jobTitle:               { type: String, trim: true },                                   // The client's job title
            type:                   { type: String, trim: true },
            comments:               { type: String, trim: true },
            status:                 { type: String, trim: true },
            approvedOn:             { type: Date},
            numTier:                { type: Number},
            numTurnover:            { type: Number },
            numLevel:               [{ type: Number }],                                             // Array of levelIDs associated with client
            contact:                [contact],                                                      // The contact details associated with the client
            assistant:              [assistant],                                                    // The details of the client's assistant
            aum:                    [aum],
            searches:               [search],
            analytics:              [analytics],
            IPAddresses:            [IPAddress],
            isIpRestricted:         { type: Boolean, index: true},
            preferences:            [preference],
            reportsViewed:          [researchViewed],
            institutionsrevenues:   [institutionRevenues],
            addresses:              [{ type: String, trim: true }],
            idAccount:              [{ type: ObjectId, index: true, ref: "account"}],
            idCountry:              { type: ObjectId, index: true, ref: "region"},
            staffCoverage:          [{ type: ObjectId, index: true }],
            role:                   { type: String, index: true},
            position:               { type: String, index: true},
            isApproved:             { type: Boolean, index: true, "default": 0 },                    // The approval details associated with the client
            isActive:               { type: Boolean, required: true, index: true, "default": 1 } // Whether the client is visible or not
            },{strict: true});

            clientSchema.plugin(useTimestamps);

            var model = mongoose.model(moduleName,clientSchema);

            return model;