2024-08-05 15:58:51 +08:00
|
|
|
import { computed } from "@ember/object";
|
2025-02-06 16:45:00 +00:00
|
|
|
import getURL from "discourse/lib/get-url";
|
2018-10-10 17:26:23 +05:30
|
|
|
import RestModel from "discourse/models/rest";
|
2015-06-25 14:53:03 -07:00
|
|
|
|
2023-02-07 12:44:38 -06:00
|
|
|
export default class Query extends RestModel {
|
|
|
|
|
static updatePropertyNames = [
|
|
|
|
|
"name",
|
|
|
|
|
"description",
|
|
|
|
|
"sql",
|
|
|
|
|
"user_id",
|
|
|
|
|
"created_at",
|
|
|
|
|
"group_ids",
|
|
|
|
|
"last_run_at",
|
|
|
|
|
];
|
2015-06-30 19:51:38 -07:00
|
|
|
|
2023-02-07 12:44:38 -06:00
|
|
|
params = {};
|
|
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
|
super(...arguments);
|
|
|
|
|
this.param_info?.resetParams();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get downloadUrl() {
|
|
|
|
|
return getURL(`/admin/plugins/explorer/queries/${this.id}.json?export=1`);
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-10 14:54:01 +11:00
|
|
|
@computed("param_info", "updating")
|
2023-02-07 12:44:38 -06:00
|
|
|
get hasParams() {
|
2024-08-20 09:42:50 +08:00
|
|
|
// When saving, we need to refresh the param-input component to clean up the old key
|
2025-02-10 14:54:01 +11:00
|
|
|
return this.param_info.length && !this.updating;
|
2024-08-20 09:42:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
beforeUpdate() {
|
2025-02-10 14:54:01 +11:00
|
|
|
this.set("updating", true);
|
2024-08-20 09:42:50 +08:00
|
|
|
}
|
2024-11-20 14:45:48 +00:00
|
|
|
|
2024-08-20 09:42:50 +08:00
|
|
|
afterUpdate() {
|
2025-02-10 14:54:01 +11:00
|
|
|
this.set("updating", false);
|
2023-02-07 12:44:38 -06:00
|
|
|
}
|
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 = {};
|
2019-07-16 12:46:32 +02:00
|
|
|
const oldParams = this.params;
|
2023-02-07 12:44:38 -06:00
|
|
|
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";
|
2021-03-06 20:16:49 +03:00
|
|
|
} else if (pinfo["type"] === "user_id") {
|
|
|
|
|
newParams[name] = null;
|
|
|
|
|
} else if (pinfo["type"] === "user_list") {
|
|
|
|
|
newParams[name] = null;
|
2024-03-12 12:06:01 -04:00
|
|
|
} else if (pinfo["type"] === "group_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
|
|
|
}
|
|
|
|
|
});
|
2023-02-07 12:44:38 -06:00
|
|
|
this.params = newParams;
|
|
|
|
|
}
|
2015-06-26 09:16:09 -07:00
|
|
|
|
|
|
|
|
updateProperties() {
|
2019-07-16 12:46:32 +02:00
|
|
|
const props = this.getProperties(Query.updatePropertyNames);
|
|
|
|
|
if (this.destroyed) {
|
|
|
|
|
props.id = this.id;
|
2015-06-30 12:52:17 -07:00
|
|
|
}
|
|
|
|
|
return props;
|
2023-02-07 12:44:38 -06:00
|
|
|
}
|
2023-02-08 13:40:53 -06:00
|
|
|
|
|
|
|
|
createProperties() {
|
|
|
|
|
if (this.sql) {
|
|
|
|
|
// Importing
|
|
|
|
|
return this.updateProperties();
|
|
|
|
|
}
|
|
|
|
|
return this.getProperties("name");
|
|
|
|
|
}
|
2023-02-07 12:44:38 -06:00
|
|
|
}
|