1
1
const axios = require ( 'axios' ) ;
2
2
const { transform } = require ( 'camaro' ) ;
3
3
4
- const { airFranceConfig } = require ( '../../../config' ) ;
4
+ //const { airFranceConfig } = require('../../../config');
5
+ const config = require ( '../../../config' ) ;
5
6
6
7
const { basicDecorator } = require ( '../../../decorators/basic' ) ;
7
8
const { mapNdcRequestData } = require ( '../../../helpers/transformInputData/createOrder' ) ;
@@ -12,46 +13,119 @@ const {
12
13
mergeHourAndDate, reduceToObjectByKey, useDictionary, reduceContactInformation, splitPropertyBySpace
13
14
} = require ( '../../../helpers/parsers' ) ;
14
15
16
+ const offer = require ( '../../../helpers/models/offer' ) ;
17
+ const hotelResolver = require ( '../../../helpers/resolvers/hotel/orderCreateWithOffer' ) ;
18
+
19
+
20
+ function returnCleanError ( err , res ) {
21
+ if ( err . code && err . message ) {
22
+ res . status ( err . code ) . json ( { message : err . message } ) ;
23
+ }
24
+
25
+ // Default Error
26
+ else {
27
+ res . status ( 500 ) . json ( { message : 'A server error occured ' , details : err } ) ;
28
+ }
29
+ }
30
+
15
31
module . exports = basicDecorator ( async ( req , res ) => {
16
32
const requestBody = req . body ;
17
33
18
- const ndcRequestData = mapNdcRequestData ( requestBody ) ;
19
- const ndcBody = orderCreateRequestTemplate ( ndcRequestData ) ;
20
- const response = await axios . post ( 'https://ndc-rct.airfranceklm.com/passenger/distribmgmt/001451v01/EXT' ,
21
- ndcBody ,
22
- {
23
- headers : {
24
- 'Content-Type' : 'text/xml;charset=UTF-8' ,
25
- 'Accept-Encoding' : 'gzip,deflate' ,
26
- SOAPAction : '"http://www.af-klm.com/services/passenger/ProvideOrderCreate/provideOrderCreate"' ,
27
- api_key : airFranceConfig . apiKey ,
28
- } ,
29
- } ) ;
34
+ // Retrieve the offer
35
+ if ( requestBody . offerId ) {
36
+ offer . offerManager . getOffer ( requestBody . offerId )
37
+
38
+ . then ( storedOffer => {
39
+ // Check if there is no offer returned
40
+ if ( storedOffer == null ) {
41
+ res . status ( 404 ) . json ( { message : 'Offer expired or not found' } ) ;
42
+ }
30
43
31
- const { errors } = await transform ( response . data , ErrorsTransformTemplate ) ;
32
- if ( errors . length ) throw new Error ( `${ errors [ 0 ] . message } ` ) ;
44
+ // Handle an Accomodation offer
45
+ if ( storedOffer instanceof offer . AccommodationOffer ) {
46
+ // Resolve this query for an hotel offer
47
+ hotelResolver ( storedOffer , requestBody . passengers )
48
+ . then ( orderCreationResults => {
49
+ res . send ( orderCreationResults ) ;
50
+ //res.status(200).json(orderCreationResults);
51
+ } )
52
+ . catch ( err => {
53
+ returnCleanError ( err , res ) ;
54
+ } ) ;
55
+ }
33
56
34
- const createResults = await transform ( response . data , provideOrderCreateTransformTemplate ) ;
57
+ // Handle a flight offer
58
+ else if ( storedOffer instanceof offer . FlightOffer ) {
35
59
36
- createResults . order . itinerary . segments =
37
- mergeHourAndDate ( createResults . order . itinerary . segments , 'splittedDepartureDate' , 'splittedDepartureTime' , 'departureTime' ) ;
60
+ // TODO: Move this to dedicated module
61
+ const ndcRequestData = mapNdcRequestData ( requestBody ) ;
62
+ const ndcBody = orderCreateRequestTemplate ( ndcRequestData ) ;
63
+ axios . post (
64
+ 'https://ndc-rct.airfranceklm.com/passenger/distribmgmt/001451v01/EXT' ,
65
+ ndcBody ,
66
+ {
67
+ headers : {
68
+ 'Content-Type' : 'text/xml;charset=UTF-8' ,
69
+ 'Accept-Encoding' : 'gzip,deflate' ,
70
+ SOAPAction : '"http://www.af-klm.com/services/passenger/ProvideOrderCreate/provideOrderCreate"' ,
71
+ api_key : config . airFranceConfig . apiKey ,
72
+ } ,
73
+ }
74
+ )
75
+ . then ( response => {
76
+ // Attempt to parse as a an error
77
+ transform ( response . data , ErrorsTransformTemplate )
78
+ . then ( errors => {
79
+ // If an error is found, stop here
80
+ if ( errors . length ) throw new Error ( `${ errors [ 0 ] . message } ` ) ;
38
81
39
- createResults . order . itinerary . segments =
40
- mergeHourAndDate ( createResults . order . itinerary . segments , 'splittedArrivalDate' , 'splittedArrivalTime' , 'arrivalTime' ) ;
82
+ // Otherwise parse as a result
83
+ transform ( response . data , provideOrderCreateTransformTemplate )
84
+ . then ( createResults => {
85
+ createResults . order . itinerary . segments =
86
+ mergeHourAndDate ( createResults . order . itinerary . segments , 'splittedDepartureDate' , 'splittedDepartureTime' , 'departureTime' ) ;
87
+
88
+ createResults . order . itinerary . segments =
89
+ mergeHourAndDate ( createResults . order . itinerary . segments , 'splittedArrivalDate' , 'splittedArrivalTime' , 'arrivalTime' ) ;
90
+
91
+ createResults . order . itinerary . segments = reduceToObjectByKey ( createResults . order . itinerary . segments ) ;
92
+
93
+ createResults . order . price . commission = createResults . order . price . commission . reduce ( ( total , { value} ) => total + parseFloat ( value ) , 0 ) . toString ( ) ;
94
+ createResults . order . price . taxes = createResults . order . price . taxes . reduce ( ( total , { value} ) => total + parseFloat ( value ) , 0 ) . toString ( ) ;
95
+
96
+ createResults . order . contactList = reduceToObjectByKey ( createResults . order . contactList ) ;
97
+ createResults . order . passengers = useDictionary ( createResults . order . passengers , createResults . order . contactList , 'contactInformation' ) ;
98
+ createResults . order . passengers = splitPropertyBySpace ( createResults . order . passengers , 'firstnames' ) ;
99
+ createResults . order . passengers = splitPropertyBySpace ( createResults . order . passengers , 'lastnames' ) ;
100
+ createResults . order . passengers = reduceContactInformation ( createResults . order . passengers ) ;
101
+ createResults . order . passengers = reduceToObjectByKey ( createResults . order . passengers ) ;
102
+
103
+ delete createResults . order . contactList ;
104
+
105
+ res . status ( 200 ) . json ( createResults ) ;
106
+ } )
41
107
42
- createResults . order . itinerary . segments = reduceToObjectByKey ( createResults . order . itinerary . segments ) ;
108
+ } )
109
+ } )
43
110
44
- createResults . order . price . commission = createResults . order . price . commission . reduce ( ( total , { value} ) => total + parseFloat ( value ) , 0 ) . toString ( ) ;
45
- createResults . order . price . taxes = createResults . order . price . taxes . reduce ( ( total , { value} ) => total + parseFloat ( value ) , 0 ) . toString ( ) ;
111
+ }
46
112
47
- createResults . order . contactList = reduceToObjectByKey ( createResults . order . contactList ) ;
48
- createResults . order . passengers = useDictionary ( createResults . order . passengers , createResults . order . contactList , 'contactInformation' ) ;
49
- createResults . order . passengers = splitPropertyBySpace ( createResults . order . passengers , 'firstnames' ) ;
50
- createResults . order . passengers = splitPropertyBySpace ( createResults . order . passengers , 'lastnames' ) ;
51
- createResults . order . passengers = reduceContactInformation ( createResults . order . passengers ) ;
52
- createResults . order . passengers = reduceToObjectByKey ( createResults . order . passengers ) ;
113
+ // Handle other types of offer
114
+ else {
115
+ res . status ( 500 ) . json ( { message : 'Unable to understand the offer type' } ) ;
116
+ }
117
+
118
+ } )
119
+
120
+ . catch ( err => {
121
+ // Handle Errors with a defined code and message
122
+ returnCleanError ( err , res ) ;
123
+ } ) ;
124
+ }
53
125
54
- delete createResults . order . contactList ;
126
+ // Unable to find the offerId
127
+ else {
128
+ res . status ( 400 ) . json ( { message : 'Missing mandatory field: offerId' } ) ;
55
129
56
- res . status ( 200 ) . json ( createResults ) ;
130
+ }
57
131
} ) ;
0 commit comments