Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const dbConfig = require("./utils/mongo-config");
- const mongo = require("mongodb");
- const moment = require("moment")
- async function getRequest(event,dataRequest,dbConnection){
- if (event.ruc) dataRequest[0]["$match"]["business.ruc"] = event.ruc;
- if (event.businessName)
- dataRequest[0]["$match"]["business.name"] = event.businessName;
- if (event.email) dataRequest[0]["$match"]["user.email"] = event.email;
- if (event.detailCode)
- dataRequest[0]["$match"]["detail.code"] = event.detailCode;
- if (event.moduleCode)
- dataRequest[0]["$match"]["module.code"] = event.moduleCode;
- if (event.subModuleCode)
- dataRequest[0]["$match"]["subModule.code"] = event.subModuleCode;
- if (event.typeCode) dataRequest[0]["$match"]["type.code"] = event.typeCode;
- if (event.startDate) {
- dataRequest[0]["$match"]["creationDate"] = {};
- dataRequest[0]["$match"]["creationDate"]["$gte"] = moment(event.startDate).format('YYYY-MM-DDTHH:mm:ss.SSSZ')
- dataRequest[0]["$match"]["creationDate"]["$lte"] = moment(event.startDate,"YYYY-MM-DD").format('YYYY-MM-DDTHH:mm:ss.SSSZ')
- if (event.endDate)
- dataRequest[0]["$match"]["creationDate"]["$lte"] = moment(event.endDate,"YYYY-MM-DD").format('YYYY-MM-DDTHH:mm:ss.SSSZ')
- } else if (event.endDate) {
- dataRequest[0]["$match"]["creationDate"] = {};
- dataRequest[0]["$match"]["creationDate"]["$gte"] = moment(0).format('YYYY-MM-DDTHH:mm:ss.SSSZ')
- dataRequest[0]["$match"]["creationDate"]["$lte"] = moment(event.endDate,"YYYY-MM-DD").format('YYYY-MM-DDTHH:mm:ss.SSSZ')
- }
- console.log("dataRequest", JSON.stringify(dataRequest));
- let data = await dbConfig.listDatabase(dbConnection, dataRequest);
- return data
- }
- exports.handler = async (event, context) => {
- try {
- console.log(
- `=> List Navigation History with params: ${JSON.stringify(event)}`
- );
- context.callbackWaitsForEmptyEventLoop = false;
- let dataRequest;
- if (event != undefined) {
- dataRequest = [
- {
- $match: {}
- }
- ];
- } else {
- dataRequest = [
- {
- $match: {}
- }
- ];
- }
- if(!event.userId){
- let dbConnection = await dbConfig.connectToDatabase(dbConfig.MONGO_URL);
- if (!dbConnection)
- throw new Error("Cannot get the connection with the database");
- let data = await getRequest(event,dataRequest,dbConnection)
- if (data.body.length === 0) {
- return {
- statusCode: 204,
- body: data.body
- };
- }
- console.log("=> Successfull commerces list",data.body.length);
- return {
- statusCode: 200,
- body: data.body
- };
- }
- if(event.userId){
- let dbConnection = await dbConfig.connectToDatabase(dbConfig.MONGO_URL);
- if (!dbConnection)
- throw new Error("Cannot get the connection with the database");
- let data = await dbConfig.queryDatabase(dbConnection, {
- _id: mongo.ObjectID(event.userId),
- "status.code": "A"
- },{});
- if (!data) throw new Error('Cannot get data from the query');
- let responseArray = []
- console.log("dataRes", data.body)
- if(data.body.business.length>0){
- for (let i = 0; i < data.body.business.length; i++) {
- console.log("Negocio", i)
- let element = data.body.business[i];
- let dataRequest = [
- {
- $match: {}
- }
- ];
- event.ruc = element.ruc
- event.name = ""
- event.email = ""
- let dataRequestResponse = await getRequest(event, dataRequest,dbConnection)
- console.log("=> Successfull commerces list",dataRequestResponse.body.length);
- for (let j = 0; j < dataRequestResponse.body.length; j++) {
- responseArray.push(dataRequestResponse.body[j]);
- }
- }
- }
- if (responseArray.length === 0) {
- return {
- statusCode: 204,
- body: responseArray
- };
- }
- console.log("=> Successfull commerces list",responseArray.length);
- return {
- statusCode: 200,
- body: responseArray
- };
- }
- } catch (err) {
- console.log("=> Error", err);
- return {
- statusCode: err.statusCode || 500,
- body: {
- error: err.name || "Exception",
- message: err.message || "Unknown error"
- }
- };
- }
- }
- //2030-01-23T09:00:00.100Z
- //2030-01-23T10:00:01.100Z
- (async () => {
- try {
- let event =
- {
- "ruta": "HISTORIAL",
- "ruc": "",
- "email": "",
- "detailCode": "",
- "moduleCode": "",
- "subModuleCode": "",
- "typeCode": "",
- "startDate": "2030-01-23T15:00:00.000Z",
- "endDate": "2030-01-24T15:00:00.000Z",
- "userId": ""
- }
- console.log(
- `=> List Navigation History with params: ${JSON.stringify(event)}`
- );
- //context.callbackWaitsForEmptyEventLoop = false;
- let dataRequest;
- if (event != undefined) {
- dataRequest = [
- {
- $match: {}
- }
- ];
- } else {
- dataRequest = [
- {
- $match: {}
- }
- ];
- }
- if(!event.userId){
- let dbConnection = await dbConfig.connectToDatabase(dbConfig.MONGO_URL);
- if (!dbConnection)
- throw new Error("Cannot get the connection with the database");
- let data = await getRequest(event,dataRequest,dbConnection)
- if (data.body.length === 0) {
- return {
- statusCode: 204,
- body: data.body
- };
- }
- console.log("=> Successfull commerces list2",data);
- console.log("=> Successfull commerces list",data.length);
- return {
- statusCode: 200,
- body: data.body
- };
- }
- if(event.userId){
- let dbConnection = await dbConfig.connectToDatabase(dbConfig.MONGO_URL);
- if (!dbConnection)
- throw new Error("Cannot get the connection with the database");
- let data = await dbConfig.queryDatabase(dbConnection, {
- _id: mongo.ObjectID(event.userId),
- "status.code": "A"
- },{});
- if (!data) throw new Error('Cannot get data from the query');
- let responseArray = []
- console.log("dataRes", data.body)
- if(data.body.business.length>0){
- for (let i = 0; i < data.body.business.length; i++) {
- console.log("Negocio", i)
- let element = data.body.business[i];
- let dataRequest = [
- {
- $match: {}
- }
- ];
- event.ruc = element.ruc
- event.name = ""
- event.email = ""
- let dataRequestResponse = await getRequest(event, dataRequest,dbConnection)
- console.log("=> Successfull commerces list",dataRequestResponse.body.length);
- for (let j = 0; j < dataRequestResponse.body.length; j++) {
- responseArray.push(dataRequestResponse.body[j]);
- }
- }
- }
- if (responseArray.length === 0) {
- return {
- statusCode: 204,
- body: responseArray
- };
- }
- console.log("=> Successfull commerces list2",responseArray);
- console.log("=> Successfull commerces list",responseArray.length);
- return {
- statusCode: 200,
- body: responseArray
- };
- }
- } catch (err) {
- console.log("=> Error", err);
- return {
- statusCode: err.statusCode || 500,
- body: {
- error: err.name || "Exception",
- message: err.message || "Unknown error"
- }
- };
- }
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement