-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5985 from beyondessential/release-2024-45
Release 2024-45
- Loading branch information
Showing
68 changed files
with
1,378 additions
and
1,053 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
packages/central-server/src/tests/utilities/createSupportTicket.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** | ||
* Tupaia | ||
* Copyright (c) 2017 - 2024 Beyond Essential Systems Pty Ltd | ||
*/ | ||
|
||
import { expect } from 'chai'; | ||
import sinon from 'sinon'; | ||
import proxyquire from 'proxyquire'; | ||
|
||
const fetchWithTimeoutStub = sinon.stub().resolves(); | ||
const requireEnvStub = sinon.stub().returns('test_value'); | ||
|
||
// Use proxyquire to replace 'fetchWithTimeout' with the stub - See [here](https://stackoverflow.com/a/52591287) for an explanation about why destructured imports can't be stubbed | ||
const { createSupportTicket } = proxyquire('../../utilities/createSupportTicket', { | ||
'@tupaia/utils': { | ||
fetchWithTimeout: fetchWithTimeoutStub, | ||
requireEnv: requireEnvStub, | ||
getIsProductionEnvironment: () => true, | ||
}, | ||
}); | ||
|
||
describe('Create support ticket', () => { | ||
after(() => { | ||
// Reset the stub after each test | ||
fetchWithTimeoutStub.reset(); | ||
requireEnvStub.reset(); | ||
}); | ||
it("should not create a support ticket if ZENDESK_NOTIFICATIONS_DISABLE is set to 'true'", async () => { | ||
process.env.ZENDESK_NOTIFICATIONS_DISABLE = 'true'; | ||
await createSupportTicket('test_subject', 'test_message'); | ||
sinon.assert.notCalled(fetchWithTimeoutStub); | ||
}); | ||
|
||
it('should create a support ticket if ZENDESK_NOTIFICATIONS_DISABLE is not set to true', async () => { | ||
process.env.ZENDESK_NOTIFICATIONS_DISABLE = 'false'; | ||
await createSupportTicket('test_subject', 'test_message'); | ||
expect(fetchWithTimeoutStub).to.have.been.calledOnce; | ||
expect(fetchWithTimeoutStub).to.have.been.calledWith('test_value/tickets', { | ||
method: 'POST', | ||
headers: { | ||
Authorization: `Basic ${Buffer.from('test_value/token:test_value').toString('base64')}`, | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ | ||
ticket: { subject: 'test_subject', comment: { body: 'test_message' } }, | ||
}), | ||
}); | ||
}); | ||
}); |
58 changes: 58 additions & 0 deletions
58
packages/central-server/src/utilities/createSupportTicket.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/** | ||
* Tupaia | ||
* Copyright (c) 2017 - 2024 Beyond Essential Systems Pty Ltd | ||
*/ | ||
import { fetchWithTimeout, getIsProductionEnvironment, requireEnv } from '@tupaia/utils'; | ||
import { sendEmail } from '@tupaia/server-utils'; | ||
|
||
const emailInternally = async (subject, message) => { | ||
const sendTo = requireEnv('DEV_EMAIL_ADDRESS'); | ||
return sendEmail(sendTo, { | ||
subject, | ||
templateName: 'generic', | ||
templateContext: { | ||
userName: 'Tupaia Admin', | ||
message, | ||
}, | ||
}); | ||
}; | ||
|
||
export const createSupportTicket = async (subject, message) => { | ||
// If ZENDESK_NOTIFICATIONS_DISABLE is set to true, do not create a support ticket | ||
if (process.env.ZENDESK_NOTIFICATIONS_DISABLE === 'true') return; | ||
|
||
// If we are not in a production environment, send an email to the dev team instead of creating a support ticket | ||
if (!getIsProductionEnvironment()) { | ||
return emailInternally(subject, message); | ||
} | ||
|
||
try { | ||
const zendeskApi = requireEnv('ZENDESK_API_URL'); | ||
const apiToken = requireEnv('ZENDESK_API_TOKEN'); | ||
const email = requireEnv('ZENDESK_EMAIL'); | ||
|
||
const url = `${zendeskApi}/tickets`; | ||
|
||
const ticketData = { | ||
subject, | ||
comment: { | ||
body: message, | ||
}, | ||
}; | ||
|
||
const base64Credentials = Buffer.from(`${email}/token:${apiToken}`).toString('base64'); | ||
|
||
const requestConfig = { | ||
method: 'POST', | ||
headers: { | ||
Authorization: `Basic ${base64Credentials}`, | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ ticket: ticketData }), | ||
}; | ||
|
||
await fetchWithTimeout(url, requestConfig); | ||
} catch (error) { | ||
console.error('Error creating support ticket:', error); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.