1
0
Fork 0

TokenDao: Callbacks=>promises

This makes minimal changes to TokenedService, even though there is obvious
refactoring that can be done to reduce duplication, because the class is
currently untested.

* src/server/service/TokenedService.js (_getQuoteToken, generateToken,
    killToken, acceptToken, completeToken): Expect promise.
* src/server/token/TokenDao.ts (updateToken, getToken): Remove callback
    param, return Promise.
* test/server/token/TokenDaoTest.ts: Use promises.
master
Mike Gerwitz 2019-09-09 15:28:35 -04:00
parent 9eb5d6c118
commit c90757a6d3
3 changed files with 83 additions and 187 deletions

View File

@ -242,18 +242,9 @@ module.exports = Trait( 'TokenedService' )
*/ */
'private _getQuoteToken': function( quote, tokid, callback ) 'private _getQuoteToken': function( quote, tokid, callback )
{ {
this._dao.getToken( this._dao.getToken( quote.getId(), this._ns, tokid )
quote.getId(), .then( token =>
this._ns,
tokid,
function( err, token )
{ {
if ( err )
{
callback( err, null );
return;
}
if ( tokid && !token ) if ( tokid && !token )
{ {
callback( callback(
@ -265,8 +256,8 @@ module.exports = Trait( 'TokenedService' )
} }
callback( null, token ); callback( null, token );
} } )
); .catch( err => callback( err, null ) );
}, },
@ -595,29 +586,9 @@ module.exports = Trait( 'TokenedService' )
var tokid = this._tokgen( program, quote ), var tokid = this._tokgen( program, quote ),
status = this.getDefaultTokenStatus(); status = this.getDefaultTokenStatus();
this._dao.updateToken( this._dao.updateToken( quote.getId(), this._ns, tokid, status, null )
quote.getId(), .then( () => callback( null, { id: tokid, status: status } ) )
this._ns, .catch( err => callback( err, null ) );
tokid,
status,
null,
function( err )
{
if ( err )
{
callback( err, null );
return;
}
callback(
null,
{
id: tokid,
status: status,
}
);
}
);
}, },
@ -642,33 +613,13 @@ module.exports = Trait( 'TokenedService' )
* *
* @param {function(?Error,Object)} callback continuation * @param {function(?Error,Object)} callback continuation
*/ */
'virtual virtual protected killToken': function( quote, token, callback ) 'virtual protected killToken': function( quote, token, callback )
{ {
callback = callback || function() {}; callback = callback || function() {};
this._dao.updateToken( this._dao.updateToken( quote.getId(), this._ns, token.id, 'DEAD', null )
quote.getId(), .then( () => callback( null, { id: token, status: 'DEAD' } ) )
this._ns, .catch( err => callback( err, null ) );
token.id,
'DEAD',
null,
function( err )
{
if ( err )
{
callback( err, null );
return;
}
callback(
null,
{
id: token,
status: 'DEAD',
}
);
}
);
}, },
@ -686,29 +637,9 @@ module.exports = Trait( 'TokenedService' )
{ {
callback = callback || function() {}; callback = callback || function() {};
this._dao.updateToken( this._dao.updateToken( quote.getId(), this._ns, token.id, 'ACCEPTED', null )
quote.getId(), .then( () => callback( null, { id: token, status: 'ACCEPTED' } ) )
this._ns, .catch( err => callback( err, null ) );
token.id,
'ACCEPTED',
null,
function( err )
{
if ( err )
{
callback( err, null );
return;
}
callback(
null,
{
id: token,
status: 'ACCEPTED',
}
);
}
);
}, },
@ -725,31 +656,9 @@ module.exports = Trait( 'TokenedService' )
*/ */
'virtual protected completeToken': function( quote, token, data, callback ) 'virtual protected completeToken': function( quote, token, data, callback )
{ {
callback = callback || function() {}; this._dao.updateToken( quote.getId(), this._ns, token.id, 'DONE', data )
.then( () => callback( null, { id: token, status: 'DONE' } ) )
this._dao.updateToken( .catch( err => callback( err, null ) );
quote.getId(),
this._ns,
token.id,
'DONE',
data,
function( err )
{
if ( err )
{
callback( err, null );
return;
}
callback(
null,
{
id: token,
status: 'DONE',
}
);
}
);
}, },
} ); } );

View File

@ -79,9 +79,6 @@ export = class TokenDao
* @param token token value * @param token token value
* @param data token data, if any * @param data token data, if any
* @param status arbitrary token type * @param status arbitrary token type
* @param callback with error or null (success)
*
* @return self
*/ */
updateToken( updateToken(
quote_id: number, quote_id: number,
@ -89,8 +86,7 @@ export = class TokenDao
token: string, token: string,
type: TokenType, type: TokenType,
data: string | null, data: string | null,
callback: ( err: Error|null ) => void, ): Promise<void>
): this
{ {
const root = this._genRoot( ns ) + '.'; const root = this._genRoot( ns ) + '.';
@ -113,21 +109,28 @@ export = class TokenDao
[ root + token + '.statusLog' ]: token_entry, [ root + token + '.statusLog' ]: token_entry,
}; };
this._collection.update( return new Promise( ( resolve, reject ) =>
{ id: +quote_id }, {
{ this._collection.update(
$set: token_data, { id: +quote_id },
$push: token_log {
}, $set: token_data,
{ upsert: true }, $push: token_log
},
{ upsert: true },
function ( err: Error|null ) function ( err: Error|null )
{ {
callback( err || null ); if ( err )
} {
); reject( err );
return;
}
return this; resolve();
}
);
} );
} }
@ -141,16 +144,11 @@ export = class TokenDao
* @param quote_id quote identifier * @param quote_id quote identifier
* @param ns token namespace * @param ns token namespace
* @param token_id token identifier (unique to NS) * @param token_id token identifier (unique to NS)
* @param callback
* *
* @return self * @return token data
*/ */
getToken( getToken( quote_id: number, ns: string, token_id: string ):
quote_id: number, Promise<TokenData|null>
ns: string,
token_id: string,
callback: ( err: Error|null, data: TokenData|null ) => void,
): this
{ {
const root = this._genRoot( ns ) + '.'; const root = this._genRoot( ns ) + '.';
const fields: any = {}; const fields: any = {};
@ -164,37 +162,37 @@ export = class TokenDao
fields[ root + token_id ] = 1; fields[ root + token_id ] = 1;
} }
this._collection.findOne( return new Promise( ( resolve, reject ) =>
{ id: +quote_id }, {
{ fields: fields }, this._collection.findOne(
( err: Error|null, data: TokenQueryResult ) => { id: +quote_id },
{ { fields: fields },
if ( err || !data ) ( err: Error|null, data: TokenQueryResult ) =>
{ {
callback( err, null ); if ( err || !data )
return; {
} reject( err );
return;
}
const field = <TokenNamespaceResults>data[ this._rootField ] || {}; const field = <TokenNamespaceResults>data[ this._rootField ]
|| {};
if ( !field[ ns ] ) if ( !field[ ns ] )
{ {
callback( null, null ); resolve( null );
return; return;
} }
const ns_data = <TokenNamespaceData>field[ ns ]; const ns_data = <TokenNamespaceData>field[ ns ];
callback( resolve( ( token_id )
null,
( token_id )
? this._getRequestedToken( token_id, ns_data ) ? this._getRequestedToken( token_id, ns_data )
: this._getLatestToken( ns_data ) : this._getLatestToken( ns_data )
); );
} }
); );
} );
return this;
} }

View File

@ -19,7 +19,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
import { expect } from 'chai'; import { expect, use as chai_use } from 'chai';
import { import {
TokenQueryResult, TokenQueryResult,
@ -28,12 +28,14 @@ import {
import Sut = require( "../../../src/server/token/TokenDao" ); import Sut = require( "../../../src/server/token/TokenDao" );
chai_use( require( 'chai-as-promised' ) );
describe( 'server.token.TokenDao', () => describe( 'server.token.TokenDao', () =>
{ {
describe( '#updateToken', () => describe( '#updateToken', () =>
{ {
it( 'updates token with given data', done => it( 'updates token with given data', () =>
{ {
const field = 'foo_field'; const field = 'foo_field';
const qid = 12345; const qid = 12345;
@ -79,12 +81,12 @@ describe( 'server.token.TokenDao', () =>
findOne() {}, findOne() {},
}; };
new Sut( coll, field ) return new Sut( coll, field )
.updateToken( qid, ns, tok_id, tok_type, data, done ); .updateToken( qid, ns, tok_id, tok_type, data );
} ); } );
it( 'proxies error to callback', done => it( 'proxies error to callback', () =>
{ {
const expected_error = Error( "expected error" ); const expected_error = Error( "expected error" );
@ -97,12 +99,9 @@ describe( 'server.token.TokenDao', () =>
findOne() {}, findOne() {},
}; };
new Sut( coll, 'foo' ) return expect(
.updateToken( 0, 'ns', 'id', 'DONE', null, err => new Sut( coll, 'foo' ).updateToken( 0, 'ns', 'id', 'DONE', null )
{ ).to.eventually.be.rejectedWith( expected_error );
expect( err ).to.equal( expected_error );
done();
} );
} ); } );
} ); } );
@ -194,7 +193,7 @@ describe( 'server.token.TokenDao', () =>
}, },
], ],
] ).forEach( ( [ label, tok_id, result, expected ] ) => ] ).forEach( ( [ label, tok_id, result, expected ] ) =>
it( label, done => it( label, () =>
{ {
const coll: MongoCollection = { const coll: MongoCollection = {
findOne( _selector, _fields, callback ) findOne( _selector, _fields, callback )
@ -205,19 +204,14 @@ describe( 'server.token.TokenDao', () =>
update() {}, update() {},
}; };
new Sut( coll, field ) return expect(
.getToken( qid, ns, tok_id, ( err, data ) => new Sut( coll, field ).getToken( qid, ns, tok_id )
{ ).to.eventually.deep.equal( expected );
expect( err ).to.equal( null );
expect( data ).to.deep.equal( expected );
done();
} );
} ) } )
); );
it( 'proxies error to callback', done => it( 'proxies error to callback', () =>
{ {
const expected_error = Error( "expected error" ); const expected_error = Error( "expected error" );
@ -230,14 +224,9 @@ describe( 'server.token.TokenDao', () =>
update() {}, update() {},
}; };
new Sut( coll, 'foo' ) return expect(
.getToken( 0, 'ns', 'id', ( err, data ) => new Sut( coll, 'foo' ).getToken( 0, 'ns', 'id' )
{ ).to.eventually.be.rejectedWith( expected_error );
expect( err ).to.equal( expected_error );
expect( data ).to.equal( null );
done();
} );
} ); } );
} ); } );
} ); } );