|
1 |
| -// Module dependencies |
2 | 1 | const MongoClient = require('mongodb').MongoClient;
|
3 |
| - |
4 |
| -// Atlas connection outside of Lambda handler |
| 2 | +const ObjectID = require('mongodb').ObjectID; |
5 | 3 | const atlasUri = process.env['MONGODB_ATLAS_CLUSTER_URI'];
|
6 | 4 |
|
7 |
| -// Cache DB connection for future use |
| 5 | +const COLLNAME = 'Customers'; |
| 6 | + |
8 | 7 | let cachedDb;
|
9 | 8 |
|
10 |
| -// AWS event handler |
11 |
| -exports.handler = (evt, ctx, cb) => { |
| 9 | +exports.GET_handler = (evt, ctx, cb) => { |
12 | 10 |
|
13 |
| - // Set to false to allow re-use of database connections across lambda calls |
14 |
| - // See http://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-context.html |
15 |
| - ctx.callbackWaitsForEmptyEventLoop = false; |
| 11 | + // Set to false to allow re-use of database connections across lambda calls |
| 12 | + // See http://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-context.html |
| 13 | + ctx.callbackWaitsForEmptyEventLoop = false; |
16 | 14 |
|
17 |
| - // Executes query |
18 |
| - function executeQuery(db, coll, query, options, cb) { |
| 15 | + var options = { limit: 50 }; // to-do paging |
19 | 16 |
|
20 |
| - // Find documents in tasks collection |
21 |
| - db.collection(coll).find(query, options).toArray(function(err, docs) { |
22 |
| - if (err) process.exit(1); |
23 |
| - cb(null, docs); |
24 |
| - }); |
| 17 | + var query = {}; |
| 18 | + if ('id' in evt) { |
| 19 | + query._id = ObjectID.createFromHexString(evt.id); |
| 20 | + } |
25 | 21 |
|
26 |
| - } |
| 22 | + executeQuery(cachedDb, COLLNAME, query, options, cb); |
| 23 | +}; |
| 24 | + |
| 25 | +exports.GET_Renewals_handler = (evt, ctx, cb) => { |
27 | 26 |
|
28 |
| - try { |
| 27 | + // Set to false to allow re-use of database connections across lambda calls |
| 28 | + // See http://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-context.html |
| 29 | + ctx.callbackWaitsForEmptyEventLoop = false; |
29 | 30 |
|
30 |
| - if (!cachedDb || !cachedDb.serverConfig.isConnected()) { |
| 31 | + var options = { limit: 50 }; // to-do paging |
31 | 32 |
|
32 |
| - console.log(`=== CONNECTING TO MONGODB ATLAS ===`); |
| 33 | + var query = {}; |
33 | 34 |
|
34 |
| - MongoClient.connect(atlasUri, (err, db) => { |
| 35 | + if ('policyType' in evt) { |
| 36 | + query['policies.policyType'] = evt.policyType; |
| 37 | + } |
35 | 38 |
|
36 |
| - if (err) { |
37 |
| - console.error(`An error occurred! ${err}`); |
38 |
| - process.exit(1); |
39 |
| - } |
| 39 | + if ('expiresInDays' in evt) { |
40 | 40 |
|
41 |
| - // Assign db connection to variable for further use |
42 |
| - cachedDb = db; |
| 41 | + var startDt = new Date(); |
| 42 | + var endDt = new Date(startDt.getTime() + (evt.expiresInDays * 24 * 60 * 60 * 1000)); |
43 | 43 |
|
44 |
| - // Execute query |
45 |
| - executeQuery(cachedDb, evt.collection, evt.query, evt.options, cb); |
| 44 | + query['policies.nextRenewalDt'] = { '$gte': startDt, '$lte': endDt }; |
| 45 | + } |
46 | 46 |
|
47 |
| - }); |
| 47 | + if (('locLat' in evt) && ('locLon' in evt) && ('mileRadius' in evt)) { |
48 | 48 |
|
49 |
| - } else { |
50 |
| - executeQuery(cachedDb, evt.collection, evt.query, evt.options, cb); |
51 |
| - } |
| 49 | + var lon = evt.locLon; |
| 50 | + var lat = evt.locLat; |
| 51 | + var radius = evt.mileRadius / 3959; // to radians |
52 | 52 |
|
53 |
| - } catch (err) { |
54 |
| - console.error(`An error occurred! ${err}`); |
| 53 | + var geoQuery = { "$geoWithin" : { "$centerSphere" : [[lat,lon],radius] }}; |
| 54 | + |
| 55 | + if ('policyType' in evt) { |
| 56 | + switch (evt.policyType) { |
| 57 | + |
| 58 | + case 'home': |
| 59 | + query['policies.address.location'] = geoQuery; |
| 60 | + break; |
| 61 | + |
| 62 | + case 'auto': |
| 63 | + query['address.location'] = geoQuery; |
| 64 | + break; |
| 65 | + } |
55 | 66 | }
|
| 67 | + } |
| 68 | + |
| 69 | + executeQuery(cachedDb, COLLNAME, query, options, cb); |
| 70 | +}; |
| 71 | + |
| 72 | +function executeQuery(db, collName, query, options, cb) { |
| 73 | + |
| 74 | + function execute(db, collName, query, options, cb) { |
| 75 | + |
| 76 | + console.log('QUERY on ' + collName + ': ' + JSON.stringify(query)); |
| 77 | + |
| 78 | + db.collection(collName).find(query, options).toArray(function(err, docs) { |
| 79 | + if (err) process.exit(1); |
| 80 | + |
| 81 | + console.log('FOUND ' + docs.length + ' documents.'); |
| 82 | + |
| 83 | + cb(null, docs); |
| 84 | + }); |
| 85 | + } |
| 86 | + |
| 87 | + try { |
| 88 | + if (!cachedDb || !cachedDb.serverConfig.isConnected()) { |
| 89 | + |
| 90 | + console.log('=== CONNECTING TO MONGODB ATLAS ==='); |
56 | 91 |
|
| 92 | + MongoClient.connect(atlasUri, (err, db) => { |
| 93 | + if (err) { |
| 94 | + console.error(`An error occurred! ${err}`); |
| 95 | + process.exit(1); |
| 96 | + } |
| 97 | + cachedDb = db; |
| 98 | + execute(cachedDb, collName, query, options, cb); |
| 99 | + }); |
| 100 | + } else { |
| 101 | + execute(cachedDb, collName, query, options, cb); |
| 102 | + } |
| 103 | + } catch (err) { |
| 104 | + console.error(`An error occurred! ${err}`); |
| 105 | + } |
57 | 106 | }
|
0 commit comments