|
|
const without = require('lodash/without'); |
|
|
|
|
|
|
|
|
function _toConsumableArray(arr) { |
|
|
if (Array.isArray(arr)) { |
|
|
for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { |
|
|
arr2[i] = arr[i]; |
|
|
} |
|
|
return arr2; |
|
|
} else { |
|
|
return Array.from(arr); |
|
|
} |
|
|
} |
|
|
|
|
|
exports.up = function(r, conn) { |
|
|
|
|
|
return ( |
|
|
r |
|
|
.table('usersSettings') |
|
|
.filter({ |
|
|
notifications: { |
|
|
types: { |
|
|
newMessageInThreads: { |
|
|
email: false, |
|
|
}, |
|
|
}, |
|
|
}, |
|
|
}) |
|
|
.map(rec => rec('userId')) |
|
|
.distinct() |
|
|
.run(conn) |
|
|
.then(cursor => cursor.toArray()) |
|
|
.then(optedOut => { |
|
|
return Promise.all([ |
|
|
optedOut, |
|
|
r |
|
|
.table('usersSettings') |
|
|
.filter({ |
|
|
notifications: { |
|
|
types: { |
|
|
newMessageInThreads: { |
|
|
email: true, |
|
|
}, |
|
|
}, |
|
|
}, |
|
|
}) |
|
|
.map(rec => rec('userId')) |
|
|
.distinct() |
|
|
.run(conn) |
|
|
.then(cursor => cursor.toArray()), |
|
|
]); |
|
|
}) |
|
|
.then(([optedOut, optedIn]) => { |
|
|
return [ |
|
|
optedOut, |
|
|
|
|
|
without.apply( |
|
|
undefined, |
|
|
[optedIn].concat(_toConsumableArray(optedOut)) |
|
|
), |
|
|
]; |
|
|
}) |
|
|
|
|
|
.then(([optedOut, optedIn]) => { |
|
|
return Promise.all([ |
|
|
optedOut, |
|
|
optedIn, |
|
|
r.table('usersSettings').delete().run(conn), |
|
|
]); |
|
|
}) |
|
|
.then(([optedOut, optedIn]) => { |
|
|
|
|
|
const optedOutInsertions = optedOut.map(userId => |
|
|
r |
|
|
.table('usersSettings') |
|
|
.insert({ |
|
|
userId, |
|
|
notifications: { |
|
|
types: { |
|
|
newMessageInThreads: { |
|
|
email: false, |
|
|
}, |
|
|
newThreadCreated: { |
|
|
email: true, |
|
|
}, |
|
|
}, |
|
|
}, |
|
|
}) |
|
|
.run(conn) |
|
|
); |
|
|
|
|
|
const optedInInsertions = optedIn.map(userId => |
|
|
r |
|
|
.table('usersSettings') |
|
|
.insert({ |
|
|
userId, |
|
|
notifications: { |
|
|
types: { |
|
|
newMessageInThreads: { |
|
|
email: true, |
|
|
}, |
|
|
newThreadCreated: { |
|
|
email: true, |
|
|
}, |
|
|
}, |
|
|
}, |
|
|
}) |
|
|
.run(conn) |
|
|
); |
|
|
return Promise.all( |
|
|
[].concat( |
|
|
_toConsumableArray(optedOutInsertions), |
|
|
_toConsumableArray(optedInInsertions) |
|
|
) |
|
|
); |
|
|
}) |
|
|
); |
|
|
}; |
|
|
|
|
|
exports.down = function(r, conn) { |
|
|
return Promise.resolve(); |
|
|
}; |
|
|
|