Skip to content

Commit

Permalink
Add save Method for Parse.Config (#684)
Browse files Browse the repository at this point in the history
* Add Save Method

* Add tests and add fix CoreManager tests for the new method

* Update to the latest JS standard + new save() strategy + Add save to CoreManager

* Remove useMasterKey
  • Loading branch information
Moumouls authored and flovilmart committed Nov 14, 2018
1 parent e0c51ab commit 3162d52
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/CoreManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ module.exports = {
},

setConfigController(controller: ConfigController) {
requireMethods('ConfigController', ['current', 'get'], controller);
requireMethods('ConfigController', ['current', 'get', 'save'], controller);
config['ConfigController'] = controller;
},

Expand Down
65 changes: 53 additions & 12 deletions src/ParseConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import CoreManager from './CoreManager';
import decode from './decode';
import encode from './encode';
import escape from './escape';
import ParseError from './ParseError';
import Storage from './Storage';
Expand Down Expand Up @@ -66,7 +67,7 @@ class ParseConfig {
* exists, else an empty Parse.Config.
*/
static current() {
var controller = CoreManager.getConfigController();
const controller = CoreManager.getConfigController();
return controller.current();
}

Expand All @@ -77,9 +78,25 @@ class ParseConfig {
* configuration object when the get completes.
*/
static get() {
var controller = CoreManager.getConfigController();
const controller = CoreManager.getConfigController();
return controller.get();
}

/**
* Save value keys to the server.
* @static
* @return {Promise} A promise that is resolved with a newly-created
* configuration object or with the current with the update.
*/
static save(attrs) {
const controller = CoreManager.getConfigController();
//To avoid a mismatch with the local and the cloud config we get a new version
return controller.save(attrs).then(() => {
return controller.get();
},(error) => {
return Promise.reject(error);
});
}
}

var currentConfig = null;
Expand All @@ -88,7 +105,7 @@ var CURRENT_CONFIG_KEY = 'currentConfig';

function decodePayload(data) {
try {
var json = JSON.parse(data);
const json = JSON.parse(data);
if (json && typeof json === 'object') {
return decode(json);
}
Expand All @@ -103,14 +120,14 @@ var DefaultController = {
return currentConfig;
}

var config = new ParseConfig();
var storagePath = Storage.generatePath(CURRENT_CONFIG_KEY);
var configData;
const config = new ParseConfig();
const storagePath = Storage.generatePath(CURRENT_CONFIG_KEY);
let configData;
if (!Storage.async()) {
configData = Storage.getItem(storagePath);

if (configData) {
var attributes = decodePayload(configData);
const attributes = decodePayload(configData);
if (attributes) {
config.attributes = attributes;
currentConfig = config;
Expand All @@ -121,7 +138,7 @@ var DefaultController = {
// Return a promise for async storage controllers
return Storage.getItemAsync(storagePath).then((configData) => {
if (configData) {
var attributes = decodePayload(configData);
const attributes = decodePayload(configData);
if (attributes) {
config.attributes = attributes;
currentConfig = config;
Expand All @@ -132,22 +149,22 @@ var DefaultController = {
},

get() {
var RESTController = CoreManager.getRESTController();
const RESTController = CoreManager.getRESTController();

return RESTController.request(
'GET', 'config', {}, {}
).then((response) => {
if (!response || !response.params) {
var error = new ParseError(
const error = new ParseError(
ParseError.INVALID_JSON,
'Config JSON response invalid.'
);
return Promise.reject(error);
}

var config = new ParseConfig();
const config = new ParseConfig();
config.attributes = {};
for (var attr in response.params) {
for (const attr in response.params) {
config.attributes[attr] = decode(response.params[attr]);
}
currentConfig = config;
Expand All @@ -158,6 +175,30 @@ var DefaultController = {
return config;
});
});
},

save(attrs) {
var RESTController = CoreManager.getRESTController();
const encodedAttrs = {};
for(const key in attrs){
encodedAttrs[key] = encode(attrs[key])
}
return RESTController.request(
'PUT',
'config',
{ params: encodedAttrs },
{ useMasterKey: true }
).then(response => {
if(response && response.result){
return Promise.resolve()
} else {
const error = new ParseError(
ParseError.INTERNAL_SERVER_ERROR,
'Error occured updating Config.'
);
return Promise.reject(error)
}
})
}
};

Expand Down
6 changes: 4 additions & 2 deletions src/__tests__/CoreManager-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,16 @@ describe('CoreManager', () => {

expect(CoreManager.setConfigController.bind(null, {
current: function() {},
get: function() {}
get: function() {},
save : function() {}
})).not.toThrow();
});

it('can set and get ConfigController', () => {
const controller = {
current: function() {},
get: function() {}
get: function() {},
save : function() {}
};

CoreManager.setConfigController(controller);
Expand Down
43 changes: 42 additions & 1 deletion src/__tests__/ParseConfig-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,48 @@ describe('ParseConfig', () => {
});
});

it('can save a config object with masterkey', (done) => {
//Load a request that match the get() & save() request
CoreManager.setRESTController({
request() {
return Promise.resolve({
params : {
str : 'hello2',
num : 46
},
result: true
});
},
ajax() {}
});
ParseConfig.save({str: 'hello2','num':46}).then((config) => {
expect(config.get('str')).toBe('hello2');
expect(config.get('num')).toBe(46);
const path = Storage.generatePath('currentConfig');
expect(JSON.parse(Storage.getItem(path))).toEqual({
str: 'hello2',
num: 46
});
done();
});
});

it('rejects save on invalid response', (done) => {
CoreManager.setRESTController({
request() {
return Promise.resolve({result: false});
},
ajax() {}
});
ParseConfig.save({str: 'hello2','num':46}).then((config) => {
expect(config).toBe(1)
done();
},(error) => {
expect(error.code).toBe(1)
done();
});
});

it('rejects the promise when an invalid payload comes back', (done) => {
CoreManager.setRESTController({
request() {
Expand All @@ -110,7 +152,6 @@ describe('ParseConfig', () => {
ParseConfig.get().then(null, (error) => {
expect(error.code).toBe(107);
expect(error.message).toBe('Config JSON response invalid.');

done();
});
});
Expand Down

0 comments on commit 3162d52

Please sign in to comment.