Skip to content
This repository was archived by the owner on Apr 8, 2024. It is now read-only.

Commit 7664527

Browse files
authored
Merge pull request #42 from uniquelyparticular/fix/installationMigration
Fix/installation migration
2 parents 6b974c9 + 2749a4e commit 7664527

File tree

3 files changed

+151
-96
lines changed

3 files changed

+151
-96
lines changed

src/flags.js

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,41 +4,46 @@ const ROLE_ADMIN = 'admin'
44
const ROLE_AGENT = 'agent'
55

66
/** ORDER FLAGS (Commerce Provider) **/
7-
const FLAG_ORDER_HISTORY = 'order_history'
8-
const FLAG_ORDER_DETAILS = 'order_details'
9-
const FLAG_ORDER_FILTER = 'order_filter'
10-
const FLAG_ORDER_PIN = 'order_pin'
11-
const FLAG_ORDER_SORT = 'order_sort'
12-
const FLAG_ORDER_PAYMENT = 'order_payment'
13-
const FLAG_ORDER_REFUND = 'order_refund'
14-
const FLAG_ORDER_CREATE = 'order_create'
15-
const FLAG_ORDER_DUPLICATE = 'order_duplicate'
16-
const FLAG_ORDER_MODIFY = 'order_modify'
7+
const FLAG_ORDERS_LIST = 'orders_list' // FREE
8+
const FLAG_ORDER_DETAILS = 'order_details' // FREE
9+
10+
const FLAG_ORDER_FILTER = 'order_filter' // STARTER
11+
const FLAG_ORDER_PIN = 'order_pin' // STARTER
12+
const FLAG_ORDER_SORT = 'order_sort' // STARTER
13+
14+
const FLAG_ORDER_TIMELINE = 'order_timeline' // PROFESIONAL
15+
16+
const FLAG_ORDER_PAYMENT = 'order_payment' // ENTERPRISE
17+
const FLAG_ORDER_REFUND = 'order_refund' // ENTERPRISE
18+
const FLAG_ORDER_CREATE = 'order_create' // ENTERPRISE
19+
const FLAG_ORDER_DUPLICATE = 'order_duplicate' // ENTERPRISE
20+
const FLAG_ORDER_MODIFY = 'order_modify' // ENTERPRISE
1721

1822
/** PAYMENT FLAGS (Payment or Commerce Provider) **/
19-
const FLAG_PAYMENT_TRANSACTIONS = 'payment_transactions'
23+
const FLAG_PAYMENT_TRANSACTIONS = 'payment_transactions' // PROFESSIONAL
2024
const FLAG_PAYMENT_USER_INTERFACE = 'payment_user_interface' // TODO: implement
2125
const FLAG_PAYMENT_STORED_INTRUMENTS = 'payment_stored_instruments' // TODO: implement
2226

2327
/** SHIPPING FLAGS (Shipping Provider) **/
24-
const FLAG_SHIPPING_STATUS = 'shipping_status'
25-
const FLAG_SHIPPING_LABEL = 'shipping_label' // TODO: implement
28+
const FLAG_SHIPPING_STATUS = 'shipping_status' // PROFESSIONAL
29+
const FLAG_SHIPPING_LABEL = 'shipping_label' // ENTERPRISE TODO: implement
2630

2731
/** SEARCH FLAGS (Search or Commerce Provider?) **/
28-
const FLAG_SEARCH_CUSTOMERS = 'search_customers'
29-
const FLAG_SEARCH_PRODUCTS = 'search_products' // TODO: implement
32+
const FLAG_SEARCH_CUSTOMERS = 'search_customers' // STARTER
33+
const FLAG_SEARCH_PRODUCTS = 'search_products' // ENTERPRISE TODO: implement
3034

3135
/** TAX FLAGS (Tax Provider?) **/
32-
const FLAG_TAX_CALCULATOR = 'tax_calculator' // TODO: implement
36+
const FLAG_TAX_CALCULATOR = 'tax_calculator' // ENTERPRISE TODO: implement
3337

3438
module.exports = [
3539
ROLE_ADMIN,
3640
ROLE_AGENT,
37-
FLAG_ORDER_HISTORY,
41+
FLAG_ORDERS_LIST,
3842
FLAG_ORDER_DETAILS,
3943
FLAG_ORDER_FILTER,
4044
FLAG_ORDER_PIN,
4145
FLAG_ORDER_SORT,
46+
FLAG_ORDER_TIMELINE,
4247
FLAG_ORDER_PAYMENT,
4348
FLAG_ORDER_REFUND,
4449
FLAG_ORDER_CREATE,

src/index.js

Lines changed: 95 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const {
33
decryptAES,
44
encryptAES,
55
cleanProviders,
6-
getFeatureFlags,
6+
getSavedData,
77
isNewThisPeriod,
88
saveProviderData,
99
createUsageRecords
@@ -23,15 +23,20 @@ const {
2323
const { URL } = require('whatwg-url')
2424
const 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

3742
process.on('unhandledRejection', (reason, p) => {
@@ -44,19 +49,31 @@ process.on('unhandledRejection', (reason, p) => {
4449
})
4550

4651
const 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+
)
5060
const 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+
)
5266
const 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

6178
const 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-
128136
const 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

272304
module.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

Comments
 (0)