2017-09-29 13:27:03 +02:00
|
|
|
import { ajax } from 'discourse/lib/ajax';
|
2017-09-23 04:34:07 +02:00
|
|
|
|
2017-11-01 05:21:14 +01:00
|
|
|
const wizardProperties = [
|
|
|
|
'name',
|
|
|
|
'background',
|
|
|
|
'save_submissions',
|
|
|
|
'multiple_submissions',
|
|
|
|
'after_signup',
|
|
|
|
'after_time',
|
|
|
|
'after_time_scheduled',
|
2017-11-22 10:34:21 +01:00
|
|
|
'required',
|
2017-11-29 10:48:49 +01:00
|
|
|
'prompt_completion',
|
2019-11-04 18:49:30 +01:00
|
|
|
'restart_on_revisit',
|
2017-12-17 04:43:18 +01:00
|
|
|
'min_trust',
|
2018-07-16 06:28:24 +02:00
|
|
|
'theme_id'
|
2017-11-01 05:21:14 +01:00
|
|
|
];
|
|
|
|
|
2017-09-23 04:34:07 +02:00
|
|
|
const CustomWizard = Discourse.Model.extend({
|
2017-10-13 15:02:34 +02:00
|
|
|
save() {
|
|
|
|
return new Ember.RSVP.Promise((resolve, reject) => {
|
2019-06-05 23:23:15 +02:00
|
|
|
|
2017-10-13 15:02:34 +02:00
|
|
|
const id = this.get('id');
|
2017-10-30 07:24:51 +01:00
|
|
|
if (!id || !id.underscore()) return reject({ error: 'id_required' });
|
2017-10-13 15:02:34 +02:00
|
|
|
|
|
|
|
let wizard = { id: id.underscore() };
|
|
|
|
|
2017-11-01 05:21:14 +01:00
|
|
|
wizardProperties.forEach((p) => {
|
|
|
|
const value = this.get(p);
|
|
|
|
if (value) wizard[p] = value;
|
|
|
|
});
|
|
|
|
|
2017-11-01 10:50:03 +01:00
|
|
|
if (wizard['after_time'] && !wizard['after_time_scheduled']) {
|
2017-11-01 05:21:14 +01:00
|
|
|
return reject({ error: 'after_time_need_time' });
|
|
|
|
};
|
|
|
|
|
2017-10-13 15:02:34 +02:00
|
|
|
const steps = this.get('steps');
|
2017-10-30 07:24:51 +01:00
|
|
|
if (steps.length > 0) {
|
|
|
|
const stepsResult = this.buildSteps(steps);
|
|
|
|
if (stepsResult.error) {
|
2017-11-01 05:21:14 +01:00
|
|
|
reject({ error: stepsResult.error });
|
2017-10-30 07:24:51 +01:00
|
|
|
} else {
|
2017-11-01 05:21:14 +01:00
|
|
|
wizard['steps'] = stepsResult.steps;
|
2017-10-30 07:24:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (steps.length < 1 || !wizard['steps'] || wizard['steps'].length < 1) {
|
|
|
|
return reject({ error: 'steps_required' });
|
|
|
|
}
|
2017-10-13 15:02:34 +02:00
|
|
|
|
|
|
|
ajax("/admin/wizards/custom/save", {
|
|
|
|
type: 'PUT',
|
|
|
|
data: {
|
|
|
|
wizard: JSON.stringify(wizard)
|
|
|
|
}
|
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
|
|
|
|
2017-10-30 07:24:51 +01:00
|
|
|
buildSteps(stepsObj) {
|
2017-09-29 13:27:03 +02:00
|
|
|
let steps = [];
|
2017-10-30 07:24:51 +01:00
|
|
|
let error = null;
|
2017-09-29 13:27:03 +02:00
|
|
|
|
2017-10-13 15:02:34 +02:00
|
|
|
stepsObj.some((s) => {
|
2017-10-30 07:24:51 +01:00
|
|
|
if (!s.id || !s.id.underscore()) {
|
|
|
|
error = 'id_required';
|
|
|
|
return;
|
|
|
|
};
|
2017-10-07 04:27:38 +02:00
|
|
|
|
2017-10-13 15:02:34 +02:00
|
|
|
let step = { id: s.id.underscore() };
|
2017-09-29 13:27:03 +02:00
|
|
|
|
2017-10-07 04:27:38 +02:00
|
|
|
if (s.title) step['title'] = s.title;
|
2017-10-13 15:02:34 +02:00
|
|
|
if (s.key) step['key'] = s.key;
|
2017-10-07 04:27:38 +02:00
|
|
|
if (s.banner) step['banner'] = s.banner;
|
2017-11-03 09:22:50 +01:00
|
|
|
if (s.raw_description) step['raw_description'] = s.raw_description;
|
2019-07-02 06:49:14 +02:00
|
|
|
if (s.required_data) step['required_data'] = s.required_data;
|
2019-07-27 09:01:29 +02:00
|
|
|
if (s.required_data_message) step['required_data_message'] = s.required_data_message;
|
2019-07-02 06:49:14 +02:00
|
|
|
if (s.permitted_params) step['permitted_params'] = s.permitted_params;
|
2017-10-07 04:27:38 +02:00
|
|
|
|
2017-09-29 13:27:03 +02:00
|
|
|
const fields = s.get('fields');
|
2017-10-13 15:02:34 +02:00
|
|
|
if (fields.length) {
|
|
|
|
step['fields'] = [];
|
2017-10-07 04:27:38 +02:00
|
|
|
|
2017-10-13 15:02:34 +02:00
|
|
|
fields.some((f) => {
|
2017-10-30 07:24:51 +01:00
|
|
|
let id = f.id;
|
2017-10-07 04:27:38 +02:00
|
|
|
|
2017-10-30 07:24:51 +01:00
|
|
|
if (!id || !id.underscore()) {
|
|
|
|
error = 'id_required';
|
|
|
|
return;
|
|
|
|
}
|
2017-11-23 10:02:25 +01:00
|
|
|
|
|
|
|
if (!f.type) {
|
|
|
|
error = 'type_required';
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-10-13 15:02:34 +02:00
|
|
|
f.set('id', id.underscore());
|
2017-10-05 02:36:46 +02:00
|
|
|
|
2017-10-30 07:24:51 +01:00
|
|
|
if (f.label === '') delete f.label;
|
|
|
|
if (f.description === '') delete f.description;
|
|
|
|
|
|
|
|
if (f.type === 'dropdown') {
|
|
|
|
const choices = f.choices;
|
2020-03-19 08:58:45 +01:00
|
|
|
if ((!choices || choices.length < 1) && !f.choices_key) {
|
2017-11-01 05:21:14 +01:00
|
|
|
error = 'field.need_choices';
|
|
|
|
return;
|
|
|
|
}
|
2017-11-23 10:02:25 +01:00
|
|
|
|
|
|
|
if (f.dropdown_none === '') delete f.dropdown_none;
|
2017-10-13 15:02:34 +02:00
|
|
|
}
|
2017-10-07 04:27:38 +02:00
|
|
|
|
2017-10-17 15:17:53 +02:00
|
|
|
delete f.isNew;
|
|
|
|
|
2017-10-13 15:02:34 +02:00
|
|
|
step['fields'].push(f);
|
|
|
|
});
|
2017-10-30 07:24:51 +01:00
|
|
|
|
|
|
|
if (error) return;
|
2017-10-13 15:02:34 +02:00
|
|
|
}
|
2017-10-07 04:27:38 +02:00
|
|
|
|
2017-10-13 15:02:34 +02:00
|
|
|
const actions = s.actions;
|
|
|
|
if (actions.length) {
|
|
|
|
step['actions'] = [];
|
2017-10-07 04:27:38 +02:00
|
|
|
|
2017-10-13 15:02:34 +02:00
|
|
|
actions.some((a) => {
|
|
|
|
let id = a.get('id');
|
2017-10-30 07:24:51 +01:00
|
|
|
if (!id || !id.underscore()) {
|
|
|
|
error = 'id_required';
|
|
|
|
return;
|
|
|
|
}
|
2019-06-05 23:23:15 +02:00
|
|
|
//check if api_body is valid JSON
|
|
|
|
let api_body = a.get('api_body');
|
2019-07-18 01:18:08 +02:00
|
|
|
if (api_body) {
|
2019-06-05 23:23:15 +02:00
|
|
|
try {
|
|
|
|
JSON.parse(api_body);
|
|
|
|
} catch (e) {
|
|
|
|
error = 'invalid_api_body';
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2017-10-06 04:59:02 +02:00
|
|
|
|
2017-10-13 15:02:34 +02:00
|
|
|
a.set('id', id.underscore());
|
2017-09-29 13:27:03 +02:00
|
|
|
|
2017-10-17 15:17:53 +02:00
|
|
|
delete a.isNew;
|
|
|
|
|
2017-10-13 15:02:34 +02:00
|
|
|
step['actions'].push(a);
|
|
|
|
});
|
|
|
|
|
2017-10-30 07:24:51 +01:00
|
|
|
if (error) return;
|
2017-10-13 15:02:34 +02:00
|
|
|
}
|
2017-09-29 13:27:03 +02:00
|
|
|
|
|
|
|
steps.push(step);
|
|
|
|
});
|
|
|
|
|
2017-10-30 07:24:51 +01:00
|
|
|
if (error) {
|
|
|
|
return { error };
|
|
|
|
} else {
|
|
|
|
return { steps };
|
|
|
|
};
|
2017-09-23 04:34:07 +02:00
|
|
|
},
|
|
|
|
|
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 => {
|
2017-09-23 04:34:07 +02:00
|
|
|
return result.wizards.map(w => CustomWizard.create(w));
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
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;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2017-09-29 13:27:03 +02:00
|
|
|
create(w) {
|
|
|
|
const wizard = this._super.apply(this);
|
|
|
|
let steps = Ember.A();
|
|
|
|
let props = { steps };
|
|
|
|
|
|
|
|
if (w) {
|
2017-10-05 02:36:46 +02:00
|
|
|
props['id'] = w.id;
|
2017-10-13 15:02:34 +02:00
|
|
|
props['existingId'] = true;
|
2017-11-01 05:21:14 +01:00
|
|
|
|
|
|
|
wizardProperties.forEach((p) => {
|
|
|
|
props[p] = w[p];
|
|
|
|
});
|
2017-09-29 13:27:03 +02:00
|
|
|
|
2017-10-13 15:02:34 +02:00
|
|
|
if (w.steps && w.steps.length) {
|
2017-09-29 13:27:03 +02:00
|
|
|
w.steps.forEach((s) => {
|
2017-10-13 15:02:34 +02:00
|
|
|
// clean empty strings
|
|
|
|
Object.keys(s).forEach((key) => (s[key] === '') && delete s[key]);
|
2017-09-29 13:27:03 +02:00
|
|
|
|
2017-10-13 15:02:34 +02:00
|
|
|
let fields = Ember.A();
|
2017-09-29 13:27:03 +02:00
|
|
|
|
2017-10-13 15:02:34 +02:00
|
|
|
if (s.fields && s.fields.length) {
|
|
|
|
s.fields.forEach((f) => {
|
|
|
|
Object.keys(f).forEach((key) => (f[key] === '') && delete f[key]);
|
|
|
|
|
2017-10-17 15:17:53 +02:00
|
|
|
const fieldParams = { isNew: false };
|
2018-06-14 01:59:41 +02:00
|
|
|
let field = Ember.Object.create($.extend(f, fieldParams));
|
2017-10-13 15:02:34 +02:00
|
|
|
|
|
|
|
if (f.choices) {
|
|
|
|
let choices = Ember.A();
|
2017-09-29 13:27:03 +02:00
|
|
|
|
2017-10-13 15:02:34 +02:00
|
|
|
f.choices.forEach((c) => {
|
|
|
|
choices.pushObject(Ember.Object.create(c));
|
|
|
|
});
|
2017-10-05 02:36:46 +02:00
|
|
|
|
2017-10-13 15:02:34 +02:00
|
|
|
field.set('choices', choices);
|
|
|
|
}
|
|
|
|
|
|
|
|
fields.pushObject(field);
|
|
|
|
});
|
|
|
|
}
|
2017-09-29 13:27:03 +02:00
|
|
|
|
|
|
|
let actions = Ember.A();
|
2017-10-13 15:02:34 +02:00
|
|
|
if (s.actions && s.actions.length) {
|
|
|
|
s.actions.forEach((a) => {
|
2017-10-17 15:17:53 +02:00
|
|
|
const actionParams = { isNew: false };
|
2018-06-14 01:59:41 +02:00
|
|
|
const action = Ember.Object.create($.extend(a, actionParams));
|
2017-10-17 15:17:53 +02:00
|
|
|
actions.pushObject(action);
|
2017-10-13 15:02:34 +02:00
|
|
|
});
|
|
|
|
}
|
2017-09-29 13:27:03 +02:00
|
|
|
|
|
|
|
steps.pushObject(Ember.Object.create({
|
|
|
|
id: s.id,
|
2017-10-13 15:02:34 +02:00
|
|
|
key: s.key,
|
2017-09-29 13:27:03 +02:00
|
|
|
title: s.title,
|
2017-11-03 09:22:50 +01:00
|
|
|
raw_description: s.raw_description,
|
2017-10-06 04:59:02 +02:00
|
|
|
banner: s.banner,
|
2019-07-02 06:49:14 +02:00
|
|
|
required_data: s.required_data,
|
2019-07-27 09:01:29 +02:00
|
|
|
required_data_message: s.required_data_message,
|
2019-07-02 06:49:14 +02:00
|
|
|
permitted_params: s.permitted_params,
|
2017-09-29 13:27:03 +02:00
|
|
|
fields,
|
2017-10-17 15:17:53 +02:00
|
|
|
actions,
|
|
|
|
isNew: false
|
2017-09-29 13:27:03 +02:00
|
|
|
}));
|
|
|
|
});
|
2017-10-05 02:36:46 +02:00
|
|
|
};
|
|
|
|
} else {
|
2017-10-07 04:27:38 +02:00
|
|
|
props['id'] = '';
|
|
|
|
props['name'] = '';
|
|
|
|
props['background'] = '';
|
2017-10-05 02:36:46 +02:00
|
|
|
props['save_submissions'] = true;
|
2017-10-13 15:02:34 +02:00
|
|
|
props['multiple_submissions'] = false;
|
2017-11-01 05:21:14 +01:00
|
|
|
props['after_signup'] = false;
|
|
|
|
props['after_time'] = false;
|
|
|
|
props['required'] = false;
|
2017-11-22 10:34:21 +01:00
|
|
|
props['prompt_completion'] = false;
|
2019-11-04 18:49:30 +01:00
|
|
|
props['restart_on_revisit'] = false;
|
2017-11-29 10:48:49 +01:00
|
|
|
props['min_trust'] = 0;
|
2017-10-07 04:27:38 +02:00
|
|
|
props['steps'] = Ember.A();
|
2017-09-29 13:27:03 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
wizard.setProperties(props);
|
2017-09-23 04:34:07 +02:00
|
|
|
|
|
|
|
return wizard;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
export default CustomWizard;
|