How to use groupApiWrapper method in devicefarmer-stf

Best JavaScript code snippet using devicefarmer-stf

groups.js

Source:groups.js Github

copy

Full Screen

...9const uuid = require('uuid')10const Promise = require('bluebird')11const usersapi = require('./users')12/* ---------------------------------- PRIVATE FUNCTIONS --------------------------------- */13function groupApiWrapper(email, fn, req, res) {14 dbapi.loadUser(email).then(function(user) {15 if (!user) {16 apiutil.respond(res, 404, 'Not Found (user)')17 }18 else {19 req.user = user20 fn(req, res)21 }22 })23 .catch(function(err) {24 apiutil.internalError(res, 'Failed to wrap "%s": ', fn.name, err.stack)25 })26}27function getDevice(req, serial) {28 return dbapi.loadDeviceBySerial(serial).then(function(device) {29 if (!device) {30 throw new Error(`Device not found: ${serial}`)31 }32 return apiutil.filterDevice(req, device)33 })34}35function checkConflicts(id, devices, dates) {36 function computeConflicts(conflicts, liteGroup, otherGroup) {37 if (otherGroup.id !== liteGroup.id) {38 const devices = _.intersection(liteGroup.devices, otherGroup.devices)39 if (devices.length) {40 for (let liteGroupDate of liteGroup.dates) {41 for (let otherGroupDate of otherGroup.dates) {42 if (liteGroupDate.start < otherGroupDate.stop &&43 liteGroupDate.stop > otherGroupDate.start) {44 conflicts.push({45 devices: devices46 , date: {47 start: new Date(48 Math.max(liteGroupDate.start.getTime()49 , otherGroupDate.start.getTime()))50 , stop: new Date(51 Math.min(liteGroupDate.stop.getTime()52 , otherGroupDate.stop.getTime()))53 }54 , group: otherGroup.name55 , owner: otherGroup.owner56 })57 }58 }59 }60 }61 }62 }63 return dbapi.getTransientGroups().then(function(groups) {64 const conflicts = []65 groups.forEach(function(otherGroup) {66 computeConflicts(67 conflicts68 , {id: id, devices: devices, dates: dates}69 , otherGroup)70 })71 return conflicts72 })73}74function checkSchedule(res, oldGroup, _class, email, repetitions, privilege, start, stop) {75 if (oldGroup && oldGroup.devices.length &&76 (apiutil.isOriginGroup(_class) && !apiutil.isOriginGroup(oldGroup.class) ||77 apiutil.isOriginGroup(oldGroup.class) && !apiutil.isOriginGroup(_class))) {78 return Promise.resolve(apiutil.respond(res, 403,79 'Forbidden (unauthorized class while device list is not empty)'))80 }81 if (apiutil.isAdminGroup(_class) && privilege === apiutil.USER) {82 return Promise.resolve(apiutil.respond(res, 403, 'Forbidden (unauthorized class)'))83 }84 if (isNaN(start.getTime())) {85 return Promise.resolve(apiutil.respond(res, 400, 'Bad Request (Invalid startTime format)'))86 }87 if (isNaN(stop.getTime())) {88 return Promise.resolve(apiutil.respond(res, 400, 'Bad Request (Invalid stopTime format)'))89 }90 if (start >= stop) {91 return Promise.resolve(92 apiutil.respond(res, 400, 'Bad Request (Invalid life time: startTime >= stopTime)'))93 }94 if ((stop - start) > apiutil.CLASS_DURATION[_class]) {95 return Promise.resolve(apiutil.respond(res, 400,96 'Bad Request (Invalid Life time & class combination: life time > class duration)'97 ))98 }99 switch(_class) {100 case apiutil.BOOKABLE:101 case apiutil.STANDARD:102 case apiutil.ONCE:103 if (repetitions !== 0) {104 return Promise.resolve(105 apiutil.respond(res, 400, 'Bad Request (Invalid class & repetitions combination)'))106 }107 break108 default:109 if (repetitions === 0) {110 return Promise.resolve(111 apiutil.respond(res, 400, 'Bad Request (Invalid class & repetitions combination)'))112 }113 break114 }115 return dbapi.loadUser(email).then(function(owner) {116 if (repetitions > owner.groups.quotas.repetitions) {117 return apiutil.respond(res, 400, 'Bad Request (Invalid repetitions value)')118 }119 return true120 })121}122/* ---------------------------------- PUBLIC FUNCTIONS ------------------------------------- */123function addGroupDevices(req, res) {124 const id = req.swagger.params.id.value125 const serials = apiutil.getBodyParameter(req.body, 'serials')126 const target = apiutil.getQueryParameter(req.swagger.params.redirected) ? 'device' : 'devices'127 const lock = {}128 let email = null129 function addGroupDevice(group, serial) {130 const lock = {}131 return dbapi.lockBookableDevice(req.user.groups.subscribed, serial).then(function(stats) {132 if (!stats.replaced) {133 return apiutil.lightComputeStats(res, stats)134 }135 lock.device = stats.changes[0].new_val136 return dbapi.lockGroup(lock.device.group.origin).then(function(stats) {137 if (!stats.replaced) {138 return apiutil.lightComputeStats(res, stats)139 }140 lock.group = {id: lock.device.group.origin}141 return checkConflicts(id, [serial], group.dates).then(function(conflicts) {142 return conflicts.length ?143 Promise.reject(conflicts) :144 dbapi.addGroupDevices(group, [serial])145 })146 })147 .finally(function() {148 lockutil.unlockGroup(lock)149 })150 })151 .finally(function() {152 lockutil.unlockDevice(lock)153 })154 }155 function _addGroupDevices(lockedGroup, serials) {156 let results = []157 let group = lockedGroup158 return Promise.each(serials, function(serial) {159 return addGroupDevice(group, serial).then(function(result) {160 results.push(result)161 if (result.hasOwnProperty('id')) {162 group = result163 }164 })165 })166 .then(function() {167 results = _.without(results, 'unchanged')168 if (!results.length) {169 apiutil.respond(res, 200, `Unchanged (group ${target})`, {group: {}})170 }171 else {172 results = _.without(results, 'not found')173 if (!results.length) {174 apiutil.respond(res, 404, `Not Found (group ${target})`)175 }176 else {177 apiutil.respond(res, 200, `Added (group ${target})`178 , {group: apiutil.publishGroup(results[results.length - 1])})179 }180 }181 })182 .catch(function(err) {183 if (err === 'quota is reached') {184 apiutil.respond(res, 403, 'Forbidden (groups duration quota is reached)')185 }186 else if (Array.isArray(err)) {187 apiutil.respond(res, 409, 'Conflicts Information', {conflicts: err})188 }189 else if (err !== 'busy') {190 throw err191 }192 })193 }194 lockutil.lockGroup(req, res, lock).then(function(lockingSuccessed) {195 if (lockingSuccessed) {196 let group = lock.group197 if (req.user.privilege === apiutil.ADMIN && req.user.email !== group.owner.email) {198 email = group.owner.email199 return false200 }201 if (apiutil.isOriginGroup(group.class)) {202 return apiutil.respond(res, 400, 'Bad Request (use admin API for bookable/standard groups)')203 }204 return (function() {205 if (typeof serials === 'undefined') {206 return dbapi.loadBookableDevices(req.user.groups.subscribed).then(function(devices) {207 const serials = []208 devices.forEach(function(device) {209 if (group.devices.indexOf(device.serial) < 0) {210 serials.push(device.serial)211 }212 })213 return _addGroupDevices(group, serials)214 })215 }216 else {217 return _addGroupDevices(218 group219 , _.difference(220 _.without(serials.split(','), '')221 , group.devices)222 )223 }224 })()225 }226 return false227 })228 .catch(function(err) {229 apiutil.internalError(res, `Failed to add group ${target}: `, err.stack)230 })231 .finally(function() {232 lockutil.unlockGroup(lock)233 if (email) {234 groupApiWrapper(email, addGroupDevices, req, res)235 }236 })237}238function addGroupDevice(req, res) {239 apiutil.redirectApiWrapper('serial', addGroupDevices, req, res)240}241function removeGroupDevices(req, res) {242 const serials = apiutil.getBodyParameter(req.body, 'serials')243 const target = apiutil.getQueryParameter(req.swagger.params.redirected) ? 'device' : 'devices'244 const lock = {}245 lockutil.lockGroup(req, res, lock).then(function(lockingSuccessed) {246 if (lockingSuccessed) {247 const group = lock.group248 if (apiutil.isOriginGroup(group.class)) {249 return apiutil.respond(res, 400, 'Bad Request (use admin API for bookable/standard groups)')250 }251 let serialsToRemove = group.devices252 if (typeof serials !== 'undefined') {253 serialsToRemove = _.without(serials.split(','), '')254 }255 if (!serialsToRemove.length) {256 return apiutil.respond(res, 200, `Unchanged (group ${target})`, {group: {}})257 }258 serialsToRemove = _.intersection(serialsToRemove, group.devices)259 if (!serialsToRemove.length) {260 return apiutil.respond(res, 404, `Not Found (group ${target})`)261 }262 return dbapi.removeGroupDevices(group, serialsToRemove).then(function(group) {263 apiutil.respond(res, 200, `Removed (group ${target})`, {group: apiutil.publishGroup(group)})264 })265 }266 return false267 })268 .catch(function(err) {269 apiutil.internalError(res, `Failed to remove group ${target}: `, err.stack)270 })271 .finally(function() {272 lockutil.unlockGroup(lock)273 })274}275function removeGroupDevice(req, res) {276 apiutil.redirectApiWrapper('serial', removeGroupDevices, req, res)277}278function getGroupDevice(req, res) {279 const id = req.swagger.params.id.value280 const serial = req.swagger.params.serial.value281 dbapi.getUserGroup(req.user.email, id).then(function(group) {282 if (!group) {283 apiutil.respond(res, 404, 'Not Found (group)')284 }285 else if (group.devices.indexOf(serial) < 0) {286 apiutil.respond(res, 404, 'Not Found (device)')287 }288 else {289 getDevice(req, serial).then(function(device) {290 apiutil.respond(res, 200, 'Device Information', {device: device})291 })292 }293 })294 .catch(function(err) {295 apiutil.internalError(res, 'Failed to get group device: ', err.stack)296 })297}298function getGroupUser(req, res) {299 const id = req.swagger.params.id.value300 const email = req.swagger.params.email.value301 dbapi.getUserGroup(req.user.email, id).then(function(group) {302 if (!group) {303 apiutil.respond(res, 404, 'Not Found (group)')304 }305 else if (group.users.indexOf(email) < 0) {306 apiutil.respond(res, 404, 'Not Found (user)')307 }308 else {309 usersapi.getUserByEmail(req, res)310 }311 })312 .catch(function(err) {313 apiutil.internalError(res, 'Failed to get group user: ', err.stack)314 })315}316function getGroupUsers(req, res) {317 const id = req.swagger.params.id.value318 dbapi.getUserGroup(req.user.email, id).then(function(group) {319 if (!group) {320 apiutil.respond(res, 404, 'Not Found (group)')321 }322 else {323 Promise.map(group.users, function(email) {324 return usersapi.getUserInfo(req, email).then(function(user) {325 return user || Promise.reject(`Group user not found: ${email}`)326 })327 })328 .then(function(users) {329 apiutil.respond(res, 200, 'Users Information', {users: users})330 })331 }332 })333 .catch(function(err) {334 apiutil.internalError(res, 'Failed to get group users: ', err.stack)335 })336}337function removeGroupUsers(req, res) {338 const id = req.swagger.params.id.value339 const emails = apiutil.getBodyParameter(req.body, 'emails')340 const target = apiutil.getQueryParameter(req.swagger.params.redirected) ? 'user' : 'users'341 const lock = {}342 function removeGroupUser(email, group, rootGroup) {343 if (group.users.indexOf(email) < 0) {344 return Promise.resolve('not found')345 }346 if (email === rootGroup.owner.email || email === group.owner.email) {347 return Promise.resolve('forbidden')348 }349 const lock = {}350 return dbapi.lockUser(email).then(function(stats) {351 if (!stats.replaced) {352 return apiutil.lightComputeStats(res, stats)353 }354 lock.user = stats.changes[0].new_val355 return dbapi.isRemoveGroupUserAllowed(email, group)356 .then(function(isAllowed) {357 return isAllowed ? dbapi.removeGroupUser(id, email) : 'forbidden'358 })359 })360 .finally(function() {361 lockutil.unlockUser(lock)362 })363 }364 lockutil.lockGroup(req, res, lock).then(function(lockingSuccessed) {365 if (lockingSuccessed) {366 const group = lock.group367 return dbapi.getRootGroup().then(function(rootGroup) {368 let emailsToRemove = group.users369 let results = []370 if (typeof emails !== 'undefined') {371 emailsToRemove = _.without(emails.split(','), '')372 }373 return Promise.each(emailsToRemove, function(email) {374 return removeGroupUser(email, group, rootGroup).then(function(result) {375 results.push(result)376 })377 })378 .then(function() {379 if (!results.length) {380 return apiutil.respond(res, 200, `Unchanged (group ${target})`, {group: {}})381 }382 results = _.without(results, 'not found')383 if (!results.length) {384 return apiutil.respond(res, 404, `Not Found (group ${target})`)385 }386 if (!_.without(results, 'forbidden').length) {387 return apiutil.respond(res, 403, `Forbidden (group ${target})`)388 }389 return dbapi.getGroup(id).then(function(group) {390 apiutil.respond(res, 200, `Removed (group ${target})`, {391 group: apiutil.publishGroup(group)})392 })393 })394 })395 .catch(function(err) {396 if (err !== 'busy') {397 throw err398 }399 })400 }401 return false402 })403 .catch(function(err) {404 apiutil.internalError(res, `Failed to remove group ${target}: `, err.stack)405 })406 .finally(function() {407 lockutil.unlockGroup(lock)408 })409}410function removeGroupUser(req, res) {411 apiutil.redirectApiWrapper('email', removeGroupUsers, req, res)412}413function addGroupUsers(req, res) {414 const id = req.swagger.params.id.value415 const emails = apiutil.getBodyParameter(req.body, 'emails')416 const target = apiutil.getQueryParameter(req.swagger.params.redirected) ? 'user' : 'users'417 const lock = {}418 function addGroupUser(email) {419 const lock = {}420 return dbapi.lockUser(email).then(function(stats) {421 if (!stats.replaced) {422 return apiutil.lightComputeStats(res, stats)423 }424 lock.user = stats.changes[0].new_val425 return dbapi.addGroupUser(id, email)426 })427 .finally(function() {428 lockutil.unlockUser(lock)429 })430 }431 function _addGroupUsers(emails) {432 let results = []433 return Promise.each(emails, function(email) {434 return addGroupUser(email).then(function(result) {435 results.push(result)436 })437 })438 .then(function() {439 results = _.without(results, 'unchanged')440 if (!results.length) {441 return apiutil.respond(res, 200, `Unchanged (group ${target})`, {group: {}})442 }443 if (!_.without(results, 'not found').length) {444 return apiutil.respond(res, 404, `Not Found (group ${target})`)445 }446 return dbapi.getGroup(id).then(function(group) {447 apiutil.respond(res, 200, `Added (group ${target})`, {group: apiutil.publishGroup(group)})448 })449 })450 .catch(function(err) {451 if (err !== 'busy') {452 throw err453 }454 })455 }456 lockutil.lockGroup(req, res, lock).then(function(lockingSuccessed) {457 if (!lockingSuccessed) {458 return false459 }460 const group = lock.group461 return (function() {462 if (typeof emails === 'undefined') {463 return dbapi.getUsers().then(function(users) {464 const emails = []465 users.forEach(function(user) {466 if (group.users.indexOf(user.email) < 0) {467 emails.push(user.email)468 }469 })470 return _addGroupUsers(emails)471 })472 }473 else {474 return _addGroupUsers(475 _.difference(476 _.without(emails.split(','), '')477 , group.users)478 )479 }480 })()481 })482 .catch(function(err) {483 apiutil.internalError(res, `Failed to add group ${target}: `, err.stack)484 })485 .finally(function() {486 lockutil.unlockGroup(lock)487 })488}489function addGroupUser(req, res) {490 apiutil.redirectApiWrapper('email', addGroupUsers, req, res)491}492function getGroup(req, res) {493 const id = req.swagger.params.id.value494 const fields = req.swagger.params.fields.value495 dbapi.getUserGroup(req.user.email, id).then(function(group) {496 if (!group) {497 apiutil.respond(res, 404, 'Not Found (group)')498 return499 }500 let publishedGroup = apiutil.publishGroup(group)501 if (fields) {502 publishedGroup = _.pick(publishedGroup, fields.split(','))503 }504 apiutil.respond(res, 200, 'Group Information', {group: publishedGroup})505 })506 .catch(function(err) {507 apiutil.internalError(res, 'Failed to get group: ', err.stack)508 })509}510function getGroups(req, res) {511 const fields = req.swagger.params.fields.value512 const owner = req.swagger.params.owner.value513 let getGenericGroups514 switch(owner) {515 case true:516 getGenericGroups = dbapi.getOwnerGroups517 break518 case false:519 getGenericGroups = dbapi.getOnlyUserGroups520 break521 default:522 getGenericGroups = dbapi.getUserGroups523 }524 getGenericGroups(req.user.email).then(function(groups) {525 return apiutil.respond(res, 200, 'Groups Information', {526 groups: groups.map(function(group) {527 if (fields) {528 return _.pick(apiutil.publishGroup(group), fields.split(','))529 }530 return apiutil.publishGroup(group)531 })532 })533 })534 .catch(function(err) {535 apiutil.internalError(res, 'Failed to get groups: ', err.stack)536 })537}538function createGroup(req, res) {539 const _class = typeof req.body.class === 'undefined' ? apiutil.ONCE : req.body.class540 const repetitions =541 apiutil.isOriginGroup(_class) || typeof req.body.repetitions === 'undefined' ?542 0 :543 req.body.repetitions544 const now = Date.now()545 const start =546 apiutil.isOriginGroup(_class) ?547 new Date(now) :548 new Date(req.body.startTime || now)549 const stop =550 apiutil.isOriginGroup(_class) ?551 new Date(now + apiutil.ONE_YEAR) :552 new Date(req.body.stopTime || now + apiutil.ONE_HOUR)553 checkSchedule(res, null, _class, req.user.email, repetitions, req.user.privilege,554 start, stop).then(function(checkingSuccessed) {555 if (!checkingSuccessed) {556 return557 }558 const name =559 typeof req.body.name === 'undefined' ?560 'New_' + util.format('%s', uuid.v4()).replace(/-/g, '') :561 req.body.name562 const state =563 apiutil.isOriginGroup(_class) || typeof req.body.state === 'undefined' ?564 apiutil.READY :565 req.body.state566 const isActive = state === apiutil.READY && apiutil.isOriginGroup(_class)567 const duration = 0568 const dates = apiutil.computeGroupDates({start: start, stop: stop}, _class, repetitions)569 dbapi.createUserGroup({570 name: name571 , owner: {572 email: req.user.email573 , name: req.user.name574 }575 , privilege: req.user.privilege576 , class: _class577 , repetitions: repetitions578 , isActive: isActive579 , dates: dates580 , duration: duration581 , state: state582 })583 .then(function(group) {584 if (group) {585 apiutil.respond(res, 201, 'Created', {group: apiutil.publishGroup(group)})586 }587 else {588 apiutil.respond(res, 403, 'Forbidden (groups number quota is reached)')589 }590 })591 .catch(function(err) {592 apiutil.internalError(res, 'Failed to create group: ', err.stack)593 })594 })595}596function deleteGroups(req, res) {597 const ids = apiutil.getBodyParameter(req.body, 'ids')598 const target = apiutil.getQueryParameter(req.swagger.params.redirected) ? 'group' : 'groups'599 function removeGroup(id) {600 const lock = {}601 return dbapi.lockGroupByOwner(req.user.email, id).then(function(stats) {602 if (!stats.replaced) {603 return apiutil.lightComputeStats(res, stats)604 }605 const group = lock.group = stats.changes[0].new_val606 if (group.privilege === apiutil.ROOT) {607 return 'forbidden'608 }609 if (group.class === apiutil.BOOKABLE) {610 return Promise.each(group.devices, function(serial) {611 return dbapi.isDeviceBooked(serial)612 .then(function(isBooked) {613 return isBooked ? Promise.reject('booked') : true614 })615 })616 .then(function() {617 return dbapi.deleteUserGroup(id)618 })619 .catch(function(err) {620 if (err !== 'booked') {621 throw err622 }623 return 'forbidden'624 })625 }626 else {627 return dbapi.deleteUserGroup(id)628 }629 })630 .finally(function() {631 lockutil.unlockGroup(lock)632 })633 }634 function removeGroups(ids) {635 let results = []636 return Promise.each(ids, function(id) {637 return removeGroup(id).then(function(result) {638 results.push(result)639 })640 })641 .then(function() {642 if (!results.length) {643 return apiutil.respond(res, 200, `Unchanged (${target})`)644 }645 results = _.without(results, 'not found')646 if (!results.length) {647 return apiutil.respond(res, 404, `Not Found (${target})`)648 }649 results = _.without(results, 'forbidden')650 if (!results.length) {651 return apiutil.respond(res, 403, `Forbidden (${target})`)652 }653 return apiutil.respond(res, 200, `Deleted (${target})`)654 })655 .catch(function(err) {656 if (err !== 'busy') {657 throw err658 }659 })660 }661 (function() {662 if (typeof ids === 'undefined') {663 return dbapi.getOwnerGroups(req.user.email).then(function(groups) {664 const ids = []665 groups.forEach(function(group) {666 if (group.privilege !== apiutil.ROOT) {667 ids.push(group.id)668 }669 })670 return removeGroups(ids)671 })672 }673 else {674 return removeGroups(_.without(ids.split(','), ''))675 }676 })()677 .catch(function(err) {678 apiutil.internalError(res, `Failed to delete ${target}: `, err.stack)679 })680}681function deleteGroup(req, res) {682 apiutil.redirectApiWrapper('id', deleteGroups, req, res)683}684function updateGroup(req, res) {685 const id = req.swagger.params.id.value686 const lock = {}687 function updateUserGroup(group, data) {688 return dbapi.updateUserGroup(group, data)689 .then(function(group) {690 if (group) {691 apiutil.respond(res, 200, 'Updated (group)', {group: apiutil.publishGroup(group)})692 }693 else {694 apiutil.respond(res, 403, 'Forbidden (groups duration quota is reached)')695 }696 })697 }698 lockutil.lockGroup(req, res, lock).then(function(lockingSuccessed) {699 if (!lockingSuccessed) {700 return false701 }702 const group = lock.group703 const _class = typeof req.body.class === 'undefined' ? group.class : req.body.class704 const name = typeof req.body.name === 'undefined' ? group.name : req.body.name705 const repetitions =706 typeof req.body.repetitions === 'undefined' ?707 group.repetitions :708 req.body.repetitions709 const start = new Date(req.body.startTime || group.dates[0].start)710 const stop = new Date(req.body.stopTime || group.dates[0].stop)711 let state, isActive712 if (apiutil.isOriginGroup(_class)) {713 state = apiutil.READY714 isActive = true715 }716 else {717 state = typeof req.body.state === 'undefined' ? apiutil.PENDING : req.body.state718 isActive = false719 }720 if (group.state === apiutil.READY && state === apiutil.PENDING) {721 return apiutil.respond(res, 403, 'Forbidden (group is ready)')722 }723 return checkSchedule(res, group, _class, group.owner.email, repetitions, group.privilege,724 start, stop).then(function(checkingSuccessed) {725 if (!checkingSuccessed) {726 return false727 }728 if (name === group.name &&729 start.toISOString() === group.dates[0].start.toISOString() &&730 stop.toISOString() === group.dates[0].stop.toISOString() &&731 state === group.state &&732 _class === group.class &&733 repetitions === group.repetitions) {734 return apiutil.respond(res, 200, 'Unchanged (group)', {group: {}})735 }736 const duration = group.devices.length * (stop - start) * (repetitions + 1)737 const dates = apiutil.computeGroupDates({start: start, stop: stop}, _class, repetitions)738 if (start < group.dates[0].start ||739 stop > group.dates[0].stop ||740 repetitions > group.repetitions ||741 _class !== group.class) {742 return checkConflicts(id, group.devices, dates)743 .then(function(conflicts) {744 if (!conflicts.length) {745 return updateUserGroup(group, {746 name: name747 , state: state748 , class: _class749 , isActive: isActive750 , repetitions: repetitions751 , dates: dates752 , duration: duration753 })754 }755 return apiutil.respond(res, 409, 'Conflicts Information', {conflicts: conflicts})756 })757 }758 return updateUserGroup(group, {759 name: name760 , state: state761 , class: _class762 , isActive: isActive763 , repetitions: repetitions764 , dates: dates765 , duration: duration766 })767 })768 })769 .catch(function(err) {770 apiutil.internalError(res, 'Failed to update group: ', err.stack)771 })772 .finally(function() {773 lockutil.unlockGroup(lock)774 })775}776function getGroupDevices(req, res) {777 const id = req.swagger.params.id.value778 const bookable = req.swagger.params.bookable.value779 dbapi.getUserGroup(req.user.email, id).then(function(group) {780 if (!group) {781 apiutil.respond(res, 404, 'Not Found (group)')782 return783 }784 if (bookable) {785 if (apiutil.isOriginGroup(group.class)) {786 apiutil.respond(res, 400, 'Bad Request (group is not transient)')787 return788 }789 if (req.user.privilege === apiutil.ADMIN && req.user.email !== group.owner.email) {790 groupApiWrapper(group.owner.email, getGroupDevices, req, res)791 return792 }793 dbapi.loadBookableDevices(req.user.groups.subscribed).then(function(devices) {794 Promise.map(devices, function(device) {795 return device.serial796 })797 .then(function(serials) {798 return checkConflicts(group.id, serials, group.dates)799 .then(function(conflicts) {800 let bookableSerials = serials801 conflicts.forEach(function(conflict) {802 bookableSerials = _.difference(bookableSerials, conflict.devices)803 })804 return bookableSerials...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1var stf = require('devicefarmer-stf-client');2var groupApi = client.groupApiWrapper();3groupApi.list().then(function(groups) {4 console.log(groups);5});6groupApi.create('testGroup').then(function(group) {7 console.log(group);8});9groupApi.get('testGroup').then(function(group) {10 console.log(group);11});12groupApi.update('testGroup', {name: 'testGroup1'}).then(function(group) {13 console.log(group);14});15groupApi.delete('testGroup1').then(function(group) {16 console.log(group);17});18groupApi.addDevice('testGroup', '1234567890').then(function(group) {19 console.log(group);20});21groupApi.removeDevice('testGroup', '1234567890').then(function(group) {22 console.log(group);23});24groupApi.addUser('testGroup', 'testUser').then(function(group) {25 console.log(group);26});27groupApi.removeUser('testGroup', 'testUser').then(function(group) {28 console.log(group);29});30groupApi.addOwner('testGroup', 'testOwner').then(function(group) {31 console.log(group);32});33groupApi.removeOwner('testGroup', 'testOwner').then(function(group) {34 console.log(group);35});36groupApi.getDevices('testGroup').then(function(devices) {37 console.log(devices);38});39groupApi.getUsers('testGroup').then(function(users) {40 console.log(users);41});42groupApi.getOwners('testGroup').then(function(owners) {43 console.log(owners);44});45groupApi.getPermissions('testGroup').then(function(permissions) {46 console.log(permissions);47});

Full Screen

Using AI Code Generation

copy

Full Screen

1var stf = require('devicefarmer-stf-client');2var groupApi = client.groupApiWrapper();3groupApi.getGroupList().then(function(groups) {4 console.log("Groups: " + groups);5});6var stf = require('devicefarmer-stf-client');7var groupApi = client.groupApiWrapper();8groupApi.getGroupList().then(function(groups) {9 console.log("Groups: " + groups);10});11var stf = require('devicefarmer-stf-client');12var groupApi = client.groupApiWrapper();13groupApi.addDeviceToGroup('deviceid', 'groupid').then(function(result) {14 console.log("Result: " + result);15});16var stf = require('devicefarmer-stf-client');17var groupApi = client.groupApiWrapper();18groupApi.removeDeviceFromGroup('deviceid', 'groupid').then(function(result) {19 console.log("Result: " + result);20});21var stf = require('devicefarmer-stf-client');22var groupApi = client.groupApiWrapper();23groupApi.addDevicesToGroup(['deviceid1', 'deviceid2'], 'groupid').then(function(result) {24 console.log("Result: " + result);25});

Full Screen

Using AI Code Generation

copy

Full Screen

1var stf = require('devicefarmer-stf-client');2var options = {3};4var groupApiWrapper = stf.groupApiWrapper(options);5groupApiWrapper.getGroupList(function(err, data) {6 if (err) {7 console.log(err);8 } else {9 console.log(data);10 }11});12[ { id: 1,

Full Screen

Using AI Code Generation

copy

Full Screen

1var groupApiWrapper = require('devicefarmer-stf-client').groupApiWrapper;2groupApi.getGroupDevices('group-name', function (err, res) {3 console.log(res);4});5var deviceApiWrapper = require('devicefarmer-stf-client').deviceApiWrapper;6deviceApi.getDevice('device-id', function (err, res) {7 console.log(res);8});9var deviceApiWrapper = require('devicefarmer-stf-client').deviceApiWrapper;10deviceApi.getDevice('device-id', function (err, res) {11 console.log(res);12});13var deviceApiWrapper = require('devicefarmer-stf-client').deviceApiWrapper;14deviceApi.getDevice('device-id', function (err, res) {15 console.log(res);16});17var deviceApiWrapper = require('devicefarmer-stf-client').deviceApiWrapper;18deviceApi.getDevice('device-id', function (err, res) {19 console.log(res);20});21var deviceApiWrapper = require('devicefarmer-stf-client').deviceApiWrapper;22deviceApi.getDevice('device-id', function (err, res) {23 console.log(res);24});25var deviceApiWrapper = require('devicefarmer-stf-client').deviceApiWrapper;26deviceApi.getDevice('device-id

Full Screen

Using AI Code Generation

copy

Full Screen

1var stf = require('devicefarmer-stf');2var device = client.getDevice('emulator-5554');3device.groupApiWrapper('group','default').then(function(data){4 console.log(data);5})6var stf = require('devicefarmer-stf');7var device = client.getDevice('emulator-5554');8device.groupApiWrapper('ungroup').then(function(data){9 console.log(data);10})11var stf = require('devicefarmer-stf');12var device = client.getDevice('emulator-5554');13device.groupApiWrapper('list').then(function(data){14 console.log(data);15})16var stf = require('devicefarmer-stf');17var device = client.getDevice('emulator-5554');18device.groupApiWrapper('remove','default').then(function(data){19 console.log(data);20})21var stf = require('devicefarmer-stf');22var device = client.getDevice('emulator-5554');23device.groupApiWrapper('rename','default','newName').then(function(data){24 console.log(data);25})26var stf = require('devicefarmer-stf');27var device = client.getDevice('emulator-5554');28device.groupApiWrapper('add','default').then(function(data){29 console.log(data);30})31var stf = require('devicefarmer-stf');32var device = client.getDevice('emulator-5554');33device.groupApiWrapper('add','default','emulator-5554

Full Screen

Using AI Code Generation

copy

Full Screen

1var stf = require('stf-client');2 groupApi.list().then(function(groups) {3 console.log(groups);4 });5});6var stf = require('devicefarmer-stf-client');7 groupApi.list().then(function(groups) {8 console.log(groups);9 });10});11var stf = require('devicefarmer-stf-client');12 groupApi.list().then(function(groups) {13 console.log(groups);14 });15});16var stf = require('devicefarmer-stf-client');17 groupApi.list().then(function(groups) {18 console.log(groups);19 });20});21var stf = require('devicefarmer-stf-client');22 groupApi.list().then(function(groups) {23 console.log(groups);24 });25});26var stf = require('devicefarmer-stf-client');27 groupApi.list().then(function(groups) {28 console.log(groups);29 });30});

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run devicefarmer-stf automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful