@@ -112,7 +112,7 @@ async function _ensureEditPermissionAndGetInfo(projectId, currentUser) {
112112 ) {
113113 throw new errors . ForbiddenError ( 'You don\'t have access on this project' ) ;
114114 }
115- if ( dbProject . archived ) {
115+ if ( dbProject . archived === 'true' ) {
116116 throw new errors . ForbiddenError ( 'You can\'t access on this archived project' ) ;
117117 }
118118 return dbProject ;
@@ -140,33 +140,21 @@ async function _createOrMigrateRepository(repoUrl, project, currentUser) {
140140 or a time-sequence cornercase encountered here` ) ;
141141 }
142142 try {
143- let oldIssues = await models . Issue . query ( { repoUrl : oldRepo . url } ) ;
144- let oldCopilotPaymentPromise = oldIssues . filter ( issue => issue . challengeUUID )
145- . map ( issue => models . CopilotPayment . query ( { challengeUUID : issue . challengeUUID } )
146- . then ( payments => {
147- if ( ! payments || payments . length === 0 ) {
148- /* eslint-disable-next-line no-console */
149- console . log ( `No CopilotPayment correspond to Issue with challengeUUID ${ issue . challengeUUID } .
150- The corresponding CopilotPayment may have been removed.
151- Or, there is bug in old version.` ) ;
152- return null ;
153- }
154- if ( payments . length > 1 ) {
155- throw new Error ( `Duplicate CopilotPayment correspond to one Issue with challengeUUID ${ issue . challengeUUID } .
156- There must be bug in old version` ) ;
157- }
158- return payments [ 0 ] ;
159- } ) ) ;
160- let oldCopilotPayment = await Promise . all ( oldCopilotPaymentPromise ) . filter ( payment => payment ) ;
161-
162- await models . Repository . update ( { id : oldRepo . id } , { projectId : project . id , archived : false } ) ;
163- await oldIssues . forEach ( issue => models . Issue . update ( { id : issue . id } , { projectId : project . id } ) ) ;
164- await oldCopilotPayment . forEach (
165- payment => models . CopilotPayment . update ( { id : payment . id } , { project : project . id } )
143+ const oldIssues = await dbHelper . queryIssueIdChallengeUUIDByRepoUrl ( repoUrl ) ;
144+ const issueIds = oldIssues . map ( issue => issue . id ) ;
145+ const challengeUUIDs = oldIssues . map ( issue => issue . challengeUUID ) . filter ( challengeUUID => challengeUUID ) ;
146+ const paymentIds = await Promise . all (
147+ challengeUUIDs . map ( challengeUUID => dbHelper . queryPaymentIdByChallengeUUID ( challengeUUID ) )
148+ ) ;
149+
150+ await dbHelper . update ( models . Repository , oldRepo . id , { projectId : project . id , archived : false } ) ;
151+ await Promise . all ( issueIds . map ( issueId => dbHelper . update ( models . Issue , issueId , { projectId : project . id } ) ) ) ;
152+ await Promise . all (
153+ paymentIds . map ( paymentId => dbHelper . update ( models . CopilotPayment , paymentId , { project : project . id } ) )
166154 ) ;
167155 }
168156 catch ( err ) {
169- throw new Error ( `Update ProjectId for Repository, Issue, CopilotPayment failed. Repo ${ repoUrl } . Internal Error: ${ err . message } ` ) ;
157+ throw new Error ( `Update ProjectId for Repository, Issue, CopilotPayment failed. Repo ${ repoUrl } . Internal Error: ${ err } ` ) ;
170158 }
171159 } else {
172160 try {
@@ -181,7 +169,7 @@ async function _createOrMigrateRepository(repoUrl, project, currentUser) {
181169 await addWikiRules ( { projectId : project . id } , currentUser , repoUrl ) ;
182170 }
183171 catch ( err ) {
184- throw new Error ( `Project created. Adding the webhook, issue labels, and wiki rules failed. Repo ${ repoUrl } . Internal Error: ${ err . message } ` ) ;
172+ throw new Error ( `Project created. Adding the webhook, issue labels, and wiki rules failed. Repo ${ repoUrl } . Internal Error: ${ err } ` ) ;
185173 }
186174 }
187175}
@@ -213,6 +201,8 @@ async function create(project, currentUser) {
213201 project . copilot = project . copilot ? project . copilot . toLowerCase ( ) : null ;
214202 project . id = helper . generateIdentifier ( ) ;
215203
204+ const createdProject = await dbHelper . create ( models . Project , project ) ;
205+
216206 // TODO: The following db operation should/could be moved into one transaction
217207 for ( const repoUrl of repoUrls ) { // eslint-disable-line no-restricted-syntax
218208 try {
@@ -222,7 +212,6 @@ async function create(project, currentUser) {
222212 throw new Error ( `Create or migrate repository failed. Repo ${ repoUrl } . Internal Error: ${ err . message } ` ) ;
223213 }
224214 }
225- const createdProject = await dbHelper . create ( models . Project , project ) ;
226215
227216 return createdProject ;
228217}
@@ -267,12 +256,12 @@ async function update(project, currentUser) {
267256 } ) ;
268257
269258 // TODO: move the following logic into one dynamoose transaction
270- const repoUrl2Repo = await dbHelper . queryRepositoriesByProjectId ( dbProject . id )
271- . map ( repo => { return { [ repo . url ] : repo } ; } ) ;
259+ const repos = await dbHelper . queryRepositoriesByProjectId ( dbProject . id ) ;
272260
273261 for ( const repoUrl of repoUrls ) { // eslint-disable-line no-restricted-syntax
274- if ( repoUrl in repoUrl2Repo ) {
275- await models . Repository . update ( { id : repoUrl2Repo [ repoUrl ] . id } , { archived : project . archived } ) ;
262+ if ( repos . find ( repo => repo . url === repoUrl ) ) {
263+ const repoId = repos . find ( repo => repo . url === repoUrl ) . id
264+ await dbHelper . update ( models . Repository , repoId , { archived : project . archived } ) ;
276265 } else {
277266 try {
278267 await _createOrMigrateRepository ( repoUrl , project , currentUser ) ;
@@ -319,7 +308,6 @@ async function getAll(query, currentUser) {
319308 query . lastKey = parseInt ( query . lastKey , 10 ) ;
320309 }
321310 const slicedProjects = _ . slice ( projects , query . lastKey , query . lastKey + query . perPage ) ;
322- // console.log(projects);
323311 for ( const project of slicedProjects ) { // eslint-disable-line
324312 project . repoUrls = await dbHelper . populateRepoUrls ( project . id ) ;
325313 }
0 commit comments