1 /**
  2  * @author Brian Carlsen
  3  * @version 1.0.0
  4  *
  5  * Serves as a wrapper to the MINDBODY Site Service, providing 
  6  * some additional functionality.
  7  *
  8  * All Site Service methods are available returning
  9  * 1) An extracted result using the instance method with the same name
 10  * 2) The raw array response using the instance method with the same name post-fixed with 'Response'.
 11  * 		The array consists of:
 12  * 			i) 		The object represtentation of the SOAP response
 13  * 			ii) 	The raw XML SOAP response
 14  * 			iii) 	The raw header info of the SOAP response
 15  */
 16 
 17 var Promise = require( 'bluebird' );
 18 
 19 var mboService = require( './mbo_Service' );
 20 
 21 //--------------- SiteService Class ---------------------
 22 
 23 
 24 /**
 25  * Represents the MINDOBDY Site Service.
 26  *
 27  * @constructor
 28  * @param  {string} username Username of the MINDBODY client interacting with the service.
 29  * @param  {string} password Password of the MINDBODY client interacting with the service.
 30  * @return {mbo_SiteService}          Returns the Site Service.
 31  */
 32 function mbo_SiteService( username, password ) {
 33 	mboService.call( this, 'SiteService', username, password );
 34 }
 35 mbo_SiteService.prototype = Object.create( mboService.prototype );
 36 mbo_SiteService.prototype.constructor = mbo_SiteService;
 37 
 38 
 39 /**
 40  * Retrieves activation codes for the Site required for API access.
 41  * These must be given to the site's owner to be granted access.
 42  *
 43  * Overwrites the automatically generated function due to a non-standard response format.
 44  *
 45  * @override
 46  * @return {Promise} An A+ Promise passed an object containing:
 47  *                      i)	ActivationCode: The raw activation code
 48  *                      ii) ActivationLink: The link to grant access
 49  */
 50 mbo_SiteService.prototype.GetActivationCode = function() {
 51 	return this.GetActivationCodeResponse()
 52 		.spread( function( result, raw, header ) {
 53 			return {
 54 				ActivationCode: result.GetActivationCodeResult.ActivationCode,
 55 				ActivationLink: result.GetActivationCodeResult.ActivationLink
 56 			};
 57 		} ) 
 58 		.catch( function( err ) {
 59 			throw err;
 60 		} );
 61 };
 62 
 63 /**
 64  * Retrieves activation codes for the Site required for API access.
 65  * These must be given to the site's owner to be granted access.
 66  * 
 67  * @return {Promise} An A+ Promise passed an object containing:
 68  *                      i)	code: The raw activation code
 69  *                      ii) link: The link to grant access
 70  */
 71 mbo_SiteService.prototype.getActivationCodes = function() {
 72 	return this.GetActivationCodeResponse()
 73 		.spread( function( result, raw, header ) {
 74 			return {
 75 				code: result.GetActivationCodeResult.ActivationCode,
 76 				link: result.GetActivationCodeResult.ActivationLink
 77 			};
 78 		} ) 
 79 		.catch( function( err ) {
 80 			throw err;
 81 		} );
 82 };
 83 
 84 /**
 85  * Returns whether or not the SourceCredentials have access to a site.
 86  * @param  {number}  id The id of the site to check.
 87  * @return {Promise}    Returns an A+ Promise resolved to true if the credentials have site access, false otherwise.
 88  */
 89 mbo_SiteService.prototype.hasSiteAccess = function( id ) {
 90 	return this.GetSitesResponse()
 91 		.spread( function( result, raw, header ) {
 92 			var sites 	= result.GetSitesResult.Sites.Site,
 93 				access 	= false;
 94 
 95 			sites.forEach( function( site ) {
 96 				if ( site.ID === id ) {
 97 					access = true;
 98 				}
 99 			} );
100 
101 			return access;
102 		} )
103 		.catch( function( err ) {
104 			throw err;
105 		} );
106 };
107 
108 module.exports = mbo_SiteService;