2017-09-29 13:27:03 +02:00
|
|
|
import { ajax } from 'discourse/lib/ajax';
|
2020-03-21 18:30:11 +01:00
|
|
|
import EmberObject from "@ember/object";
|
2020-04-08 04:52:07 +02:00
|
|
|
import { buildJson, buildProperties, present } from '../lib/wizard-json';
|
|
|
|
import { properties, arrays, camelCase, snakeCase } from '../lib/wizard';
|
2020-04-05 03:37:09 +02:00
|
|
|
import { Promise } from "rsvp";
|
2017-11-01 05:21:14 +01:00
|
|
|
|
2020-04-08 04:52:07 +02:00
|
|
|
const jsonStrings = ['api_body'];
|
|
|
|
const required = ['id', 'steps', 'type'];
|
|
|
|
const dependent = { after_time: 'after_time_scheduled' }
|
|
|
|
|
2020-03-21 18:30:11 +01:00
|
|
|
const CustomWizard = EmberObject.extend({
|
2017-10-13 15:02:34 +02:00
|
|
|
save() {
|
2020-04-05 03:37:09 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
2020-04-08 04:52:07 +02:00
|
|
|
let json = this.buildJson(this, 'wizard');
|
2020-04-01 12:58:30 +02:00
|
|
|
|
2020-04-08 04:52:07 +02:00
|
|
|
if (json.error) {
|
|
|
|
reject({ eror: json.error });
|
2017-10-30 07:24:51 +01:00
|
|
|
}
|
2020-04-01 12:58:30 +02:00
|
|
|
|
2017-10-13 15:02:34 +02:00
|
|
|
ajax("/admin/wizards/custom/save", {
|
|
|
|
type: 'PUT',
|
|
|
|
data: {
|
2020-04-01 12:58:30 +02:00
|
|
|
wizard: JSON.stringify(wizardJson)
|
2017-10-13 15:02:34 +02:00
|
|
|
}
|
2017-10-30 07:24:51 +01:00
|
|
|
}).then((result) => {
|
|
|
|
if (result.error) {
|
|
|
|
reject(result);
|
|
|
|
} else {
|
|
|
|
resolve(result);
|
|
|
|
}
|
|
|
|
});
|
2017-10-13 15:02:34 +02:00
|
|
|
});
|
2017-10-07 04:27:38 +02:00
|
|
|
},
|
2017-09-24 05:01:18 +02:00
|
|
|
|
2020-04-08 04:52:07 +02:00
|
|
|
buildJson(object, type, result = {}) {
|
|
|
|
for (let property of properties[type]) {
|
|
|
|
let value = object.get(property);
|
|
|
|
|
|
|
|
if (objectArrays[type]) {
|
|
|
|
result[property] = [];
|
|
|
|
|
|
|
|
for (let obj of value) {
|
|
|
|
let obj = this.buildJson(value, property, result);
|
|
|
|
|
|
|
|
if (obj.error) {
|
|
|
|
result.error = r.error;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
result[property].push(obj);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (required[property] && !value) {
|
|
|
|
result.error = 'required'
|
|
|
|
result.errorParams = { type, property };
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dependent[property] && !properties[type][dependent[property]]) {
|
|
|
|
result.error = 'dependent';
|
|
|
|
result.errorParams = {
|
|
|
|
dependentProperty: properties[type][dependent[property]],
|
|
|
|
property
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (jsonStrings[property]) {
|
|
|
|
try {
|
|
|
|
value = JSON.parse(value);
|
|
|
|
} catch (e) {
|
|
|
|
result.error = 'invalid';
|
|
|
|
result.errorParams = { property };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mapped(property, type)) {
|
|
|
|
value = this.buildMappedJson(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result.error) {
|
|
|
|
break;
|
|
|
|
} else if (value) {
|
|
|
|
result[property] = value;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
buildMappedJson(inputs) {
|
|
|
|
if (!inputs || !inputs.length) return false;
|
|
|
|
|
|
|
|
let result = [];
|
|
|
|
|
|
|
|
inputs.forEach(inpt => {
|
|
|
|
let input = {
|
|
|
|
type: inpt.type,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (present(inpt.output)) {
|
|
|
|
input.output = inpt.output;
|
|
|
|
input.output_type = snakeCase(inpt.output_type);
|
|
|
|
input.output_connector = inpt.output_connector;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (present(inpt.pairs)) {
|
|
|
|
input.pairs = [];
|
|
|
|
|
|
|
|
inpt.pairs.forEach(pr => {
|
|
|
|
if (present(pr.key) && present(pr.value)) {
|
|
|
|
|
|
|
|
let pairParams = {
|
|
|
|
index: pr.index,
|
|
|
|
key: pr.key,
|
|
|
|
key_type: snakeCase(pr.key_type),
|
|
|
|
value: pr.value,
|
|
|
|
value_type: snakeCase(pr.value_type),
|
|
|
|
connector: pr.connector
|
|
|
|
}
|
|
|
|
|
|
|
|
input.pairs.push(pairParams);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((input.type === 'assignment' && present(input.output)) ||
|
|
|
|
present(input.pairs)) {
|
|
|
|
|
|
|
|
result.push(input);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!result.length) {
|
|
|
|
result = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
|
2017-09-24 05:01:18 +02:00
|
|
|
remove() {
|
2017-09-29 13:27:03 +02:00
|
|
|
return ajax("/admin/wizards/custom/remove", {
|
2017-09-24 05:01:18 +02:00
|
|
|
type: 'DELETE',
|
|
|
|
data: {
|
|
|
|
id: this.get('id')
|
|
|
|
}
|
|
|
|
}).then(() => this.destroy());
|
2017-09-23 04:34:07 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
CustomWizard.reopenClass({
|
2017-10-22 05:37:58 +02:00
|
|
|
all() {
|
2017-09-29 13:27:03 +02:00
|
|
|
return ajax("/admin/wizards/custom/all", {
|
|
|
|
type: 'GET'
|
|
|
|
}).then(result => {
|
2020-04-01 14:16:26 +02:00
|
|
|
return result.wizards.map(wizard => {
|
|
|
|
return CustomWizard.create(wizard);
|
|
|
|
});
|
2017-09-23 04:34:07 +02:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2017-10-22 05:37:58 +02:00
|
|
|
submissions(wizardId) {
|
|
|
|
return ajax(`/admin/wizards/submissions/${wizardId}`, {
|
2017-10-05 02:36:46 +02:00
|
|
|
type: "GET"
|
|
|
|
}).then(result => {
|
|
|
|
return result.submissions;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2020-04-01 12:58:30 +02:00
|
|
|
create(wizardJson = {}) {
|
2017-09-29 13:27:03 +02:00
|
|
|
const wizard = this._super.apply(this);
|
2020-04-01 14:16:26 +02:00
|
|
|
wizard.setProperties(buildProperties(wizardJson));
|
2017-09-23 04:34:07 +02:00
|
|
|
return wizard;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
export default CustomWizard;
|