@@ -8,31 +8,54 @@ const ServicesController = Promise.promisifyAll(gonebusy.ServicesController);
88const BookingsController = Promise . promisifyAll ( gonebusy . BookingsController ) ;
99
1010const {
11- service_id,
1211 clientToken : authorization ,
1312 clientApiEndpoint
1413} = gonebusyEnv ;
1514
1615gonebusy . configuration . BASEURI = clientApiEndpoint ;
1716
18- class BusyWrapper {
19- static getServiceNamePromise ( ) {
20- return ServicesController . getServicesAsync ( { authorization } ) ;
17+ let serviceInfo ;
18+
19+ class BusyAdapter {
20+ static getServiceInfoAsync ( ) {
21+ return new Promise ( ( resolve ) => {
22+ if ( serviceInfo )
23+ resolve ( serviceInfo ) ;
24+ else {
25+ ServicesController
26+ . getServicesAsync ( { authorization } )
27+ . then ( ( response ) => {
28+ if ( response . services && response . services . length ) {
29+ const serviceEntry = response . services [ 0 ] ;
30+ serviceInfo = {
31+ name : serviceEntry . name ,
32+ id : serviceEntry . id ,
33+ max_duration : serviceEntry . max_duration ,
34+ } ;
35+ resolve ( serviceInfo ) ;
36+ } else
37+ throw new Error ( 'Failed to fetch service info' ) ;
38+ } ) ;
39+ }
40+ } ) ;
2141 }
2242
23- static getServiceAvailableSlotsByIdPromise ( date ) {
24- return ServicesController . getServiceAvailableSlotsByIdAsync ( {
25- authorization,
26- id : service_id ,
27- startDate : date ,
28- endDate : Scheduler . getNextDayString ( date )
29- } ) . then ( ( data ) => {
30- const slotData = [ ] ;
31- data . service . resources [ 0 ] . availableSlots . forEach ( ( x ) => {
32- slotData . push ( ...x . slots . split ( ', ' ) ) ;
43+ static getSlotsAsync ( date ) {
44+ return BusyAdapter
45+ . getServiceInfoAsync ( )
46+ . then ( info => ServicesController . getServiceAvailableSlotsByIdAsync ( {
47+ authorization,
48+ id : info . id ,
49+ startDate : date ,
50+ endDate : Scheduler . getNextDayString ( date )
51+ } ) )
52+ . then ( ( data ) => {
53+ const slotData = [ ] ;
54+ data . service . resources [ 0 ] . availableSlots . forEach ( ( x ) => {
55+ slotData . push ( ...x . slots . split ( ', ' ) ) ;
56+ } ) ;
57+ return slotData ;
3358 } ) ;
34- return slotData ;
35- } ) ;
3659 }
3760
3861 static getBookingsPromise ( args ) {
@@ -43,23 +66,25 @@ class BusyWrapper {
4366 }
4467
4568 static createBookingPromise ( { date, time, duration } ) {
46- const params = {
47- service_id,
48- date,
49- time
50- } ;
51- if ( undefined !== duration )
52- params . duration = duration ;
53-
54- console . log ( params ) ;
55-
56- const createBookingBody = new CreateBookingBody ( params ) ;
57- return BookingsController . createBookingAsync ( { authorization, createBookingBody } ) ;
69+ return BusyAdapter
70+ . getServiceInfoAsync ( )
71+ . then ( ( info ) => {
72+ const params = {
73+ service_id : info . id ,
74+ date,
75+ time
76+ } ;
77+ if ( undefined !== duration )
78+ params . duration = duration ;
79+ console . log ( params ) ;
80+ const createBookingBody = new CreateBookingBody ( params ) ;
81+ return BookingsController . createBookingAsync ( { authorization, createBookingBody } ) ;
82+ } ) ;
5883 }
5984
6085 static cancelBookingPromise ( id ) {
6186 return BookingsController . cancelBookingByIdAsync ( { authorization, id } ) ;
6287 }
6388}
6489
65- export default BusyWrapper ;
90+ export default BusyAdapter ;
0 commit comments