From 5afab4ba9076126a9113f453b294cd84e984ba77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20FRANCE?= Date: Fri, 30 Oct 2020 20:57:43 +0100 Subject: [PATCH] zapier on ticket create --- dev/examples/zapier/triggers/ticket.js | 233 ++++++++++++++++++ ...face_99_modZapier_ZapierTriggers.class.php | 6 - 2 files changed, 233 insertions(+), 6 deletions(-) create mode 100644 dev/examples/zapier/triggers/ticket.js diff --git a/dev/examples/zapier/triggers/ticket.js b/dev/examples/zapier/triggers/ticket.js new file mode 100644 index 00000000000..78f55495f8b --- /dev/null +++ b/dev/examples/zapier/triggers/ticket.js @@ -0,0 +1,233 @@ +const subscribeHook = (z, bundle) => { + // `z.console.log()` is similar to `console.log()`. + z.console.log('suscribing hook!'); + + // bundle.targetUrl has the Hook URL this app should call when an action is created. + const data = { + url: bundle.targetUrl, + event: bundle.event, + module: 'ticket', + action: bundle.inputData.action + }; + + const url = bundle.authData.url + '/api/index.php/zapierapi/hook'; + + // You can build requests and our client will helpfully inject all the variables + // you need to complete. You can also register middleware to control this. + const options = { + url: url, + method: 'POST', + body: data, + }; + + // You may return a promise or a normal data structure from any perform method. + return z.request(options).then((response) => JSON.parse(response.content)); +}; + +const unsubscribeHook = (z, bundle) => { + // bundle.subscribeData contains the parsed response JSON from the subscribe + // request made initially. + z.console.log('unsuscribing hook!'); + + // You can build requests and our client will helpfully inject all the variables + // you need to complete. You can also register middleware to control this. + const options = { + url: bundle.authData.url + '/api/index.php/zapierapi/hook/' + bundle.subscribeData.id, + method: 'DELETE', + }; + + // You may return a promise or a normal data structure from any perform method. + return z.request(options).then((response) => JSON.parse(response.content)); +}; + +const getTicket = (z, bundle) => { + // bundle.cleanedRequest will include the parsed JSON object (if it's not a + // test poll) and also a .querystring property with the URL's query string. + const ticket = { + id: bundle.cleanedRequest.id, + lastname: bundle.cleanedRequest.lastname, + firstname: bundle.cleanedRequest.firstname, + address: bundle.cleanedRequest.address, + zip: bundle.cleanedRequest.zip, + town: bundle.cleanedRequest.town, + email: bundle.cleanedRequest.email, + login: bundle.cleanedRequest.login, + authorId: bundle.cleanedRequest.authorId, + createdAt: bundle.cleanedRequest.createdAt, + action: bundle.cleanedRequest.action + }; + + return [ticket]; +}; + +const getFallbackRealTicket = (z, bundle) => { + // For the test poll, you should get some real data, to aid the setup process. + const module = bundle.inputData.module; + const options = { + url: bundle.authData.url + '/api/index.php/tickets/0', + }; + + return z.request(options).then((response) => [JSON.parse(response.content)]); +}; + +// const getModulesChoices = (z/*, bundle*/) => { +// // For the test poll, you should get some real data, to aid the setup process. +// const options = { +// url: bundle.authData.url + '/api/index.php/zapierapi/getmoduleschoices', +// }; + +// return z.request(options).then((response) => JSON.parse(response.content)); +// }; +// const getModulesChoices = () => { + +// return { +// orders: "Order", +// invoices: "Invoice", +// thirdparties: "Thirdparty", +// users: "User", +// tickets: "Ticket", +// contacts: "Contacts" +// }; +// }; + +// const getActionsChoices = (z, bundle) => { +// // For the test poll, you should get some real data, to aid the setup process. +// const module = bundle.inputData.module; +// const options = { +// url: url: bundle.authData.url + '/api/index.php/zapierapi/getactionschoices/thirparty`, +// }; + +// return z.request(options).then((response) => JSON.parse(response.content)); +// }; + +// We recommend writing your triggers separate like this and rolling them +// into the App definition at the end. +module.exports = { + key: 'ticket', + + // You'll want to provide some helpful display labels and descriptions + // for tickets. Zapier will put them into the UX. + noun: 'Ticket', + display: { + label: 'New Ticket', + description: 'Triggers when a new ticket action is done in Dolibarr.' + }, + + // `operation` is where the business logic goes. + operation: { + + // `inputFields` can define the fields a ticket could provide, + // we'll pass them in as `bundle.inputData` later. + inputFields: [ + { + key: 'action', + type: 'string', + helpText: 'Which action of ticket this should trigger on.', + choices: { + create: "Create", + modify: "Modify", + validate: "Validate", + } + } + ], + + type: 'hook', + + performSubscribe: subscribeHook, + performUnsubscribe: unsubscribeHook, + + perform: getTicket, + performList: getFallbackRealTicket, + + // In cases where Zapier needs to show an example record to the user, but we are unable to get a live example + // from the API, Zapier will fallback to this hard-coded sample. It should reflect the data structure of + // returned records, and have obviously dummy values that we can show to any user. + sample: { + id: 1, + track_id: 'Xaz123er', + subject: 'Subject', + message: 'Message', + createdAt: 1472069465, + lastname: 'DOE', + firstname: 'John', + email: 'john@doe.com', + address: 'Park Avenue', + zip: '12345', + town: 'NEW-YORK', + email_from: 'doe.john@example;com', + authorId: 1, + action: 'create' + }, + + // If the resource can have fields that are custom on a per-user basis, define a function to fetch the custom + // field definitions. The result will be used to augment the sample. + // outputFields: () => { return []; } + // Alternatively, a static field definition should be provided, to specify labels for the fields + outputFields: [ + { + key: 'id', + type: "integer", + label: 'ID' + }, + { + key: 'track_id', + type: "string", + label: 'TrackID' + }, + { + key: 'subject', + type: "string", + label: 'Subject' + }, + { + key: 'message', + type: "string", + label: 'Message' + }, + { + key: 'createdAt', + type: "integer", + label: 'Created At' + }, + { + key: 'lastname', + label: 'Lastname' + }, + { + key: 'firstname', + label: 'Firstname' + }, + { + key: 'email', + label: 'Email' + }, + { + key: 'address', + label: 'Address' + }, + { + key: 'zip', + label: 'Zip' + }, + { + key: 'town', + label: 'Town' + }, + { + key: 'email_from', + type: 'string', + label: 'Email from' + }, + { + key: 'authorId', + type: "integer", + label: 'Author ID' + }, + { + key: 'action', + type: 'string', + label: 'Action' + } + ] + } +}; diff --git a/htdocs/core/triggers/interface_99_modZapier_ZapierTriggers.class.php b/htdocs/core/triggers/interface_99_modZapier_ZapierTriggers.class.php index 56a683f6f3d..cb36f0fcd3a 100644 --- a/htdocs/core/triggers/interface_99_modZapier_ZapierTriggers.class.php +++ b/htdocs/core/triggers/interface_99_modZapier_ZapierTriggers.class.php @@ -80,7 +80,6 @@ class InterfaceZapierTriggers extends DolibarrTriggers $actions = explode('_', $action); $sql = 'SELECT rowid, url FROM '.MAIN_DB_PREFIX.'zapier_hook'; $sql .= ' WHERE module="'.$this->db->escape(strtolower($actions[0])).'" AND action="'.$this->db->escape(strtolower($actions[1])).'"'; - //setEventMessages($sql, null); } switch ($action) { @@ -93,7 +92,6 @@ class InterfaceZapierTriggers extends DolibarrTriggers $json = json_encode($cleaned); // call the zapierPostWebhook() function zapierPostWebhook($obj['url'], $json); - //setEventMessages($obj['url'], null); } $logtriggeraction = true; break; @@ -105,7 +103,6 @@ class InterfaceZapierTriggers extends DolibarrTriggers $json = json_encode($cleaned); // call the zapierPostWebhook() function zapierPostWebhook($obj['url'], $json); - //setEventMessages($obj['url'], null); } $logtriggeraction = true; break; @@ -133,7 +130,6 @@ class InterfaceZapierTriggers extends DolibarrTriggers $json = json_encode($cleaned); // call the zapierPostWebhook() function zapierPostWebhook($obj['url'], $json); - //setEventMessages($obj['url'], null); } $logtriggeraction = true; break; @@ -352,11 +348,9 @@ class InterfaceZapierTriggers extends DolibarrTriggers // TODO voir comment regrouper les webhooks en un post while ($resql && $obj = $this->db->fetch_array($resql)) { $cleaned = cleanObjectDatas(dol_clone($object)); - $cleaned = cleanAgendaEventsDatas($cleaned); $json = json_encode($cleaned); // call the zapierPostWebhook() function zapierPostWebhook($obj['url'], $json); - //setEventMessages($obj['url'], null); } $logtriggeraction = true; break;