Kaynağa Gözat

Merge commit '1eabdc1987bc56d44cb8c7f654ca3fe2dd11f2b4' into feat/5587-move-secret-token-to-accordion

Steven Fukase 5 yıl önce
ebeveyn
işleme
a263e05d60

+ 9 - 0
packages/slackbot-proxy/src/repositories/installation.ts

@@ -11,4 +11,13 @@ export class InstallationRepository extends Repository<Installation> {
     return this.findOne(id);
   }
 
+  async findByTeamIdOrEnterpriseId(teamIdOrEnterpriseId:string): Promise<Installation|undefined> {
+    return this.findOne({
+      where: [
+        { teamId: teamIdOrEnterpriseId },
+        { enterpriseId: teamIdOrEnterpriseId, isEnterpriseInstall: true },
+      ],
+    });
+  }
+
 }

+ 15 - 2
packages/slackbot-proxy/src/services/InstallerService.ts

@@ -33,12 +33,25 @@ export class InstallerService {
       clientSecret,
       stateSecret,
       installationStore: {
+        // upsert
         storeInstallation: async(slackInstallation: SlackInstallation<'v1' | 'v2', boolean>) => {
+          const teamIdOrEnterpriseId = slackInstallation.team?.id || slackInstallation.enterprise?.id;
+
+          if (teamIdOrEnterpriseId == null) {
+            throw new Error('teamId or enterpriseId is required.');
+          }
+
+          const existedInstallation = await repository.findByTeamIdOrEnterpriseId(teamIdOrEnterpriseId);
+
+          if (existedInstallation != null) {
+            existedInstallation.setData(slackInstallation);
+            await repository.save(existedInstallation);
+            return;
+          }
+
           const installation = new Installation();
           installation.setData(slackInstallation);
-
           await repository.save(installation);
-
           return;
         },
         fetchInstallation: async(installQuery: InstallationQuery<boolean>) => {