fix: 🐛 pass scope_id in scope payload rather than query string
This commit is contained in:
6 files changed
+45
-40
No files matched your search
@@ -1,5 +1,4 @@
|
||||
import ApplicationAdapter from './application';
|
||||
import { serializeIntoHash } from '@ember-data/adapter/-private';
|
||||
|
||||
export default class ScopeAdapter extends ApplicationAdapter {
|
||||
|
||||
@@ -11,27 +10,4 @@ export default class ScopeAdapter extends ApplicationAdapter {
|
||||
*/
|
||||
hasScopePrefix = false;
|
||||
|
||||
// =methods
|
||||
|
||||
/**
|
||||
* Identical to default method except that we pass a query parameter
|
||||
* indicating the parent scope.
|
||||
* @override
|
||||
* @param {Store} store
|
||||
* @param {Model} type
|
||||
* @param {Snapshot} snapshot
|
||||
* @return {Promise} promise
|
||||
*/
|
||||
createRecord(store, type, snapshot) {
|
||||
const parentScopeID = snapshot.attr('scope').attr('scope_id');
|
||||
let url =
|
||||
this.buildURL(type.modelName, null, snapshot, 'createRecord');
|
||||
// TODO this should be generalized into `buildURL`, but currently Ember Data
|
||||
// doesn't provide a way to manage query parameters. They are passed on
|
||||
// to the underlying ajax/fetch service.
|
||||
url = `${url}?scope_id=${parentScopeID}`;
|
||||
const data = serializeIntoHash(store, type, snapshot);
|
||||
return this.ajax(url, 'POST', { data });
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
import ApplicationSerializer from './application';
|
||||
|
||||
export default class ScopeSerializer extends ApplicationSerializer {
|
||||
|
||||
/**
|
||||
* Serialize the scope ID into scope_id.
|
||||
* @type {boolean}
|
||||
*/
|
||||
serializeScopeID = true;
|
||||
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
export { default } from 'api/serializers/scope';
|
||||
@@ -16,18 +16,4 @@ module('Unit | Adapter | scope', function(hooks) {
|
||||
assert.equal(prefix, `v1`);
|
||||
});
|
||||
|
||||
test('it generates correct createRecord URLs with a scope_id query parameter', function (assert) {
|
||||
assert.expect(1);
|
||||
this.server.post('/v1/scopes', (schema, { queryParams }) =>{
|
||||
assert.equal(queryParams.scope_id, 'o_1');
|
||||
});
|
||||
const store = this.owner.lookup('service:store');
|
||||
const scope = store.createRecord('scope', {
|
||||
scopeID: 'o_1'
|
||||
});
|
||||
const type = scope.constructor;
|
||||
const snapshot = scope._createSnapshot();
|
||||
const adapter = this.owner.lookup('adapter:scope');
|
||||
adapter.createRecord(store, type, snapshot);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,31 @@
|
||||
import { module, test } from 'qunit';
|
||||
import { setupTest } from 'ember-qunit';
|
||||
|
||||
module('Unit | Serializer | scope', function(hooks) {
|
||||
setupTest(hooks);
|
||||
|
||||
test('it serializes scope_id into payloads', function (assert) {
|
||||
assert.expect(2);
|
||||
const store = this.owner.lookup('service:store');
|
||||
const serializer = store.serializerFor('scope');
|
||||
serializer.serializeScopeID = true;
|
||||
const record = store.createRecord('scope', {
|
||||
name: 'My Scope',
|
||||
description: 'Foo',
|
||||
type: 'org',
|
||||
scope: {
|
||||
scope_id: 'global',
|
||||
type: 'global'
|
||||
}
|
||||
});
|
||||
const serializedRecord = record.serialize();
|
||||
assert.equal(serializer.serializeScopeID, true);
|
||||
assert.deepEqual(serializedRecord, {
|
||||
name: 'My Scope',
|
||||
description: 'Foo',
|
||||
type: 'org',
|
||||
scope_id: 'global'
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
@@ -26,12 +26,12 @@ export default function() {
|
||||
});
|
||||
return results;
|
||||
});
|
||||
this.post('/scopes', function ({ scopes }, request) {
|
||||
this.post('/scopes', function ({ scopes }) {
|
||||
// Parent scope comes through the payload via `scope_id`, but this needs
|
||||
// to be expanded to a scope object `scope: {id:..., type:...}` before
|
||||
// saving, which is the gist of this function.
|
||||
const attrs = this.normalizedRequestAttrs();
|
||||
const parentScopeAttrs = this.serialize(scopes.find(request.queryParams.scope_id));
|
||||
const parentScopeAttrs = this.serialize(scopes.find(attrs.scopeId));
|
||||
attrs.scope = parentScopeAttrs;
|
||||
return scopes.create(attrs);
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user