33 decryptAES,
44 encryptAES,
55 cleanProviders,
6- getFeatureFlags ,
6+ getSavedData ,
77 isNewThisPeriod,
88 saveProviderData,
99 createUsageRecords
@@ -23,15 +23,20 @@ const {
2323const { URL } = require ( 'whatwg-url' )
2424const cors = require ( 'micro-cors' ) ( )
2525
26- const _toJSON = error => {
27- return ! error
26+ const _toJSON = errorObj => {
27+ const formattedError = ! errorObj
2828 ? ''
29- : Object . getOwnPropertyNames ( error ) . reduce (
29+ : typeof errorObj === 'string'
30+ ? errorObj
31+ : Object . getOwnPropertyNames ( errorObj ) . reduce (
3032 ( jsonError , key ) => {
31- return { ...jsonError , [ key ] : error [ key ] }
33+ return { ...jsonError , [ key ] : errorObj [ key ] }
3234 } ,
3335 { type : 'error' }
3436 )
37+ return {
38+ error : formattedError
39+ }
3540}
3641
3742process . on ( 'unhandledRejection' , ( reason , p ) => {
@@ -44,19 +49,31 @@ process.on('unhandledRejection', (reason, p) => {
4449} )
4550
4651const notAuthorized = async ( req , res ) =>
47- send ( res , 401 , {
48- error : 'Referer or Origin not whitelisted'
49- } )
52+ send (
53+ res ,
54+ 401 ,
55+ _toJSON ( {
56+ type : 'notAuthorized' ,
57+ message : 'Referer or Origin not whitelisted'
58+ } )
59+ )
5060const notSupported = async ( req , res ) =>
51- send ( res , 405 , { error : 'Method not supported yet' } )
61+ send (
62+ res ,
63+ 405 ,
64+ _toJSON ( { type : 'notSupported' , message : 'Method not supported yet' } )
65+ )
5266const notEncrypted = async ( req , res ) =>
53- send ( res , 412 , { error : 'Payload must contain encrypted body' } )
54- const success = async ( req , res , payload ) => send ( res , 200 , payload )
55- const mocked = async ( req , res ) =>
56- success ( req , res , {
57- newThisPeriod : false ,
58- featureFlags : flags
59- } )
67+ send (
68+ res ,
69+ 412 ,
70+ _toJSON ( {
71+ type : 'notEncrypted' ,
72+ message : 'Payload must contain encrypted body'
73+ } )
74+ )
75+ const success = async ( req , res , encryptedPayload ) =>
76+ send ( res , 200 , encryptedPayload )
6077
6178const prepareRegex = string => {
6279 return string
@@ -116,22 +133,13 @@ const handleAuthorize = (req, res) => {
116133 }
117134}
118135
119- const handleError = ( req , res , error ) => {
120- console . warn ( 'handleError.error' , error )
121- // const jsonError = _toJSON(error)
122- // return send(res, error.statusCode || 500, jsonError)
123- //TODO!!!!!!!!!!!!!!!!!!!!!!
124- //TEMP!!!!!!!!!!!!!!!!!!!!!: swallow error
125- return mocked ( req , res )
126- }
127-
128136const getBody = async ( req , res ) => {
129137 let rawBody = { }
130138 try {
131139 rawBody = await json ( req )
132140 // console.log('rawBody',rawBody)
133141 } catch ( error ) {
134- console . error ( error )
142+ console . warn ( 'getBody, error' , error )
135143 }
136144
137145 if ( ! rawBody . encrypted ) {
@@ -144,6 +152,7 @@ const getBody = async (req, res) => {
144152 const body = safeParse (
145153 decryptAES ( rawBody . encrypted , sharedSecret , initialVector )
146154 )
155+ console . log ( 'body' , body )
147156
148157 return { body, initialVector }
149158}
@@ -164,65 +173,70 @@ const processPost = async (req, res) => {
164173 }
165174 const { body, initialVector } = decrypted
166175
167- // console.log('processPost, body',body)
176+ console . log ( 'processPost, body' , body )
168177 const { applicationId, collectionId, subscription, tracked } = body // collectionId = org, tracked = agent
169- // console.log('processPost, applicationId',applicationId)
170- // console.log('processPost, collectionId',collectionId)
171- // console.log('processPost, subscription',subscription)
172- // console.log('processPost, tracked',tracked)
178+ console . log ( 'processPost, applicationId' , applicationId )
179+ console . log ( 'processPost, collectionId' , collectionId )
180+ console . log ( 'processPost, subscription' , subscription )
181+ console . log ( 'processPost, tracked' , tracked )
173182 const { items : subscriptionItems } = subscription
174- // console.log('processPost, subscriptionItems',subscriptionItems)
183+ console . log ( 'processPost, subscriptionItems' , subscriptionItems )
175184
176185 if (
186+ ! initialVector ||
177187 ! applicationId ||
178188 ! collectionId ||
179189 ! subscription ||
180190 ! tracked ||
181191 ! subscriptionItems
182192 ) {
183- return send ( res , 400 , {
184- applicationId,
185- collectionId,
186- subscription,
187- tracked,
188- subscriptionItems
189- } )
193+ return send (
194+ res ,
195+ 400 ,
196+ _toJSON ( {
197+ type : 'requiredFields' ,
198+ method : 'processPost' ,
199+ fields : {
200+ applicationId,
201+ collectionId,
202+ subscription,
203+ tracked,
204+ subscriptionItems
205+ }
206+ } )
207+ )
190208 }
191209
192210 const responses = await Promise . all ( [
193211 await isNewThisPeriod ( applicationId , collectionId , subscription , tracked ) ,
194- await getFeatureFlags ( applicationId , collectionId , subscription )
195- ] ) . catch ( error => handleError ( req , res , error ) )
196- // console.log('processPost, responses',responses)
212+ await getSavedData ( applicationId , collectionId , subscription )
213+ ] )
197214
198215 const [ newThisPeriod , { featureFlags, providers } ] = responses
199- // console.log('processPost, newThisPeriod',newThisPeriod)
200- // console.log('processPost, featureFlags',providers)
201- // console.log('processPost, providers',providers)
216+ console . log ( 'processPost, newThisPeriod' , newThisPeriod )
217+ console . log ( 'processPost, featureFlags' , providers )
218+ console . log ( 'processPost, providers' , providers )
202219 const unencrypted = {
203220 newThisPeriod,
204221 featureFlags,
205222 providers : cleanProviders ( providers )
206223 }
207224 // console.log('processPost, unencrypted',unencrypted)
208225
209- const payload = {
226+ const encryptedPayload = {
210227 encrypted : encryptAES ( unencrypted , sharedSecret , initialVector )
211228 }
212229 // console.log('processPost, payload',payload)
213230
214231 if ( newThisPeriod ) {
215- await createUsageRecords ( subscriptionItems ) . catch ( error =>
216- handleError ( req , res , error )
217- )
218- return success ( req , res , payload )
232+ await createUsageRecords ( subscriptionItems )
233+ return success ( req , res , encryptedPayload )
219234 } else {
220- return success ( req , res , payload )
235+ return success ( req , res , encryptedPayload )
221236 }
222237 } catch ( error ) {
223- console . error ( 'Error' , error )
224- const jsonError = _toJSON ( error )
225- return send ( res , 500 , jsonError )
238+ console . error ( 'processPost, error' , error )
239+ return send ( res , 500 , _toJSON ( error ) )
226240 }
227241}
228242
@@ -243,15 +257,34 @@ const processPut = async (req, res) => {
243257 // console.log('processPut, providers', providers)
244258 // console.log('processPut, audit', audit)
245259
246- if ( ! applicationId || ! collectionId || ! providers || ! audit ) {
247- return send ( res , 400 , { applicationId, collectionId, providers, audit } )
260+ if (
261+ ! initialVector ||
262+ ! applicationId ||
263+ ! collectionId ||
264+ ! providers ||
265+ ! audit
266+ ) {
267+ return send (
268+ res ,
269+ 400 ,
270+ _toJSON ( {
271+ type : 'requiredFields' ,
272+ method : 'processPut' ,
273+ fields : {
274+ applicationId,
275+ collectionId,
276+ providers,
277+ audit
278+ }
279+ } )
280+ )
248281 }
249282
250283 await saveProviderData ( applicationId , collectionId , providers , audit ) . catch (
251284 error => handleError ( req , res , error )
252285 )
253286
254- const payload = {
287+ const encryptedPayload = {
255288 encrypted : encryptAES (
256289 {
257290 providers
@@ -261,19 +294,18 @@ const processPut = async (req, res) => {
261294 )
262295 }
263296
264- return success ( req , res , payload )
297+ return success ( req , res , encryptedPayload )
265298 } catch ( error ) {
266- console . error ( 'Error' , error )
267- const jsonError = _toJSON ( error )
268- return send ( res , 500 , jsonError )
299+ console . error ( 'processPut, error' , error )
300+ return send ( res , 500 , _toJSON ( error ) )
269301 }
270302}
271303
272304module . exports = cors (
273305 router (
274306 options ( '/*' , processOptions ) ,
275- post ( '/*' , processPost ) ,
276- put ( '/*' , processPut ) ,
307+ post ( '/*' , processPost ) , // track
308+ put ( '/*' , processPut ) , // save
277309 get ( '/*' , notSupported ) ,
278310 patch ( '/*' , notSupported ) ,
279311 del ( '/*' , notSupported ) ,
0 commit comments