Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Main #31

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
67 changes: 50 additions & 17 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,75 @@ const admin = require('firebase-admin');

const app = express();

// let cacheRosterData = null;

// TODO: Uncomment out line 13
// Refer to Picture Example Folder for help for below instructions. (hit the gear for settings, click projecgt settings, then click service accounts)
// In your firebase project settings it will give you an option to "create service account".
// This generates a service account json file. Download it, and put the file in this project.
// Enter the path to your service account json file below where it says "REPLACE_WITH_SERVICE_ACCOUNT"
// If you need more help with this step go here: https://firebase.google.com/docs/admin/setup

// const serviceAccount = require("./REPLACE_WITH_SERVICE_ACCOUNT.json");
const serviceAccount = require("./madden-companion-project-firebase-adminsdk-u16ts-0a223df9a2.json");

// TODO: Uncomment out line 17-21
// Enter your database url from firebase where it says <DATABASE_NAME> below.
// Refer to picture for reference. It's the 2nd property.
// admin.initializeApp({
// credential: admin.credential.cert(serviceAccount),
// databaseURL: "https://<DATABASE_NAME>.firebaseio.com/"
// });
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://madden-companion-project-default-rtdb.firebaseio.com"
});

app.set('port', (process.env.PORT || 3001));

app.get('*', (req, res) => {
res.send('Madden Companion Exporter');
});

// app.get('/roster/get', (request, response) => {
// res.send(cacheRosterData);
// })

app.use(express.json());
app.post('/:username/:platform/:leagueId/leagueteams', (req, res) => {
const db = admin.database();
const ref = db.ref();
let body = '';
const db = admin.database();
const ref = db.ref();

let body = '';

// Create a promise to wait for the request to finish reading
const requestBodyPromise = new Promise((resolve, reject) => {
req.on('data', chunk => {
body += chunk.toString();
body += chunk.toString();
});
req.on('end', () => {
const { leagueTeamInfoList: teams } = JSON.parse(body);
const {params: { username, leagueId }} = req;

teams.forEach(team => {
const teamRef = ref.child(`data/${username}/${leagueId}/teams/${team.teamId}`);
teamRef.set(team);
});
req.on('end', () => {
resolve();
});

res.sendStatus(200);
req.on('error', (err) => {
reject(err);
});
});

// Wait for the promise to resolve before processing the data
requestBodyPromise
.then(() => {
const { leagueTeamInfoList: teams } = JSON.parse(body);
const { params: { username, leagueId } } = req;

console.log('teams', teams);

teams.forEach(team => {
const teamRef = ref.child(`data/${username}/${leagueId}/teams/${team.teamId}`);
teamRef.set(team);
});

res.sendStatus(200);
})
.catch((err) => {
console.error('Error reading request data:', err);
res.status(500).send('Internal Server Error');
});
});

Expand Down Expand Up @@ -140,16 +169,20 @@ app.post(
// ROSTERS
app.post('/:username/:platform/:leagueId/freeagents/roster', (req, res) => {
const db = admin.database();
console.info('database object: ', db);
const ref = db.ref();
const {
params: { username, leagueId, teamId }
} = req;
let body = '';
req.on('data', chunk => {
body += chunk.toString();
console.info('data event:', body);
cacheRosterData = body;
});
req.on('end', () => {
const { rosterInfoList } = JSON.parse(body);
console.info('end', rosterInfoList);
const dataRef = ref.child(
`data/${username}/${leagueId}/freeagents`
);
Expand Down
7 changes: 7 additions & 0 deletions database.rules.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
/* Visit https://firebase.google.com/docs/database/security to learn more about security rules. */
"rules": {
".read": false,
".write": false
}
}
13 changes: 13 additions & 0 deletions madden-companion-project-firebase-adminsdk-u16ts-0a223df9a2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"type": "service_account",
"project_id": "madden-companion-project",
"private_key_id": "0a223df9a23ce4b2ce81bf8890407e90b2825b99",
"private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCUt/UfWu4a3SKO\nCR50qYJ50SZZ1fEDffsPLH7kZDnSUdveTS3a05IKQKmpUiO2EmNchdgMy4fJLTZ8\nMct5qTwVCmQQwKQG6ox4XT7wVCsI8tza7ji7V22iy1+xuYftGrsF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RuQ43wIjUF1BSX8ai9M29VQEMN7r/5wHob5Z1SJRorWVAoGAa5zW\nek8xiR5lMQQXu1YEM+cjbPeYellA8Fh0SwMs5M1IjCQj8CGVh5dYH0VqR3tLFOqV\n+iYdX+oz6p8yVP212P9TPDy9rEv5IlKlx1CZRUdya7CKPPMhqcFYDoo1lhAth9FW\nAh1VVvGbYx2u1i0QGTnSG+8jLyiY2yc/WrEz7ykCgYEApp8/eN3OM7jt9vSFrHLA\nw6Cs8v4QyDu0AcWmdzHVoyA83bMt7BuyUOdYBwBaynyvDEjedtZZM3vAk1SMTU0W\ndBU5jzLPv8B96yRA8UxauEXM9dJtaHmOmeRN5UAudvB+ucCDGjhirxFaZuFmhJVG\nAM0Jq7C/RpwptdDo1WmmOa4=\n-----END PRIVATE KEY-----\n",
"client_email": "firebase-adminsdk-u16ts@madden-companion-project.iam.gserviceaccount.com",
"client_id": "104592923713409434476",
"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/firebase-adminsdk-u16ts%40madden-companion-project.iam.gserviceaccount.com",
"universe_domain": "googleapis.com"
}
Loading