<commit_before>var express			= require('express'),
	bodyparser		= require('body-parser'),
	promises		= require('azure-mobile-apps/src/utilities/promises'),
	logger			= require('azure-mobile-apps/src/logger');

var sql				= require('mssql'),
	util			= require('../util');

module.exports = function (configuration) {
	var router		= express.Router(),
	authenticate	= require('azure-mobile-apps/src/express/middleware/authenticate')(configuration),
	authorize		= require('azure-mobile-apps/src/express/middleware/authorize');

	var completeError = function(err, res) {
		if (err) {
			console.error(err);
			if (res) res.sendStatus(500);
		}
	};

	var getUserInfo = function(user, voucher, continueWith) {
		var HERE = 'getUserInfo';
		user.getIdentity()
		.then( (identity) => {
			var claims = identity.google.claims;
			var email = claims.email_verified == 'true' ? claims.emailaddress : "";
			var firstName = claims.givenname;
			var lastName = claims.surname;
			var selectedClaims = {
				sid: user.id,
				email: email,
				firstName: firstName,
				lastName: lastName,
				alias: email ? email : firstName + lastName,
				picture: claims.picture
			};
			continueWith(voucher, selectedClaims);
		});
	};

	router.post('/', (req, res, next) => {
		var HERE = '/acceptinvitation';
		console.log('in POST ' + HERE);
		if ( ! req.azureMobile.user) {
			console.log('401 - Unauthorized');
			res.status(401);	// 401: Unauthorized
			next(401);
		}
		var voucher = req.body.voucher;
		getUserInfo(req.azureMobile.user, voucher, function(voucher, userInfo) {
console.log(HERE + ': ' + 'for sql');
			sql.connect(util.sqlConfiguration).then(function() {
console.log(HERE + ': ' + 'sql connected');
				new sql.Request()
				.input('voucher', sql.NVarChar(36), voucher)
				.input('userSid', sql.NVarChar(40), userInfo.sid)
				.input('alias', sql.NVarChar(20), userInfo.alias)
				.input('email', sql.NVarChar(40), userInfo.email)
				.input('firstName', sql.NVarChar(40), userInfo.firstName)
				.input('lastName', sql.NVarChar(40), userInfo.lastName)
				.input('picture', sql.NVarChar(255), userInfo.picture)
				.execute('sp_acceptInvitationToTertulia')
				.then(function(recordsets) {
console.log('query done');
console.log(recordsets);
					if (recordsets['returnValue'] == 0) {
						console.log('in 201 ok');
						res.status(201).end();	// 201: Created
						return;
					} else {
						console.log('in 422 error');
						res.status(422)	// 409: Conflict, 422: Unprocessable Entity (WebDAV; RFC 4918)
							.type('application/json')
							.json( { result: 'Voucher unavailable' } )
							.end();
						return next('422 - Unprocessable Entity');
					}
				}).catch(function(err) {<commit_msg>wip save<commit_after>var express			= require('express'),
	bodyparser		= require('body-parser'),
	promises		= require('azure-mobile-apps/src/utilities/promises'),
	logger			= require('azure-mobile-apps/src/logger');

var sql				= require('mssql'),
	util			= require('../util');

module.exports = function (configuration) {
	var router		= express.Router(),
	authenticate	= require('azure-mobile-apps/src/express/middleware/authenticate')(configuration),
	authorize		= require('azure-mobile-apps/src/express/middleware/authorize');

	var completeError = function(err, res) {
		if (err) {
			console.error(err);
			if (res) res.sendStatus(500);
		}
	};

	var getUserInfo = function(user, voucher, continueWith) {
		var HERE = 'getUserInfo';
		user.getIdentity()
		.then( (identity) => {
			var claims = identity.google.claims;
			var email = claims.email_verified == 'true' ? claims.emailaddress : "";
			var firstName = claims.givenname;
			var lastName = claims.surname;
			var selectedClaims = {
				sid: user.id,
				email: email,
				firstName: firstName,
				lastName: lastName,
				alias: email ? email : firstName + lastName,
				picture: claims.picture
			};
			continueWith(voucher, selectedClaims);
		});
	};

	router.post('/', (req, res, next) => {
		var HERE = '/acceptinvitation';
		console.log('in POST ' + HERE);
		if ( ! req.azureMobile.user) {
			console.log('401 - Unauthorized');
			res.status(401);	// 401: Unauthorized
			next(401);
		}
		var voucher = req.body.voucher;
		getUserInfo(req.azureMobile.user, voucher, function(voucher, userInfo) {
console.log(HERE + ': ' + 'for sql');
			sql.connect(util.sqlConfiguration).then(function() {
console.log(HERE + ': ' + 'sql connected');
console.log(HERE + ': ' + voucher);
console.log(HERE + ': ' + userInfo.sid);
console.log(HERE + ': ' + userInfo.alias);
console.log(HERE + ': ' + userInfo.email);
console.log(HERE + ': ' + userInfo.firstName);
console.log(HERE + ': ' + userInfo.lastName);
console.log(HERE + ': ' + userInfo.picture);
				new sql.Request()
				.input('voucher', sql.NVarChar(36), voucher)
				.input('userSid', sql.NVarChar(40), userInfo.sid)
				.input('alias', sql.NVarChar(20), userInfo.alias)
				.input('email', sql.NVarChar(40), userInfo.email)
				.input('firstName', sql.NVarChar(40), userInfo.firstName)
				.input('lastName', sql.NVarChar(40), userInfo.lastName)
				.input('picture', sql.NVarChar(255), userInfo.picture)
				.execute('sp_acceptInvitationToTertulia')
				.then(function(recordsets) {
console.log('query done');
console.log(recordsets);
					if (recordsets['returnValue'] == 0) {
						console.log('in 201 ok');
						res.status(201).end();	// 201: Created
						return;
					} else {
						console.log('in 422 error');
						res.status(422)	// 409: Conflict, 422: Unprocessable Entity (WebDAV; RFC 4918)
							.type('application/json')
							.json( { result: 'Voucher unavailable' } )
							.end();
						return next('422 - Unprocessable Entity');
					}
				}).catch(function(err) {
{'fraction_non_alphanumeric': 0.1695896176780077, 'fraction_numerical': 0.020694493160294633, 'mean_word_length': 3.724937862468931, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1044111', 'n_tokens_mistral': 2362, 'n_tokens_neox': 1926, 'n_words': 359}