Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2020
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.42 KB | None | 0 0
  1. const dbConfig = require("./utils/mongo-config");
  2. const mongo = require("mongodb");
  3. const moment = require("moment")
  4.  
  5. async function getRequest(event,dataRequest,dbConnection){
  6. if (event.ruc) dataRequest[0]["$match"]["business.ruc"] = event.ruc;
  7.  
  8. if (event.businessName)
  9. dataRequest[0]["$match"]["business.name"] = event.businessName;
  10.  
  11. if (event.email) dataRequest[0]["$match"]["user.email"] = event.email;
  12.  
  13. if (event.detailCode)
  14. dataRequest[0]["$match"]["detail.code"] = event.detailCode;
  15.  
  16. if (event.moduleCode)
  17. dataRequest[0]["$match"]["module.code"] = event.moduleCode;
  18.  
  19. if (event.subModuleCode)
  20. dataRequest[0]["$match"]["subModule.code"] = event.subModuleCode;
  21.  
  22. if (event.typeCode) dataRequest[0]["$match"]["type.code"] = event.typeCode;
  23.  
  24. if (event.startDate) {
  25. dataRequest[0]["$match"]["creationDate"] = {};
  26. dataRequest[0]["$match"]["creationDate"]["$gte"] = moment(event.startDate).format('YYYY-MM-DDTHH:mm:ss.SSSZ')
  27. dataRequest[0]["$match"]["creationDate"]["$lte"] = moment(event.startDate,"YYYY-MM-DD").format('YYYY-MM-DDTHH:mm:ss.SSSZ')
  28.  
  29. if (event.endDate)
  30. dataRequest[0]["$match"]["creationDate"]["$lte"] = moment(event.endDate,"YYYY-MM-DD").format('YYYY-MM-DDTHH:mm:ss.SSSZ')
  31. } else if (event.endDate) {
  32. dataRequest[0]["$match"]["creationDate"] = {};
  33. dataRequest[0]["$match"]["creationDate"]["$gte"] = moment(0).format('YYYY-MM-DDTHH:mm:ss.SSSZ')
  34. dataRequest[0]["$match"]["creationDate"]["$lte"] = moment(event.endDate,"YYYY-MM-DD").format('YYYY-MM-DDTHH:mm:ss.SSSZ')
  35. }
  36. console.log("dataRequest", JSON.stringify(dataRequest));
  37.  
  38. let data = await dbConfig.listDatabase(dbConnection, dataRequest);
  39. return data
  40. }
  41.  
  42. exports.handler = async (event, context) => {
  43. try {
  44. console.log(
  45. `=> List Navigation History with params: ${JSON.stringify(event)}`
  46. );
  47. context.callbackWaitsForEmptyEventLoop = false;
  48. let dataRequest;
  49. if (event != undefined) {
  50. dataRequest = [
  51. {
  52. $match: {}
  53. }
  54. ];
  55. } else {
  56. dataRequest = [
  57. {
  58. $match: {}
  59. }
  60. ];
  61. }
  62. if(!event.userId){
  63. let dbConnection = await dbConfig.connectToDatabase(dbConfig.MONGO_URL);
  64.  
  65. if (!dbConnection)
  66. throw new Error("Cannot get the connection with the database");
  67.  
  68. let data = await getRequest(event,dataRequest,dbConnection)
  69.  
  70.  
  71. if (data.body.length === 0) {
  72. return {
  73. statusCode: 204,
  74. body: data.body
  75. };
  76. }
  77.  
  78. console.log("=> Successfull commerces list",data.body.length);
  79.  
  80. return {
  81. statusCode: 200,
  82. body: data.body
  83. };
  84. }
  85.  
  86.  
  87. if(event.userId){
  88. let dbConnection = await dbConfig.connectToDatabase(dbConfig.MONGO_URL);
  89. if (!dbConnection)
  90. throw new Error("Cannot get the connection with the database");
  91. let data = await dbConfig.queryDatabase(dbConnection, {
  92. _id: mongo.ObjectID(event.userId),
  93. "status.code": "A"
  94. },{});
  95. if (!data) throw new Error('Cannot get data from the query');
  96. let responseArray = []
  97. console.log("dataRes", data.body)
  98. if(data.body.business.length>0){
  99. for (let i = 0; i < data.body.business.length; i++) {
  100. console.log("Negocio", i)
  101. let element = data.body.business[i];
  102. let dataRequest = [
  103. {
  104. $match: {}
  105. }
  106. ];
  107. event.ruc = element.ruc
  108. event.name = ""
  109. event.email = ""
  110. let dataRequestResponse = await getRequest(event, dataRequest,dbConnection)
  111. console.log("=> Successfull commerces list",dataRequestResponse.body.length);
  112. for (let j = 0; j < dataRequestResponse.body.length; j++) {
  113. responseArray.push(dataRequestResponse.body[j]);
  114. }
  115. }
  116. }
  117. if (responseArray.length === 0) {
  118. return {
  119. statusCode: 204,
  120. body: responseArray
  121. };
  122. }
  123.  
  124. console.log("=> Successfull commerces list",responseArray.length);
  125.  
  126. return {
  127. statusCode: 200,
  128. body: responseArray
  129. };
  130. }
  131. } catch (err) {
  132. console.log("=> Error", err);
  133.  
  134. return {
  135. statusCode: err.statusCode || 500,
  136. body: {
  137. error: err.name || "Exception",
  138. message: err.message || "Unknown error"
  139. }
  140. };
  141. }
  142. }
  143.  
  144.  
  145. //2030-01-23T09:00:00.100Z
  146. //2030-01-23T10:00:01.100Z
  147. (async () => {
  148. try {
  149. let event =
  150. {
  151. "ruta": "HISTORIAL",
  152. "ruc": "",
  153. "email": "",
  154. "detailCode": "",
  155. "moduleCode": "",
  156. "subModuleCode": "",
  157. "typeCode": "",
  158. "startDate": "2030-01-23T15:00:00.000Z",
  159. "endDate": "2030-01-24T15:00:00.000Z",
  160. "userId": ""
  161. }
  162. console.log(
  163. `=> List Navigation History with params: ${JSON.stringify(event)}`
  164. );
  165. //context.callbackWaitsForEmptyEventLoop = false;
  166. let dataRequest;
  167. if (event != undefined) {
  168. dataRequest = [
  169. {
  170. $match: {}
  171. }
  172. ];
  173. } else {
  174. dataRequest = [
  175. {
  176. $match: {}
  177. }
  178. ];
  179. }
  180. if(!event.userId){
  181. let dbConnection = await dbConfig.connectToDatabase(dbConfig.MONGO_URL);
  182.  
  183. if (!dbConnection)
  184. throw new Error("Cannot get the connection with the database");
  185.  
  186. let data = await getRequest(event,dataRequest,dbConnection)
  187.  
  188.  
  189. if (data.body.length === 0) {
  190. return {
  191. statusCode: 204,
  192. body: data.body
  193. };
  194. }
  195.  
  196. console.log("=> Successfull commerces list2",data);
  197. console.log("=> Successfull commerces list",data.length);
  198.  
  199. return {
  200. statusCode: 200,
  201. body: data.body
  202. };
  203. }
  204.  
  205.  
  206. if(event.userId){
  207. let dbConnection = await dbConfig.connectToDatabase(dbConfig.MONGO_URL);
  208. if (!dbConnection)
  209. throw new Error("Cannot get the connection with the database");
  210. let data = await dbConfig.queryDatabase(dbConnection, {
  211. _id: mongo.ObjectID(event.userId),
  212. "status.code": "A"
  213. },{});
  214. if (!data) throw new Error('Cannot get data from the query');
  215. let responseArray = []
  216. console.log("dataRes", data.body)
  217. if(data.body.business.length>0){
  218. for (let i = 0; i < data.body.business.length; i++) {
  219. console.log("Negocio", i)
  220. let element = data.body.business[i];
  221. let dataRequest = [
  222. {
  223. $match: {}
  224. }
  225. ];
  226. event.ruc = element.ruc
  227. event.name = ""
  228. event.email = ""
  229. let dataRequestResponse = await getRequest(event, dataRequest,dbConnection)
  230. console.log("=> Successfull commerces list",dataRequestResponse.body.length);
  231. for (let j = 0; j < dataRequestResponse.body.length; j++) {
  232. responseArray.push(dataRequestResponse.body[j]);
  233. }
  234. }
  235. }
  236. if (responseArray.length === 0) {
  237. return {
  238. statusCode: 204,
  239. body: responseArray
  240. };
  241. }
  242.  
  243. console.log("=> Successfull commerces list2",responseArray);
  244. console.log("=> Successfull commerces list",responseArray.length);
  245.  
  246. return {
  247. statusCode: 200,
  248. body: responseArray
  249. };
  250. }
  251. } catch (err) {
  252. console.log("=> Error", err);
  253.  
  254. return {
  255. statusCode: err.statusCode || 500,
  256. body: {
  257. error: err.name || "Exception",
  258. message: err.message || "Unknown error"
  259. }
  260. };
  261. }
  262. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement