UNPKG

poserver

Version:
119 lines (98 loc) 4.31 kB
/** * Created by tomdaley on 10/9/16. */ "use strict"; var ParsedAddress = require("./clsParsedAddress"); var ParsedName = require("./clsParsedName"); var Person = function (mongoDoc) { mongoDoc = mongoDoc || {}; var _this = this; _this.__type = "Person"; if (mongoDoc.hasOwnProperty("address")) _this.address = new ParsedAddress(mongoDoc.address); if (mongoDoc.hasOwnProperty("barnos")) _this.barnos = mongoDoc.barnos; if (mongoDoc.hasOwnProperty("birthDate")) _this.birthDate = mongoDoc.birthDate; if (mongoDoc.hasOwnProperty("cellPhone")) _this.cellPhone = mongoDoc.cellPhone; if (mongoDoc.hasOwnProperty("channelIdentities")) _this.channelIdentities = cvtObjToArr(mongoDoc.channelIdentities); if (mongoDoc.hasOwnProperty("email")) _this.email = mongoDoc.email; if (mongoDoc.hasOwnProperty("fax")) _this.fax = mongoDoc.fax; if (mongoDoc.hasOwnProperty("gender")) _this.gender = mongoDoc.gender; if (mongoDoc.hasOwnProperty("ids")) _this.ids = mongoDoc.ids; if (mongoDoc.hasOwnProperty("name")) _this.name = new ParsedName(mongoDoc.name); if (mongoDoc.hasOwnProperty("options")) _this.options = mongoDoc.options; if (mongoDoc.hasOwnProperty("telephone")) _this.telephone = mongoDoc.telephone; //This method might be called with a document retrieved from the Mongo database, in which case it will only //have the _id property, not the users_id property. However, if this method is called from revive(), it will //only have the users_id property, not the _id property. Either way, we need to make sure the result of this //method is an object having a users_id property. if (mongoDoc.hasOwnProperty("users_id")) _this.users_id = mongoDoc.users_id; if (mongoDoc.hasOwnProperty("_id")) _this.users_id = mongoDoc._id.toString(); }; Person.prototype.toDocument = function () { console.log("Creating document"); var doc = {}; doc.__type = this.__type; if (this.hasOwnProperty("address")) doc.address = this.address.toDocument(); if (this.hasOwnProperty("barnos")) doc.barnos = this.barnos; if (this.hasOwnProperty("birthDate")) doc.birthDate = this.birthDate; if (this.hasOwnProperty("cellPhone")) doc.cellPhone = this.cellPhone; if (this.hasOwnProperty("channelIdentities")) doc.channelIdentities = cvtObjToArr(this.channelIdentities); if (this.hasOwnProperty("email")) doc.email = this.email; if (this.hasOwnProperty("fax")) doc.fax = this.fax; if (this.hasOwnProperty("gender")) doc.gender = this.gender; if (this.hasOwnProperty("ids")) doc.ids = this.ids; if (this.hasOwnProperty("name")) doc.name = this.name.toDocument(); if (this.hasOwnProperty("options")) doc.options = this.options; if (this.hasOwnProperty("telephone")) doc.telephone = this.telephone; if (this.hasOwnProperty("users_id")) doc.users_id = this.users_id; if (this.hasOwnProperty("_id")) doc.users_id = this._id; return doc; }; Person.revive = function (data) { return new Person(data); }; Person.prototype.setAddress = function (addressObject) { this.address = new ParsedAddress(addressObject); }; Person.prototype.setName = function (nameObject) { this.name = new ParsedName(nameObject); }; Person.prototype.convertObjectToArray = function (z) { //If it's already an array, just send it back if (z instanceof Object && z instanceof Array) return z; //If it's an object, that means the botbuilder serializer turned our array into an object. Put it back var result = []; if (z instanceof Object) { for (var idx in z) { result.push(z[idx]); } return result; } //If it's not an object, then turn whatever it is into an array; return [z]; }; function cvtObjToArr(z) { //If it's already an array, just send it back if (z instanceof Object && z instanceof Array) return z; //If it's an object, that means the botbuilder serializer turned our array into an object. Put it back var result = []; if (z instanceof Object) { for (var idx in z) { result.push(z[idx]); } return result; } //If it's not an object, then turn whatever it is into an array; return [z]; } module.exports = Person;