1 /**
  2  * @author  Brian Carlsen
  3  * @version  1.0.0
  4  *
  5  * A factory for creating MINDBODY Services
  6  *
  7  * Emits a 'ready' event on the service after initialization.
  8  */
  9 
 10 var Promise 			= require( 'bluebird' ),
 11 	OperationalError 	= Promise.OperationalError;
 12 
 13 var mbo_ClientService	= require( './services/mbo_ClientService' ),
 14 	mbo_ClassService	= require( './services/mbo_ClassService' )
 15 	mbo_StaffService	= require( './services/mbo_StaffService' ),
 16 	mbo_SaleService		= require( './services/mbo_SaleService' ),
 17 	mbo_SiteService		= require( './services/mbo_SiteService' );
 18 
 19 /**
 20  * Sets the SOURCENAME and PASSWORD to be used with all requests.
 21  * 
 22  * @param {string} username Your MINDBODY developer's Sourcename
 23  * @param {string} password Your MINDBODY developer's Password
 24  */
 25 module.exports.setSourceCredentials = function( username, password ) {
 26 	this.sourceName = username;
 27 	this.sourcePassword = password;
 28 };
 29 
 30 /**
 31  * Creates a service to interact with the MINDBODY Client Service API.
 32  * 
 33  * @param  {string} username The Username of the MINDBODY client to be used with all requests.
 34  * @param  {string} password The Password of the MINDBODY client to be used with all requests.
 35  * @param  {number|Array} siteIds  The Site ID, or an Array of Site IDs to be used with all requests.
 36  * @return {mbo_ClientService}          The service to interact with the MINDBODY API.
 37  */
 38 module.exports.createClientService = function( username, password, siteIds ) {
 39 	return _defineService( mbo_ClientService, username, password, siteIds );
 40 };
 41 
 42 /**
 43  * Creates a service to interact with the MINDBODY Client Service API asynchronously.
 44  * 
 45  * @param  {string} username The Username of the MINDBODY client to be used with all requests.
 46  * @param  {string} password The Password of the MINDBODY client to be used with all requests.
 47  * @param  {number|Array} siteIds  The Site ID, or an Array of Site IDs to be used with all requests.
 48  * @return {Promise}          An A+ Promise, which resolves with the service to interact with the MINDBODY API.
 49  */
 50 module.exports.createClientServiceAsync = function( username, password, siteIds ) {
 51 	return _defineServiceAsync( mbo_ClientService, username, password, siteIds );
 52 };
 53 
 54 //-------------------------------------------//
 55 
 56 /**
 57  * Creates a service to interact with the MINDBODY Class Service API.
 58  * 
 59  * @param  {string} username The Username of the MINDBODY client to be used with all requests.
 60  * @param  {string} password The Password of the MINDBODY client to be used with all requests.
 61  * @param  {number|Array} siteIds  The Site ID, or an Array of Site IDs to be used with all requests.
 62  * @return {mbo_ClassService}          The service to interact with the MINDBODY API.
 63  */
 64 module.exports.createClassService = function( username, password, siteIds ) {
 65 	return _defineService( mbo_ClassService, username, password, siteIds );
 66 };
 67 
 68 /**
 69  * Creates a service to interact with the MINDBODY Class Service API asynchronously.
 70  * 
 71  * @param  {string} username The Username of the MINDBODY client to be used with all requests.
 72  * @param  {string} password The Password of the MINDBODY client to be used with all requests.
 73  * @param  {number|Array} siteIds  The Site ID, or an Array of Site IDs to be used with all requests.
 74  * @return {Promise}          An A+ Promise, which resolves with the service to interact with the MINDBODY API.
 75  */
 76 module.exports.createClassServiceAsync = function( username, password, siteIds ) {
 77 	return _defineServiceAsync( mbo_ClassService, username, password, siteIds );
 78 };
 79 
 80 //-------------------------------------------------------//
 81 
 82 /**
 83  * Creates a service to interact with the MINDBODY Site Service API.
 84  * 
 85  * @param  {string} username The Username of the MINDBODY client to be used with all requests.
 86  * @param  {string} password The Password of the MINDBODY client to be used with all requests.
 87  * @param  {number|Array} siteIds  The Site ID, or an Array of Site IDs to be used with all requests.
 88  * @return {mbo_SiteService}          The service to interact with the MINDBODY API.
 89  */
 90 module.exports.createSiteService = function( username, password, siteIds ) {
 91 	return _defineService( mbo_SiteService, username, password, siteIds );
 92 };
 93 
 94 /**
 95  * Creates a service to interact with the MINDBODY Site Service API asynchronously.
 96  * 
 97  * @param  {string} username The Username of the MINDBODY client to be used with all requests.
 98  * @param  {string} password The Password of the MINDBODY client to be used with all requests.
 99  * @param  {number|Array} siteIds  The Site ID, or an Array of Site IDs to be used with all requests.
100  * @return {Promise}          An A+ Promise, which resolves with the service to interact with the MINDBODY API.
101  */
102 module.exports.createSiteServiceAsync = function( username, password, siteIds ) {
103 	return _defineServiceAsync( mbo_SiteService, username, password, siteIds );
104 };
105 
106 //---------------------------------------------------------//
107 
108 /**
109  * Creates a service to interact with the MINDBODY Staff Service API.
110  * 
111  * @param  {string} username The Username of the MINDBODY client to be used with all requests.
112  * @param  {string} password The Password of the MINDBODY client to be used with all requests.
113  * @param  {number|Array} siteIds  The Site ID, or an Array of Site IDs to be used with all requests.
114  * @return {mbo_StaffService}          The service to interact with the MINDBODY API.
115  */
116 module.exports.createStaffService = function( username, password, siteIds ) {
117 	return _defineService( mbo_StaffService, username, password, siteIds );
118 };
119 
120 /**
121  * Creates a service to interact with the MINDBODY Client Service API asynchronously.
122  * 
123  * @param  {string} username The Username of the MINDBODY client to be used with all requests.
124  * @param  {string} password The Password of the MINDBODY client to be used with all requests.
125  * @param  {number|Array} siteIds  The Site ID, or an Array of Site IDs to be used with all requests.
126  * @return {Promise}          An A+ Promise, which resolves with the service to interact with the MINDBODY API.
127  */
128 module.exports.createStaffServiceAsync = function( username, password, siteIds ) {
129 	return _defineServiceAsync( mbo_StaffService, username, password, siteIds );
130 };
131 
132 //-------------------------------------------//
133 
134 /**
135  * Creates a service to interact with the MINDBODY Sale Service API.
136  * 
137  * @param  {string} username The Username of the MINDBODY client to be used with all requests.
138  * @param  {string} password The Password of the MINDBODY client to be used with all requests.
139  * @param  {number|Array} siteIds  The Site ID, or an Array of Site IDs to be used with all requests.
140  * @return {mbo_SaleService}          The service to interact with the MINDBODY API.
141  */
142 module.exports.createSaleService = function( username, password, siteIds ) {
143 	return _defineService( mbo_SaleService, username, password, siteIds );
144 };
145 
146 
147 /**
148  * Creates a service to interact with the MINDBODY Sale Service API asynchronously.
149  * 
150  * @param  {string} username The Username of the MINDBODY client to be used with all requests.
151  * @param  {string} password The Password of the MINDBODY client to be used with all requests.
152  * @param  {number|Array} siteIds  The Site ID, or an Array of Site IDs to be used with all requests.
153  * @return {Promise}          An A+ Promise, which resolves with the service to interact with the MINDBODY API.
154  */
155 module.exports.createSaleServiceAsync = function( username, password, siteIds ) {
156 	return _defineServiceAsync( mbo_SaleService, username, password, siteIds );
157 };
158 
159 
160 //------ Internal Methods ------
161 
162 /**
163  * Sets up the Service.
164  * 
165  * @emits 	ready
166  * @param  {mbo_Service} serviceClass The service to be initialized.
167  * @param  {string} username The Username of the MINDBODY client to be used with all requests.
168  * @param  {string} password The Password of the MINDBODY client to be used with all requests.
169  * @param  {number|Array} siteIds  The Site ID, or an Array of Site IDs to be used with all requests.
170  * @return {mbo_Service}              Returns an instantiated instance of the passed in Service class.
171  */
172 var _defineService = function( serviceClass, username, password, siteIds ) {
173 	var self = module.exports;
174 
175 	if ( !( self.sourceName && self.sourcePassword ) ) {
176 		throw new Error( 'Source credentials have not been set.' );
177 	}
178 
179 	var service = new serviceClass( self.sourceName, self.sourcePassword );
180 	service.on( 'initialized', function() {
181 		if ( username && password ) {
182 			service.setUserCredentials( username, password, siteIds );
183 		}
184 		
185 		service.emit( 'ready' );
186 	} );
187 
188 	return service;
189 }
190 
191 /**
192  * Sets up the Service using Bluebird Promises.
193  * 
194  * @emits 	ready
195  * @param  {mbo_Service} serviceClass The service to be initialized.
196  * @param  {string} username The Username of the MINDBODY client to be used with all requests.
197  * @param  {string} password The Password of the MINDBODY client to be used with all requests.
198  * @param  {number|Array} siteIds  The Site ID, or an Array of Site IDs to be used with all requests.
199  * @return {Promise}              Returns an A+ Promise passed an instantiated instance of the passed in Service class.
200  */
201 var _defineServiceAsync = function( serviceClass, username, password, siteIds ) {
202 	var self = module.exports;
203 
204 	return new Promise( function( resolve, reject ) {
205 		if ( !( self.sourceName && self.sourcePassword ) ) {
206 			throw new Error( 'Source credentials have not been set.' );
207 		}
208 
209 		var service = new serviceClass( self.sourceName, self.sourcePassword )
210 		service.on( 'initialized', function() {
211 			if ( username && password ) {
212 				service.setUserCredentials( username, password, siteIds );
213 			}
214 
215 			resolve( service );
216 		} );
217 
218 		service.on( 'error', function( err ) {
219 			reject( err );
220 		} );
221 	} );
222 };
223