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

Handle attachments web socket notifications on patient flow page #1371

Merged
merged 6 commits into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/js/apps/patients/sidebar/action-sidebar_app.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export default App.extend(extend({
'change:_owner': this.onChangeOwner,
'destroy': this.onDestroy,
'add:comment': this.onCommentAdded,
'add:attachment': this.onAttachmentAdded,
});

this.showChildView('heading', new HeadingView({ model: this.action }));
Expand Down Expand Up @@ -82,6 +83,11 @@ export default App.extend(extend({

Radio.request('ws', 'add', model);
},
onAttachmentAdded(model) {
this.attachments.add(model);

Radio.request('ws', 'add', model);
},
paulfalgout marked this conversation as resolved.
Show resolved Hide resolved
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Separate from onAddAttachment
Having both onAttachmentAdded and onAddAttachment is potentially confusing. Following the suggestion to rename it clearly differentiates WS vs. manual additions.

onStart(options, activity, comments, attachments) {
this.activityCollection = new Backbone.Collection([...activity.models, ...comments.models]);
this.comments = comments;
Expand Down Expand Up @@ -230,6 +236,8 @@ export default App.extend(extend({
});
attachment.upload(file);

Radio.request('ws', 'add', file);
paulfalgout marked this conversation as resolved.
Show resolved Hide resolved

this.listenTo(attachment, 'upload:failed', () => {
Radio.request('alert', 'show:error', intl.patients.sidebar.actionSidebarApp.uploadError);
});
Expand All @@ -238,12 +246,12 @@ export default App.extend(extend({
model.destroy();
},
addSubscriptions() {
Radio.request('ws', 'add', this.comments.models);
Radio.request('ws', 'add', [...this.comments.models, ...this.attachments.models]);
},
removeSubscriptions() {
// for when sidebar is closed before comments api request is finished
if (!this.comments) return;
if (!this.comments || !this.attachments) return;
paulfalgout marked this conversation as resolved.
Show resolved Hide resolved

Radio.request('ws', 'unsubscribe', this.comments.models);
Radio.request('ws', 'unsubscribe', [...this.comments.models, ...this.attachments.models]);
},
}, SidebarMixin));
16 changes: 16 additions & 0 deletions src/js/entities-service/entities/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,22 @@ const _Model = BaseModel.extend({

this.trigger('add:comment', commentModel);
},
AttachmentAdded({ file, attributes }) {
const attachmentModel = Radio.request('entities', 'files:model', {
id: file.id,
path: attributes.path,
created_at: dayjs.utc().format(),
_action: this.id,
_patient: this.getPatient().id,
_view: attributes.urls.view,
_download: attributes.urls.download,
});

const newFilesRelationship = [...this.get('_files'), { id: file.id, type: 'files' }];
this.set({ _files: newFilesRelationship });

this.trigger('add:attachment', attachmentModel);
},
ResourceDeleted() {
this.destroy({ isDeleted: true });
},
Expand Down
109 changes: 104 additions & 5 deletions test/integration/patients/patient/patient-flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ context('patient flow page', function() {

specify('patient flow action sidebar', function() {
const testCommentId = uuid();
const testOtherFileId = uuid();

const testPatient = getPatient({
attributes: {
Expand All @@ -119,25 +120,39 @@ context('patient flow page', function() {
},
});

const testProgramAction = getProgramAction({
attributes: {
allowed_uploads: ['pdf'],
},
});

const testFlowAction = getAction({
attributes: {
name: 'Test Action',
duration: 10,
outreach: 'disabled',
sharing: 'disabled',
updated_at: testTsSubtract(1),
allowed_uploads: ['pdf'],
},
relationships: {
flow: getRelationship(testFlow),
state: getRelationship(stateTodo),
owner: getRelationship(teamNurse),
form: getRelationship(testForm),
patient: getRelationship(testPatient),
'flow': getRelationship(testFlow),
'state': getRelationship(stateTodo),
'owner': getRelationship(teamNurse),
'form': getRelationship(testForm),
'patient': getRelationship(testPatient),
'program-action': getRelationship(testProgramAction),
'files': getRelationship([{ id: testOtherFileId }], 'files'),
},
});

cy
.routesForPatientAction()
.routeSettings(fx => {
fx.data.push({ id: 'upload_attachments', attributes: { value: true } });

return fx;
})
.routeFlow(fx => {
fx.data = mergeJsonApi(testFlow, {
relationships: {
Expand All @@ -150,13 +165,32 @@ context('patient flow page', function() {
.routeFlowActions(fx => {
fx.data = [testFlowAction];

fx.included.push(testProgramAction);

return fx;
})
.routePatientByFlow(fx => {
fx.data = testPatient;

return fx;
})
.routeActionFiles(fx => {
fx.data = [
{
id: testOtherFileId,
attributes: {
path: `patients/${ testPatient.id }/Other_file.pdf`,
created_at: testTsSubtract(1),
},
meta: {
view: `https://www.bucket_name.s3.amazonaws.com/patients/${ testPatient.id }/view/Other_File.pdf`,
download: `https://www.bucket_name.s3.amazonaws.com/patients/${ testPatient.id }/download/Other_File.pdf`,
},
},
];

return fx;
})
.routeActionActivity()
.visit(`/flow/${ testFlow.id }/action/${ testFlowAction.id }`)
.wait('@routeFlow')
Expand Down Expand Up @@ -389,6 +423,40 @@ context('patient flow page', function() {
.find('.comment__item')
.should('have.length', 3);

cy.sendWs({
category: 'AttachmentAdded',
resource: {
type: 'patient-actions',
id: testFlowAction.id,
},
payload: {
clinician: {
type: 'clinicians',
id: uuid(),
},
file: {
type: 'files',
id: uuid(),
},
attributes: {
path: `patients/${ testPatient.id }/HRA.pdf`,
bucket: 'bucket_name',
urls: {
view: `https://www.bucket_name.s3.amazonaws.com/patients/${ testPatient.id }/view/HRA.pdf`,
download: `https://www.bucket_name.s3.amazonaws.com/patients/${ testPatient.id }/download/HRA.pdf`,
},
},
},
});

cy
.get('[data-attachments-files-region]')
.children()
.as('attachmentItems')
.should('have.length', 2)
.first()
.contains('HRA.pdf');

cy.sendWs({
category: 'ResourceDeleted',
resource: {
Expand Down Expand Up @@ -2795,6 +2863,37 @@ context('patient flow page', function() {
.find('[data-state-region]')
.should('contain', 'Done');

cy.sendWs({
category: 'AttachmentAdded',
resource: {
type: 'patient-actions',
id: testSocketAction.id,
},
payload: {
clinician: {
type: 'clinicians',
id: uuid(),
},
file: {
type: 'files',
id: uuid(),
},
attributes: {
path: 'patients/1/HRA.pdf',
bucket: 'bucket_name',
urls: {
view: 'https://www.bucket_name.s3.amazonaws.com/patients/1/view/HRA.pdf',
download: 'https://www.bucket_name.s3.amazonaws.com/patients/1/download/HRA.pdf',
},
},
},
});

cy
.get('.patient-flow__list')
.find('.table-list__item .fa-paperclip')
.should('exist');

cy.sendWs({
category: 'ResourceDeleted',
resource: {
Expand Down