|
| 1 | +import xmlrpc from "xmlrpc"; |
| 2 | + |
1 | 3 | export default { |
2 | 4 | type: "app", |
3 | 5 | app: "odoo", |
4 | | - propDefinitions: {}, |
| 6 | + propDefinitions: { |
| 7 | + fields: { |
| 8 | + type: "string[]", |
| 9 | + label: "Fields", |
| 10 | + description: "The fields to return in the results. If not provided, all fields will be returned.", |
| 11 | + optional: true, |
| 12 | + async options() { |
| 13 | + const fields = await this.getFields([], { |
| 14 | + attributes: [ |
| 15 | + "string", |
| 16 | + ], |
| 17 | + }); |
| 18 | + return Object.keys(fields)?.map((key) => ({ |
| 19 | + value: key, |
| 20 | + label: fields[key].string, |
| 21 | + })) || []; |
| 22 | + }, |
| 23 | + }, |
| 24 | + }, |
5 | 25 | methods: { |
6 | | - // this.$auth contains connected account data |
7 | | - authKeys() { |
8 | | - console.log(Object.keys(this.$auth)); |
| 26 | + getClient(type = "common") { |
| 27 | + return xmlrpc.createSecureClient(`${this.$auth.server_url}/xmlrpc/2/${type}`); |
| 28 | + }, |
| 29 | + async getUid() { |
| 30 | + const db = this.$auth.db; |
| 31 | + const username = this.$auth.username; |
| 32 | + const password = this.$auth.password; |
| 33 | + const common = this.getClient("common"); |
| 34 | + const uid = await new Promise((resolve, reject) => { |
| 35 | + common.methodCall("authenticate", [ |
| 36 | + db, |
| 37 | + username, |
| 38 | + password, |
| 39 | + {}, |
| 40 | + ], (error, value) => { |
| 41 | + if (error) reject(error); |
| 42 | + resolve(value); |
| 43 | + }); |
| 44 | + }); |
| 45 | + return uid; |
| 46 | + }, |
| 47 | + async makeRequest(method, filter = [], args = {}) { |
| 48 | + const db = this.$auth.db; |
| 49 | + const uid = await this.getUid(); |
| 50 | + const password = this.$auth.password; |
| 51 | + const models = this.getClient("object"); |
| 52 | + const results = await new Promise((resolve, reject) => { |
| 53 | + models.methodCall("execute_kw", [ |
| 54 | + db, |
| 55 | + uid, |
| 56 | + password, |
| 57 | + "res.partner", |
| 58 | + method, |
| 59 | + filter, |
| 60 | + args, |
| 61 | + ], (error, value) => { |
| 62 | + if (error) reject(error); |
| 63 | + resolve(value); |
| 64 | + }); |
| 65 | + }); |
| 66 | + return results; |
| 67 | + }, |
| 68 | + async getFieldProps({ update = false } = {}) { |
| 69 | + const props = {}; |
| 70 | + const fields = await this.getFields(); |
| 71 | + Object.keys(fields).forEach((key) => { |
| 72 | + if (fields[key].readonly === true) return; |
| 73 | + props[key] = { |
| 74 | + type: fields[key].type === "integer" || fields[key].type === "boolean" |
| 75 | + ? fields[key].type |
| 76 | + : fields[key].type.includes("id") |
| 77 | + ? "integer" |
| 78 | + : fields[key].type.includes("2many") |
| 79 | + ? "string[]" |
| 80 | + : "string", |
| 81 | + label: fields[key].string, |
| 82 | + description: `Value for "${key}"`, |
| 83 | + optional: (key !== "name" || update) && fields[key].required === false, |
| 84 | + }; |
| 85 | + }); |
| 86 | + return props; |
| 87 | + }, |
| 88 | + getFields(filter = [], args = {}) { |
| 89 | + return this.makeRequest("fields_get", filter, args); |
| 90 | + }, |
| 91 | + searchAndReadRecords(filter = [], args = {}) { |
| 92 | + return this.makeRequest("search_read", filter, args); |
| 93 | + }, |
| 94 | + readRecord(data) { |
| 95 | + return this.makeRequest("read", data); |
| 96 | + }, |
| 97 | + createRecord(data) { |
| 98 | + return this.makeRequest("create", data); |
| 99 | + }, |
| 100 | + updateRecord(data) { |
| 101 | + return this.makeRequest("write", data); |
9 | 102 | }, |
10 | 103 | }, |
11 | 104 | }; |
0 commit comments