Skip to content

Commit

Permalink
Merge pull request #100 from Samagra-Anamaya/feat-healthchecks
Browse files Browse the repository at this point in the history
Adding scripts
  • Loading branch information
geeky-abhishek authored Nov 22, 2023
2 parents b983784 + fcd0c19 commit 15ac85d
Show file tree
Hide file tree
Showing 11 changed files with 1,207,219 additions and 0 deletions.
345,440 changes: 345,440 additions & 0 deletions packages/bff_scripts/_users.json

Large diffs are not rendered by default.

34 changes: 34 additions & 0 deletions packages/bff_scripts/addVillage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const axios = require('axios');
const fs = require('fs');

const VILLAGES_JSON_PATH = 'input/villages.json';
const ADD_VILLAGE_URL = 'http://localhost:3003/utils/addVillage';

async function addVillage(village) {
try {
const response = await axios.post(ADD_VILLAGE_URL, village);
console.log(`Village ${village.villageName} added successfully!`);
} catch (error) {
console.error(`Error adding village ${village.villageName}`, error.response ? error.response.data : error.message);
}
}

function loadVillages() {
try {
const villagesJson = fs.readFileSync(VILLAGES_JSON_PATH, 'utf8');
const villages = JSON.parse(villagesJson);
return villages;
} catch (error) {
console.error('Error loading villages:', error.message);
return [];
}
}

async function main() {
const villages = loadVillages();
for (const village of villages) {
await addVillage(village);
}
}

main();
50,671 changes: 50,671 additions & 0 deletions packages/bff_scripts/data.csv

Large diffs are not rendered by default.

50,671 changes: 50,671 additions & 0 deletions packages/bff_scripts/data2.csv

Large diffs are not rendered by default.

112 changes: 112 additions & 0 deletions packages/bff_scripts/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
const axios = require("axios");
const csv = require("csv-parser");
const fs = require("fs");
const { find } = require("lodash");

const FUSION_AUTH_BASE_URL = "";
const FUSION_AUTH_ENDPOINT = "/api/signup";
const users = [];
const _users = {};
const HEADERS = {
"x-application-id": "",
"Content-Type": "application/json",
};

async function create_user_in_fusionauth(user) {
console.log({ user });
try {
const response = await axios.post(
`${FUSION_AUTH_BASE_URL}${FUSION_AUTH_ENDPOINT}`,
user,
{ headers: HEADERS }
);
console.log({ response });
console.log(`User ${user.user.email} created successfully!`);
} catch (error) {
console.error(
`Error creating user ${user.user.email}: ${
error.response ? error.response.data : error.message
}`
);
}
}

function csv_to_users(filename) {
return new Promise((resolve, reject) => {
fs.createReadStream(filename)
.pipe(csv())
.on("data", (row) => {
// Assuming the CSV has columns 'email', 'password', and 'username'
const prev = find(users, (prevU) => {
return prevU.data.gpCode == `${row["gpCode"]}`;
});
_users[row["gpCode"]] = {
..._users[row["gpCode"]],
user: {
password: `pass_${row["gpCode"]}`,
username: `gp_${row["gpCode"]}`,
data: {
..._users[row["gpCode"]]?.user?.data,
gpCode: row["gpCode"],
villages: _users[row["gpCode"]]?.user?.data?.villages
? [
..._users[row["gpCode"]]?.user?.data?.villages,
{
villageCode: row["villageCode"],
villageName: row["villageName"],
},
]
: [
{
villageCode: row["villageCode"],
villageName: row["villageName"],
},
],
}
},

registration: {
applicationId: "",
preferredLanguages: ["en"],
roles: ["enumerator"],
usernameStatus: "ACTIVE",
},
};
})
.on("end", () => {
resolve(users);
})
.on("error", (error) => {
reject(error);
});
});
}

async function main() {
try {
const users = await csv_to_users("data2.csv");
fs.writeFile("users.json", JSON.stringify(users, null, 2), (err) => {
if (err) {
console.error(err);
}

console.log("User data saved successfully");
});
fs.writeFile("_users.json", JSON.stringify(_users, null, 2), (err) => {
if (err) {
console.error(err);
}

console.log("_User data saved successfully");
});

//console.log({ users });
// for (const user of users) {
// await create_user_in_fusionauth(user);
// }
} catch (error) {
console.error(`An error occurred: ${error.message}`);
}
}

main();
17 changes: 17 additions & 0 deletions packages/bff_scripts/input/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[
{
"id": 1,
"stateCode": 21,
"stateName": "ODISHA",
"districtCode": 344,
"districtName": "ANUGUL",
"itdaName": "",
"blockCode": 3276,
"blockName": "ANUGUL",
"isTspBlock": "N",
"gpCode": 115550,
"gpName": "ANGARBANDHA",
"spdpVillageId": 404281,
"villageName": "Angarbandha"
}
]
Loading

0 comments on commit 15ac85d

Please sign in to comment.