|
| 1 | +import _1crm from "../../_1crm.app.mjs"; |
| 2 | + |
| 3 | +export default { |
| 4 | + props: { |
| 5 | + _1crm, |
| 6 | + checkDuplicates: { |
| 7 | + type: "boolean", |
| 8 | + label: "Check Duplicates", |
| 9 | + description: "Check Duplicates Flag", |
| 10 | + default: true, |
| 11 | + reloadProps: true, |
| 12 | + }, |
| 13 | + }, |
| 14 | + methods: { |
| 15 | + getMethod() { |
| 16 | + return "create"; |
| 17 | + }, |
| 18 | + getUpdateId() { |
| 19 | + return ""; |
| 20 | + }, |
| 21 | + getType(type) { |
| 22 | + switch (type) { |
| 23 | + case "bool": return "boolean"; |
| 24 | + case "int": return "integer"; |
| 25 | + case "multienum": return "string[]"; |
| 26 | + default: return "string"; |
| 27 | + } |
| 28 | + }, |
| 29 | + filterFields(fields) { |
| 30 | + const groups = []; |
| 31 | + return Object.keys(fields) |
| 32 | + .filter( (key) => !("editable" in fields[key])) |
| 33 | + .filter( (key) => { |
| 34 | + if (fields[key].multi_select_group && !groups.includes(fields[key].multi_select_group)) { |
| 35 | + groups.push(fields[key].multi_select_group); |
| 36 | + return true; |
| 37 | + } |
| 38 | + if (!fields[key].multi_select_group ) return true; |
| 39 | + }) |
| 40 | + .reduce( (res, key) => (res[key] = fields[key], res), {} ); |
| 41 | + }, |
| 42 | + fixValues(data) { |
| 43 | + return Object.keys(data) |
| 44 | + .reduce( (res, key) => (res[key] = (typeof data[key] === "boolean" |
| 45 | + ? +data[key] |
| 46 | + : (Array.isArray(data[key])) |
| 47 | + ? data[key].join(",") |
| 48 | + : data[key]), res), {} ); |
| 49 | + }, |
| 50 | + }, |
| 51 | + async additionalProps() { |
| 52 | + const method = this.getMethod(); |
| 53 | + const props = {}; |
| 54 | + let { fields } = await this._1crm.getFields({ |
| 55 | + module: this.getModule(), |
| 56 | + }); |
| 57 | + delete fields.assigned_user; |
| 58 | + delete fields.assigned_user_id; |
| 59 | + |
| 60 | + fields = this.filterFields(fields); |
| 61 | + |
| 62 | + for (const [ |
| 63 | + key, |
| 64 | + value, |
| 65 | + ] of Object.entries(fields)) { |
| 66 | + props[key] = { |
| 67 | + type: this.getType(value.type), |
| 68 | + label: value.vname, |
| 69 | + description: value.comment, |
| 70 | + optional: (method === "create") |
| 71 | + ? !value.required |
| 72 | + : true, |
| 73 | + options: value.options, |
| 74 | + }; |
| 75 | + } |
| 76 | + return props; |
| 77 | + }, |
| 78 | + async run({ $ }) { |
| 79 | + const { |
| 80 | + _1crm, |
| 81 | + checkDuplicates, |
| 82 | + ...data |
| 83 | + } = this; |
| 84 | + |
| 85 | + const method = this.getMethod(); |
| 86 | + const fn = (method === "create") |
| 87 | + ? _1crm.createModel |
| 88 | + : _1crm.updateModel; |
| 89 | + |
| 90 | + const response = await fn({ |
| 91 | + $, |
| 92 | + data: { |
| 93 | + data: this.fixValues(data), |
| 94 | + }, |
| 95 | + params: { |
| 96 | + check_duplicates: checkDuplicates, |
| 97 | + }, |
| 98 | + updateId: this.getUpdateId(), |
| 99 | + model: this.getModule(), |
| 100 | + }); |
| 101 | + if (response.errors) throw new Error(response.errors); |
| 102 | + |
| 103 | + $.export("$summary", this.getSummary(response)); |
| 104 | + return response; |
| 105 | + }, |
| 106 | +}; |
0 commit comments