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