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';
export const mapOperationToJSONRPC = {
count: 'search_count',
create: 'create',
get: '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) {
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(
this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions,
db: string,

View file

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

View file

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

View file

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

View file

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

View file

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