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

View File

@ -79,9 +79,6 @@ export = class TokenDao
* @param token token value
* @param data token data, if any
* @param status arbitrary token type
* @param callback with error or null (success)
*
* @return self
*/
updateToken(
quote_id: number,
@ -89,8 +86,7 @@ export = class TokenDao
token: string,
type: TokenType,
data: string | null,
callback: ( err: Error|null ) => void,
): this
): Promise<void>
{
const root = this._genRoot( ns ) + '.';
@ -113,21 +109,28 @@ export = class TokenDao
[ root + token + '.statusLog' ]: token_entry,
};
this._collection.update(
{ id: +quote_id },
{
$set: token_data,
$push: token_log
},
{ upsert: true },
return new Promise( ( resolve, reject ) =>
{
this._collection.update(
{ id: +quote_id },
{
$set: token_data,
$push: token_log
},
{ upsert: true },
function ( err: Error|null )
{
callback( err || null );
}
);
function ( err: Error|null )
{
if ( err )
{
reject( err );
return;
}
return this;
resolve();
}
);
} );
}
@ -141,16 +144,11 @@ export = class TokenDao
* @param quote_id quote identifier
* @param ns token namespace
* @param token_id token identifier (unique to NS)
* @param callback
*
* @return self
* @return token data
*/
getToken(
quote_id: number,
ns: string,
token_id: string,
callback: ( err: Error|null, data: TokenData|null ) => void,
): this
getToken( quote_id: number, ns: string, token_id: string ):
Promise<TokenData|null>
{
const root = this._genRoot( ns ) + '.';
const fields: any = {};
@ -164,37 +162,37 @@ export = class TokenDao
fields[ root + token_id ] = 1;
}
this._collection.findOne(
{ id: +quote_id },
{ fields: fields },
( err: Error|null, data: TokenQueryResult ) =>
{
if ( err || !data )
return new Promise( ( resolve, reject ) =>
{
this._collection.findOne(
{ id: +quote_id },
{ fields: fields },
( err: Error|null, data: TokenQueryResult ) =>
{
callback( err, null );
return;
}
if ( err || !data )
{
reject( err );
return;
}
const field = <TokenNamespaceResults>data[ this._rootField ] || {};
const field = <TokenNamespaceResults>data[ this._rootField ]
|| {};
if ( !field[ ns ] )
{
callback( null, null );
return;
}
if ( !field[ ns ] )
{
resolve( null );
return;
}
const ns_data = <TokenNamespaceData>field[ ns ];
const ns_data = <TokenNamespaceData>field[ ns ];
callback(
null,
( token_id )
resolve( ( token_id )
? this._getRequestedToken( token_id, 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/>.
*/
import { expect } from 'chai';
import { expect, use as chai_use } from 'chai';
import {
TokenQueryResult,
@ -28,12 +28,14 @@ import {
import Sut = require( "../../../src/server/token/TokenDao" );
chai_use( require( 'chai-as-promised' ) );
describe( 'server.token.TokenDao', () =>
{
describe( '#updateToken', () =>
{
it( 'updates token with given data', done =>
it( 'updates token with given data', () =>
{
const field = 'foo_field';
const qid = 12345;
@ -79,12 +81,12 @@ describe( 'server.token.TokenDao', () =>
findOne() {},
};
new Sut( coll, field )
.updateToken( qid, ns, tok_id, tok_type, data, done );
return new Sut( coll, field )
.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" );
@ -97,12 +99,9 @@ describe( 'server.token.TokenDao', () =>
findOne() {},
};
new Sut( coll, 'foo' )
.updateToken( 0, 'ns', 'id', 'DONE', null, err =>
{
expect( err ).to.equal( expected_error );
done();
} );
return expect(
new Sut( coll, 'foo' ).updateToken( 0, 'ns', 'id', 'DONE', null )
).to.eventually.be.rejectedWith( expected_error );
} );
} );
@ -194,7 +193,7 @@ describe( 'server.token.TokenDao', () =>
},
],
] ).forEach( ( [ label, tok_id, result, expected ] ) =>
it( label, done =>
it( label, () =>
{
const coll: MongoCollection = {
findOne( _selector, _fields, callback )
@ -205,19 +204,14 @@ describe( 'server.token.TokenDao', () =>
update() {},
};
new Sut( coll, field )
.getToken( qid, ns, tok_id, ( err, data ) =>
{
expect( err ).to.equal( null );
expect( data ).to.deep.equal( expected );
done();
} );
return expect(
new Sut( coll, field ).getToken( qid, ns, tok_id )
).to.eventually.deep.equal( expected );
} )
);
it( 'proxies error to callback', done =>
it( 'proxies error to callback', () =>
{
const expected_error = Error( "expected error" );
@ -230,14 +224,9 @@ describe( 'server.token.TokenDao', () =>
update() {},
};
new Sut( coll, 'foo' )
.getToken( 0, 'ns', 'id', ( err, data ) =>
{
expect( err ).to.equal( expected_error );
expect( data ).to.equal( null );
done();
} );
return expect(
new Sut( coll, 'foo' ).getToken( 0, 'ns', 'id' )
).to.eventually.be.rejectedWith( expected_error );
} );
} );
} );