bidireccionalgmailcontacts-celerex
Advanced tools
Comparing version 1.0.2 to 1.0.3
{ | ||
"name": "bidireccionalgmailcontacts-celerex", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "", | ||
@@ -13,4 +13,7 @@ "main": "src/index.js", | ||
"dependencies": { | ||
"axios": "^0.26.0", | ||
"moment": "^2.29.1", | ||
"moment-timezone": "^0.5.34", | ||
"mongoose": "^6.2.3" | ||
} | ||
} |
166
src/index.js
const mongoose = require("mongoose"); | ||
const auth = require("../models/auth"); | ||
const axios = require("axios"); | ||
const moment = require("moment"); | ||
const momentTZ = require("moment-timezone"); | ||
/******************Gobla Variables */ | ||
const maxMinutes = 2; | ||
let hoursOffWorkFlowJob = [ | ||
18, 19, 20, 21, 22, 23, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, | ||
]; | ||
/******************************* */ | ||
const getUsers = async (serverMG, database) => { | ||
@@ -15,3 +26,3 @@ try { | ||
} catch (error) { | ||
throw new Error; | ||
throw new Error(); | ||
} | ||
@@ -22,2 +33,141 @@ | ||
const hasBeenUpdated = (contactObj, now, isNewday) => { | ||
//console.log(contactObj.names); | ||
//este pedazo de codigo es el encargado de saber si el contacto fue editado durante los 5 minutos | ||
let sourceTypeContactArray; | ||
let updateTime; | ||
try { | ||
const { sources } = contactObj.metadata; | ||
if (!sources) return false; | ||
sourceTypeContactArray = sources.filter( | ||
(source) => source.type === "CONTACT" | ||
); | ||
if (sourceTypeContactArray.length < 0) return false; | ||
updateTime = sourceTypeContactArray[0].updateTime; | ||
let duration = moment.duration(now.diff(updateTime)); | ||
let minutes = parseInt(duration.asMinutes()); | ||
//console.log("minutes" + minutes); | ||
if (minutes <= maxMinutes) return true; | ||
if (isNewday) { | ||
const fullMxDate = momentTZ(updateTime).tz("America/Mexico_City"); | ||
const hoursOfFullMx = fullMxDate.hour(); | ||
//console.log("hoursOfFullMx " + hoursOfFullMx) | ||
const isUpdatedOffOfTime = hoursOffWorkFlowJob.some( | ||
(hourOff) => hourOff == hoursOfFullMx | ||
); | ||
const nomMx2 = momentTZ(new Date()).tz("America/Mexico_City"); | ||
const nomMx3 = momentTZ(new Date()).tz("America/Mexico_City"); | ||
const yesterdayDate = nomMx2.subtract(1, "days").startOf("day"); | ||
//console.log(yesterdayDate); | ||
const wasYesterdayModify = fullMxDate.isAfter(yesterdayDate); | ||
if (isUpdatedOffOfTime && wasYesterdayModify) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} catch (error) { | ||
console.log(error); | ||
throw new Error("Something went wrong getting the information"); | ||
} | ||
}; | ||
const personFields = | ||
"names,biographies,occupations,genders,emailAddresses,organizations,phoneNumbers,addresses,nicknames,birthdays,events,relations,memberships,metadata,userDefined"; | ||
const getContactsUser = async (token, email, nowTime, isNewday) => { | ||
//hace un fetch para obtener los contactos actualizados, de 10 en 10 si es necesario se obtiene mas | ||
let allContactsPerUSer = []; | ||
let nextPageToken = null; | ||
let iteraciones = 0; | ||
let isFirstTime = true; | ||
let paramsAxios = { | ||
personFields, | ||
sortOrder: `LAST_MODIFIED_DESCENDING`, | ||
sources: `READ_SOURCE_TYPE_CONTACT`, | ||
pageSize: 10, | ||
}; | ||
while (nextPageToken || isFirstTime) { | ||
if (nextPageToken) { | ||
paramsAxios = { ...paramsAxios, pageToken: nextPageToken }; | ||
} | ||
try { | ||
const response = await axios({ | ||
url: `https://people.googleapis.com/v1/people/me/connections`, | ||
headers: { | ||
Authorization: `Bearer ${token}`, | ||
}, | ||
params: paramsAxios, | ||
}); | ||
const googleObj = response.data; | ||
const connections = googleObj.connections; | ||
const maxGotContactsInRequest = connections.length; | ||
if (maxGotContactsInRequest < 1) | ||
return { isError: false, connections: [] }; | ||
const firstContact = connections[0]; | ||
if (hasBeenUpdated(firstContact, nowTime, isNewday)) { | ||
console.log("actualizado el primero"); | ||
//checar el el ultimo | ||
lastContact = connections[maxGotContactsInRequest - 1]; | ||
if (hasBeenUpdated(lastContact, nowTime, isNewday)) { | ||
console.log("El ultimo contacto ha sido actualizado"); | ||
const allContactsPerUSerAux = [...allContactsPerUSer]; | ||
allContactsPerUSer = [...allContactsPerUSerAux, ...connections]; | ||
nextPageToken = googleObj.nextPageToken; | ||
} else { | ||
nextPageToken = null; | ||
const segmentContacts = connections.filter((conObj) => | ||
hasBeenUpdated(conObj, nowTime, isNewday) | ||
); | ||
const allContactsPerUSerAux = [...allContactsPerUSer]; | ||
allContactsPerUSer = [...allContactsPerUSerAux, ...segmentContacts]; | ||
} | ||
} else { | ||
//el "primer" contacto de el segmento actual no fue actualizado | ||
if (isFirstTime) { | ||
//no contacts updated | ||
nextPageToken = null; | ||
console.log("no contacts updated"); | ||
//console.log(`No hay ningun contacto para actualizar ${email}`) | ||
//verificar si el usuario Actualizo durante las horas que el sistema estuvo apagado | ||
} else { | ||
nextPageToken = null; | ||
//console.log("no mas contactos por actualizar en este segemento") | ||
} | ||
} | ||
isFirstTime = false; | ||
iteraciones++; | ||
} catch (error) { | ||
console.log(error); | ||
console.log(error.response.data); | ||
console.log(`An Error ocurred failed to fetch Contacts ${email}`); | ||
return { isError: true, connections: [] }; | ||
//return { error: true, msg: "An Error ocurred failed to fetch Contacts" } | ||
} | ||
} | ||
//console.log(allContactsPerUSer) | ||
return { isError: false, connections: allContactsPerUSer }; | ||
}; | ||
/* getUsers() | ||
@@ -31,4 +181,18 @@ .then((res) => { | ||
/* let now = moment(new Date()); | ||
const isNewday = true; | ||
const token = ""; | ||
const email = "pruebasdev"; */ | ||
/* getContactsUser(token, email, now, isNewday) | ||
.then(({ isError, connections }) => { | ||
//console.log(connections); | ||
}) | ||
.catch((error) => { | ||
console.log(error); | ||
}); */ | ||
module.exports = { | ||
getUsers, | ||
getContactsUser, | ||
}; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6594
3
170
4
+ Addedaxios@^0.26.0
+ Addedmoment@^2.29.1
+ Addedmoment-timezone@^0.5.34
+ Addedaxios@0.26.1(transitive)
+ Addedfollow-redirects@1.15.9(transitive)
+ Addedmoment@2.30.1(transitive)
+ Addedmoment-timezone@0.5.46(transitive)