68 lines
1.5 KiB
JavaScript
Raw Normal View History

2020-07-06 15:25:19 -04:00
import getURL from "discourse-common/lib/get-url";
2018-10-10 17:26:23 +05:30
import RestModel from "discourse/models/rest";
export default class Query extends RestModel {
static updatePropertyNames = [
"name",
"description",
"sql",
"user_id",
"created_at",
"group_ids",
"last_run_at",
];
params = {};
constructor() {
super(...arguments);
this.param_info?.resetParams();
}
2015-06-30 19:51:38 -07:00
get downloadUrl() {
return getURL(`/admin/plugins/explorer/queries/${this.id}.json?export=1`);
}
get hasParams() {
return this.param_info.length;
}
2015-06-30 19:51:38 -07:00
2015-06-30 15:12:12 -07:00
resetParams() {
2015-07-02 09:19:30 -07:00
const newParams = {};
const oldParams = this.params;
this.param_info.forEach((pinfo) => {
2015-07-14 16:01:38 -07:00
const name = pinfo.identifier;
if (oldParams[pinfo.identifier]) {
2015-07-02 09:19:30 -07:00
newParams[name] = oldParams[name];
2018-10-10 17:26:23 +05:30
} else if (pinfo["default"] !== null) {
newParams[name] = pinfo["default"];
} else if (pinfo["type"] === "boolean") {
newParams[name] = "false";
} else if (pinfo["type"] === "user_id") {
newParams[name] = null;
} else if (pinfo["type"] === "user_list") {
newParams[name] = null;
2015-06-30 15:12:12 -07:00
} else {
2018-10-10 17:26:23 +05:30
newParams[name] = "";
2015-06-30 15:12:12 -07:00
}
});
this.params = newParams;
}
updateProperties() {
const props = this.getProperties(Query.updatePropertyNames);
if (this.destroyed) {
props.id = this.id;
2015-06-30 12:52:17 -07:00
}
return props;
}
createProperties() {
if (this.sql) {
// Importing
return this.updateProperties();
}
return this.getProperties("name");
}
}