import { createAction, Property } from "@activepieces/pieces-framework"; import Odoo from "../../commom/index"; import { odooAuth } from '../auth'; import { odooRpc } from '../common/client'; import * as z from 'zod/mini' import { propsValidation } from '@activepieces/pieces-common'; export default createAction({ name: 'get_records', classification: 'SEARCH', auth: odooAuth, displayName: 'Custom Search and read records', description: 'Records can be listed and filtered', audience: 'human', aiMetadata: { description: 'Searches and reads records from any Odoo model via the XML-RPC search_read call, returning the chosen fields with optional offset/limit paging. Supply a model name and an optional Odoo domain (a list of [field, operator, value] criteria); an empty domain returns all records of the model. Read-only and idempotent. Use this generic reader when no model-specific get action fits.', idempotent: true }, props: { // Properties to ask from the user, in this ask we will take number of model: Property.ShortText({ displayName: 'Model', description: "Model name. e.g.: res.partner", required: true, defaultValue: 'res.partner', }), domain: Property.Json({ displayName: 'Search domains', required: false, description: 'A domain is a list of criteria, each criterion being a triple of (field_name, operator, value). See https://www.odoo.com/documentation/17.0/developer/reference/backend/orm.html#reference-orm-domains for details.', defaultValue: [ ["is_company", "=", true], ], }), fields: Property.Array({ displayName: 'Fields', description: 'Returns the requested fields of the records. When undefined, returns all fields.', required: false, }), offset: Property.Number({ displayName: 'Offset', required: false, }), limit: Property.Number({ displayName: 'Limit', required: false, }), order: Property.ShortText({ displayName: 'Sort Order', description: 'Optional. Field names with asc or desc, separated by commas, for example "create_date desc, id desc". Leave empty for Odoo\'s default order.', required: false, }), }, async run(context) { await propsValidation.validateZod(context.propsValue, { limit: z.optional(z.number().check(z.minimum(1))), }); const odoo = new Odoo({ url: context.auth.props.base_url, port: odooRpc.resolvePort(context.auth.props.port), db: context.auth.props.database, username: context.auth.props.username, password: context.auth.props.api_key, }) try { await odoo.connect(); const domainArray = context.propsValue.domain ? (context.propsValue.domain as unknown as any[]) : []; return await odoo.getRecords({model: context.propsValue.model, domain: domainArray, fields: context.propsValue.fields, offset: context.propsValue.offset, limit: context.propsValue.limit, order: context.propsValue.order || undefined}) } catch (err) { return err } } });