Improve Odoo node by implementing search_count action

This commit is contained in:
Walid Nouh 2022-09-29 22:23:46 +02:00 committed by Walid Nouh
parent 616d62aa8e
commit f7a531b937
6 changed files with 115 additions and 2 deletions

View file

@ -13,6 +13,7 @@ const serviceJSONRPC = 'object';
const methodJSONRPC = 'execute'; const methodJSONRPC = 'execute';
export const mapOperationToJSONRPC = { export const mapOperationToJSONRPC = {
count: 'search_count',
create: 'create', create: 'create',
get: 'read', get: 'read',
getAll: 'search_read', getAll: 'search_read',
@ -73,7 +74,7 @@ export interface IOdooResponceFields {
}>; }>;
} }
type OdooCRUD = 'create' | 'update' | 'delete' | 'get' | 'getAll'; type OdooCRUD = 'create' | 'update' | 'delete' | 'get' | 'getAll' | 'count';
export function odooGetDBName(databaseName: string | undefined, url: string) { export function odooGetDBName(databaseName: string | undefined, url: string) {
if (databaseName) return databaseName; if (databaseName) return databaseName;
@ -205,6 +206,42 @@ export async function odooCreate(
} }
} }
export async function odooCount(
this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions,
db: string,
userID: number,
password: string,
resource: string,
operation: OdooCRUD,
url: string,
filters?: IOdooFilterOperations,
) {
try {
const body = {
jsonrpc: '2.0',
method: 'call',
params: {
service: serviceJSONRPC,
method: methodJSONRPC,
args: [
db,
userID,
password,
mapOdooResources[resource] || resource,
mapOperationToJSONRPC[operation],
(filters && processFilters(filters)) || [],
],
},
id: Math.floor(Math.random() * 100),
};
const result = await odooJSONRPCRequest.call(this, body, url);
return { result };
} catch (error) {
throw new NodeApiError(this.getNode(), error as JsonObject);
}
}
export async function odooGet( export async function odooGet(
this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions,
db: string, db: string,

View file

@ -29,6 +29,7 @@ import {
IOdooFilterOperations, IOdooFilterOperations,
odooCreate, odooCreate,
odooDelete, odooDelete,
odooCount,
odooGet, odooGet,
odooGetAll, odooGetAll,
odooGetDBName, odooGetDBName,
@ -318,6 +319,18 @@ export class Odoo implements INodeType {
for (let i = 0; i < items.length; i++) { for (let i = 0; i < items.length; i++) {
try { try {
if (resource === 'contact') { if (resource === 'contact') {
if (operation === 'count') {
responseData = await odooCount.call(
this,
db,
userID,
password,
resource,
operation,
url,
);
}
if (operation === 'create') { if (operation === 'create') {
let additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject; let additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
@ -458,6 +471,20 @@ export class Odoo implements INodeType {
); );
} }
if (operation === 'count') {
const filter = this.getNodeParameter('filterRequest', i) as IOdooFilterOperations;
responseData = await odooCount.call(
this,
db,
userID,
password,
customResource,
operation,
url,
filter,
);
}
if (operation === 'delete') { if (operation === 'delete') {
const customResourceId = this.getNodeParameter('customResourceId', i) as string; const customResourceId = this.getNodeParameter('customResourceId', i) as string;
responseData = await odooDelete.call( responseData = await odooDelete.call(
@ -541,6 +568,18 @@ export class Odoo implements INodeType {
} }
if (resource === 'note') { if (resource === 'note') {
if (operation === 'count') {
responseData = await odooCount.call(
this,
db,
userID,
password,
resource,
operation,
url,
);
}
if (operation === 'create') { if (operation === 'create') {
// const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject; // const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
const memo = this.getNodeParameter('memo', i) as string; const memo = this.getNodeParameter('memo', i) as string;
@ -645,6 +684,18 @@ export class Odoo implements INodeType {
} }
if (resource === 'opportunity') { if (resource === 'opportunity') {
if (operation === 'count') {
responseData = await odooCount.call(
this,
db,
userID,
password,
resource,
operation,
url,
);
}
if (operation === 'create') { if (operation === 'create') {
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject; const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
const name = this.getNodeParameter('opportunityName', i) as string; const name = this.getNodeParameter('opportunityName', i) as string;

View file

@ -13,6 +13,12 @@ export const contactOperations: INodeProperties[] = [
}, },
}, },
options: [ options: [
{
name: 'Count',
value: 'count',
description: 'Count items',
action: 'Count items',
},
{ {
name: 'Create', name: 'Create',
value: 'create', value: 'create',

View file

@ -29,6 +29,12 @@ export const customResourceOperations: INodeProperties[] = [
}, },
}, },
options: [ options: [
{
name: 'Count',
value: 'count',
description: 'Count items',
action: 'Count items',
},
{ {
name: 'Create', name: 'Create',
value: 'create', value: 'create',
@ -202,7 +208,7 @@ export const customResourceDescription: INodeProperties[] = [
placeholder: 'Add condition', placeholder: 'Add condition',
displayOptions: { displayOptions: {
show: { show: {
operation: ['getAll'], operation: ['getAll', 'count'],
resource: ['custom'], resource: ['custom'],
}, },
}, },
@ -283,6 +289,7 @@ export const customResourceDescription: INodeProperties[] = [
}, },
], ],
}, },
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
/* custom:update */ /* custom:update */
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */

View file

@ -13,6 +13,12 @@ export const noteOperations: INodeProperties[] = [
}, },
}, },
options: [ options: [
{
name: 'Count',
value: 'count',
description: 'Count items',
action: 'Count items',
},
{ {
name: 'Create', name: 'Create',
value: 'create', value: 'create',

View file

@ -13,6 +13,12 @@ export const opportunityOperations: INodeProperties[] = [
}, },
}, },
options: [ options: [
{
name: 'Count',
value: 'count',
description: 'Count items',
action: 'Count items',
},
{ {
name: 'Create', name: 'Create',
value: 'create', value: 'create',