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

UI shouldn't send empty requests to metadata schema service #1251

Merged
merged 2 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 10 additions & 1 deletion app/services/metadata-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ export default class MetadataSchemaService extends Service {
* @returns {array} list of schemas relevant to the given repositories
*/
async getMetadataSchemas(repositories) {
if (!repositories) {
return [];
}

const areObjects = repositories.map((repos) => typeof repos).includes('object');
if (areObjects) {
// If we've gotten repository objects, map them to their IDs
Expand All @@ -71,6 +75,10 @@ export default class MetadataSchemaService extends Service {
},
};

if (!Array.isArray(repositories) || repositories.length === 0) {
return [];
}

try {
let response = await this._fetchSchemas(this.url(repositories, true), options);
if (response.status >= 400) {
Expand Down Expand Up @@ -407,7 +415,8 @@ export default class MetadataSchemaService extends Service {
'volume',
];

const schemas = await this.getMetadataSchemas(await submission.repositories);
const repos = await submission.repositories.toArray();
const schemas = await this.getMetadataSchemas(repos);
const titleMap = this.getFieldTitleMap(schemas);
const metadata = JSON.parse(submission.metadata);

Expand Down
2 changes: 1 addition & 1 deletion tests/integration/components/workflow-metadata-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module('Integration | Component | workflow-metadata', (hooks) => {
setupMirage(hooks);

hooks.beforeEach(function () {
const repositories = A();
const repositories = A([{ id: 0 }]);
const journal = EmberObject.create({
issns: [],
});
Expand Down
11 changes: 10 additions & 1 deletion tests/unit/services/metadata-schema-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ module('Unit | Service | metadata-schema', (hooks) => {
test('Should format complex metadata', async function (assert) {
const service = this.owner.lookup('service:metadata-schema');
const submission = EmberObject.create({
repositories: [],
repositories: [{ id: 0 }],
metadata: JSON.stringify({
issns: [{ issn: '123-moo' }, { issn: 'moo-321', pubType: 'Print' }],
}),
Expand Down Expand Up @@ -270,4 +270,13 @@ module('Unit | Service | metadata-schema', (hooks) => {

assert.deepEqual(service.mergeBlobs(b1, b2, list), expected);
});

test('No backend request if no repositories are passed', async function (assert) {
const service = this.owner.lookup('service:metadata-schema');
const serviceFetchFake = sinon.replace(service, '_fetchSchemas', sinon.fake());

service.getMetadataSchemas([]);

assert.ok(serviceFetchFake.notCalled, '_fetchSchemas should not have been called');
});
});
Loading