|
@@ -7,44 +7,60 @@ import loggerFactory from '~/utils/logger';
|
|
|
|
|
|
|
|
const logger = loggerFactory('growi:migrate:update-configs-for-slackbot');
|
|
const logger = loggerFactory('growi:migrate:update-configs-for-slackbot');
|
|
|
|
|
|
|
|
|
|
+// create default data
|
|
|
|
|
+const defaultDataForBroadcastUse = {};
|
|
|
|
|
+defaultSupportedCommandsNameForBroadcastUse.forEach((commandName) => {
|
|
|
|
|
+ defaultDataForBroadcastUse[commandName] = false;
|
|
|
|
|
+});
|
|
|
|
|
+const defaultDataForSingleUse = {};
|
|
|
|
|
+defaultSupportedCommandsNameForSingleUse.forEach((commandName) => {
|
|
|
|
|
+ defaultDataForSingleUse[commandName] = false;
|
|
|
|
|
+});
|
|
|
|
|
+
|
|
|
module.exports = {
|
|
module.exports = {
|
|
|
async up(db) {
|
|
async up(db) {
|
|
|
logger.info('Apply migration');
|
|
logger.info('Apply migration');
|
|
|
- mongoose.connect(getMongoUri(), mongoOptions);
|
|
|
|
|
|
|
+ await mongoose.connect(getMongoUri(), mongoOptions);
|
|
|
|
|
|
|
|
const SlackAppIntegration = getModelSafely('SlackAppIntegration') || require('~/server/models/slack-app-integration')();
|
|
const SlackAppIntegration = getModelSafely('SlackAppIntegration') || require('~/server/models/slack-app-integration')();
|
|
|
|
|
|
|
|
const slackAppIntegrations = await SlackAppIntegration.find();
|
|
const slackAppIntegrations = await SlackAppIntegration.find();
|
|
|
|
|
|
|
|
- // create default data
|
|
|
|
|
- const defaultDataForBroadcastUse = {};
|
|
|
|
|
- defaultSupportedCommandsNameForBroadcastUse.forEach((commandName) => {
|
|
|
|
|
- defaultDataForBroadcastUse[commandName] = false;
|
|
|
|
|
- });
|
|
|
|
|
- const defaultDataForSingleUse = {};
|
|
|
|
|
- defaultSupportedCommandsNameForSingleUse.forEach((commandName) => {
|
|
|
|
|
- defaultDataForSingleUse[commandName] = false;
|
|
|
|
|
- });
|
|
|
|
|
|
|
+ if (slackAppIntegrations.length === 0) return;
|
|
|
|
|
|
|
|
// create operations
|
|
// create operations
|
|
|
const operations = slackAppIntegrations.map((doc) => {
|
|
const operations = slackAppIntegrations.map((doc) => {
|
|
|
- const copyForBroadcastUse = { ...defaultDataForBroadcastUse };
|
|
|
|
|
- const copyForSingleUse = { ...defaultDataForSingleUse };
|
|
|
|
|
- // when the document does NOT have supportedCommandsFor... columns
|
|
|
|
|
- if (doc._doc.supportedCommandsForBroadcastUse == null) {
|
|
|
|
|
- defaultSupportedCommandsNameForBroadcastUse.forEach((commandName) => {
|
|
|
|
|
|
|
+ let copyForBroadcastUse = { ...defaultDataForBroadcastUse };
|
|
|
|
|
+ let copyForSingleUse = { ...defaultDataForSingleUse };
|
|
|
|
|
+ // when the document already has permissionsFor... colums
|
|
|
|
|
+ if (doc._doc.permissionsForBroadcastUseCommands != null) {
|
|
|
|
|
+ // merge
|
|
|
|
|
+ copyForBroadcastUse = {
|
|
|
|
|
+ ...defaultDataForBroadcastUse,
|
|
|
|
|
+ ...Object.fromEntries(doc._doc.permissionsForBroadcastUseCommands),
|
|
|
|
|
+ };
|
|
|
|
|
+ copyForSingleUse = {
|
|
|
|
|
+ ...defaultDataForSingleUse,
|
|
|
|
|
+ ...Object.fromEntries(doc._doc.permissionsForSingleUseCommands),
|
|
|
|
|
+ };
|
|
|
|
|
+ }
|
|
|
|
|
+ // when the document has supportedCommandsFor... columns
|
|
|
|
|
+ else if (doc._doc.supportedCommandsForBroadcastUse != null) {
|
|
|
|
|
+ // merge
|
|
|
|
|
+ doc._doc.supportedCommandsForBroadcastUse.forEach((commandName) => {
|
|
|
copyForBroadcastUse[commandName] = true;
|
|
copyForBroadcastUse[commandName] = true;
|
|
|
});
|
|
});
|
|
|
- defaultSupportedCommandsNameForSingleUse.forEach((commandName) => {
|
|
|
|
|
|
|
+ doc._doc.supportedCommandsForSingleUse.forEach((commandName) => {
|
|
|
copyForSingleUse[commandName] = true;
|
|
copyForSingleUse[commandName] = true;
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|
|
|
- // // when the document has supportedCommandsFor... columns
|
|
|
|
|
|
|
+ // when the document does NOT have supportedCommandsFor... columns
|
|
|
else {
|
|
else {
|
|
|
- doc._doc.supportedCommandsForBroadcastUse.forEach((commandName) => {
|
|
|
|
|
|
|
+ // turn on all
|
|
|
|
|
+ defaultSupportedCommandsNameForBroadcastUse.forEach((commandName) => {
|
|
|
copyForBroadcastUse[commandName] = true;
|
|
copyForBroadcastUse[commandName] = true;
|
|
|
});
|
|
});
|
|
|
- doc._doc.supportedCommandsForSingleUse.forEach((commandName) => {
|
|
|
|
|
|
|
+ defaultSupportedCommandsNameForSingleUse.forEach((commandName) => {
|
|
|
copyForSingleUse[commandName] = true;
|
|
copyForSingleUse[commandName] = true;
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|
|
@@ -52,35 +68,35 @@ module.exports = {
|
|
|
return {
|
|
return {
|
|
|
updateOne: {
|
|
updateOne: {
|
|
|
filter: { _id: doc._id },
|
|
filter: { _id: doc._id },
|
|
|
- update: [
|
|
|
|
|
- {
|
|
|
|
|
- $set: {
|
|
|
|
|
- permissionsForBroadcastUseCommands: copyForBroadcastUse,
|
|
|
|
|
- permissionsForSingleUseCommands: copyForSingleUse,
|
|
|
|
|
- },
|
|
|
|
|
|
|
+ update: {
|
|
|
|
|
+ $set: {
|
|
|
|
|
+ permissionsForBroadcastUseCommands: copyForBroadcastUse,
|
|
|
|
|
+ permissionsForSingleUseCommands: copyForSingleUse,
|
|
|
},
|
|
},
|
|
|
- {
|
|
|
|
|
- $unset: ['supportedCommandsForBroadcastUse', 'supportedCommandsForSingleUse'],
|
|
|
|
|
|
|
+ $unset: {
|
|
|
|
|
+ supportedCommandsForBroadcastUse: '',
|
|
|
|
|
+ supportedCommandsForSingleUse: '',
|
|
|
},
|
|
},
|
|
|
- ],
|
|
|
|
|
|
|
+ },
|
|
|
},
|
|
},
|
|
|
};
|
|
};
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
- await SlackAppIntegration.bulkWrite(operations);
|
|
|
|
|
|
|
+ await db.collection('slackappintegrations').bulkWrite(operations);
|
|
|
|
|
|
|
|
logger.info('Migration has successfully applied');
|
|
logger.info('Migration has successfully applied');
|
|
|
},
|
|
},
|
|
|
|
|
|
|
|
async down(db, next) {
|
|
async down(db, next) {
|
|
|
logger.info('Rollback migration');
|
|
logger.info('Rollback migration');
|
|
|
- // return next();
|
|
|
|
|
- mongoose.connect(getMongoUri(), mongoOptions);
|
|
|
|
|
|
|
+ await mongoose.connect(getMongoUri(), mongoOptions);
|
|
|
|
|
|
|
|
const SlackAppIntegration = getModelSafely('SlackAppIntegration') || require('~/server/models/slack-app-integration')();
|
|
const SlackAppIntegration = getModelSafely('SlackAppIntegration') || require('~/server/models/slack-app-integration')();
|
|
|
|
|
|
|
|
const slackAppIntegrations = await SlackAppIntegration.find();
|
|
const slackAppIntegrations = await SlackAppIntegration.find();
|
|
|
|
|
|
|
|
|
|
+ if (slackAppIntegrations.length === 0) return next();
|
|
|
|
|
+
|
|
|
// create operations
|
|
// create operations
|
|
|
const operations = slackAppIntegrations.map((doc) => {
|
|
const operations = slackAppIntegrations.map((doc) => {
|
|
|
const dataForBroadcastUse = [];
|
|
const dataForBroadcastUse = [];
|
|
@@ -99,22 +115,21 @@ module.exports = {
|
|
|
return {
|
|
return {
|
|
|
updateOne: {
|
|
updateOne: {
|
|
|
filter: { _id: doc._id },
|
|
filter: { _id: doc._id },
|
|
|
- update: [
|
|
|
|
|
- {
|
|
|
|
|
- $set: {
|
|
|
|
|
- supportedCommandsForBroadcastUse: dataForBroadcastUse,
|
|
|
|
|
- supportedCommandsForSingleUse: dataForSingleUse,
|
|
|
|
|
- },
|
|
|
|
|
|
|
+ update: {
|
|
|
|
|
+ $set: {
|
|
|
|
|
+ supportedCommandsForBroadcastUse: dataForBroadcastUse,
|
|
|
|
|
+ supportedCommandsForSingleUse: dataForSingleUse,
|
|
|
},
|
|
},
|
|
|
- {
|
|
|
|
|
- $unset: ['permissionsForBroadcastUseCommands', 'permissionsForSingleUseCommands'],
|
|
|
|
|
|
|
+ $unset: {
|
|
|
|
|
+ permissionsForBroadcastUseCommands: '',
|
|
|
|
|
+ permissionsForSingleUseCommands: '',
|
|
|
},
|
|
},
|
|
|
- ],
|
|
|
|
|
|
|
+ },
|
|
|
},
|
|
},
|
|
|
};
|
|
};
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
- await SlackAppIntegration.bulkWrite(operations);
|
|
|
|
|
|
|
+ await db.collection('slackappintegrations').bulkWrite(operations);
|
|
|
|
|
|
|
|
next();
|
|
next();
|
|
|
logger.info('Migration has successfully applied');
|
|
logger.info('Migration has successfully applied');
|