@@ -179,6 +179,43 @@ async function queryOneIssue(model, repositoryId, number, provider) {
179179 } ) ;
180180}
181181
182+ /**
183+ * Get Issue's id and challengeUUID by repoUrl
184+ * @param {String } repoUrl The repo url
185+ * @returns {Promise<Object> }
186+ */
187+ async function queryIssueIdChallengeUUIDByRepoUrl ( repoUrl ) {
188+ return await new Promise ( ( resolve , reject ) => {
189+ models . Issue . scan ( 'repoUrl' ) . eq ( repoUrl )
190+ . attributes ( [ 'id' , 'challengeUUID' ] )
191+ . exec ( ( err , result ) => {
192+ if ( err ) {
193+ return reject ( err ) ;
194+ }
195+ return resolve ( result ) ;
196+ } ) ;
197+ } ) ;
198+ }
199+
200+
201+ /**
202+ * Get CopilotPayment's id by challengeUUID
203+ * @param {String } challengeUUID The challengeUUID
204+ * @returns {Promise<String> }
205+ */
206+ async function queryPaymentIdByChallengeUUID ( challengeUUID ) {
207+ return await new Promise ( ( resolve , reject ) => {
208+ models . CopilotPayment . scan ( 'challengeUUID' ) . eq ( challengeUUID )
209+ . attributes ( [ 'id' ] )
210+ . exec ( ( err , result ) => {
211+ if ( err ) {
212+ return reject ( err ) ;
213+ }
214+ return resolve ( result . id ) ;
215+ } ) ;
216+ } ) ;
217+ }
218+
182219/**
183220 * Get single data by query parameters
184221 * @param {Object } model The dynamoose model to query
@@ -248,6 +285,27 @@ async function queryOneUserMappingByTCUsername(model, tcusername) {
248285 } ) ;
249286}
250287
288+ /**
289+ * Get single data by query parameters
290+ * @param {Object } model The dynamoose model to query
291+ * @param {String } provider The git provider
292+ * @param {String } gitUsername The git username
293+ * @returns {Promise<void> }
294+ */
295+ async function queryTCUsernameByGitUsername ( model , provider , gitUsername ) {
296+ return await new Promise ( ( resolve , reject ) => {
297+ model . queryOne ( `${ provider } Username` ) . eq ( gitUsername )
298+ . all ( )
299+ . exec ( ( err , result ) => {
300+ if ( err ) {
301+ logger . debug ( `queryTCUsernameByGitUsername. Error. ${ err } ` ) ;
302+ return reject ( err ) ;
303+ }
304+ return resolve ( result . topcoderUsername ) ;
305+ } ) ;
306+ } ) ;
307+ }
308+
251309/**
252310 * Get single data by query parameters
253311 * @param {Object } model The dynamoose model to query
@@ -257,7 +315,7 @@ async function queryOneUserMappingByTCUsername(model, tcusername) {
257315async function queryOneActiveProject ( model , repoUrl ) {
258316 return await new Promise ( ( resolve , reject ) => {
259317 queryOneActiveRepository ( models . Repository , repoUrl ) . then ( ( repo ) => {
260- if ( ! repo ) resolve ( null ) ;
318+ if ( ! repo || repo . length === 0 ) resolve ( null ) ;
261319 else model . queryOne ( 'id' ) . eq ( repo . projectId ) . consistent ( )
262320 . exec ( ( err , result ) => {
263321 if ( err ) {
@@ -470,6 +528,35 @@ async function queryOneOrganisation(model, organisation) {
470528 } ) ;
471529}
472530
531+ /**
532+ * Query one active repository
533+ * @param {String } url the repository url
534+ * @returns {Promise<Object> }
535+ */
536+ async function queryOneRepository ( url ) {
537+ return await new Promise ( ( resolve , reject ) => {
538+ models . Repository . query ( {
539+ url,
540+ } )
541+ . all ( )
542+ . exec ( ( err , repos ) => {
543+ if ( err ) {
544+ return reject ( err ) ;
545+ }
546+ if ( ! repos || repos . length === 0 ) resolve ( null ) ;
547+ if ( repos . length > 1 ) {
548+ let error = `Repository's url is unique in this version.
549+ This Error must be caused by old data in the Repository table.
550+ The old version can only guarrentee that the active Repository's url is unique.
551+ Please migrate the old Repository table.` ;
552+ logger . debug ( `queryOneRepository. Error. ${ error } ` ) ;
553+ reject ( error ) ;
554+ }
555+ return resolve ( repos [ 0 ] ) ;
556+ } ) ;
557+ } ) ;
558+ }
559+
473560/**
474561 * Query one active repository
475562 * @param {Object } model the dynamoose model
@@ -480,8 +567,8 @@ async function queryOneActiveRepository(model, url) {
480567 return await new Promise ( ( resolve , reject ) => {
481568 model . queryOne ( {
482569 url,
483- archived : 'false'
484570 } )
571+ . filter ( 'archived' ) . eq ( 'false' )
485572 . all ( )
486573 . exec ( ( err , result ) => {
487574 if ( err ) {
@@ -502,8 +589,8 @@ async function queryActiveRepositoriesExcludeByProjectId(url, projectId) {
502589 return await new Promise ( ( resolve , reject ) => {
503590 models . Repository . query ( {
504591 url,
505- archived : 'false'
506592 } )
593+ . filter ( 'archived' ) . eq ( 'false' )
507594 . filter ( 'projectId' )
508595 . not ( ) . eq ( projectId )
509596 . all ( )
@@ -580,6 +667,8 @@ async function populateRepoUrls(projectId) {
580667}
581668
582669module . exports = {
670+ queryIssueIdChallengeUUIDByRepoUrl,
671+ queryPaymentIdByChallengeUUID,
583672 getById,
584673 getByKey,
585674 scan,
@@ -597,13 +686,15 @@ module.exports = {
597686 queryOneActiveProject,
598687 queryOneActiveProjectWithFilter,
599688 queryOneActiveRepository,
689+ queryOneRepository,
600690 queryOneOrganisation,
601691 queryOneIssue,
602692 queryOneUserByType,
603693 queryOneUserByTypeAndRole,
604694 queryOneUserGroupMapping,
605695 queryOneUserTeamMapping,
606696 queryOneUserMappingByTCUsername,
697+ queryTCUsernameByGitUsername,
607698 queryRepositoriesByProjectId,
608699 queryRepositoryByProjectIdFilterUrl
609700} ;
0 commit comments