Taichi Masuyama 4 лет назад
Родитель
Сommit
9434519563

+ 13 - 36
packages/app/src/migrations/20210906194521-slack-app-integration-set-default-value.js

@@ -12,45 +12,22 @@ module.exports = {
     logger.info('Apply migration');
     logger.info('Apply migration');
     mongoose.connect(config.mongoUri, config.mongodb.options);
     mongoose.connect(config.mongoUri, config.mongodb.options);
 
 
-    const defaultValuesForBroadcastUse = defaultSupportedCommandsNameForBroadcastUse.map(commandName => [commandName, true]);
-    const defaultValuesForSingleUse = defaultSupportedCommandsNameForSingleUse.map(commandName => [commandName, true]);
-
-    await db.collection('slackappintegrations').updateMany(
-      {},
-      [
-        {
-          $set: {
-            permissionsForBroadcastUseCommands: new Map(defaultValuesForBroadcastUse),
-            permissionsForSingleUseCommands: new Map(defaultValuesForSingleUse),
-          },
-        },
-        {
-          $unset: ['supportedCommandsForSingleUse', 'supportedCommandsForBroadcastUse'],
-        },
-      ],
-    );
+    // Add columns + set all default commands if supportedCommandsForBroadcastUse column does not exist
+    const SlackAppIntegration = getModelSafely('SlackAppIntegration') || require('~/server/models/slack-app-integration')();
+
+    // Add togetter command if supportedCommandsForBroadcastUse already exists
+    const slackAppIntegrations = await SlackAppIntegration.find();
+    slackAppIntegrations.forEach(async(doc) => {
+      if (!doc.supportedCommandsForSingleUse.includes('togetter')) {
+        doc.supportedCommandsForSingleUse.push('togetter');
+      }
+      await doc.save();
+    });
 
 
     logger.info('Migration has successfully applied');
     logger.info('Migration has successfully applied');
   },
   },
 
 
-  async down(db, next) {
-    logger.info('Rollback migration');
-    mongoose.connect(config.mongoUri, config.mongodb.options);
-
-    await db.collection('slackappintegrations').updateMany(
-      {},
-      [
-        {
-          $set: {
-            supportedCommandsForBroadcastUse: defaultSupportedCommandsNameForBroadcastUse,
-            supportedCommandsForSingleUse: defaultSupportedCommandsNameForSingleUse,
-          },
-        },
-        {
-          $unset: ['permissionsForBroadcastUseCommands', 'permissionsForSingleUseCommands'],
-        },
-      ],
-    );
-    next();
+  async down() {
+    // no rollback
   },
   },
 };
 };

+ 3 - 2
packages/app/src/server/models/slack-app-integration.js

@@ -1,12 +1,13 @@
 const crypto = require('crypto');
 const crypto = require('crypto');
 const mongoose = require('mongoose');
 const mongoose = require('mongoose');
+const { defaultSupportedCommandsNameForBroadcastUse, defaultSupportedCommandsNameForSingleUse } = require('@growi/slack');
 
 
 const schema = new mongoose.Schema({
 const schema = new mongoose.Schema({
   tokenGtoP: { type: String, required: true, unique: true },
   tokenGtoP: { type: String, required: true, unique: true },
   tokenPtoG: { type: String, required: true, unique: true },
   tokenPtoG: { type: String, required: true, unique: true },
   isPrimary: { type: Boolean, unique: true, sparse: true },
   isPrimary: { type: Boolean, unique: true, sparse: true },
-  supportedCommandsForBroadcastUse: { type: [String], default: [] },
-  supportedCommandsForSingleUse: { type: [String], default: [] },
+  supportedCommandsForBroadcastUse: { type: [String], default: defaultSupportedCommandsNameForBroadcastUse },
+  supportedCommandsForSingleUse: { type: [String], default: defaultSupportedCommandsNameForSingleUse },
 });
 });
 
 
 class SlackAppIntegration {
 class SlackAppIntegration {