Skip to content

Instantly share code, notes, and snippets.

@iamvanja
Last active January 15, 2019 20:28
Show Gist options
  • Save iamvanja/b164b28a7552d96cda211bbda4819652 to your computer and use it in GitHub Desktop.
Save iamvanja/b164b28a7552d96cda211bbda4819652 to your computer and use it in GitHub Desktop.
/* eslint-disable no-console */
import { } from 'config/env'
import { APP_ROLE_PUBLISHER } from 'config/constants'
import idManager from 'services/idManager'
import get from 'lodash/get'
import eachLimit from 'async/eachLimit'
/**
* Migrate publishers from momentum.roles to skpn.roles.
* The script can be used for any type of migration, with adjusted parameters.
*/
const MOMENTUM_ROLE_PUBLISHER = 'publishing network'
const getData = (page = 1, limit = 10) => {
const criteria = JSON.stringify({
extras: {
'momentum.roles': '*' + MOMENTUM_ROLE_PUBLISHER
}
})
const options = {
flatten: true,
page,
limit,
order: '[{"property":"created","direction":"desc"}]',
json: criteria
}
return idManager.user
.then(api => api.findPaginatedContentByJson(options))
.then(response => response.body)
.then(users => users.content || [])
.catch(err => {
throw new Error(err)
})
}
const augmentUsers = users =>
new Promise((resolve, reject) => {
const augmented = []
users.forEach(user => {
const path = 'momentum.roles'
const newPath = 'skpn.roles'
const momentumRoles = get(user, `extras["${path}"]`) || []
const skpnRoles = get(user, `extras["${newPath}"]`) || []
if (momentumRoles.includes(MOMENTUM_ROLE_PUBLISHER) &&
!skpnRoles.includes(APP_ROLE_PUBLISHER)
) {
const newExtras = {
[newPath]: [APP_ROLE_PUBLISHER]
}
augmented.push({
id: user.id,
extras: newExtras
})
} else {
console.log('skipping user', user.id, user.extras)
}
})
return resolve(augmented)
})
const simulateRequest = (data) => {
console.log('simulateRequest', data)
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(data)
}, 100)
})
}
const updateUsers = augmentedUsers =>
new Promise((resolve, reject) => {
return eachLimit(
augmentedUsers,
100,
(user, done) => {
Promise.resolve()
.then(() => {
return isDryRun
? simulateRequest
: idManager.user
.then(api => api.updateRecordById)
})
.then(method => {
return method({
flatten: true,
userId: user.id,
body: JSON.stringify(user),
check_consistency: true
})
})
.then(user => {
++updatedUsersCount
done()
})
.catch(err => {
console.log('error while updating user:', user.id)
console.error('ERR:', err)
done()
})
},
err => err ? reject(err) : resolve()
)
})
const args = process.argv.slice(2)
const isDryRun = args.includes('dry-run')
let page = 0
const limit = 30
let fetchedUsersCount = 0
let augmentedUsersCount = 0
let updatedUsersCount = 0
const init = (page, limit) => {
Promise.resolve()
.then(() => console.log('Fetching...', { page, limit }))
.then(() => getData(page, limit))
.then(users => {
if (!users.length) {
throw new Error('end')
}
return users
})
.then(users => {
fetchedUsersCount += users.length
console.log('Preparing Dashboard extras...')
return augmentUsers(users)
})
.then(augmentedUsers => {
augmentedUsersCount += augmentedUsers.length
console.log('Updating user data...')
return updateUsers(augmentedUsers)
})
.then(() => init(++page, limit))
.catch(err => {
if (err.message === 'end') {
console.log('FINISH!')
console.log({
fetchedUsersCount,
augmentedUsersCount,
updatedUsersCount
})
} else {
console.error('Main Promise chain ERR', err)
}
})
}
init(page, limit)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment