From c26b884bc7aed3dd6c57134dad7f9fd99f6f161a Mon Sep 17 00:00:00 2001 From: Keith Holliday Date: Thu, 9 Mar 2017 14:26:31 -0700 Subject: [PATCH] Group migrations (#8528) * Added create group migration * Add migration for unlimited group subscription * Add migration to update group members with group plans * Added error catch * Added comments --- .../groups/add-unlimited-subscription.js | 33 ++++++++++++++ migrations/groups/create-group.js | 32 +++++++++++++ .../groups/update-groups-with-group-plans.js | 29 ++++++++++++ migrations/migration-runner.js | 45 ++++++++++--------- 4 files changed, 118 insertions(+), 21 deletions(-) create mode 100644 migrations/groups/add-unlimited-subscription.js create mode 100644 migrations/groups/create-group.js create mode 100644 migrations/groups/update-groups-with-group-plans.js diff --git a/migrations/groups/add-unlimited-subscription.js b/migrations/groups/add-unlimited-subscription.js new file mode 100644 index 0000000000..653024d0b8 --- /dev/null +++ b/migrations/groups/add-unlimited-subscription.js @@ -0,0 +1,33 @@ +var migrationName = 'AddUnlimitedSubscription'; +var authorName = 'TheHollidayInn'; // in case script author needs to know when their ... +var authorUuid = ''; //... own data is done + +/* + * This migrations will add a free subscription to a specified group + */ + +import { model as Group } from '../../website/server/models/group'; + +// @TODO: this should probably be a GroupManager library method +async function addUnlimitedSubscription (groupId) { + let group = await Group.findById(groupId); + + group.purchased.plan.customerId = "group-unlimited"; + group.purchased.plan.dateCreated = new Date(); + group.purchased.plan.dateUpdated = new Date(); + group.purchased.plan.paymentMethod = "Group Unlimited"; + group.purchased.plan.planId = "group_monthly"; + group.purchased.plan.dateTerminated = null; + // group.purchased.plan.owner = ObjectId(); + group.purchased.plan.subscriptionId = ""; + + return group.save(); +}; + +module.exports = async function addUnlimitedSubscriptionCreator () { + let groupId = process.argv[2]; + + if (!groupId) throw Error('Group ID is required'); + + let result = await addUnlimitedSubscription(groupId) +}; diff --git a/migrations/groups/create-group.js b/migrations/groups/create-group.js new file mode 100644 index 0000000000..79a4c03e63 --- /dev/null +++ b/migrations/groups/create-group.js @@ -0,0 +1,32 @@ +import Bluebird from 'bluebird'; + +import { model as Group } from '../../website/server/models/group'; +import { model as User } from '../../website/server/models/user'; + +// @TODO: this should probably be a GroupManager library method +async function createGroup (name, privacy, type, leaderId) { + let user = await User.findById(leaderId); + + let group = new Group({ + name, + privacy, + type, + }); + + group.leader = user._id; + user.guilds.push(group._id); + + return Bluebird.all([group.save(), user.save()]); +}; + +module.exports = async function groupCreator () { + let name = process.argv[2]; + let privacy = process.argv[3]; + let type = process.argv[4]; + let leaderId = process.argv[5]; + + let result = await createGroup(name, privacy, type, leaderId) +}; + + + diff --git a/migrations/groups/update-groups-with-group-plans.js b/migrations/groups/update-groups-with-group-plans.js new file mode 100644 index 0000000000..0c3d196e9f --- /dev/null +++ b/migrations/groups/update-groups-with-group-plans.js @@ -0,0 +1,29 @@ +var migrationName = 'ResyncGroupPlanMembers'; +var authorName = 'TheHollidayInn'; // in case script author needs to know when their ... +var authorUuid = ''; //... own data is done + +/* + * This migrations will iterate through all groups with a group plan a subscription and resync the free + * subscription to all members + */ + +import Bluebird from 'bluebird'; + +import { model as Group } from '../../website/server/models/group'; +import * as payments from '../../website/server/libs/payments'; + +async function updateGroupsWithGroupPlans () { + let groups = await Group.find({ + 'purchased.plan.customerId': 'group-unlimited', + }).exec(); + + let promises = []; + groups.forEach(function(group) { + promises.push(payments.addSubscriptionToGroupUsers(group)); + promises.push(group.save()) + }); + + return await Bluebird.all(promises); +}; + +module.exports = updateGroupsWithGroupPlans; diff --git a/migrations/migration-runner.js b/migrations/migration-runner.js index 349c5a44fd..33b6d35279 100644 --- a/migrations/migration-runner.js +++ b/migrations/migration-runner.js @@ -1,21 +1,24 @@ -require("babel-register"); -require("babel-polyfill"); - -// This file must use ES5, everything required can be in ES6 - -function setUpServer () { - var nconf = require('nconf'); - var mongoose = require('mongoose'); - var Bluebird = require('bluebird'); - var setupNconf = require('../website/server/libs/setupNconf'); - setupNconf(); - // We require src/server and npt src/index because - // 1. nconf is already setup - // 2. we don't need clustering - require('../website/server/server'); // eslint-disable-line global-require -} -setUpServer(); - -// Replace this with your migration -var processUsers = require('./new_stuff'); -processUsers(); +require("babel-register"); +require("babel-polyfill"); + +// This file must use ES5, everything required can be in ES6 + +function setUpServer () { + var nconf = require('nconf'); + var mongoose = require('mongoose'); + var Bluebird = require('bluebird'); + var setupNconf = require('../website/server/libs/setupNconf'); + setupNconf(); + // We require src/server and npt src/index because + // 1. nconf is already setup + // 2. we don't need clustering + require('../website/server/server'); // eslint-disable-line global-require +} +setUpServer(); + +// Replace this with your migration +var processUsers = require('./groups/update-groups-with-group-plans'); +processUsers() + .catch(function (err) { + console.log(err) + })