@journeyapps-solutions/cc-util-sync-collective 1.0.1 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.js CHANGED
@@ -30,14 +30,22 @@ class SyncCollective {
30
30
  */
31
31
  constructor(options) {
32
32
 
33
- //
34
33
  if (typeof options === 'object') {
35
- assert(options, `${NAME} - options not provided`);
36
34
  assert(options.collective, `${NAME} - options.collective not provided.`);
37
35
  assert(options.url, `${NAME} - options.url not provided.`);
38
- assert(options.api_user, `${NAME} - options.api_user not provided.`);
39
- assert(options.api_pass, `${NAME} - options.api_pass not provided.`);
40
- assert(options.endpoint_api_user, `${NAME} - options.endpoint_api_user not provided.`);
36
+
37
+ //Local variables used in getting the record via fetch
38
+ assert(options.credentials, `${NAME} - options.credentials not provided.`);
39
+ assert(options.credentials.local, `${NAME} - options.credentials.local not provided.`);
40
+ assert(options.credentials.local.api_user, `${NAME} - options.credentials.local.api_user not provided.`);
41
+ assert(options.credentials.local.api_pass, `${NAME} - options.credentials.local.api_pass not provided.`);
42
+
43
+ //Remove Variables for sending the update
44
+ assert(options.credentials.remote, `${NAME} - options.credentials.remote not provided.`);
45
+ assert(options.credentials.remote.api_user, `${NAME} - options.credentials.remote.api_user not provided.`);
46
+ assert(options.credentials.remote.api_pass, `${NAME} - options.credentials.remote.api_pass not provided.`);
47
+
48
+ assert(options.endpoint.api_user, `${NAME} - options.endpoint.api_user not provided.`);
41
49
  }
42
50
 
43
51
  this.config = options;
@@ -87,7 +95,6 @@ class SyncCollective {
87
95
  _object.synced = true;
88
96
 
89
97
  let _collective = yield options.endpoint.collective();
90
- console.log("Collective", _collective);
91
98
 
92
99
  let _url = SyncUtilMaster.buildUrl(_object, options.endpoint.url, _collective.name);
93
100
  let _header = SyncUtilMaster.buildHeader(options.endpoint.api_user, options.endpoint.api_pass);
@@ -95,7 +102,6 @@ class SyncCollective {
95
102
 
96
103
  console.log(`${NAME} - Syncing Object [id:${options.objectToSync.id}]`);
97
104
  console.log(`${NAME} - URL - ` + _url);
98
- console.log(`${NAME} - HEADER - ` + JSON.stringify(_header));
99
105
  console.log(`${NAME} - METHOD - ` + options.operation);
100
106
  console.log(`${NAME} - BODY - ` + JSON.stringify(_body));
101
107
 
@@ -131,17 +137,20 @@ class SyncCollective {
131
137
  assert(typeof options === 'object', `${NAME} - options not provided`);
132
138
  assert(options.objectToSync, `${NAME} - options.objectToSync not provided.`);
133
139
 
134
- let _url = SyncUtilSlave.buildUrl(options.objectToSync, _this2.config.url, _this2.config.collective);
135
- let _header = SyncUtilSlave.buildHeader(_this2.config.api_user, _this2.config.api_pass);
136
- let _body = SyncUtilSlave.buildJsonBody(options.objectToSync, _this2.config.collective);
140
+ //We do a fetch due to have enumerable ID's
141
+ let _fetchUrl = SyncUtilSlave.buildFetchUrl(_data, _this2.config.url, options.objectToSync.type.name);
142
+ let _data = yield _this2._fetchRecord(_fetchUrl);
143
+
144
+ let _url = SyncUtilSlave.buildUrl(_data, _this2.config.url, _this2.config.collective);
145
+ let _header = SyncUtilSlave.buildHeader(_this2.config.credentials.remote.api_user, _this2.config.credentials.remote.api_pass);
146
+ let _body = SyncUtilSlave.buildJsonBody(_data, _this2.config.collective);
137
147
 
138
148
  //We need to set the api_user
139
- let _tableName = SyncUtilSlave.buildTableName(options.objectToSync, _this2.config.collective);
140
- _body[_tableName].api_user = _this2.config.endpoint_api_user;
149
+ let _tableName = SyncUtilSlave.buildTableName(_data, _this2.config.collective);
150
+ _body[_tableName].api_user = _this2.config.endpoint.api_user;
141
151
 
142
- console.log(`${NAME} - Syncing Object [id:${options.objectToSync.id}]`);
152
+ console.log(`${NAME} - Syncing Object [id:${_data.id}]`);
143
153
  console.log(`${NAME} - URL - ` + _url);
144
- console.log(`${NAME} - HEADER - ` + JSON.stringify(_header));
145
154
  console.log(`${NAME} - BODY - ` + JSON.stringify(_body));
146
155
 
147
156
  let _response = yield _this2._sync(_url, _this2.operations.create, _header, _body);
@@ -188,7 +197,45 @@ class SyncCollective {
188
197
 
189
198
  //Check the response, and throw the appriopate error if required.
190
199
  if (_response.status === 401 || _response.status == 403) {
191
- throw new Error("User not authenticated");
200
+ throw new Error("Remote User not authenticated");
201
+ } else if (!_response.ok) {
202
+ throw _response;
203
+ }
204
+
205
+ return _response;
206
+ } catch (error) {
207
+ console.error(`${NAME} - ${error}`);
208
+ throw error;
209
+ }
210
+ })();
211
+ }
212
+
213
+ /**
214
+ * Fetch the local object to use for sending.
215
+ * Currently there is a an shotcomming on CC DB Module which stop us from enumerating through the ID's. Thus we need to do a normal JSON fetch.
216
+ *
217
+ * @param {[type]} data Object used
218
+ */
219
+ _fetchRecord(url) {
220
+ var _this4 = this;
221
+
222
+ return _asyncToGenerator(function* () {
223
+ try {
224
+
225
+ let _header = SyncUtilSlave.buildHeader(_this4.config.credentials.local.api_user, _this4.config.credentials.local.api_pass);
226
+
227
+ console.log(`GET ${url}`);
228
+
229
+ let _response = yield fetch(`${url}`, {
230
+ headers: header,
231
+ method: "GET"
232
+ });
233
+
234
+ console.log(`${NAME} - RESPONSE [ok=${_response.ok};status=${_response.status}]`);
235
+
236
+ //Check the response, and throw the appriopate error if required.
237
+ if (_response.status === 401 || _response.status == 403) {
238
+ throw new Error("Local User not authenticated");
192
239
  } else if (!_response.ok) {
193
240
  throw _response;
194
241
  }
package/lib/sync-util.js CHANGED
@@ -7,14 +7,20 @@ class SyncUtil {
7
7
  /**
8
8
  * Build the url of the endpoint
9
9
  *
10
- * @param object The Object that should be synced
11
- * @param endpoint The Endpoint that the data is being synced to
12
10
  * @return String of the url for the endpoint
13
11
  */
14
12
  buildUrl(object, url, collective) {
15
13
  return `${url}/objects/` + this.buildTableName(object, collective) + `/${object.id}.json`;
16
14
  }
17
15
 
16
+ /**
17
+ * Builds a fetch URL based on the model
18
+ * @return String of the url for the endpoint
19
+ */
20
+ buildFetchUrl(object, url, model) {
21
+ return `${url}/objects/${model}/${object.id}.json`;
22
+ }
23
+
18
24
  /**
19
25
  * Build the object to be sent to the endpoint in the correct format for
20
26
  * the JSON to appear correct according to the V4 API
@@ -80,7 +86,8 @@ class SyncUtil {
80
86
  */
81
87
  buildHeader(api_user, api_pass) {
82
88
  let _header = {};
83
- _header['Authorization'] = 'Basic ' + base64.encode(api_user + ":" + api_pass), _header['Content-Type'] = 'application/json';
89
+ _header['Authorization'] = 'Basic ' + base64.encode(api_user + ":" + api_pass);
90
+ _header['Content-Type'] = 'application/json';
84
91
  return _header;
85
92
  }
86
93
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@journeyapps-solutions/cc-util-sync-collective",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "description": "Sync Data between projects. Either Directly, or via Slave & Master Configuration",
5
5
  "main": "./index.js",
6
6
  "scripts": {