|
| 1 | +var async = require('async'); |
| 2 | +/** |
| 3 | + * Resave sys_user structure for those affected by https://github.com/CoderDojo/community-platform/issues/1202 |
| 4 | + */ |
| 5 | +module.exports = function (args, done) { |
| 6 | + var seneca = this; |
| 7 | + var plugin = args.role; |
| 8 | + var entity = seneca.make$('sys_user'); |
| 9 | + entity.native$(function (err, client, release) { |
| 10 | + var finishUp = function (err) { |
| 11 | + release(); |
| 12 | + done(err); |
| 13 | + }; |
| 14 | + if (err) finishUp(err); |
| 15 | + client.query('SELECT p.id, u.id as userId FROM cd_profiles p JOIN sys_user u on u.id = p.user_id WHERE u.name != p.name;', |
| 16 | + function (err, res) { |
| 17 | + if (err) finishUp(err); |
| 18 | + async.each(res.rows, function (faulty, eCb) { |
| 19 | + async.waterfall([ |
| 20 | + // Recover originalProfile |
| 21 | + function (wfCb) { |
| 22 | + seneca.act({role: plugin, cmd: 'load', id: faulty.id}, function (err, profile) { |
| 23 | + if (err) eCb(err); |
| 24 | + wfCb(null, profile); |
| 25 | + }); |
| 26 | + }, |
| 27 | + // Recover original user for perm bypass |
| 28 | + function (profile, wfCb) { |
| 29 | + // it's not a typo, camelCase is not applied through native$ |
| 30 | + seneca.act({role: 'cd-users', cmd: 'load', id: faulty.userid}, function (err, user) { |
| 31 | + if (err) eCb(err); |
| 32 | + wfCb(null, profile, user); |
| 33 | + }); |
| 34 | + }, |
| 35 | + // re-Save profiles |
| 36 | + function (profile, user, wfCb) { |
| 37 | + if (['attendee-o13', 'attendee-u13'].indexOf(profile.userType) > -1) { |
| 38 | + seneca.act({role: plugin, cmd: 'update-youth-profile', profile: profile, user: user}, wfCb); |
| 39 | + } else { |
| 40 | + seneca.act({role: plugin, cmd: 'create', profile: profile, user: user}, wfCb); |
| 41 | + } |
| 42 | + } |
| 43 | + ], eCb); |
| 44 | + }, finishUp); |
| 45 | + }); |
| 46 | + }); |
| 47 | +}; |
0 commit comments