Skip to content
This repository was archived by the owner on Dec 14, 2023. It is now read-only.

Commit 7fbf1d6

Browse files
committed
Backfill script for #1202
1 parent d711d69 commit 7fbf1d6

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
};

profiles.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ module.exports = function (options) {
5151
seneca.add({role: plugin, cmd: 'ninjas_for_user'}, require('./lib/profiles/load-children-for-user'));
5252
// Perms
5353
seneca.add({role: plugin, cmd: 'is_own_profile'}, require('./lib/profiles/is-own-profile'));
54+
// one-shot
55+
seneca.add({role: plugin, cmd: '1202_syncSysUserName'}, require('./lib/fixes/1202-sync-sys-user-profile'));
5456

5557
function cmd_search (args, done) {
5658
if (!args.query) {

0 commit comments

Comments
 (0)