Fixed knowledgestate properties for anthology.

This commit is contained in:
Norm Rasmussen
2024-04-05 17:16:58 -04:00
parent 0aa9c45ed8
commit dafb43e6ce
6 changed files with 630 additions and 111 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 368 KiB

View File

@ -0,0 +1,203 @@
const axios = require("axios")
const fs = require("fs");
const { parse } = require("csv-parse");
// let xApiKey = 'XxDBlgH3CNSiD6yDgqZgpjjxB' //Austin SCHOOL API KEY
let xApiKey = 'HdZFoXGCFpt8NnTOzIQY0kVDj' // <<< This is for sandata's school
let allNPUsers = [];
let allUsers = [];
let getAllNPUsers = async (num) => {
let page = num;
console.log(`RETRIEVING PAGE ${num} OF USERS`)
await axios({
method: 'get',
// url: 'https://api.northpass.com/v2/people/?page=' + page + '&limit=100',
url: 'https://api.northpass.com/v2/people/?page=' + page + '&limit=100',
headers: {
'accept': '*/*',
'x-api-key': xApiKey,
'content-type': 'application/json'
}
})
.then(async (res) => {
if (res.data.links.next != null) {
page++;
for (let i = 0; i < res.data.data.length; i++) {
allNPUsers.push(res.data.data[i]);
}
await getAllNPUsers(page);
} else {
for (let i = 0; i < res.data.data.length; i++) {
allNPUsers.push(res.data.data[i]);
}
}
})
.catch(err => {
console.log(err);
})
}
// let getAllNPGroups = async (num) => {
// let page = num;
// console.log(`RETRIEVING PAGE ${num} OF GROUPS`)
// await axios({
// method: 'get',
// // url: 'https://api.northpass.com/v2/groups/?page=' + page + '&limit=100',
// url: 'https://api.northpass.com/v2/groups/?page=' + page + '&limit=100',
// headers: {
// 'accept': '*/*',
// 'x-api-key': xApiKey,
// 'content-type': 'application/json'
// }
// })
// .then(async (res) => {
// // console.log(res.data.data)
// // console.log(res.data.links.next != null);
// if (res.data.links.next != null) {
// // console.log("not on last page")
// page++;
// for (let i = 0; i < res.data.data.length; i++) {
// //console.log(res.data.data[i].attributes.name)
// allNPGroups[res.data.data[i].attributes.name] = res.data.data[i].id
// }
// await getAllNPGroups(page);
// } else {
// //console.log("on last page")
// for (let i = 0; i < res.data.data.length; i++) {
// //console.log(res.data.data[i].attributes.name)
// // allNPGroups[res.data.data[i].id] = res.data.data[i].attributes.name
// allNPGroups[res.data.data[i].attributes.name] = res.data.data[i].id
// }
// }
// })
// .catch(err => {
// console.log(err);
// })
// };
// -----------------------------------------------------------------------------------------------------------------------------------------------------------
// CLEAR FUNCTIONS
const clearSchoolUsers = async (someArray, start) => {
let iterator = start;
let user = someArray[iterator];
// console.log(user.attributes.email)
if(allUsers.indexOf(user.attributes.email.toLowerCase()) > -1){
// console.log('SHOULD BE CLEARED >>> ' + user.attributes.email)
// console.log(user);
await axios({
method: "delete",
// url: `https://api.northpass.com/v2/people/${user.id}`,
url: `https://api.northpass.com/v2/people/${user.id}`,
headers: {
accept: "*/*",
"x-api-key": xApiKey,
"content-type": "application/json",
},
})
.then(async () => {
console.log(`TERMINATED USER WITH EMAIL: ${user.attributes.email}`)
console.log('\x1b[36m%s\x1b[0m', `COMPLETED DELETING USER AT INDEX ${iterator}`);
iterator += 1;
if(iterator >= someArray.length){
return;
} else {
await clearSchoolUsers(someArray, iterator)
}
})
} else {
iterator += 1;
if(iterator >= someArray.length){
return;
} else {
await clearSchoolUsers(someArray, iterator)
}
}
}
// const clearSchoolGroups = async (someArray, start) => {
// // console.log(someArray)
// let iterator = start;
// let groupName = someArray[iterator];
// let groupIDVal = allNPGroups[groupName];
// if(groupName != 'PRIMARY ACCOUNT'){
// await axios({
// method: "delete",
// // url: `https://api.northpass.com/v2/groups/${groupIDVal}`,
// url: `https://api.northpass.com/v2/groups/${groupIDVal}`,
// headers: {
// accept: "*/*",
// "x-api-key": xApiKey,
// "content-type": "application/json",
// },
// })
// .then(async () => {
// console.log(`TERMINATED GROUP WITH UUID: ${groupIDVal}`)
// console.log('\x1b[36m%s\x1b[0m', `COMPLETED DELETING GROUP AT INDEX ${iterator}`);
// iterator += 1;
// if(iterator >= someArray.length){
// return;
// } else {
// await clearSchoolGroups(someArray, iterator)
// }
// })
// } else {
// iterator += 1;
// if(iterator >= someArray.length){
// return;
// } else {
// await clearSchoolGroups(someArray, iterator)
// }
// }
// }
let removeUsers = async () => {
await getAllNPUsers(1)
.then(async () => {
await clearSchoolUsers(allNPUsers, 0);
})
}
// let removeGroups = async () => {
// await getAllNPGroups(1)
// .then(async () => {
// // console.log(Object.keys(allNPGroups))
// await clearSchoolGroups(Object.keys(allNPGroups), 0);
// })
// }
fs.createReadStream("./sandataPAusers.csv")
.pipe(parse({ delimiter: ','}))
.on("data", function (row) {
if(row[0] !== 'Email'){
// allUsers.push({
// email: row[0]
// });
allUsers.push(row[0].toLowerCase());
}
})
.on("error", function (error) {
console.log(error.message);
})
.on("end", async function () {
console.log("FINISHED COLLECTING ALL NEW USERS");
console.log(allUsers);
console.log(allUsers.length);
// await executeFunctionChain();
await removeUsers();
})
// removeGroups();

66
Scripts/Sandata/import.js Normal file
View File

@ -0,0 +1,66 @@
const axios = require("axios")
const fs = require("fs");
const { parse } = require("csv-parse");
//let xApiKey = "XxDBlgH3CNSiD6yDgqZgpjjxB" // <<< This is for Ausitn's School
let xApiKey = 'HdZFoXGCFpt8NnTOzIQY0kVDj' // <<< This is for sandata's school
let allUsers = [];
fs.createReadStream("./sandataNevadaUsers.csv")
.pipe(parse({ delimiter: ','}))
.on("data", function (row) {
if(row[0] !== 'Email'){
allUsers.push({
email: row[0],
group: 'ST - Nevada (DHCFP)'
});
}
})
.on("error", function (error) {
console.log(error.message);
})
.on("end", async function () {
console.log("FINISHED COLLECTING ALL NEW USERS");
// console.log(allUsers[0]);
console.log(allUsers.length);
await executeFunctionChain();
})
let executeFunctionChain = async () => {
for(let i = 0; i < allUsers.length; i++){
await axios({
method: 'post',
url: 'https://api.northpass.com/v2/bulk/people',
data: {
"data": {
"attributes": {
"people": [{
"email": allUsers[i].email,
// "email": "joojinhyung@gmail.com",
"groups": allUsers[i].group
//"groups": [10593ca2-daa7-41fb-ad12-1de77d51fe81] >> wrong, uses name not id
}]
}
}
},
headers: {
'accept': 'application/json',
'x-api-key': xApiKey,
'content-type': 'application/json'
}
})
.then((res) => {
console.log(`SUCCESSFULLY ADDED USER ${allUsers[i].email} TO GROUP ${allUsers[i].group}`)
console.log(`COMPLETED USER ${i + 1}`)
// console.log(res.data);
return;
})
.catch((err) => {
console.log(err.data);
})
}
}

View File

@ -0,0 +1,193 @@
const axios = require("axios")
const fs = require("fs");
const { parse } = require("csv-parse");
//let xApiKey = "XxDBlgH3CNSiD6yDgqZgpjjxB" // <<< This is for Ausitn's School
let xApiKey = 'HdZFoXGCFpt8NnTOzIQY0kVDj' // <<< This is for sandata's school
let allNPUsers = [];
let allUsers = [];
let userExistsObj = {};
const getAllNPUsers = async (num) => {
if(num === 1){
console.log('\x1b[33m%s\x1b[0m', `RETRIEVING ALL NP USERS`);
}
console.log(`RETRIEVING PAGE ${num} OF USERS`)
let page = num;
await axios({
method: 'get',
url: 'https://api.northpass.com/v2/people/?page=' + page + '&limit=100',
headers: {
'accept': '*/*',
'x-api-key': xApiKey,
'content-type': 'application/json'
}
})
.then(async (res) => {
if (res.data.links.next != null) {
page++;
for (let i = 0; i < res.data.data.length; i++) {
allNPUsers.push(res.data.data[i]);
}
await getAllNPUsers(page);
} else {
for (let i = 0; i < res.data.data.length; i++) {
allNPUsers.push(res.data.data[i]);
}
}
})
.catch(err => {
console.log(err);
})
}
const userExists = (userEmail) => {
userEmail = userEmail.split('').map((letter) => letter.toLowerCase()).join('').trim();
let userExistsFlag = false;
for(let i = 0; i < allNPUsers.length; i++){
if(allNPUsers[i].attributes.email == userEmail){
userExistsFlag = true;
userExistsObj = {
exists: true,
data: allNPUsers[i]
}
return true;
} else if(userExistsFlag === false && i === allNPUsers.length-1){
userExistsObj = {
exists: false,
data: []
}
return false;
}
}
}
fs.createReadStream("./sandataNevadaUsers.csv")
.pipe(parse({ delimiter: ','}))
.on("data", function (row) {
if(row[0] !== 'Email'){
let user = {
email: row[0]
};
if(row[1].length > 0){
user['agency_name'] = row[1];
}
if(row[2].length > 0){
user['medicaid_id'] = row[2];
}
if(row[3].length > 0){
user['provider_id'] = row[3];
}
// user['provider_id'] = '';
if(row[4]){
if(row[4].length > 0){
user['agency_id'] = row[4];
}
}
allUsers.push(user);
}
})
.on("error", function (error) {
console.log(error.message);
})
.on("end", async function () {
console.log("FINISHED COLLECTING ALL USERS");
// console.log(allUsers);
console.log(allUsers.length);
await executeFunctionChain();
})
let executeFunctionChain = async () => {
await getAllNPUsers(1)
.then(async () => {
console.log('\x1b[36m%s\x1b[0m', `COMPLETED IMPORTING ALL NP USERS`);
console.log(allNPUsers.length);
let tracker = 0;
for(let i = 0; i < allUsers.length; i++){
userExistsObj = {};
let someUser = allUsers[i];
// console.log(someUser.email);
if(userExists(someUser.email)){
// console.log('INDEX >>>>>>>>>>>>>>>>>>>>>>>>>>>>> ' + i)
let userID = userExistsObj.data.id;
let propsObj = {};
if(someUser.agency_name){
propsObj['agency_name'] = someUser.agency_name;
}
if(someUser.medicaid_id){
propsObj['medicaid_id'] = someUser.medicaid_id;
}
if(someUser.provider_id){
propsObj['provider_id'] = someUser.provider_id;
}
// else {
// propsObj['provider_id'] = null;
// }
if(someUser.agency_id){
propsObj['agency_id'] = someUser.agency_id;
}
propsObj['successfully_completed_secondary_registration'] = true;
tracker++;
await requestHandler(userID, propsObj)
.then((res) => {
console.log('\x1b[31m%s\x1b[0m', `SUCCESSFULLY SET PROPS FOR USER ${someUser.email} AT INDEX ${i}`);
// console.log(`COMPLETED USER ${i + 1}`);
// if(i == allUsers.length-1){
// console.log(`${tracker} users updated`);
// }
return;
})
.catch((err) => {
console.log(err);
})
}
}
})
.catch((err) => {console.log(err)})
}
let requestHandler = async (userID, propsObj) => {
// console.log(userID);
console.log(propsObj);
await axios({
method: "POST",
url: "https://api.northpass.com/v2/properties/people/bulk",
data: {
data: [{
id: userID,
type:'person_properties',
attributes: {
'properties': propsObj
},
}],
},
headers: {
'accept': 'application/json',
'x-api-key': xApiKey,
'content-type': 'application/json'
}
})
// .catch((err) => {
// console.log(err);
// })
}