Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code template libraries update with code template #6389

Open
amithole0206 opened this issue Jan 9, 2025 · 1 comment
Open

Code template libraries update with code template #6389

amithole0206 opened this issue Jan 9, 2025 · 1 comment

Comments

@amithole0206
Copy link

Hi,

I want to create or update code template libraries that contain code templates.
We tried using the /codeTemplateLibraries PUT method, but we were unable to update the code templates.

Could you please help in solving the issue

example

{
"codeTemplateLibrary": {
"@Version": "4.4.2",
"id": "b2814aa4-3440-46ef-a572-4b42f867ed83",
"name": "TrialComplete",
"revision": 1,
"lastModified": {
"time": 1734569699360,
"timezone": "Asia/Calcutta"
},
"description": null,
"includeNewChannels": true,
"enabledChannelIds": {
"string": [
"5503783d-a9f0-4d9e-9662-3d6ccbaf8f41",
"63c2f54e-0680-4de2-8eac-5276af0d3d40",
"66d7e942-e443-4289-ac52-38297df09411",
"57f6cddd-9d92-4efc-adc5-3d8c90fcab4a",
"9843deb1-3ce6-42d5-900a-c98f6a318927"
]
},
"disabledChannelIds": null,
"codeTemplates": {
"codeTemplate": [
{
"@Version": "4.4.2",
"id": "4ce9df36-003e-41ff-b238-88e5b5185f0d",
"name": "authToken",
"revision": 1,
"lastModified": {
"time": 1734569699610,
"timezone": "Asia/Calcutta"
},
"contextSet": {
"delegate": {
"contextType": [
"CHANNEL_POSTPROCESSOR",
"DESTINATION_FILTER_TRANSFORMER",
"DESTINATION_RESPONSE_TRANSFORMER",
"SOURCE_RECEIVER",
"SOURCE_FILTER_TRANSFORMER",
"DESTINATION_DISPATCHER"
]
}
},
"properties": {
"@Class": "com.mirth.connect.model.codetemplates.BasicCodeTemplateProperties",
"type": "FUNCTION",
"code": "\n /**\n Modify the description here. Modify the function name and parameters as needed. One function per\n template is recommended; create a new code template for each new function.\n\n @return {String} return Bearer Token\n */\n function authToken() {\n tokenExpiryDt = new Date(globalMap.get('tokenExpiry'));\n myDt = new Date();\n if (myDt >= tokenExpiryDt) {\n try {\n\n postBody = {};\n postBody.username = $cfg('TCEP_USER');\n postBody.password = $cfg('TCEP_PASS');\n postBody.client_id = 'TCLabInterface';\n postBody.client_secret = $cfg('TCEP_IDENTITY_API_CLIENT_SECRET');\n postBody.grant_type = 'password';\n postBody.scope = 'TrialLabApis TrialSystemApis TrialDesignApis TrialJobApis';\n\n var formBody = [];\n for (var property in postBody) {\n var encodedKey = encodeURIComponent(property);\n var encodedValue = encodeURIComponent(postBody[property]);\n formBody.push(encodedKey + "=" + encodedValue);\n }\n formBody = formBody.join("&");\n\n headers = [\n {"key": "Content-Type", "value": "application/x-www-form-urlencoded"},\n {"key": "Content-Length", "value": postBody.length}\n ];\n\n response = httpRequest($cfg('TCEP_IDENTITY_API_URL') + 'connect/token', 'POST', headers, formBody);\n responseBody = JSON.parse(response.body);\n dt = new Date();\n dt.setSeconds(dt.getSeconds() + responseBody.expires_in);\n globalMap.put('tokenExpiry', dt.toISOString());\n globalMap.put('token', "Bearer " + responseBody.access_token)\n globalMap.put('authInUse', 'n');\n return "Bearer " + responseBody.access_token;\n } catch(e) {\n globalMap.put('authInUse', 'n');\n throw(e);\n }\n }\n else {\n return globalMap.get('token');\n }\n }"
}
}
]
}
}
}

@pacmano1
Copy link
Collaborator

pacmano1 commented Jan 9, 2025

Please use code snippets and format your code.

e.g.:

{
    "codeTemplateLibrary": {
        "@Version": "4.4.2",
        "id": "b2814aa4-3440-46ef-a572-4b42f867ed83",
        "name": "TrialComplete",
        "revision": 1,
        "lastModified": {
            "time": 1734569699360,
            "timezone": "Asia/Calcutta"
        },
        "description": null,
        "includeNewChannels": true,
        "enabledChannelIds": {
            "string": [
                "5503783d-a9f0-4d9e-9662-3d6ccbaf8f41",
                "63c2f54e-0680-4de2-8eac-5276af0d3d40",
                "66d7e942-e443-4289-ac52-38297df09411",
                "57f6cddd-9d92-4efc-adc5-3d8c90fcab4a",
                "9843deb1-3ce6-42d5-900a-c98f6a318927"
            ]
        },
        "disabledChannelIds": null,
        "codeTemplates": {
            "codeTemplate": [
                {
                    "@Version": "4.4.2",
                    "id": "4ce9df36-003e-41ff-b238-88e5b5185f0d",
                    "name": "authToken",
                    "revision": 1,
                    "lastModified": {
                        "time": 1734569699610,
                        "timezone": "Asia/Calcutta"
                    },
                    "contextSet": {
                        "delegate": {
                            "contextType": [
                                "CHANNEL_POSTPROCESSOR",
                                "DESTINATION_FILTER_TRANSFORMER",
                                "DESTINATION_RESPONSE_TRANSFORMER",
                                "SOURCE_RECEIVER",
                                "SOURCE_FILTER_TRANSFORMER",
                                "DESTINATION_DISPATCHER"
                            ]
                        }
                    },
                    "properties": {
                        "@Class": "com.mirth.connect.model.codetemplates.BasicCodeTemplateProperties",
                        "type": "FUNCTION",
                        "code": "\n /**\n Modify the description here. Modify the function name and parameters as needed. One function per\n template is recommended; create a new code template for each new function.\n\n @return {String} return Bearer Token\n */\n function authToken() {\n tokenExpiryDt = new Date(globalMap.get('tokenExpiry'));\n myDt = new Date();\n if (myDt >= tokenExpiryDt) {\n try {\n\n postBody = {};\n postBody.username = $cfg('TCEP_USER');\n postBody.password = $cfg('TCEP_PASS');\n postBody.client_id = 'TCLabInterface';\n postBody.client_secret = $cfg('TCEP_IDENTITY_API_CLIENT_SECRET');\n postBody.grant_type = 'password';\n postBody.scope = 'TrialLabApis TrialSystemApis TrialDesignApis TrialJobApis';\n\n var formBody = [];\n for (var property in postBody) {\n var encodedKey = encodeURIComponent(property);\n var encodedValue = encodeURIComponent(postBody[property]);\n formBody.push(encodedKey + "=" + encodedValue);\n }\n formBody = formBody.join("&");\n\n headers = [\n {"key": "Content-Type", "value": "application/x-www-form-urlencoded"},\n {"key": "Content-Length", "value": postBody.length}\n ];\n\n response = httpRequest($cfg('TCEP_IDENTITY_API_URL') + 'connect/token', 'POST', headers, formBody);\n responseBody = JSON.parse(response.body);\n dt = new Date();\n dt.setSeconds(dt.getSeconds() + responseBody.expires_in);\n globalMap.put('tokenExpiry', dt.toISOString());\n globalMap.put('token', "Bearer " + responseBody.access_token)\n globalMap.put('authInUse', 'n');\n return "Bearer " + responseBody.access_token;\n } catch(e) {\n globalMap.put('authInUse', 'n');\n throw(e);\n }\n }\n else {\n return globalMap.get('token');\n }\n }"
                    }
                }
            ]
        }
    }
}

What error are you getting?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants