|
| 1 | +/*jslint nomen: true */ |
| 2 | +/* |
| 3 | + * Copyright (C) 2016 TopCoder Inc., All Rights Reserved. |
| 4 | + * |
| 5 | + * @version 1.0 |
| 6 | + * @author TCSCODER |
| 7 | + */ |
| 8 | +"use strict"; |
| 9 | +var _ = require('underscore'); |
| 10 | +var async = require('async'); |
| 11 | +var DuplicateResourceError = require('../errors/DuplicateResourceError'); |
| 12 | + |
| 13 | +/** |
| 14 | + * This is the function that will actually get all admins. |
| 15 | + * |
| 16 | + * @param {Object} api The api object that is used to access the global infrastructure |
| 17 | + * @param {Object} connection The connection object for the current request |
| 18 | + * @param {Object} dbConnectionMap The database connection map for the current request |
| 19 | + * @param {Function} next The callback to be called after this function is done |
| 20 | + */ |
| 21 | +var getAdmins = function (api, connection, dbConnectionMap, next) { |
| 22 | + var helper = api.helper; |
| 23 | + async.waterfall([function (cb) { |
| 24 | + cb(helper.checkAdmin(connection, "You need to login for this api.", 'You don\'t have access to this api.')); |
| 25 | + }, function (cb) { |
| 26 | + api.dataAccess.executeQuery("get_admins", {}, dbConnectionMap, cb); |
| 27 | + }, function (result, cb) { |
| 28 | + var ret = {}, i, entity, type, id; |
| 29 | + for (i = 0; i < result.length; i = i + 1) { |
| 30 | + type = result[i].type.trim(); |
| 31 | + id = result[i].user_id; |
| 32 | + if (!ret[id]) { |
| 33 | + ret[id] = { |
| 34 | + id: result[i].user_id, |
| 35 | + name: result[i].handle, |
| 36 | + adminGroup: false, |
| 37 | + adminRole: false, |
| 38 | + managerResource: false |
| 39 | + }; |
| 40 | + } |
| 41 | + entity = ret[id]; |
| 42 | + if (type === 'Admin Group') { |
| 43 | + entity.adminGroup = true; |
| 44 | + } else if (type === 'Admin Role') { |
| 45 | + entity.adminRole = true; |
| 46 | + } else if (type === 'Manager Resource') { |
| 47 | + entity.managerResource = true; |
| 48 | + } |
| 49 | + } |
| 50 | + cb(null, { |
| 51 | + allAdmins: _.values(ret) |
| 52 | + }); |
| 53 | + }], function (err, result) { |
| 54 | + if (err) { |
| 55 | + helper.handleError(api, connection, err); |
| 56 | + } else { |
| 57 | + connection.response = result; |
| 58 | + } |
| 59 | + next(connection, true); |
| 60 | + }); |
| 61 | +}; |
| 62 | + |
| 63 | +/** |
| 64 | + * The API for getting all admins |
| 65 | + */ |
| 66 | +exports.admins = { |
| 67 | + name: "admins", |
| 68 | + description: "retrieve all TopCoder admins", |
| 69 | + inputs: { |
| 70 | + required: [], |
| 71 | + optional: [] |
| 72 | + }, |
| 73 | + blockedConnectionTypes: [], |
| 74 | + outputExample: {}, |
| 75 | + version: 'v2', |
| 76 | + transaction: 'read', // this action is read-only |
| 77 | + databases: ['tcs_catalog'], |
| 78 | + run: function (api, connection, next) { |
| 79 | + if (connection.dbConnectionMap) { |
| 80 | + api.log("Execute admins#run", 'debug'); |
| 81 | + getAdmins(api, connection, connection.dbConnectionMap, next); |
| 82 | + } else { |
| 83 | + api.helper.handleNoConnection(api, connection, next); |
| 84 | + } |
| 85 | + } |
| 86 | +}; |
| 87 | + |
| 88 | +/** |
| 89 | + * This is the function that will actually create admin. |
| 90 | + * |
| 91 | + * @param {Object} api The api object that is used to access the global infrastructure |
| 92 | + * @param {Object} connection The connection object for the current request |
| 93 | + * @param {Object} dbConnectionMap The database connection map for the current request |
| 94 | + * @param {Function} next The callback to be called after this function is done |
| 95 | + */ |
| 96 | +var createAdmin = function (api, connection, dbConnectionMap, next) { |
| 97 | + var helper = api.helper, username = connection.params.username, userId, operatorId, parameters, |
| 98 | + result = { |
| 99 | + success: true |
| 100 | + }; |
| 101 | + async.waterfall([function (cb) { |
| 102 | + cb(helper.checkAdmin(connection, "You need to login for this api.", 'You don\'t have access to this api.')); |
| 103 | + }, function (cb) { |
| 104 | + operatorId = connection.caller.userId; |
| 105 | + helper.validateUserAndGetUserId(username, dbConnectionMap, cb); |
| 106 | + }, function (id, cb) { |
| 107 | + userId = id; |
| 108 | + async.auto({ |
| 109 | + nextUserGroupId: function (ca) { |
| 110 | + api.dataAccess.executeQuery("get_next_admin_user_group_id", {}, dbConnectionMap, ca); |
| 111 | + }, |
| 112 | + nextResourceId: function (ca) { |
| 113 | + api.dataAccess.executeQuery("get_next_admin_resource_id", {}, dbConnectionMap, ca); |
| 114 | + } |
| 115 | + }, cb); |
| 116 | + }, function (results, cb) { |
| 117 | + parameters = { |
| 118 | + userId: userId, |
| 119 | + userGroupId: results.nextUserGroupId[0].next_id, |
| 120 | + operatorId: operatorId, |
| 121 | + resourceId: results.nextResourceId[0].next_id |
| 122 | + }; |
| 123 | + api.dataAccess.executeQuery("insert_admin_group", parameters, dbConnectionMap, function (err) { |
| 124 | + if (helper.isDuplicateResourceError(err)) { |
| 125 | + cb(new DuplicateResourceError("User " + username + " has already been added to Admin Group", err)); |
| 126 | + } else { |
| 127 | + cb(err); |
| 128 | + } |
| 129 | + }); |
| 130 | + }, function (cb) { |
| 131 | + api.dataAccess.executeQuery("clear_user_rating", parameters, dbConnectionMap, function (err) { |
| 132 | + cb(err); |
| 133 | + }); |
| 134 | + }, function (cb) { |
| 135 | + api.dataAccess.executeQuery("get_admin_resource", { |
| 136 | + userId: userId |
| 137 | + }, dbConnectionMap, cb); |
| 138 | + }, function (resourceIds, cb) { |
| 139 | + if (!resourceIds || !resourceIds.length) { |
| 140 | + api.dataAccess.executeQuery("insert_new_admin_resource", parameters, dbConnectionMap, function (err) { |
| 141 | + if (err) { |
| 142 | + return cb(err); |
| 143 | + } |
| 144 | + api.dataAccess.executeQuery("insert_new_admin_resource_info", parameters, dbConnectionMap, function (err) { |
| 145 | + cb(err); |
| 146 | + }); |
| 147 | + }); |
| 148 | + } else { |
| 149 | + cb(null); |
| 150 | + } |
| 151 | + }, function (cb) { |
| 152 | + api.dataAccess.executeQuery("insert_admin_role", parameters, dbConnectionMap, function (err) { |
| 153 | + if (helper.isDuplicateResourceError(err)) { |
| 154 | + cb(new DuplicateResourceError("User " + username + " has already been assigned Admin role", err)); |
| 155 | + } else { |
| 156 | + cb(err); |
| 157 | + } |
| 158 | + }); |
| 159 | + }], function (err) { |
| 160 | + if (err) { |
| 161 | + helper.handleError(api, connection, err); |
| 162 | + } else { |
| 163 | + result.message = username + " has been successfully added as TopCoder Admin"; |
| 164 | + connection.response = result; |
| 165 | + } |
| 166 | + next(connection, true); |
| 167 | + }); |
| 168 | + |
| 169 | +}; |
| 170 | + |
| 171 | +/** |
| 172 | + * The API for creating admin |
| 173 | + */ |
| 174 | +exports.createAdmin = { |
| 175 | + name: "createAdmin", |
| 176 | + description: "create TopCoder admin", |
| 177 | + inputs: { |
| 178 | + required: ['username'], |
| 179 | + optional: [] |
| 180 | + }, |
| 181 | + blockedConnectionTypes: [], |
| 182 | + outputExample: {}, |
| 183 | + version: 'v2', |
| 184 | + cacheEnabled: false, |
| 185 | + transaction: 'write', |
| 186 | + databases: ['tcs_catalog', 'common_oltp'], |
| 187 | + run: function (api, connection, next) { |
| 188 | + if (connection.dbConnectionMap) { |
| 189 | + api.log("Execute createAdmin#run", 'debug'); |
| 190 | + createAdmin(api, connection, connection.dbConnectionMap, next); |
| 191 | + } else { |
| 192 | + api.helper.handleNoConnection(api, connection, next); |
| 193 | + } |
| 194 | + } |
| 195 | +}; |
| 196 | + |
| 197 | +/** |
| 198 | + * This is the function that will actually remove admin. |
| 199 | + * |
| 200 | + * @param {Object} api The api object that is used to access the global infrastructure |
| 201 | + * @param {Object} connection The connection object for the current request |
| 202 | + * @param {Object} dbConnectionMap The database connection map for the current request |
| 203 | + * @param {Function} next The callback to be called after this function is done |
| 204 | + */ |
| 205 | +var removeAdmin = function (api, connection, dbConnectionMap, next) { |
| 206 | + var helper = api.helper, username = connection.params.username, operatorId, parameters, |
| 207 | + result = { |
| 208 | + success: true |
| 209 | + }; |
| 210 | + async.waterfall([function (cb) { |
| 211 | + cb(helper.checkAdmin(connection, "You need to login for this api.", 'You don\'t have access to this api.')); |
| 212 | + }, function (cb) { |
| 213 | + operatorId = connection.caller.userId; |
| 214 | + helper.validateUserAndGetUserId(username, dbConnectionMap, cb); |
| 215 | + }, function (userId, cb) { |
| 216 | + parameters = { |
| 217 | + userId: userId, |
| 218 | + operatorId: operatorId |
| 219 | + }; |
| 220 | + api.dataAccess.executeQuery("remove_admin_group", parameters, dbConnectionMap, function (err) { |
| 221 | + cb(err); |
| 222 | + }); |
| 223 | + }, function (cb) { |
| 224 | + api.dataAccess.executeQuery("remove_admin_resource_info", parameters, dbConnectionMap, function (err) { |
| 225 | + cb(err); |
| 226 | + }); |
| 227 | + }, function (cb) { |
| 228 | + api.dataAccess.executeQuery("remove_admin_resource", parameters, dbConnectionMap, function (err) { |
| 229 | + cb(err); |
| 230 | + }); |
| 231 | + }, function (cb) { |
| 232 | + api.dataAccess.executeQuery("remove_admin_role", parameters, dbConnectionMap, function (err) { |
| 233 | + cb(err); |
| 234 | + }); |
| 235 | + }], function (err) { |
| 236 | + if (err) { |
| 237 | + helper.handleError(api, connection, err); |
| 238 | + } else { |
| 239 | + result.message = "TopCoder Admin: " + username + " has been successfully removed"; |
| 240 | + connection.response = result; |
| 241 | + } |
| 242 | + next(connection, true); |
| 243 | + }); |
| 244 | + |
| 245 | +}; |
| 246 | + |
| 247 | +/** |
| 248 | + * The API for removing admin |
| 249 | + */ |
| 250 | +exports.removeAdmin = { |
| 251 | + name: "removeAdmin", |
| 252 | + description: "remove TopCoder admin", |
| 253 | + inputs: { |
| 254 | + required: ['username'], |
| 255 | + optional: [] |
| 256 | + }, |
| 257 | + blockedConnectionTypes: [], |
| 258 | + outputExample: {}, |
| 259 | + version: 'v2', |
| 260 | + cacheEnabled: false, |
| 261 | + transaction: 'write', |
| 262 | + databases: ['tcs_catalog', 'common_oltp'], |
| 263 | + run: function (api, connection, next) { |
| 264 | + if (connection.dbConnectionMap) { |
| 265 | + api.log("Execute removeAdmin#run", 'debug'); |
| 266 | + removeAdmin(api, connection, connection.dbConnectionMap, next); |
| 267 | + } else { |
| 268 | + api.helper.handleNoConnection(api, connection, next); |
| 269 | + } |
| 270 | + } |
| 271 | +}; |
0 commit comments