Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/config

/node_modules/

/scripts/.env
/scripts/node_modules/

.env
38 changes: 0 additions & 38 deletions README.md

This file was deleted.

Binary file removed assets/browser_ss.png
Binary file not shown.
Binary file removed assets/event_ss.png
Binary file not shown.
6 changes: 0 additions & 6 deletions schedular_function.js → functions/recommend.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
// YOUR_BASE_DIRECTORY/netlify/functions/test-scheduled-function.js
const { schedule } = require("@netlify/functions");
const { addDataToSheet } = require("./googlesheet_updater");

let count = 1;
let cron = "* * * * *";
const handler = async function(event, context) {
// console.log("Received event:", event);
console.log("cron counter:", count)
count++;

await addDataToSheet()

return {
statusCode: 200,
};
Expand Down
5 changes: 5 additions & 0 deletions netlify.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[build]
functions = "functions"

[functions."recommend"]
schedule = "* * * * *"
11 changes: 0 additions & 11 deletions netlify/functions/netlify.toml

This file was deleted.

18 changes: 0 additions & 18 deletions netlify/functions/schedular.js

This file was deleted.

Binary file added script/.DS_Store
Binary file not shown.
2 changes: 2 additions & 0 deletions script/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
.env
11 changes: 11 additions & 0 deletions script/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Instructions for setup

## env file content
```bash
MONGO_URI=<URI>
```

## Commant to run the script
```bash
node index.js
```
13 changes: 13 additions & 0 deletions script/assets/credentials.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"installed":{
"client_id":"908403157308-k2mv5krmbg8ia7463uild32oisfkf8u9.apps.googleusercontent.com",
"project_id":"peppy-plateau-323305",
"auth_uri":"https://accounts.google.com/o/oauth2/auth",
"token_uri":"https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs",
"client_secret":"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",
"redirect_uris":[
"http://localhost"
]
}
}
13 changes: 13 additions & 0 deletions script/assets/keys.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"type": "service_account",
"project_id": "peppy-plateau-323305",
"private_key_id": "8feb5d079fdacc568279fb479c456c7dddbf58a4",
"private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDRHqIwfBNkG6EP\nQr/eoQixSD4Ob5XeymaBzXNPQtB+ucYfVXxNjHrpok5fieddtMJ5z7ho4PcApDLu\nVbn+DZsoQZdSnH8ctpwLEZOGPH/JSYyDPk5VAMsQ1cMtu6QjVy4g4pJWCDxeTAuU\nXKq7lAXbZkhv1GUz2FJfbk86mW3ZjoyNLLbp895xCuH7LpvQe4E/OoYaa26LNZPH\n8/JikYethaFREXz8l7FrzQKtyDoTgJ8ypggh4Ob2pD8VpYIvPlsyBhgyHrS8f3gn\nLnCzX6iMKftKD8fF42qM8ajBYXDSTZdHI0ESn/8eC2l6hVGnpufUcry7nJIBNB4g\nAJ8yCCVRAgMBAAECggEAKNdqRrzczqbkwEXBklnyCCm7UPdzFkWCWqSE+jxq2u0u\naA06jyy6Dqov01HsGW2Fs+OhOP6CzyHCfrDId/kxTd3tj2lYHAQAGttObB+ht27B\nXhqLkudqQXn2OhbVGj/bFvoZKWtHWwVQF6Q3hhYFRNHHoOui7kq338J7/85aWWw2\nfaKVU/GDje82FMzckYmQmD+Ub3TgLmGwVMWbp52HtfgsJ0l5JoJf86kdBxgC5OMf\no/VZSc4ubUIyIOl6OSWX3uKDuDmlQ6LC2IALp8chhTwXStdfxsO58uhe9sXfay1Q\n/9BYXPmmjqbc6b2ITQ0dHxNvt0BTYLHQobhQU+gWpwKBgQD9poBSTSDOfw7PVpPV\naSsaKLiTbhvTgc3oUe/blSAhXH+sgM2Cw5utTwQeLWM1uyYKirWW0t++5lzKydVj\nvkot+znCVBEU3xjmYCqmzsVkW1QzTTs7dIUcsoyS1lcCDqjPNovAco7/WwUuoDJK\nKc47Qb6XMii/ak7UNS6Uunt+8wKBgQDTDoiShhtTPSmLVmSGiHkC+k2SfRU4d73r\nO/mUg1D97CRD2PF+1o9h8gWSOWQbcCT30XiXzpmguDF3Ns9dvcAfzdD+VtSkeCqM\nJuaVmr52ObQE84vsRNFdRWH1Q5PVjqgrCTwQuXeVGgJjcT3HEUD8F6XR+tsk4drP\n0mFvfxyDqwKBgQDe/VSMhg/UWfEcKCHZGRbybWAvBDrkS5ZIqlfys/xGN/pqHeYO\nEoc26mNVYFN6tiewVtdjW8mmGwDyqlO112GBYP4bMCrALY3HYj283cS9AGlanIf5\nMeugGfvpTxO41Ehluq9Lpaq/JTa+EcmEV5Jdl+PFKiyWcxAQERz9VaAiLwKBgB61\nvqmTWeaCv8jGiVoKAZGtWhw2lx/gTIIt9+LkOKS594znxDvwfGyECst8DlTvvHFS\nyXlNVo1MP+ZY01OQiKQYDpDzkX1c/tPl1SdvnMcXHlqaFYILgWtQ7TAC5T/twgqB\nDXxhgETxw+c0UwjkOdB0oGwtVDXJp00WD5dpp3yBAoGBAJOfNHsbbfGRaHTkFU+K\n/4W2lygLkPqUlNyM9twvZ9KQQrRCvrV7MbF2u7YY4f6SDdKlwDR9bxJyNAGd24Ak\ndpCDovAvpHRHauZiw3mj7O0fcp5gp7WJAwbK6A/sGlQlvyIkcyc8kzmsvxmGLJTO\nxwiNpmtkElqlG0BPk/HKJPPa\n-----END PRIVATE KEY-----\n",
"client_email": "google-sheet-test-data@peppy-plateau-323305.iam.gserviceaccount.com",
"client_id": "110343577438257846771",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/google-sheet-test-data%40peppy-plateau-323305.iam.gserviceaccount.com"
}

24 changes: 24 additions & 0 deletions script/config/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require('dotenv').config();

const config = {
production: {
uri: process.env.MONGO_URI,
sdeDBName: process.env.SDE_DB,
userDBName: process.env.USER_DB,
sheetId: process.env.SHEET_ID,
},
development: {
uri: process.env.MONGO_URI,
sdeDBName: process.env.SDE_DB,
userDBName: process.env.USER_DB,
sheetId: process.env.SHEET_ID,
},
test: {
uri: process.env.MONGO_URI,
sdeDBName: process.env.SDE_DB,
userDBName: process.env.USER_DB,
sheetId: process.env.SHEET_ID,
}
};

module.exports = config;
13 changes: 13 additions & 0 deletions script/global_constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const state_colors = {
untouched: "#F6F6F6",
done: "#A5FD7C",
skip: "#FAF19E"
};

const db = {
roadmapDb:"tool_roadmap",
userDb: "user"
};

module.exports.state_colors = state_colors;
module.exports.db = db;
30 changes: 30 additions & 0 deletions script/helper/dbhelper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
async function fetchStates(db) {

let data = [];
console.log("fetching states from db");
try {
const collection = db.collection("states");
const cursor = collection.find({});
data = await cursor.toArray();
}
catch(error) {
let msg = `[Error] fetching error: ${error}`
console.log(msg);
}
return data;
}

async function fetchUserById(db, id) {
let data = null;
console.log("fetching user from db");
try {
const collection = db.collection("users");
data = await collection.findOne({ _id: id });
} catch (error) {
let msg = `[Error] fetching error: ${error}`;
console.error(msg);
}
return data;
}

module.exports = { fetchStates, fetchUserById };
49 changes: 49 additions & 0 deletions script/helper/misc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const crypto = require("crypto");

function sortJSONByKeys(data) {
const result = {};

const sortedKeys = Object.keys(data).sort();

for (const key of sortedKeys) {
result[key] = data[key];
}

return result;
}

function hashingJSON(data) {
const flattedJSON = flattenJSON(data);
const message = JSON.stringify(sortJSONByKeys(flattedJSON));

const encrypted = crypto.createHmac('sha256', Securitykey).update(message).digest('hex');

return encrypted;
}

function flattenJSON(data) {
const result = {};

function recurse(cur, prop) {
if (Object(cur) !== cur) {
result[prop] = cur;
} else if (Array.isArray(cur)) {
for (var i = 0, l = cur.length; i < l; i++)
recurse(cur[i], prop + "[" + i + "]");
if (l == 0) result[prop] = [];
} else {
var isEmpty = true;
for (var p in cur) {
isEmpty = false;
recurse(cur[p], prop ? prop + ":::" + p : p);
}
if (isEmpty && prop) result[prop] = {};
}
}

recurse(data, "");

return result;
}

module.exports = { hashingJSON, flattenJSON };
21 changes: 11 additions & 10 deletions googlesheet_updater.js → script/helper/sheetupdater.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
const { google } = require('googleapis');

async function addDataToSheet(email = "", event = "", message = "") {
console.log("Script started!");
// Replace the sheet ID and key file path with your own values
const sheetId = "1Dw7hlhh1Bq7RBtVJ3TJABmdgwnaQsQs7gycqYV4Ihw4";
const keyFilePath = "./config/keys.json"; // replace with the path to your key file
const env = process.env.NODE_ENV || 'development';
const { sheetId } = require("../config/config")[env]

async function addDataToSheet(email, message = "") {
console.log("sheet updating script started!");
// Replace the key file path with your own values
const keyFilePath = "../assets/keys.json"; // replace with the path to your key file

const date = new Date();

Expand All @@ -15,8 +17,7 @@ async function addDataToSheet(email = "", event = "", message = "") {
});

// Create the row of data to add
// const rowData = [email, event, date, message]; for Swing Auth API
const rowData = ["CHCHCHCHCHC", "Male", "Sanskrit"]
const rowData = [email, event, date, message]; // for Swing Auth API

try {
// Authorize the client
Expand All @@ -39,11 +40,11 @@ async function addDataToSheet(email = "", event = "", message = "") {
// Send the request
const response = await sheets.spreadsheets.values.append(request);

console.log('Data added to sheet at column:', response.data["tableRange"]);
console.log("Script ended!");
console.log('data added at column:', response.data["tableRange"]);
console.log("sheet updating script ends!");
} catch (error) {
console.log(error.response.data.error.errors)
}
}

module.exports = { addDataToSheet };
module.exports = { addDataToSheet }
23 changes: 23 additions & 0 deletions script/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const { fetchStates } = require("./helper/dbhelper");
const { connect, disconnect } = require("./utils/dbconnection");
const recommedationEngine = require("./utils/recommendation");

async function main() {

try {
const { roadmapDbInstance } = await connect();

const states = await fetchStates(roadmapDbInstance);
console.log(`fetched ${states.length} states`);
states.forEach((state) => {
const recommendedContent = recommedationEngine(state);
console.log(recommendedContent);
});
await disconnect();
}
catch (err) {
console.log(err);
}
}

main();
12 changes: 12 additions & 0 deletions script/model/hashedlabel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const mongoose = require('mongoose');

// Define schema for your data
const schema = new mongoose.Schema({
label: {
type: String,
required: true
}
},{ timestamps: true });

const hashedSchema = mongoose.model('hashed', schema);
module.exports = { hashedSchema };
Loading