Skip to content

Commit

Permalink
rewrote saveStrings to use async/await, see #187
Browse files Browse the repository at this point in the history
  • Loading branch information
jbphet committed Nov 26, 2019
1 parent bc50080 commit 99898da
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 52 deletions.
93 changes: 44 additions & 49 deletions js/routeHandlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -565,70 +565,65 @@ module.exports.testStrings = function( req, res ) {
};

/**
* Route for saving strings to the short-term storage area (when the user presses the "Save" button on the translate sim
* page). Strings are added to the postgres database, and NOT to the GitHub long term storage area.
* Route handler for saving strings to the short-term storage area (when the user presses the "Save" button on the
* translate sim page). Strings are added to the postgres database, and NOT to the GitHub long term storage area.
* @param req
* @param res
*/
module.exports.saveStrings = function( req, res ) {
module.exports.saveStrings = async function( req, res ) {

const simName = req.params.simName;
const targetLocale = req.params.targetLocale;
const userId = ( req.session.userId ) ? req.session.userId : 0;

const pool = new Pool();
const repos = {};
let error = false;

const finished = _.after( Object.keys( req.body ).length, function() {
winston.log( 'info', 'finished string saving for ' + simName + '_' + targetLocale );
res.json( {
'success': !error
} );
} );

const repos = {};
for ( const string in req.body ) {
if ( Object.hasOwnProperty.call( req.body, string ) ) {
// loop through the string descriptions in the post request, saving each one
for ( const stringDescription in req.body ) {

// data submitted is in the form "[repository] [key]", for example "area-builder area-builder.title"
const repoAndKey = string.split( ' ' );
const repo = repoAndKey[ 0 ];
const key = repoAndKey[ 1 ];
if ( !Object.hasOwnProperty.call( req.body, stringDescription ) ) {
continue;
}

if ( !repos[ repo ] ) {
repos[ repo ] = {};
}
// string descriptions should be in the form "[repository] [key]", for example "area-builder area-builder.title"
const repoAndKey = stringDescription.split( ' ' );
const repo = repoAndKey[ 0 ];
const key = repoAndKey[ 1 ];

const stringValue = req.body[ string ];
const ts = new Date();

( function( key, stringValue ) {
const pool = new Pool();

if ( key && stringValue && stringValue.length > 0 ) {
pool.query( 'SELECT upsert_saved_translations' +
'($1::bigint, $2::varchar(255), $3::varchar(255), $4::varchar(8), $5::varchar(255), $6::timestamp)',
[ userId, key, repo, targetLocale, stringValue, ts ],
function( err, rows, result ) {
if ( err ) {
winston.log(
'error',
'inserting row: (' + userId + ', ' + key + ', ' + stringValue + ', ' + targetLocale + '), error = ' + err
);
error = true;
}
finished();
} );
}
else {
finished();
}

} )( key, stringValue );
// if this repo hasn't been encountered yet, add it to our repos object
if ( !repos[ repo ] ) {
repos[ repo ] = {};
}
else {
finished();

const stringValue = req.body[ stringDescription ];
const timestamp = new Date();

if ( key && stringValue && stringValue.length > 0 ) {
try {
const queryResponse = await pool.query(
'SELECT upsert_saved_translations' +
'($1::bigint, $2::varchar(255), $3::varchar(255), $4::varchar(8), $5::varchar(255), $6::timestamp)',
[ userId, key, repo, targetLocale, stringValue, timestamp ]
);
console.log( 'JSON.stringify( queryResponse ) = ' + JSON.stringify( queryResponse ) );
}
catch( err ) {
winston.error( 'error saving string values to DB, err = ' + err );
error = true;
break;
}
}
}

if ( !error ) {
winston.log( 'info', 'finished string saving for ' + simName + '_' + targetLocale );
}

// send the response
res.json( {
'success': !error
} );
};

/**
Expand Down
4 changes: 1 addition & 3 deletions js/stringSubmissionQueue.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,8 @@ const PRODUCTION_SERVER_URL = RosettaConstants.PRODUCTION_SERVER_URL;
const SKIP_BUILD_REQUEST = typeof global.preferences.debugRosettaSkipBuildRequest !== 'undefined' &&
global.preferences.debugRosettaSkipBuildRequest === 'true';

// TODO: Remove this comment (it's a change that was committed to the new chipper-2.0 branch to test it out).

/**
* task queue into which translation request are pushed
* task queue into which translation requests are pushed
* @public
*/
module.exports.stringSubmissionQueue = async ( req, res ) => {
Expand Down

0 comments on commit 99898da

Please sign in to comment.