Skip to content

Commit

Permalink
Fix search queries, consolidate file queries
Browse files Browse the repository at this point in the history
  • Loading branch information
jabrah committed Feb 27, 2024
1 parent 66a7f2d commit 6445cf0
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 14 deletions.
9 changes: 2 additions & 7 deletions app/routes/submissions/new.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import CheckSessionRoute from '../check-session-route';
import { inject as service } from '@ember/service';
import { set } from '@ember/object';
import { hash } from 'rsvp';
import { fileForSubmissionQuery } from '../../util/paginated-query';

export default class NewRoute extends CheckSessionRoute {
@service('workflow')
Expand Down Expand Up @@ -64,13 +65,7 @@ export default class NewRoute extends CheckSessionRoute {
sort: '+performedDate',
});

files = this.store
.query('file', {
filter: {
file: `submission.id==${newSubmission.get('id')}`,
},
})
.then((files) => [...files.toArray()]);
files = this.store.query('file', fileForSubmissionQuery(newSubmission.id)).then((files) => [...files.toArray()]);

// Also seed workflow.doiInfo with metadata from the Submission
const metadata = newSubmission.get('metadata');
Expand Down
14 changes: 8 additions & 6 deletions app/services/submission-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import ENV from 'pass-ui/config/environment';
import { task } from 'ember-concurrency-decorators';
import { get } from '@ember/object';
import SubmissionModel from '../models/submission';
import { fileForSubmissionQuery, submissionsWithPublicationQuery } from '../util/paginated-query';

/**
* Service to manage submissions.
Expand Down Expand Up @@ -255,19 +256,20 @@ export default class SubmissionHandlerService extends Service {
}

// Get submissions for this file
const files = await this.store.query('file', { filter: { submission: submission.id } });
const files = await this.store.query('file', fileForSubmissionQuery(submission.id));
await Promise.all(files.map((file) => file.destroyRecord()));

const publication = await submission.publication;

// Search for Submissions that reference this publication
const subsWithThisPublication = await this.store.query('submission', { filter: { publication: publication.id } });
if (subsWithThisPublication.length === 1) {
const submissionId = submission.id;
submission.deleteRecord();
await submission.save();

const subsWithThisPublication = await this.store.query('submission', submissionsWithPublicationQuery(publication));
if (subsWithThisPublication.length === 0) {
publication.deleteRecord();
await publication.save();
}

submission.deleteRecord();
return submission.save();
}
}
20 changes: 20 additions & 0 deletions app/util/paginated-query.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/**
* Centralizing JSON-API queries are formatted using RSQL, which can be a bit awkward to write.
*/

/**
* Paginated query for the submissions/index route
*
Expand Down Expand Up @@ -105,6 +109,22 @@ export function grantsIndexSubmissionQuery(user) {
};
}

export function fileForSubmissionQuery(submissionId) {
return {
filter: {
file: `submission.id==${submissionId}`,
},
};
}

export function submissionsWithPublicationQuery(publication) {
return {
filter: {
submission: `publication.id==${publication.id}`,
},
};
}

function filter(value, ...props) {
if (!value) {
return '';
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/services/submission-handler-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ module('Unit | Service | submission-handler', (hooks) => {
// Get files for the submission
return Promise.resolve(files);
case 'submission':
return Promise.resolve([submission]);
return Promise.resolve([]);
default:
return Promise.reject();
}
Expand Down

0 comments on commit 6445cf0

Please sign in to comment.