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

Merge pull request #4259 from weseek/imprv/7348-slackappintegration-set-default-as-true

imprv: 7348 slackappintegration set default as true
Yuki Takei 4 лет назад
Родитель
Сommit
55975911dc

+ 33 - 0
packages/app/src/migrations/20210906194521-slack-app-integration-set-default-value.js

@@ -0,0 +1,33 @@
+import mongoose from 'mongoose';
+
+import { defaultSupportedCommandsNameForBroadcastUse, defaultSupportedCommandsNameForSingleUse } from '@growi/slack';
+import { getModelSafely } from '~/server/util/mongoose-utils';
+import config from '^/config/migrate';
+import loggerFactory from '~/utils/logger';
+
+const logger = loggerFactory('growi:migrate:slack-app-integration-set-default-value');
+
+module.exports = {
+  async up(db) {
+    logger.info('Apply migration');
+    mongoose.connect(config.mongoUri, config.mongodb.options);
+
+    // 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');
+  },
+
+  async down() {
+    // no rollback
+  },
+};

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

@@ -1,12 +1,13 @@
 const crypto = require('crypto');
 const mongoose = require('mongoose');
+const { defaultSupportedCommandsNameForBroadcastUse, defaultSupportedCommandsNameForSingleUse } = require('@growi/slack');
 
 const schema = new mongoose.Schema({
   tokenGtoP: { type: String, required: true, unique: true },
   tokenPtoG: { type: String, required: true, unique: 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 {