@@ -228,7 +228,96 @@ exports.challengeHelper = function (api, next) {
228228 }
229229 next ( null , result . terms ) ;
230230 } ) ;
231+ } ,
232+ getChallengeTermsNoAuth : function ( connection , challengeId , role , requireRegOpen , dbConnectionMap , next ) {
233+
234+ var helper = api . helper ,
235+ sqlParams = { } ,
236+ result = { } ,
237+ userId = connection . caller . userId ;
238+
239+ async . waterfall ( [
240+ function ( cb ) {
241+
242+ //Simple validations of the incoming parameters
243+ var error = helper . checkPositiveInteger ( challengeId , 'challengeId' ) ||
244+ helper . checkMaxInt ( challengeId , 'challengeId' ) ;
245+
246+ if ( error ) {
247+ cb ( error ) ;
248+ return ;
249+ }
250+
251+ sqlParams . challengeId = challengeId ;
252+
253+ // We are here. So all validations have passed.
254+ // Next we get all roles
255+ api . dataAccess . executeQuery ( "all_resource_roles" , { } , dbConnectionMap , cb ) ;
256+ } , function ( rows , cb ) {
257+ // Prepare a comma separated string of resource role names that must match
258+ var commaSepRoleIds = "" ,
259+ compiled = _ . template ( "<%= resource_role_id %>," ) ,
260+ ctr = 0 ,
261+ resourceRoleFound ;
262+ if ( _ . isUndefined ( role ) ) {
263+ rows . forEach ( function ( row ) {
264+ commaSepRoleIds += compiled ( { resource_role_id : row . resource_role_id } ) ;
265+ ctr += 1 ;
266+ if ( ctr === rows . length ) {
267+ commaSepRoleIds = commaSepRoleIds . slice ( 0 , - 1 ) ;
268+ }
269+ } ) ;
270+ } else {
271+ resourceRoleFound = _ . find ( rows , function ( row ) {
272+ return ( row . name === role ) ;
273+ } ) ;
274+ if ( _ . isUndefined ( resourceRoleFound ) ) {
275+ //The role passed in is not recognized
276+ cb ( new BadRequestError ( "The role: " + role + " was not found." ) ) ;
277+ return ;
278+ }
279+ commaSepRoleIds = resourceRoleFound . resource_role_id ;
280+ }
281+
282+ // Get the terms
283+ sqlParams . resourceRoleIds = commaSepRoleIds ;
284+ api . dataAccess . executeQuery ( "challenge_terms_of_use_noauth" , sqlParams , dbConnectionMap , cb ) ;
285+ } , function ( rows , cb ) {
286+ //We could just have down result.data = rows; but we need to change keys to camel case as per requirements
287+ result . terms = [ ] ;
288+ _ . each ( rows , function ( row ) {
289+
290+ result . terms . push ( {
291+ termsOfUseId : row . terms_of_use_id ,
292+ title : row . title ,
293+ url : row . url ,
294+ agreeabilityType : row . agreeability_type ,
295+ agreed : row . agreed ,
296+ templateId : row . docusign_template_id
297+ } ) ;
298+ } ) ;
299+
300+ var ids = { } ;
301+ result . terms = result . terms . filter ( function ( row ) {
302+ if ( ids [ row . termsOfUseId ] ) {
303+ return false ;
304+ } else {
305+ ids [ row . termsOfUseId ] = true ;
306+ return true ;
307+ }
308+ } ) ;
309+
310+ cb ( ) ;
311+ }
312+ ] , function ( err ) {
313+ if ( err ) {
314+ next ( err ) ;
315+ return ;
316+ }
317+ next ( null , result . terms ) ;
318+ } ) ;
231319 }
320+
232321 } ;
233322
234323 next ( ) ;
0 commit comments