On the backend I have two Schemas:
User:
var UserSchema = new mongoose.Schema({
isActive: {type: Boolean, default: false},
isVerified: {type: Boolean, default: false},
firstName: {type: String, required: [true, "can't be blank"]},
lastName: {type: String, required: [true, "can't be blank"]},
email: {type: String, lowercase: true, unique: true, required: [true, "can't be blank"], match: [/\S+@\S+\.\S+/, 'is invalid'], index: true},
phone: {type: Number, unique: true, required: true, required: [true, "can't be blank"]}, // validate phone
role: {type: String, enum: ['invalid', 'precustomer', 'customer', 'broker', 'brokerAdmin', 'dealer']},
hash: String,
salt: String,
address: {type: Object, required: [isRequired('address'), "can't be blank"]}, //require customer
organization: { type: mongoose.Schema.Types.ObjectId,
ref: 'Organization',
required: [isRequired('organization'), "can't be blank"]}, //require broker && dealer
deliverySchedule: String // for now.. make this custom obj soon --- OPTIONAL
}, {timestamps: true});
Application:
var mongoose = require('mongoose');
var uniqueValidator = require('mongoose-unique-validator');
var ApplicationSchema = new mongoose.Schema({
customer: {type: mongoose.Schema.Types.ObjectId, ref: 'User', required: [true, "can't be blank"]},
dealer: {type: mongoose.Schema.Types.ObjectId, ref: 'User', required: [true, "can't be blank"]},
broker: {type: mongoose.Schema.Types.ObjectId, ref: 'User', required: [true, "can't be blank"]},
files: {type: mongoose.Schema.Types.ObjectId, ref: 'Files', required: [true, "can't be blank"]},
}, {timestamps: true});
mongoose.model('Application', ApplicationSchema);
};
On the front end, in order for a "Broker" to create an "Application," he must first add a "customer" to the application. To do this, a Broker will search for a user by email, and the server can respond with either:
-
email exists (User Object = id, email, ...)
-
No user found ({})
It would not be possible to store an _id in "Create Application" API endpoint, because when a "Broker" creates an "Application", the "customer" is sent a link in their email to register (new _id), and the _id linked to the Application is lost (as far as I know.) I would like my "customer" "dealer" and "broker" to be String
instead of mongoose.Schema.Types.ObjectId
, however I would like it to work with .populate('customer dealer broker') using email instead of ID.
Would it make more sense to remove the required validation for name, phone, etc from the User Schema and only require an email for "registration" and request the rest from the "customer" after clicking the confirmation link?
via Moshe
No comments:
Post a Comment