diff --git a/doc/GroupFeature.pdf b/doc/GroupFeature.pdf index dbcba951..0cdd0f02 100644 Binary files a/doc/GroupFeature.pdf and b/doc/GroupFeature.pdf differ diff --git a/lib/db/api.js b/lib/db/api.js index c1b07beb..8281c7ca 100644 --- a/lib/db/api.js +++ b/lib/db/api.js @@ -38,6 +38,79 @@ dbapi.unlockBookingObjects = function() { dbapi.createBootStrap = function(env) { const now = Date.now() + function updateUsersForMigration(group) { + return dbapi.getUsers().then(function(users) { + return Promise.map(users, function(user) { + return db.run(r.table('users').get(user.email).update({ + privilege: user.email !== group.owner.email ? apiutil.USER : apiutil.ADMIN + , groups: { + subscribed: [] + , lock: false + , quotas: { + allocated: { + number: group.envUserGroupsNumber + , duration: group.envUserGroupsDuration + } + , consumed: { + number: 0 + , duration: 0 + } + , defaultGroupsNumber: user.email !== group.owner.email ? + 0 : + group.envUserGroupsNumber + , defaultGroupsDuration: user.email !== group.owner.email ? + 0 : + group.envUserGroupsDuration + , defaultGroupsRepetitions: user.email !== group.owner.email ? + 0 : + group.envUserGroupsRepetitions + , repetitions: group.envUserGroupsRepetitions + } + } + })) + .then(function(stats) { + if (stats.replaced) { + return dbapi.addGroupUser(group.id, user.email) + } + return stats + }) + }) + }) + } + + function getDevices() { + return db.run(r.table('devices')) + .then(function(cursor) { + return cursor.toArray() + }) + } + + function updateDevicesForMigration(group) { + return getDevices().then(function(devices) { + return Promise.map(devices, function(device) { + return db.run(r.table('devices').get(device.serial).update({ + group: { + id: group.id + , name: group.name + , lifeTime: group.dates[0] + , owner: group.owner + , origin: group.id + , class: group.class + , repetitions: group.repetitions + , originName: group.name + , lock: false + }} + )) + .then(function(stats) { + if (stats.replaced) { + return dbapi.addOriginGroupDevice(group, device.serial) + } + return stats + }) + }) + }) + } + return dbapi.createGroup({ name: env.STF_ROOT_GROUP_NAME , owner: { @@ -65,6 +138,12 @@ dbapi.createBootStrap = function(env) { , email: group.owner.email , ip: '127.0.0.1' }) + .then(function() { + return updateUsersForMigration(group) + }) + .then(function() { + return updateDevicesForMigration(group) + }) .then(function() { return dbapi.reserveUserGroupInstance(group.owner.email) })