feat: Convert all app files to JS
This commit is contained in:
98
packages/backend/src/apps/odoo/actions/create-lead/index.js
Normal file
98
packages/backend/src/apps/odoo/actions/create-lead/index.js
Normal file
@@ -0,0 +1,98 @@
|
||||
import defineAction from '../../../../helpers/define-action.js';
|
||||
import { authenticate, asyncMethodCall } from '../../common/xmlrpc-client.js';
|
||||
|
||||
export default defineAction({
|
||||
name: 'Create Lead',
|
||||
key: 'createLead',
|
||||
description: '',
|
||||
arguments: [
|
||||
{
|
||||
label: 'Name',
|
||||
key: 'name',
|
||||
type: 'string',
|
||||
required: true,
|
||||
description: 'Lead name',
|
||||
variables: true,
|
||||
},
|
||||
{
|
||||
label: 'Type',
|
||||
key: 'type',
|
||||
type: 'dropdown',
|
||||
required: true,
|
||||
variables: true,
|
||||
options: [
|
||||
{
|
||||
label: 'Lead',
|
||||
value: 'lead',
|
||||
},
|
||||
{
|
||||
label: 'Opportunity',
|
||||
value: 'opportunity',
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
label: 'Email',
|
||||
key: 'email',
|
||||
type: 'string',
|
||||
required: false,
|
||||
description: 'Email of lead contact',
|
||||
variables: true,
|
||||
},
|
||||
{
|
||||
label: 'Contact Name',
|
||||
key: 'contactName',
|
||||
type: 'string',
|
||||
required: false,
|
||||
description: 'Name of lead contact',
|
||||
variables: true,
|
||||
},
|
||||
{
|
||||
label: 'Phone Number',
|
||||
key: 'phoneNumber',
|
||||
type: 'string',
|
||||
required: false,
|
||||
description: 'Phone number of lead contact',
|
||||
variables: true,
|
||||
},
|
||||
{
|
||||
label: 'Mobile Number',
|
||||
key: 'mobileNumber',
|
||||
type: 'string',
|
||||
required: false,
|
||||
description: 'Mobile number of lead contact',
|
||||
variables: true,
|
||||
},
|
||||
],
|
||||
|
||||
async run($) {
|
||||
const uid = await authenticate($);
|
||||
const id = await asyncMethodCall($, {
|
||||
method: 'execute_kw',
|
||||
params: [
|
||||
$.auth.data.databaseName,
|
||||
uid,
|
||||
$.auth.data.apiKey,
|
||||
'crm.lead',
|
||||
'create',
|
||||
[
|
||||
{
|
||||
name: $.step.parameters.name,
|
||||
type: $.step.parameters.type,
|
||||
email_from: $.step.parameters.email,
|
||||
contact_name: $.step.parameters.contactName,
|
||||
phone: $.step.parameters.phoneNumber,
|
||||
mobile: $.step.parameters.mobileNumber,
|
||||
},
|
||||
],
|
||||
],
|
||||
path: 'object',
|
||||
});
|
||||
|
||||
$.setActionItem({
|
||||
raw: {
|
||||
id: id,
|
||||
},
|
||||
});
|
||||
},
|
||||
});
|
@@ -1,103 +0,0 @@
|
||||
import defineAction from '../../../../helpers/define-action';
|
||||
import { authenticate, asyncMethodCall } from '../../common/xmlrpc-client';
|
||||
|
||||
export default defineAction({
|
||||
name: 'Create Lead',
|
||||
key: 'createLead',
|
||||
description: '',
|
||||
arguments: [
|
||||
{
|
||||
label: 'Name',
|
||||
key: 'name',
|
||||
type: 'string' as const,
|
||||
required: true,
|
||||
description: 'Lead name',
|
||||
variables: true,
|
||||
},
|
||||
{
|
||||
label: 'Type',
|
||||
key: 'type',
|
||||
type: 'dropdown' as const,
|
||||
required: true,
|
||||
variables: true,
|
||||
options: [
|
||||
{
|
||||
label: 'Lead',
|
||||
value: 'lead'
|
||||
},
|
||||
{
|
||||
label: 'Opportunity',
|
||||
value: 'opportunity'
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
label: "Email",
|
||||
key: 'email',
|
||||
type: 'string' as const,
|
||||
required: false,
|
||||
description: 'Email of lead contact',
|
||||
variables: true,
|
||||
},
|
||||
{
|
||||
label: "Contact Name",
|
||||
key: 'contactName',
|
||||
type: 'string' as const,
|
||||
required: false,
|
||||
description: 'Name of lead contact',
|
||||
variables: true
|
||||
},
|
||||
{
|
||||
label: 'Phone Number',
|
||||
key: 'phoneNumber',
|
||||
type: 'string' as const,
|
||||
required: false,
|
||||
description: 'Phone number of lead contact',
|
||||
variables: true
|
||||
},
|
||||
{
|
||||
label: 'Mobile Number',
|
||||
key: 'mobileNumber',
|
||||
type: 'string' as const,
|
||||
required: false,
|
||||
description: 'Mobile number of lead contact',
|
||||
variables: true
|
||||
}
|
||||
],
|
||||
|
||||
async run($) {
|
||||
const uid = await authenticate($);
|
||||
const id = await asyncMethodCall(
|
||||
$,
|
||||
{
|
||||
method: 'execute_kw',
|
||||
params: [
|
||||
$.auth.data.databaseName,
|
||||
uid,
|
||||
$.auth.data.apiKey,
|
||||
'crm.lead',
|
||||
'create',
|
||||
[
|
||||
{
|
||||
name: $.step.parameters.name,
|
||||
type: $.step.parameters.type,
|
||||
email_from: $.step.parameters.email,
|
||||
contact_name: $.step.parameters.contactName,
|
||||
phone: $.step.parameters.phoneNumber,
|
||||
mobile: $.step.parameters.mobileNumber
|
||||
}
|
||||
]
|
||||
],
|
||||
path: 'object',
|
||||
},
|
||||
);
|
||||
|
||||
$.setActionItem(
|
||||
{
|
||||
raw: {
|
||||
id: id
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
});
|
3
packages/backend/src/apps/odoo/actions/index.js
Normal file
3
packages/backend/src/apps/odoo/actions/index.js
Normal file
@@ -0,0 +1,3 @@
|
||||
import createLead from './create-lead/index.js';
|
||||
|
||||
export default [createLead];
|
@@ -1,3 +0,0 @@
|
||||
import createLead from './create-lead';
|
||||
|
||||
export default [createLead];
|
@@ -1,23 +1,24 @@
|
||||
import verifyCredentials from './verify-credentials';
|
||||
import isStillVerified from './is-still-verified';
|
||||
import verifyCredentials from './verify-credentials.js';
|
||||
import isStillVerified from './is-still-verified.js';
|
||||
|
||||
export default {
|
||||
fields: [
|
||||
{
|
||||
key: 'host',
|
||||
label: 'Host Name',
|
||||
type: 'string' as const,
|
||||
type: 'string',
|
||||
required: true,
|
||||
readOnly: false,
|
||||
value: null,
|
||||
placeholder: null,
|
||||
description: 'Host name of your Odoo Server (e.g. sub.domain.com without the protocol)',
|
||||
description:
|
||||
'Host name of your Odoo Server (e.g. sub.domain.com without the protocol)',
|
||||
clickToCopy: false,
|
||||
},
|
||||
{
|
||||
key: 'port',
|
||||
label: 'Port',
|
||||
type: 'string' as const,
|
||||
type: 'string',
|
||||
required: true,
|
||||
readOnly: false,
|
||||
value: '443',
|
||||
@@ -28,7 +29,7 @@ export default {
|
||||
{
|
||||
key: 'secure',
|
||||
label: 'Secure',
|
||||
type: 'dropdown' as const,
|
||||
type: 'dropdown',
|
||||
required: true,
|
||||
readOnly: false,
|
||||
value: 'true',
|
||||
@@ -49,7 +50,7 @@ export default {
|
||||
{
|
||||
key: 'databaseName',
|
||||
label: 'Database Name',
|
||||
type: 'string' as const,
|
||||
type: 'string',
|
||||
required: true,
|
||||
readOnly: false,
|
||||
value: null,
|
||||
@@ -60,27 +61,28 @@ export default {
|
||||
{
|
||||
key: 'email',
|
||||
label: 'Email Address',
|
||||
type: 'string' as const,
|
||||
type: 'string',
|
||||
required: true,
|
||||
readOnly: false,
|
||||
value: null,
|
||||
placeholder: null,
|
||||
description: 'Email Address of the account that will be interacting with the database',
|
||||
clickToCopy: false
|
||||
description:
|
||||
'Email Address of the account that will be interacting with the database',
|
||||
clickToCopy: false,
|
||||
},
|
||||
{
|
||||
key: 'apiKey',
|
||||
label: 'API Key',
|
||||
type: 'string' as const,
|
||||
type: 'string',
|
||||
required: true,
|
||||
readOnly: false,
|
||||
value: null,
|
||||
placeholder: null,
|
||||
description: 'API Key for your Odoo account',
|
||||
clickToCopy: false
|
||||
}
|
||||
clickToCopy: false,
|
||||
},
|
||||
],
|
||||
|
||||
verifyCredentials,
|
||||
isStillVerified
|
||||
isStillVerified,
|
||||
};
|
8
packages/backend/src/apps/odoo/auth/is-still-verified.js
Normal file
8
packages/backend/src/apps/odoo/auth/is-still-verified.js
Normal file
@@ -0,0 +1,8 @@
|
||||
import verifyCredentials from './verify-credentials.js';
|
||||
|
||||
const isStillVerified = async ($) => {
|
||||
await verifyCredentials($);
|
||||
return true;
|
||||
};
|
||||
|
||||
export default isStillVerified;
|
@@ -1,9 +0,0 @@
|
||||
import {IGlobalVariable} from '@automatisch/types';
|
||||
import verifyCredentials from './verify-credentials';
|
||||
|
||||
const isStillVerified = async ($: IGlobalVariable) => {
|
||||
await verifyCredentials($);
|
||||
return true;
|
||||
}
|
||||
|
||||
export default isStillVerified;
|
@@ -1,7 +1,6 @@
|
||||
import { IGlobalVariable } from '@automatisch/types';
|
||||
import { authenticate } from '../common/xmlrpc-client';
|
||||
import { authenticate } from '../common/xmlrpc-client.js';
|
||||
|
||||
const verifyCredentials = async ($: IGlobalVariable) => {
|
||||
const verifyCredentials = async ($) => {
|
||||
try {
|
||||
await authenticate($);
|
||||
|
||||
@@ -11,6 +10,6 @@ const verifyCredentials = async ($: IGlobalVariable) => {
|
||||
} catch (error) {
|
||||
throw new Error('Failed while authorizing!');
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export default verifyCredentials;
|
53
packages/backend/src/apps/odoo/common/xmlrpc-client.js
Normal file
53
packages/backend/src/apps/odoo/common/xmlrpc-client.js
Normal file
@@ -0,0 +1,53 @@
|
||||
import { join } from 'node:path';
|
||||
import xmlrpc from 'xmlrpc';
|
||||
|
||||
export const asyncMethodCall = async ($, { method, params, path }) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
const client = getClient($, { path });
|
||||
|
||||
client.methodCall(method, params, (error, response) => {
|
||||
if (error != null) {
|
||||
// something went wrong on the server side, display the error returned by Odoo
|
||||
reject(error);
|
||||
}
|
||||
|
||||
resolve(response);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
export const getClient = ($, { path = 'common' }) => {
|
||||
const host = $.auth.data.host;
|
||||
const port = Number($.auth.data.port);
|
||||
const secure = $.auth.data.secure === 'true';
|
||||
const createClientFunction = secure
|
||||
? xmlrpc.createSecureClient
|
||||
: xmlrpc.createClient;
|
||||
|
||||
return createClientFunction({
|
||||
host,
|
||||
port,
|
||||
path: join('/xmlrpc/2', path),
|
||||
});
|
||||
};
|
||||
|
||||
export const authenticate = async ($) => {
|
||||
const uid = await asyncMethodCall($, {
|
||||
method: 'authenticate',
|
||||
params: [
|
||||
$.auth.data.databaseName,
|
||||
$.auth.data.email,
|
||||
$.auth.data.apiKey,
|
||||
[],
|
||||
],
|
||||
});
|
||||
|
||||
if (!Number.isInteger(uid)) {
|
||||
// failed to authenticate
|
||||
throw new Error(
|
||||
'Failed to connect to the Odoo server. Please, check the credentials!'
|
||||
);
|
||||
}
|
||||
|
||||
return uid;
|
||||
};
|
@@ -1,69 +0,0 @@
|
||||
import { join } from 'node:path';
|
||||
import xmlrpc from 'xmlrpc';
|
||||
import { IGlobalVariable } from "@automatisch/types";
|
||||
|
||||
type AsyncMethodCallPayload = {
|
||||
method: string;
|
||||
params: any[];
|
||||
path?: string;
|
||||
}
|
||||
|
||||
export const asyncMethodCall = async <T = number>($: IGlobalVariable, { method, params, path }: AsyncMethodCallPayload): Promise<T> => {
|
||||
return new Promise(
|
||||
(resolve, reject) => {
|
||||
const client = getClient($, { path });
|
||||
|
||||
client.methodCall(
|
||||
method,
|
||||
params,
|
||||
(error, response) => {
|
||||
if (error != null) {
|
||||
// something went wrong on the server side, display the error returned by Odoo
|
||||
reject(error);
|
||||
}
|
||||
|
||||
resolve(response);
|
||||
}
|
||||
)
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
export const getClient = ($: IGlobalVariable, { path = 'common' }) => {
|
||||
const host = $.auth.data.host as string;
|
||||
const port = Number($.auth.data.port as string);
|
||||
const secure = $.auth.data.secure === 'true';
|
||||
const createClientFunction = secure ? xmlrpc.createSecureClient : xmlrpc.createClient;
|
||||
|
||||
return createClientFunction(
|
||||
{
|
||||
host,
|
||||
port,
|
||||
path: join('/xmlrpc/2', path),
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
export const authenticate = async ($: IGlobalVariable) => {
|
||||
const uid = await asyncMethodCall(
|
||||
$,
|
||||
{
|
||||
method: 'authenticate',
|
||||
params: [
|
||||
$.auth.data.databaseName,
|
||||
$.auth.data.email,
|
||||
$.auth.data.apiKey,
|
||||
[]
|
||||
]
|
||||
}
|
||||
);
|
||||
|
||||
if (!Number.isInteger(uid)) {
|
||||
// failed to authenticate
|
||||
throw new Error(
|
||||
'Failed to connect to the Odoo server. Please, check the credentials!'
|
||||
);
|
||||
}
|
||||
|
||||
return uid;
|
||||
}
|
@@ -1,6 +1,6 @@
|
||||
import defineApp from '../../helpers/define-app';
|
||||
import auth from './auth';
|
||||
import actions from './actions';
|
||||
import defineApp from '../../helpers/define-app.js';
|
||||
import auth from './auth/index.js';
|
||||
import actions from './actions/index.js';
|
||||
|
||||
export default defineApp({
|
||||
name: 'Odoo',
|
||||
@@ -12,5 +12,5 @@ export default defineApp({
|
||||
apiBaseUrl: '',
|
||||
primaryColor: '9c5789',
|
||||
auth,
|
||||
actions
|
||||
actions,
|
||||
});
|
Reference in New Issue
Block a user