diff --git a/app/routes/submissions/new.js b/app/routes/submissions/new.js index 53e0eba3..b4908bff 100644 --- a/app/routes/submissions/new.js +++ b/app/routes/submissions/new.js @@ -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') @@ -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'); diff --git a/app/services/submission-handler.js b/app/services/submission-handler.js index 85ad470e..5d538a79 100644 --- a/app/services/submission-handler.js +++ b/app/services/submission-handler.js @@ -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. @@ -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(); } } diff --git a/app/util/paginated-query.js b/app/util/paginated-query.js index bf18c752..56268ad3 100644 --- a/app/util/paginated-query.js +++ b/app/util/paginated-query.js @@ -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 * @@ -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 ''; diff --git a/tests/unit/services/submission-handler-test.js b/tests/unit/services/submission-handler-test.js index a3ac6bc9..d5b5265f 100644 --- a/tests/unit/services/submission-handler-test.js +++ b/tests/unit/services/submission-handler-test.js @@ -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(); }