diff --git a/packages/backend/src/service.ts b/packages/backend/src/service.ts index 6adb400a..1bf7ccbf 100644 --- a/packages/backend/src/service.ts +++ b/packages/backend/src/service.ts @@ -1289,27 +1289,53 @@ export class Service { }, }) + // If the canonical deployment is not present then query the chain for the deployment record for backwards compatibility if (!canonicalDeployment) { - log(`Canonical deployment for deployment with id ${deployment.id} not found`); + log(`Canonical deployment for deployment with id ${deployment.id} not found, querying the chain..`); + const currentDeploymentURL = `https://${(deployment.project.name).toLowerCase()}.${deployment.deployer.baseDomain}`; - return false; - } + const deploymentRecords = + await this.laconicRegistry.getDeploymentRecordsByFilter({ + application: deployment.applicationRecordId, + url: currentDeploymentURL, + }); + + if (!deploymentRecords.length) { + log( + `No ApplicationDeploymentRecord found for URL ${currentDeploymentURL} and ApplicationDeploymentRecord id ${deployment.applicationDeploymentRecordId}`, + ); + + return false; + } + + // Multiple records are fetched, take the latest record + const latestRecord = deploymentRecords + .sort((a, b) => new Date(b.createTime).getTime() - new Date(a.createTime).getTime())[0]; - const dnsResult = await this.laconicRegistry.createApplicationDeploymentRemovalRequest({ - deploymentId: canonicalDeployment.applicationDeploymentRecordId!, - deployerLrn: canonicalDeployment.deployer.deployerLrn, - auctionId: canonicalDeployment.project.auctionId, - payment: canonicalDeployment.project.txHash + deploymentId: latestRecord.id, + deployerLrn: deployment.deployer.deployerLrn, + auctionId: deployment.project.auctionId, + payment: deployment.project.txHash }); + } else { + // If canonical deployment is found in the DB, then send the removal request with that deployment record Id + const result = + await this.laconicRegistry.createApplicationDeploymentRemovalRequest({ + deploymentId: canonicalDeployment.applicationDeploymentRecordId!, + deployerLrn: canonicalDeployment.deployer.deployerLrn, + auctionId: canonicalDeployment.project.auctionId, + payment: canonicalDeployment.project.txHash + }); - await this.db.updateDeploymentById(canonicalDeployment.id, { - status: DeploymentStatus.Deleting, - applicationDeploymentRemovalRequestId: - dnsResult.applicationDeploymentRemovalRequestId, - applicationDeploymentRemovalRequestData: - dnsResult.applicationDeploymentRemovalRequestData, - }); + await this.db.updateDeploymentById(canonicalDeployment.id, { + status: DeploymentStatus.Deleting, + applicationDeploymentRemovalRequestId: + result.applicationDeploymentRemovalRequestId, + applicationDeploymentRemovalRequestData: + result.applicationDeploymentRemovalRequestData, + }); + } } const result =