2021-02-24 08:43:35 +01:00
|
|
|
import { ajax } from "discourse/lib/ajax";
|
2020-03-21 18:30:11 +01:00
|
|
|
import EmberObject from "@ember/object";
|
2021-02-24 08:43:35 +01:00
|
|
|
import { buildProperties, present, mapped } from "../lib/wizard-json";
|
|
|
|
import { listProperties, camelCase, snakeCase } from "../lib/wizard";
|
|
|
|
import wizardSchema from "../lib/wizard-schema";
|
2020-04-05 03:37:09 +02:00
|
|
|
import { Promise } from "rsvp";
|
2021-02-24 08:43:35 +01:00
|
|
|
import { popupAjaxError } from "discourse/lib/ajax-error";
|
2017-11-01 05:21:14 +01:00
|
|
|
|
2020-03-21 18:30:11 +01:00
|
|
|
const CustomWizard = EmberObject.extend({
|
2020-04-13 14:17:22 +02:00
|
|
|
save(opts) {
|
2021-02-24 08:43:35 +01:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
let wizard = this.buildJson(this, "wizard");
|
|
|
|
|
2020-04-13 14:17:22 +02:00
|
|
|
if (wizard.error) {
|
|
|
|
reject(wizard);
|
2017-10-30 07:24:51 +01:00
|
|
|
}
|
2021-02-24 08:43:35 +01:00
|
|
|
|
2020-04-13 14:17:22 +02:00
|
|
|
let data = {
|
2021-02-24 08:43:35 +01:00
|
|
|
wizard,
|
2020-04-13 14:17:22 +02:00
|
|
|
};
|
2021-02-24 08:43:35 +01:00
|
|
|
|
2020-04-13 14:17:22 +02:00
|
|
|
if (opts.create) {
|
|
|
|
data.create = true;
|
|
|
|
}
|
2021-02-24 08:43:35 +01:00
|
|
|
|
2020-04-13 14:17:22 +02:00
|
|
|
ajax(`/admin/wizards/wizard/${wizard.id}`, {
|
2021-02-24 08:43:35 +01:00
|
|
|
type: "PUT",
|
2020-04-13 14:17:22 +02:00
|
|
|
contentType: "application/json",
|
2021-02-24 08:43:35 +01:00
|
|
|
data: JSON.stringify(data),
|
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 09:59:54 +02:00
|
|
|
buildJson(object, type, result = {}) {
|
2020-04-10 09:57:49 +02:00
|
|
|
let objectType = object.type || null;
|
2021-02-24 08:43:35 +01:00
|
|
|
|
2020-04-16 04:04:27 +02:00
|
|
|
if (wizardSchema[type].types) {
|
2020-04-10 09:57:49 +02:00
|
|
|
if (!objectType) {
|
2020-04-08 09:59:54 +02:00
|
|
|
result.error = {
|
2021-02-24 08:43:35 +01:00
|
|
|
type: "required",
|
|
|
|
params: { type, property: "type" },
|
|
|
|
};
|
2020-04-08 09:59:54 +02:00
|
|
|
return result;
|
2020-04-08 04:52:07 +02:00
|
|
|
}
|
2020-04-08 09:59:54 +02:00
|
|
|
}
|
2021-02-24 08:43:35 +01:00
|
|
|
|
2020-04-20 11:41:13 +02:00
|
|
|
for (let property of listProperties(type, { objectType })) {
|
2020-04-08 09:59:54 +02:00
|
|
|
let value = object.get(property);
|
2021-02-24 08:43:35 +01:00
|
|
|
|
2020-04-11 08:22:12 +02:00
|
|
|
result = this.validateValue(property, value, object, type, result);
|
2021-02-24 08:43:35 +01:00
|
|
|
|
2020-04-10 09:57:49 +02:00
|
|
|
if (result.error) {
|
|
|
|
break;
|
2020-04-08 04:52:07 +02:00
|
|
|
}
|
2021-02-24 08:43:35 +01:00
|
|
|
|
2020-04-10 09:57:49 +02:00
|
|
|
if (mapped(property, type)) {
|
|
|
|
value = this.buildMappedJson(value);
|
|
|
|
}
|
2021-02-24 08:43:35 +01:00
|
|
|
|
2020-04-10 09:57:49 +02:00
|
|
|
if (value !== undefined && value !== null) {
|
|
|
|
result[property] = value;
|
|
|
|
}
|
2021-02-24 08:43:35 +01:00
|
|
|
}
|
|
|
|
|
2020-04-10 09:57:49 +02:00
|
|
|
if (!result.error) {
|
2021-02-24 08:43:35 +01:00
|
|
|
for (let arrayObjectType of Object.keys(
|
|
|
|
wizardSchema[type].objectArrays
|
|
|
|
)) {
|
2020-04-16 04:04:27 +02:00
|
|
|
let arraySchema = wizardSchema[type].objectArrays[arrayObjectType];
|
2020-04-10 09:57:49 +02:00
|
|
|
let objectArray = object.get(arraySchema.property);
|
2021-02-24 08:43:35 +01:00
|
|
|
|
2020-04-10 09:57:49 +02:00
|
|
|
if (arraySchema.required && !present(objectArray)) {
|
2020-04-08 09:59:54 +02:00
|
|
|
result.error = {
|
2021-02-24 08:43:35 +01:00
|
|
|
type: "required",
|
|
|
|
params: { type, property: arraySchema.property },
|
|
|
|
};
|
2020-04-10 09:57:49 +02:00
|
|
|
break;
|
2020-04-08 04:52:07 +02:00
|
|
|
}
|
2020-04-10 09:57:49 +02:00
|
|
|
|
|
|
|
result[arraySchema.property] = [];
|
2021-02-24 08:43:35 +01:00
|
|
|
|
2020-04-10 09:57:49 +02:00
|
|
|
for (let item of objectArray) {
|
|
|
|
let itemProps = this.buildJson(item, arrayObjectType);
|
2021-02-24 08:43:35 +01:00
|
|
|
|
2020-04-10 09:57:49 +02:00
|
|
|
if (itemProps.error) {
|
|
|
|
result.error = itemProps.error;
|
2020-04-08 09:59:54 +02:00
|
|
|
break;
|
|
|
|
} else {
|
2020-04-10 09:57:49 +02:00
|
|
|
result[arraySchema.property].push(itemProps);
|
2020-04-08 09:59:54 +02:00
|
|
|
}
|
|
|
|
}
|
2021-02-24 08:43:35 +01:00
|
|
|
}
|
2020-04-10 09:57:49 +02:00
|
|
|
}
|
2021-02-24 08:43:35 +01:00
|
|
|
|
2020-04-08 04:52:07 +02:00
|
|
|
return result;
|
2020-04-08 09:59:54 +02:00
|
|
|
},
|
2021-02-24 08:43:35 +01:00
|
|
|
|
2020-04-11 08:22:12 +02:00
|
|
|
validateValue(property, value, object, type, result) {
|
2020-04-16 04:04:27 +02:00
|
|
|
if (wizardSchema[type].required.indexOf(property) > -1 && !value) {
|
2020-04-10 09:57:49 +02:00
|
|
|
result.error = {
|
2021-02-24 08:43:35 +01:00
|
|
|
type: "required",
|
|
|
|
params: { type, property },
|
|
|
|
};
|
2020-04-10 09:57:49 +02:00
|
|
|
}
|
2021-02-24 08:43:35 +01:00
|
|
|
|
2020-04-16 04:04:27 +02:00
|
|
|
let dependent = wizardSchema[type].dependent[property];
|
2020-04-10 09:57:49 +02:00
|
|
|
if (dependent && value && !object[dependent]) {
|
|
|
|
result.error = {
|
2021-02-24 08:43:35 +01:00
|
|
|
type: "dependent",
|
|
|
|
params: { property, dependent },
|
|
|
|
};
|
2020-04-10 09:57:49 +02:00
|
|
|
}
|
2021-02-24 08:43:35 +01:00
|
|
|
|
|
|
|
if (property === "api_body") {
|
2020-04-10 09:57:49 +02:00
|
|
|
try {
|
|
|
|
value = JSON.parse(value);
|
|
|
|
} catch (e) {
|
|
|
|
result.error = {
|
2021-02-24 08:43:35 +01:00
|
|
|
type: "invalid",
|
|
|
|
params: { type, property },
|
|
|
|
};
|
2020-04-10 09:57:49 +02:00
|
|
|
}
|
|
|
|
}
|
2021-02-24 08:43:35 +01:00
|
|
|
|
2020-04-10 09:57:49 +02:00
|
|
|
return result;
|
|
|
|
},
|
2020-04-08 04:52:07 +02:00
|
|
|
|
|
|
|
buildMappedJson(inputs) {
|
|
|
|
if (!inputs || !inputs.length) return false;
|
2021-02-24 08:43:35 +01:00
|
|
|
|
2020-04-08 04:52:07 +02:00
|
|
|
let result = [];
|
2021-02-24 08:43:35 +01:00
|
|
|
|
|
|
|
inputs.forEach((inpt) => {
|
2020-04-08 04:52:07 +02:00
|
|
|
let input = {
|
|
|
|
type: inpt.type,
|
|
|
|
};
|
2021-02-24 08:43:35 +01:00
|
|
|
|
2020-04-11 08:22:12 +02:00
|
|
|
if (inpt.connector) {
|
|
|
|
input.connector = inpt.connector;
|
|
|
|
}
|
2021-02-24 08:43:35 +01:00
|
|
|
|
2020-04-08 04:52:07 +02:00
|
|
|
if (present(inpt.output)) {
|
|
|
|
input.output = inpt.output;
|
|
|
|
input.output_type = snakeCase(inpt.output_type);
|
|
|
|
input.output_connector = inpt.output_connector;
|
|
|
|
}
|
2021-02-24 08:43:35 +01:00
|
|
|
|
2020-04-08 04:52:07 +02:00
|
|
|
if (present(inpt.pairs)) {
|
|
|
|
input.pairs = [];
|
2021-02-24 08:43:35 +01:00
|
|
|
|
|
|
|
inpt.pairs.forEach((pr) => {
|
2020-04-08 04:52:07 +02:00
|
|
|
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),
|
2021-02-24 08:43:35 +01:00
|
|
|
connector: pr.connector,
|
|
|
|
};
|
|
|
|
|
2020-04-08 04:52:07 +02:00
|
|
|
input.pairs.push(pairParams);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2021-02-24 08:43:35 +01:00
|
|
|
|
|
|
|
if (
|
|
|
|
(input.type === "assignment" && present(input.output)) ||
|
|
|
|
present(input.pairs)
|
|
|
|
) {
|
2020-04-08 04:52:07 +02:00
|
|
|
result.push(input);
|
|
|
|
}
|
|
|
|
});
|
2021-02-24 08:43:35 +01:00
|
|
|
|
2020-04-08 04:52:07 +02:00
|
|
|
if (!result.length) {
|
|
|
|
result = false;
|
|
|
|
}
|
2021-02-24 08:43:35 +01:00
|
|
|
|
2020-04-08 04:52:07 +02:00
|
|
|
return result;
|
|
|
|
},
|
|
|
|
|
2017-09-24 05:01:18 +02:00
|
|
|
remove() {
|
2020-04-13 14:17:22 +02:00
|
|
|
return ajax(`/admin/wizards/wizard/${this.id}`, {
|
2021-02-24 08:43:35 +01:00
|
|
|
type: "DELETE",
|
|
|
|
})
|
|
|
|
.then(() => this.destroy())
|
|
|
|
.catch(popupAjaxError);
|
|
|
|
},
|
2017-09-23 04:34:07 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
CustomWizard.reopenClass({
|
2017-10-22 05:37:58 +02:00
|
|
|
all() {
|
2020-04-13 14:17:22 +02:00
|
|
|
return ajax("/admin/wizards/wizard", {
|
2021-02-24 08:43:35 +01:00
|
|
|
type: "GET",
|
|
|
|
})
|
|
|
|
.then((result) => {
|
|
|
|
return result.wizard_list;
|
|
|
|
})
|
|
|
|
.catch(popupAjaxError);
|
2017-09-23 04:34:07 +02:00
|
|
|
},
|
|
|
|
|
2017-10-22 05:37:58 +02:00
|
|
|
submissions(wizardId) {
|
|
|
|
return ajax(`/admin/wizards/submissions/${wizardId}`, {
|
2021-02-24 08:43:35 +01:00
|
|
|
type: "GET",
|
2020-11-09 04:32:36 +01:00
|
|
|
}).catch(popupAjaxError);
|
2017-10-05 02:36:46 +02:00
|
|
|
},
|
|
|
|
|
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;
|
2021-02-24 08:43:35 +01:00
|
|
|
},
|
2017-09-23 04:34:07 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
export default CustomWizard;
|