Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions components/odoo/actions/create-record/create-record.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import odoo from "../../odoo.app.mjs";

export default {
key: "odoo-create-record",
name: "Create Record",
description: "Create a new record in Odoo. [See the documentation](https://www.odoo.com/documentation/18.0/developer/reference/external_api.html#create-records)",
version: "0.0.1",
type: "action",
props: {
odoo: {

Check warning on line 10 in components/odoo/actions/create-record/create-record.mjs

View workflow job for this annotation

GitHub Actions / Lint Code Base

Component prop odoo must have a description. See https://pipedream.com/docs/components/guidelines/#props

Check warning on line 10 in components/odoo/actions/create-record/create-record.mjs

View workflow job for this annotation

GitHub Actions / Lint Code Base

Component prop odoo must have a label. See https://pipedream.com/docs/components/guidelines/#props
...odoo,
reloadProps: true,
},
},
async additionalProps() {
return await this.odoo.getFieldProps();
},
async run({ $ }) {
const {
odoo,
...data
} = this;
const response = await odoo.createRecord([
data,
]);
$.export("$summary", `Successfully created record with ID: ${response}`);
return response;
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import odoo from "../../odoo.app.mjs";
import { parseObject } from "../../common/utils.mjs";

export default {
key: "odoo-search-read-records",
name: "Search and Read Records",
description: "Search and read records from Odoo. [See the documentation](https://www.odoo.com/documentation/18.0/developer/reference/external_api.html#search-and-read)",
version: "0.0.1",
type: "action",
props: {
odoo,
filter: {
type: "string",
label: "Search Filter",
description: "The criterion to search by. E.g. `[[[\"is_company\", \"=\", true]]]`. [See the documentation](https://www.odoo.com/documentation/18.0/developer/reference/backend/orm.html#search-domains) for information about constructing a search domain",
optional: true,
},
fields: {
propDefinition: [
odoo,
"fields",
],
},
},
async run({ $ }) {
const args = this.fields
? {
fields: this.fields,
}
: {};
const response = await this.odoo.searchAndReadRecords(parseObject(this.filter), args);
$.export("$summary", `Successfully retrieved ${response.length} record${response.length === 1
? ""
: "s"}`);
return response;
},
};
62 changes: 62 additions & 0 deletions components/odoo/actions/update-record/update-record.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import odoo from "../../odoo.app.mjs";
const DEFAULT_LIMIT = 20;

export default {
key: "odoo-update-record",
name: "Update Record",
description: "Update an existing record in Odoo. [See the documentation](https://www.odoo.com/documentation/18.0/developer/reference/external_api.html#update-records)",
version: "0.0.1",
type: "action",
props: {
odoo: {

Check warning on line 11 in components/odoo/actions/update-record/update-record.mjs

View workflow job for this annotation

GitHub Actions / Lint Code Base

Component prop odoo must have a description. See https://pipedream.com/docs/components/guidelines/#props

Check warning on line 11 in components/odoo/actions/update-record/update-record.mjs

View workflow job for this annotation

GitHub Actions / Lint Code Base

Component prop odoo must have a label. See https://pipedream.com/docs/components/guidelines/#props
...odoo,
reloadProps: true,
},
},
async additionalProps() {
const fieldProps = await this.odoo.getFieldProps({
update: true,
});
const recordId = {
type: "integer",
label: "Record ID",
description: "The ID of the record to update",
options: async ({ page }) => await this.getRecordIdOptions(page),
};
return {
recordId,
...fieldProps,
};
},
methods: {
async getRecordIdOptions(page) {
const records = await this.odoo.searchAndReadRecords([], {
limit: DEFAULT_LIMIT,
offset: page * DEFAULT_LIMIT,
});
return records?.map(({
id: value, display_name: label,
}) => ({
value,
label,
})) || [];
},
},
async run({ $ }) {
const {
odoo,
// eslint-disable-next-line no-unused-vars
getRecordIdOptions,
recordId,
...data
} = this;
const response = await odoo.updateRecord([
[
recordId,
],
data,
]);
$.export("$summary", `Successfully updated record with ID: ${recordId}`);
return response;
},
};
25 changes: 25 additions & 0 deletions components/odoo/common/utils.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export const parseObject = (obj) => {
if (!obj) {
return undefined;
}
if (typeof obj === "string") {
try {
return JSON.parse(obj);
} catch (e) {
return obj;
}
}
if (Array.isArray(obj)) {
return obj.map(parseObject);
}
if (typeof obj === "object") {
return Object.fromEntries(Object.entries(obj).map(([
key,
value,
]) => [
key,
parseObject(value),
]));
}
return obj;
};
101 changes: 97 additions & 4 deletions components/odoo/odoo.app.mjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,104 @@
import xmlrpc from "xmlrpc";

export default {
type: "app",
app: "odoo",
propDefinitions: {},
propDefinitions: {
fields: {
type: "string[]",
label: "Fields",
description: "The fields to return in the results. If not provided, all fields will be returned.",
optional: true,
async options() {
const fields = await this.getFields([], {
attributes: [
"string",
],
});
return Object.keys(fields)?.map((key) => ({
value: key,
label: fields[key].string,
})) || [];
},
},
},
methods: {
// this.$auth contains connected account data
authKeys() {
console.log(Object.keys(this.$auth));
getClient(type = "common") {
return xmlrpc.createSecureClient(`${this.$auth.server_url}/xmlrpc/2/${type}`);
},
async getUid() {
const db = this.$auth.db;
const username = this.$auth.username;
const password = this.$auth.password;
const common = this.getClient("common");
const uid = await new Promise((resolve, reject) => {
common.methodCall("authenticate", [
db,
username,
password,
{},
], (error, value) => {
if (error) reject(error);
resolve(value);
});
});
return uid;
},
async makeRequest(method, filter = [], args = {}) {
const db = this.$auth.db;
const uid = await this.getUid();
const password = this.$auth.password;
const models = this.getClient("object");
const results = await new Promise((resolve, reject) => {
models.methodCall("execute_kw", [
db,
uid,
password,
"res.partner",
method,
filter,
args,
], (error, value) => {
if (error) reject(error);
resolve(value);
});
});
return results;
},
async getFieldProps({ update = false } = {}) {
const props = {};
const fields = await this.getFields();
Object.keys(fields).forEach((key) => {
if (fields[key].readonly === true) return;
props[key] = {
type: fields[key].type === "integer" || fields[key].type === "boolean"
? fields[key].type
: fields[key].type.includes("id")
? "integer"
: fields[key].type.includes("2many")
? "string[]"
: "string",
label: fields[key].string,
description: `Value for "${key}"`,
optional: (key !== "name" || update) && fields[key].required === false,
};
});
return props;
},
getFields(filter = [], args = {}) {
return this.makeRequest("fields_get", filter, args);
},
searchAndReadRecords(filter = [], args = {}) {
return this.makeRequest("search_read", filter, args);
},
readRecord(data) {
return this.makeRequest("read", data);
},
createRecord(data) {
return this.makeRequest("create", data);
},
updateRecord(data) {
return this.makeRequest("write", data);
},
},
};
5 changes: 4 additions & 1 deletion components/odoo/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pipedream/odoo",
"version": "0.0.1",
"version": "0.1.0",
"description": "Pipedream Odoo Components",
"main": "odoo.app.mjs",
"keywords": [
Expand All @@ -11,5 +11,8 @@
"author": "Pipedream <[email protected]> (https://pipedream.com/)",
"publishConfig": {
"access": "public"
},
"dependencies": {
"xmlrpc": "^1.3.2"
}
}
31 changes: 28 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading