|
| 1 | +/** |
| 2 | + * @file |
| 3 | + * Example 009: Delete user product permission profiles using an email address |
| 4 | + * @author DocuSign |
| 5 | + */ |
| 6 | + |
| 7 | +const path = require('path') |
| 8 | +const validator = require('validator'); |
| 9 | +const { deleteUserProductPermissionProfile, getProductPermissionProfilesByEmail } = require('../examples/deleteUserProductPermissionProfile'); |
| 10 | +const dsConfig = require('../../../config/index.js').config; |
| 11 | +const { getOrganizationId } = require("../getOrganizationId.js"); |
| 12 | +const { checkUserExistsByEmail } = require("../getData.js"); |
| 13 | + |
| 14 | +const eg009DeleteUserProductPermissionProfile = exports; |
| 15 | +const eg = 'eg009' // This example reference.; |
| 16 | +const mustAuthenticate = '/ds/mustAuthenticate'; |
| 17 | +const minimumBufferMin = 3; |
| 18 | + |
| 19 | +/** |
| 20 | + * Delete user product permission profiles using an email address |
| 21 | + * @param {object} req Request obj |
| 22 | + * @param {object} res Response obj |
| 23 | + */ |
| 24 | +eg009DeleteUserProductPermissionProfile.createController = async (req, res) => { |
| 25 | + // At this point we should have a good token. But we |
| 26 | + // double-check here to enable a better UX to the user. |
| 27 | + const isTokenOK = req.dsAuth.checkToken(minimumBufferMin); |
| 28 | + if (!isTokenOK) { |
| 29 | + req.flash("info", "Sorry, you need to re-authenticate."); |
| 30 | + // Save the current operation so it will be resumed after authentication |
| 31 | + req.dsAuth.setEg(req, eg); |
| 32 | + return res.redirect(mustAuthenticate); |
| 33 | + } |
| 34 | + |
| 35 | + const userEmail = req.session.clmEmail; |
| 36 | + if (!userEmail || !await checkUserExistsByEmail(req, userEmail)) { |
| 37 | + return res.render("pages/admin-examples/eg009DeleteUserProductPermissionProfile", { |
| 38 | + eg: eg, csrfToken: req.csrfToken(), |
| 39 | + title: "Delete user product permission profiles using an email address", |
| 40 | + emailOk: false, |
| 41 | + sourceFile: sourceFile, |
| 42 | + sourceUrl: dsConfig.githubExampleUrl + 'admin/examples/' + sourceFile, |
| 43 | + documentation: dsConfig.documentation + eg, |
| 44 | + showDoc: dsConfig.documentation |
| 45 | + }); |
| 46 | + } |
| 47 | + |
| 48 | + const body = req.body; |
| 49 | + const args = { |
| 50 | + accessToken: req.user.accessToken, |
| 51 | + basePath: dsConfig.adminAPIUrl, |
| 52 | + accountId: req.session.accountId, |
| 53 | + organizationId: req.session.organizationId, |
| 54 | + email: req.session.clmEmail, |
| 55 | + productId: validator.escape(body.productId) |
| 56 | + } |
| 57 | + |
| 58 | + let results = null; |
| 59 | + try { |
| 60 | + results = await deleteUserProductPermissionProfile(args); |
| 61 | + } |
| 62 | + catch (error) { |
| 63 | + const errorBody = error && error.response && error.response.body; |
| 64 | + // we can pull the DocuSign error code and message from the response body |
| 65 | + const errorCode = error && error.status || errorBody && errorBody.errorCode; |
| 66 | + const errorMessage = errorBody && errorBody.error_description || errorBody.message; |
| 67 | + |
| 68 | + // In production, may want to provide customized error messages and |
| 69 | + // remediation advice to the user. |
| 70 | + res.render("pages/error", { err: error, errorCode, errorMessage }); |
| 71 | + } |
| 72 | + if (results) { |
| 73 | + res.render("pages/example_done", { |
| 74 | + title: "Delete user product permission profiles using an email address", |
| 75 | + h1: "Delete user product permission profiles using an email address", |
| 76 | + message: "Results from MultiProductUserManagement:removeUserProductPermission method:", |
| 77 | + json: JSON.stringify(results).replace(/'/g, '') |
| 78 | + }); |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +/** |
| 83 | + * Form page for this application |
| 84 | + */ |
| 85 | +eg009DeleteUserProductPermissionProfile.getController = async (req, res) => { |
| 86 | + // Check that the authentication token is ok with a long buffer time. |
| 87 | + // If needed, now is the best time to ask the user to authenticate |
| 88 | + // since they have not yet entered any information into the form. |
| 89 | + const isTokenOK = req.dsAuth.checkToken(); |
| 90 | + if (!isTokenOK) { |
| 91 | + req.flash("info", "Sorry, you need to re-authenticate."); |
| 92 | + // Save the current operation so it will be resumed after authentication |
| 93 | + req.dsAuth.setEg(req, eg); |
| 94 | + return res.redirect(mustAuthenticate); |
| 95 | + } |
| 96 | + |
| 97 | + await getOrganizationId(req); |
| 98 | + |
| 99 | + const userEmail = req.session.clmEmail; |
| 100 | + const sourceFile = (path.basename(__filename))[5].toLowerCase() + (path.basename(__filename)).substr(6); |
| 101 | + if (!userEmail || !await checkUserExistsByEmail(req, userEmail)) { |
| 102 | + return res.render("pages/admin-examples/eg009DeleteUserProductPermissionProfile", { |
| 103 | + eg: eg, csrfToken: req.csrfToken(), |
| 104 | + title: "Delete user product permission profiles using an email address", |
| 105 | + emailOk: false, |
| 106 | + sourceFile: sourceFile, |
| 107 | + sourceUrl: dsConfig.githubExampleUrl + 'admin/examples/' + sourceFile, |
| 108 | + documentation: dsConfig.documentation + eg, |
| 109 | + showDoc: dsConfig.documentation |
| 110 | + }); |
| 111 | + } |
| 112 | + |
| 113 | + try { |
| 114 | + const args = { |
| 115 | + accessToken: req.user.accessToken, |
| 116 | + basePath: dsConfig.adminAPIUrl, |
| 117 | + accountId: req.session.accountId, |
| 118 | + organizationId: req.session.organizationId, |
| 119 | + email: req.session.clmEmail |
| 120 | + }; |
| 121 | + |
| 122 | + const productPermissionProfiles = await getProductPermissionProfilesByEmail(args); |
| 123 | + let permissionProfileList = []; |
| 124 | + let permissionName; |
| 125 | + |
| 126 | + // Create the permission profile list that will be used on example page |
| 127 | + if (productPermissionProfiles && productPermissionProfiles.length > 0) { |
| 128 | + productPermissionProfiles.forEach(product => { |
| 129 | + let permissionProfiles = product["permission_profiles"]; |
| 130 | + |
| 131 | + permissionProfiles.forEach(profile => { |
| 132 | + permissionName = product["product_name"].includes("CLM") |
| 133 | + ? `CLM - ${profile["permission_profile_name"]}` |
| 134 | + : `eSignature - ${profile["permission_profile_name"]}`; |
| 135 | + |
| 136 | + // Add current permission profile data to the list if it was not added yet |
| 137 | + if (permissionProfileList.filter(prof => prof["productId"] === product["product_id"]).length === 0) { |
| 138 | + permissionProfileList.push({ productId: product["product_id"], permissionName }); |
| 139 | + } |
| 140 | + }); |
| 141 | + }); |
| 142 | + } |
| 143 | + |
| 144 | + res.render("pages/admin-examples/eg009DeleteUserProductPermissionProfile", { |
| 145 | + eg: eg, csrfToken: req.csrfToken(), |
| 146 | + title: "Delete user product permission profiles using an email address", |
| 147 | + permissionProfileList: permissionProfileList, |
| 148 | + emailOk: true, |
| 149 | + email: req.session.clmEmail, |
| 150 | + sourceFile: sourceFile, |
| 151 | + sourceUrl: dsConfig.githubExampleUrl + 'admin/examples/' + sourceFile, |
| 152 | + documentation: dsConfig.documentation + eg, |
| 153 | + showDoc: dsConfig.documentation |
| 154 | + }); |
| 155 | + } |
| 156 | + catch (error) { |
| 157 | + const errorBody = error && error.response && error.response.body; |
| 158 | + // we can pull the DocuSign error code and message from the response body |
| 159 | + const errorCode = errorBody && errorBody.errorCode; |
| 160 | + const errorMessage = errorBody && errorBody.message; |
| 161 | + |
| 162 | + // In production, may want to provide customized error messages and |
| 163 | + // remediation advice to the user. |
| 164 | + res.render("pages/error", { err: error, errorCode: errorCode, errorMessage: errorMessage }); |
| 165 | + } |
| 166 | +} |
0 commit comments