Просмотр исходного кода

Merge pull request #3614 from weseek/imprv/5519-5573-update-InstallationRepository

Imprv/5519 5573 update installation repository
itizawa 5 лет назад
Родитель
Сommit
1eabdc1987

+ 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>) => {

+ 1 - 1
src/client/js/components/Admin/SlackIntegration/CustomBotWithoutProxySettingsAccordion.jsx

@@ -23,7 +23,7 @@ const CustomBotWithoutProxySettingsAccordion = (props) => {
     setOpenAccordionIndexes(accordionIndexes);
   };
 
-  const onTestConnectionHandler = async () => {
+  const onTestConnectionHandler = async() => {
     setConnectionErrorCode(null);
     setConnectionErrorMessage(null);
     try {