-
Notifications
You must be signed in to change notification settings - Fork 13.8k
chore: Improve Transcript service call chain #34920
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b49059d
Dont call service to request transcript
KevLehman f2acbca
remove empty
KevLehman 2ae12f0
test
KevLehman 2555c5c
fix ts
KevLehman 0a06fc7
cr
KevLehman bc5c621
Merge branch 'develop' into chore/improve-service-flow
kodiakhq[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
44 changes: 44 additions & 0 deletions
44
apps/meteor/ee/app/livechat-enterprise/server/lib/requestPdfTranscript.ts
This file contains hidden or 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,44 @@ | ||
| import { OmnichannelTranscript, QueueWorker } from '@rocket.chat/core-services'; | ||
| import type { AtLeast, IOmnichannelRoom } from '@rocket.chat/core-typings'; | ||
| import { LivechatRooms } from '@rocket.chat/models'; | ||
|
|
||
| import { logger } from './logger'; | ||
|
|
||
| const serviceName = 'omnichannel-transcript' as const; | ||
| export const requestPdfTranscript = async ( | ||
| room: AtLeast<IOmnichannelRoom, '_id' | 'open' | 'v' | 'pdfTranscriptRequested'> | null, | ||
| requestedBy: string, | ||
| ): Promise<void> => { | ||
| if (!room) { | ||
| throw new Error('room-not-found'); | ||
| } | ||
|
|
||
| if (room.open) { | ||
| throw new Error('room-still-open'); | ||
| } | ||
|
|
||
| if (!room.v) { | ||
| throw new Error('improper-room-state'); | ||
| } | ||
|
|
||
| // Don't request a transcript if there's already one requested :) | ||
| if (room.pdfTranscriptRequested) { | ||
| // TODO: use logger | ||
| logger.info(`Transcript already requested for room ${room._id}`); | ||
| return; | ||
| } | ||
|
|
||
| // TODO: change this with a timestamp, allowing users to request a transcript again after a while if the first one fails | ||
| await LivechatRooms.setTranscriptRequestedPdfById(room._id); | ||
|
|
||
| const details = { details: { rid: room._id, userId: requestedBy, from: serviceName } }; | ||
| // Make the whole process sync when running on test mode | ||
| // This will prevent the usage of timeouts on the tests of this functionality :) | ||
| if (process.env.TEST_MODE) { | ||
| await OmnichannelTranscript.workOnPdf(details); | ||
| return; | ||
| } | ||
|
|
||
| logger.info(`Queuing work for room ${room._id}`); | ||
| await QueueWorker.queueWork('work', `${serviceName}.workOnPdf`, details); | ||
| }; | ||
77 changes: 77 additions & 0 deletions
77
apps/meteor/tests/unit/server/livechat/lib/requestPdfTranscript.spec.ts
This file contains hidden or 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,77 @@ | ||
| import { expect } from 'chai'; | ||
| import { describe, it, beforeEach, after } from 'mocha'; | ||
| import proxyquire from 'proxyquire'; | ||
| import sinon from 'sinon'; | ||
|
|
||
| const setStub = sinon.stub(); | ||
| const workOnPdfStub = sinon.stub(); | ||
| const queueWorkStub = sinon.stub(); | ||
|
|
||
| const { requestPdfTranscript } = proxyquire | ||
| .noCallThru() | ||
| .load('../../../../../ee/app/livechat-enterprise/server/lib/requestPdfTranscript.ts', { | ||
| '@rocket.chat/models': { | ||
| LivechatRooms: { | ||
| setTranscriptRequestedPdfById: setStub, | ||
| }, | ||
| }, | ||
| '@rocket.chat/core-services': { | ||
| OmnichannelTranscript: { | ||
| workOnPdf: workOnPdfStub, | ||
| }, | ||
| QueueWorker: { | ||
| queueWork: queueWorkStub, | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| describe('requestPdfTranscript', () => { | ||
| const currentTestModeValue = process.env.TEST_MODE; | ||
|
|
||
| beforeEach(() => { | ||
| setStub.reset(); | ||
| workOnPdfStub.reset(); | ||
| queueWorkStub.reset(); | ||
| }); | ||
|
|
||
| after(() => { | ||
| process.env.TEST_MODE = currentTestModeValue; | ||
| }); | ||
|
|
||
| it('should throw an error if room parameter is null', async () => { | ||
| await expect(requestPdfTranscript(null, 'userId')).to.be.rejectedWith('room-not-found'); | ||
| }); | ||
| it('should throw an error if room is still open', async () => { | ||
| await expect(requestPdfTranscript({ open: true }, 'userId')).to.be.rejectedWith('room-still-open'); | ||
| }); | ||
| it('should throw an error if room doesnt have a v property', async () => { | ||
| await expect(requestPdfTranscript({}, 'userId')).to.be.rejectedWith('improper-room-state'); | ||
| }); | ||
| it('should not request a transcript if it was already requested', async () => { | ||
| await requestPdfTranscript({ v: 1, pdfTranscriptRequested: true }, 'userId'); | ||
| expect(setStub.callCount).to.equal(0); | ||
| expect(workOnPdfStub.callCount).to.equal(0); | ||
| expect(queueWorkStub.callCount).to.equal(0); | ||
| }); | ||
| it('should set pdfTranscriptRequested to true on room', async () => { | ||
| await requestPdfTranscript({ _id: 'roomId', v: {}, pdfTranscriptRequested: false }, 'userId'); | ||
| expect(setStub.calledWith('roomId')).to.be.true; | ||
| }); | ||
| it('should call workOnPdf if TEST_MODE is true', async () => { | ||
| process.env.TEST_MODE = 'true'; | ||
| await requestPdfTranscript({ _id: 'roomId', v: {} }, 'userId'); | ||
| expect(workOnPdfStub.getCall(0).calledWithExactly({ details: { rid: 'roomId', userId: 'userId', from: 'omnichannel-transcript' } })).to | ||
| .be.true; | ||
| expect(queueWorkStub.calledOnce).to.be.false; | ||
| }); | ||
| it('should queue work if TEST_MODE is not set', async () => { | ||
| delete process.env.TEST_MODE; | ||
| await requestPdfTranscript({ _id: 'roomId', v: {} }, 'userId'); | ||
| expect(workOnPdfStub.calledOnce).to.be.false; | ||
| expect( | ||
| queueWorkStub.getCall(0).calledWithExactly('work', 'omnichannel-transcript.workOnPdf', { | ||
| details: { rid: 'roomId', userId: 'userId', from: 'omnichannel-transcript' }, | ||
| }), | ||
| ).to.be.true; | ||
| }); | ||
| }); |
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -43,5 +43,8 @@ | |
| "typings": "./dist/index.d.ts", | ||
| "files": [ | ||
| "/dist" | ||
| ] | ||
| ], | ||
| "volta": { | ||
| "extends": "../../../package.json" | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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
12 changes: 10 additions & 2 deletions
12
packages/core-services/src/types/IOmnichannelTranscriptService.ts
This file contains hidden or 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 |
|---|---|---|
| @@ -1,6 +1,14 @@ | ||
| import type { IUser, IRoom } from '@rocket.chat/core-typings'; | ||
|
|
||
| type WorkDetails = { | ||
| rid: IRoom['_id']; | ||
| userId: IUser['_id']; | ||
| }; | ||
|
|
||
| type WorkDetailsWithSource = WorkDetails & { | ||
| from: string; | ||
| }; | ||
|
|
||
| export interface IOmnichannelTranscriptService { | ||
| requestTranscript({ details }: { details: { userId: IUser['_id']; rid: IRoom['_id'] } }): Promise<void>; | ||
| workOnPdf({ template, details }: { template: string; details: any }): Promise<void>; | ||
| workOnPdf({ details }: { details: WorkDetailsWithSource }): Promise<void>; | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.