Simple Schema array generator

Hello,

I’m currently building a Meteor app for booking cleaners. I’m looking to integrate a search feature that will check a UK postcode with those of the cleaners in my collection. For each cleaner’s profile I’m using simple schema and collection2 to insert their data.

I would like to know whether I can integrate an NPM API wrapper into one of the schema’s keys to generate an array of postcodes x miles from the cleaners postcode using this tool https://www.npmjs.com/package/uk-postcodes-node

I’m new to Meteor and would like to know whether I’m on the right track? or do I need to perform the postcode generator somewhere else in my app?

This is where I’ve got to so far:

CleanerProfiles = new Meteor.Collection('cleanerprofiles');

CleanerProfiles.allow({
    insert: function(userId, doc) {
        return !!userId;
    }
});

CleanerProfileSchema = new SimpleSchema({
	firstname: {
		type: String,
		label: "First Name",
		max:50
	},
	lastname: {
		type: String,
		label: "Last Name",
		max:50
	},
    type: {
        type: String,
        label: "Dometic and/or commercial cleaning",
        allowedValues: ['domestic', 'commercial', 'both'],
        autoform: {
          options: [
            {label: "Domestic", value: "domestic"},
            {label: "Commercial", value: "commercial"},
            {label: "Both", value: "both"}
          ]
        }
    },
	email: {
        type: String,
        label: "E-mail",
        regEx: SimpleSchema.RegEx.Email,
        max:50
	},
	tel: {
        type: String,
        label: "Phone",
        optional: false,
        max:50
    },
	streetnumber: {
        type: String,
        label: "Street Number",
        max: 50
    },
    streetname: {
        type: String,
        label: "Street Name",
        max: 50
    },
    city: {
        type: String,
        label: "City",
        max: 50
    },
    country: {
        type: String,
        label: "Country",
        max: 50
    },
    postcode: {
        type: String, 
        label: "Post Code",
        max: 50
    },
    distance: {
        type: String,
        label: "How many miles are you willing to travel?",
        max: 50
    },
    radius: {
        type: [String],
        label: "Radius",
        custom: function () {
            var postcode = this.field('postcode');
            var radius = this.field('distance');

            UKPostcodes.nearestPostcodes(postcode, radius, function (error, postcodes) {
                console.log(data);
            })
        },
        autoform: {
            type: "hidden"
        }
    },
    author: {
    	type: String,
    	label: "Author",
    	autoValue: function() {
    		return this.userId
    	},
    	autoform: {
    		type: "hidden"
    	}
    },
    createdAt: {
    	type: Date,
    	label: "Created At",
    	autoValue: function() {
    		return new Date()
    	},
    	autoform: {
    		type: "hidden"
    	}
    }
});

CleanerProfiles.attachSchema( CleanerProfileSchema );

Not sure on the syntax, but you’ve got the right idea. You just need to add the post codes before you insert (which it looks like you are doing), or make the field optional and add them later in an update() call.

1 Like

Interesting approach but this is definitely not a “Schema” concern.
I would recommend using https://atmospherejs.com/matb33/collection-hooks

And do 2 hooks: after.insert and after.update, check if “postCode”/“radius” changed and update the property “eligiblePostCodes” or whatever name you prefer. A schema concern would be to have eligiblePostCodes: {type: [String]}

Be sure to use Meteor.wrapAsync for synced api calls, unless you are doing this in a Fiber :slight_smile:

1 Like