286 lines
7.5 KiB
JavaScript
286 lines
7.5 KiB
JavaScript
const express = require('express')
|
|
const bodyParser = require("body-parser");
|
|
var fs = require('fs');
|
|
var readline = require('readline');
|
|
var google = require('googleapis');
|
|
var googleAuth = require('google-auth-library');
|
|
|
|
const app = express()
|
|
const router = express.Router();
|
|
|
|
router.use(bodyParser.urlencoded({ extended: false }));
|
|
router.use(bodyParser.json());
|
|
|
|
require('dotenv').config()
|
|
|
|
const PEOPLE_DB = process.env.PEOPLE_DB.split('/')[5];
|
|
const PAIRS_LIST = process.env.PAIRS_LIST.split('/')[5];
|
|
|
|
var SCOPES = ['https://www.googleapis.com/auth/spreadsheets'];
|
|
var TOKEN_DIR = (process.env.HOME || process.env.HOMEPATH ||
|
|
process.env.USERPROFILE) + '/.credentials/';
|
|
var TOKEN_PATH = TOKEN_DIR + 'sheets.googleapis.com-nodejs-quickstart.json';
|
|
|
|
var oauth2Client = null;
|
|
|
|
var pairsForSave = [];
|
|
var lastRow = null; //last row with names
|
|
|
|
|
|
function executeAPI(callback1,callback2){
|
|
fs.readFile('client_secret.json', function processClientSecrets(err, content) {
|
|
console.log("reading client secret");
|
|
if (err) {
|
|
console.log('Error loading client secret file: ' + err);
|
|
return;
|
|
}
|
|
// Authorize a client with the loaded credentials, then call the
|
|
// Google Sheets API.
|
|
authorize(JSON.parse(content), callback1, callback2);
|
|
});
|
|
}
|
|
|
|
function authorize(credentials, callback1, callback2){
|
|
var clientSecret = credentials.installed.client_secret;
|
|
var clientId = credentials.installed.client_id;
|
|
var redirectUrl = credentials.installed.redirect_uris[0];
|
|
var auth = new googleAuth();
|
|
|
|
oauth2Client = new auth.OAuth2(clientId, clientSecret, redirectUrl);
|
|
|
|
// Check if we have previously stored a token.
|
|
fs.readFile(TOKEN_PATH, function(err, token) {
|
|
console.log(TOKEN_PATH);
|
|
if (err) {
|
|
getNewToken(callback1, callback2);
|
|
} else {
|
|
oauth2Client.credentials = JSON.parse(token);
|
|
callback1(callback2);
|
|
}
|
|
});
|
|
}
|
|
|
|
function getNewToken(callback1, callback2) {
|
|
var authUrl = oauth2Client.generateAuthUrl({
|
|
access_type: 'offline',
|
|
scope: SCOPES
|
|
});
|
|
console.log('Authorize this app by visiting this url: ', authUrl);
|
|
var rl = readline.createInterface({
|
|
input: process.stdin,
|
|
output: process.stdout
|
|
});
|
|
rl.question('Enter the code from that page here: ', function(code) {
|
|
rl.close();
|
|
oauth2Client.getToken(code, function(err, token) {
|
|
if (err) {
|
|
console.log('Error while trying to retrieve access token', err);
|
|
return;
|
|
}
|
|
oauth2Client.credentials = token;
|
|
storeToken(token);
|
|
callback1(callback2);
|
|
});
|
|
});
|
|
}
|
|
|
|
function storeToken(token) {
|
|
try {
|
|
fs.mkdirSync(TOKEN_DIR);
|
|
} catch (err) {
|
|
if (err.code != 'EEXIST') {
|
|
throw err;
|
|
}
|
|
}
|
|
console.log(JSON.stringify(token));
|
|
fs.writeFile(TOKEN_PATH, JSON.stringify(token));
|
|
console.log('Token stored to ' + TOKEN_PATH);
|
|
}
|
|
|
|
function getPairs(callback){
|
|
auth=oauth2Client;
|
|
ranges = ['2017!A:A','2017!B:B']; //get all rows with names
|
|
var sheets = google.sheets('v4');
|
|
sheets.spreadsheets.values.batchGet({
|
|
auth: auth,
|
|
spreadsheetId: PAIRS_LIST,
|
|
ranges: ranges
|
|
}, function(err, result) {
|
|
if(err) {
|
|
// Handle error
|
|
console.log(err);
|
|
return null;
|
|
} else {
|
|
const pairs = [];
|
|
|
|
lastRow = result.valueRanges[0].values.length;
|
|
|
|
for (let i=0;i<lastRow;i++){
|
|
let name1 = result.valueRanges[0].values[i][0];
|
|
if (name1){
|
|
let name2 = result.valueRanges[1].values[i][0];
|
|
pairs.push({name1:name1,name2:name2});
|
|
}
|
|
}
|
|
callback(pairs);
|
|
}
|
|
});
|
|
}
|
|
|
|
function getAvailableNames(callback){
|
|
auth=oauth2Client;
|
|
ranges = ['\'Other preferences\'!A2:A','\'Other preferences\'!E2:E']; //get all rows with names
|
|
var sheets = google.sheets('v4');
|
|
sheets.spreadsheets.values.batchGet({
|
|
auth: auth,
|
|
spreadsheetId: PEOPLE_DB,
|
|
ranges: ranges
|
|
}, function(err, result) {
|
|
if(err) {
|
|
// Handle error
|
|
console.log(err);
|
|
return null;
|
|
} else {
|
|
const names = [];
|
|
|
|
for (let i=0;i<result.valueRanges[0].values.length;i++){
|
|
let name = result.valueRanges[0].values[i][0];
|
|
let available = result.valueRanges[1].values[i][0];
|
|
|
|
if (name && (available=='x')){
|
|
names.push(name);
|
|
}
|
|
}
|
|
callback(names);
|
|
}
|
|
});
|
|
}
|
|
|
|
function MakePairs(callback){
|
|
executeAPI(getAvailableNames,function(names){
|
|
executeAPI(getPairs, function(pairs){
|
|
let next_i=false;
|
|
const newPairs = [];
|
|
const usedNames = [];
|
|
for (let i=0;i<names.length;i++){
|
|
next_i=false;
|
|
for (let j=0;j<names.length;j++){
|
|
if (i!=j){
|
|
|
|
let found=false;
|
|
|
|
pairs.filter((pair)=>{
|
|
if ((pair.name1==names[i] && pair.name2==names[j])||(pair.name1==names[j] && pair.name2==names[i])){
|
|
console.log("Par pronaden !");
|
|
console.log(names[i] + " i " + names[j]);
|
|
found=true;
|
|
return;
|
|
}
|
|
});
|
|
|
|
if (!found){
|
|
usedNames.filter((name)=>{
|
|
if (names[i] === name || names[j] === name){
|
|
console.log("Ime vec koristeno");
|
|
found=true;
|
|
return;
|
|
}
|
|
});
|
|
}
|
|
|
|
|
|
if (!found){
|
|
next_i=true;
|
|
newPairs.push({name1:names[i], name2:names[j]});
|
|
usedNames.push(names[i]);
|
|
usedNames.push(names[j]);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
if (next_i) continue;
|
|
}
|
|
pairsForSave=newPairs;
|
|
callback.send(newPairs);
|
|
});
|
|
});
|
|
//Napravi par -> Provjeri da li postoji -> Snimi ili ponovi postupak
|
|
|
|
}
|
|
|
|
function SavePairs(callback){
|
|
if (lastRow== null) return;
|
|
|
|
auth=oauth2Client;
|
|
|
|
const values_column1 = [];
|
|
const values_column2 = [];
|
|
|
|
for (let i=0;i<pairsForSave.length;i++){
|
|
values_column1.push([pairsForSave[i].name1]);
|
|
values_column2.push([pairsForSave[i].name2]);
|
|
}
|
|
|
|
const data = [];
|
|
|
|
let range1= '2017!A'+(lastRow+2)+':A'+(lastRow+2+pairsForSave.length);
|
|
let range2= '2017!B'+(lastRow+2)+':B'+(lastRow+2+pairsForSave.length);
|
|
|
|
|
|
data.push({
|
|
range:range1,
|
|
values: values_column1
|
|
});
|
|
|
|
data.push({
|
|
range:range2,
|
|
values: values_column2
|
|
});
|
|
|
|
var body = {
|
|
data: data,
|
|
valueInputOption:"RAW"
|
|
};
|
|
var sheets = google.sheets('v4');
|
|
sheets.spreadsheets.values.batchUpdate({
|
|
spreadsheetId: PAIRS_LIST,
|
|
auth:auth,
|
|
resource: body
|
|
}, function(err, result) {
|
|
if(err) {
|
|
// Handle error
|
|
console.log(err);
|
|
callback.send({result:false});
|
|
} else {
|
|
lastRow=null;
|
|
pairsForSave=[];
|
|
callback.send({result:true});
|
|
}
|
|
});
|
|
|
|
}
|
|
|
|
app.use(function(req, res, next) {
|
|
res.header("Access-Control-Allow-Origin", "*");
|
|
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, X-Last-Record-Id, X-Total-Count");
|
|
res.header("Access-Control-Expose-Headers", "X-Last-Record-Id, X-Total-Count");
|
|
res.header("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
|
|
res.header('Access-Control-Allow-Credentials', 'true');
|
|
next();
|
|
});
|
|
|
|
app.get('/getPairs',(req,resp)=>{
|
|
pairsForSave=[];
|
|
lastRow=null;
|
|
MakePairs(resp);
|
|
});
|
|
|
|
app.get('/savePairs', (req,resp)=>{
|
|
SavePairs(resp);
|
|
//resp.send({result:true});
|
|
});
|
|
|
|
|
|
app.listen(3005, function () {
|
|
console.log('server na portu 3005');
|
|
}) |