Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 29 additions & 7 deletions configurator.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {platformSDK} from './sdks/platformSDK'
import { {{&type}}SDK } from './sdks/{{&type}}SDK'
{{/variables}}

const {register} = require('serverless-cloud-elements-runtime')
const {register, buildConfig} = require('serverless-cloud-elements-runtime')

interface Configuration {
{{#variables}}
Expand Down Expand Up @@ -33,17 +33,31 @@ instanceId: number,
instanceName: string
}

export function configurator(input: any) : {trigger: Trigger, config: Configuration, platform: platformSDK, done: any} {
export async function configurator(input: any) : {trigger: Trigger, config: Configuration, platform: platformSDK, done: any} {
let body = input[0].body;
let trigger
if (body) {
body = typeof body === 'object' ? body : JSON.parse(body);
const trigger = body.message;
trigger = body.message;
}
const config = {

const config = await buildConfig(
input[1],
input[0].path,
body && body.message && body.message.instanceId,
'{{&baseUrl}}',
'{{&authHeader}}',
[
{{#variables}}
{{&name}}: new {{&type}}SDK('{{&baseUrl}}', '{{&authHeader}}{{#token}}, Element {{&token}}{{/token}}'),
{
name: '{{&name}}',
type: '{{&type}}',
token: '{{&token}}',
id: '{{&id}}'
}
{{/variables}}
};
null
])

{{#variables}}
register({{&type}}SDK, obj => new {{&type}}SDK(obj.domain.replace('/elements/api-v2', ''), obj.authorizationHeader))
Expand All @@ -53,5 +67,13 @@ const platform = new platformSDK('{{&baseUrl}}', '{{&authHeader}}');

const done = response => input[2](null, {statusCode: 200, body: JSON.stringify(response)});

return {trigger, config, platform, done}
const result = {trigger, config, platform, done}

if (config) {
for (let key of Object.keys(config)) {
result[key] = config[key]
}
}

return result
}
28 changes: 12 additions & 16 deletions example/contactSync.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
const {configurator} = require('./configurator');

async function syncContact(myContact, dest, total) {
const foundContacts = await dest.getByObjectName('myContact').where(`Email='${myContact.Email}'`).run();
if (foundContacts.length === 1) {
await dest.replaceObjectNameByObjectId('myContact', foundContacts[0].Id, myContact).run();
console.log(`${foundContacts[0].Id} updated`);
} else {
const newContact = await dest.createByObjectName('myContact', myContact).run();
console.log(`${newContact.Id} created`);
}
}

async function eventHandler() {
const {trigger, config} = configurator(arguments);
for (let i = 0; i < trigger.events.length; i++) {
const event = trigger.events[i];
const myContact = await config.source.getMyContactById(event.objectId).run();
const {trigger, source, dest} = await configurator(arguments);
$checkpoint('eventReceived');
for (let event of trigger.events) {
const myContact = await source.getMyContactById(event.objectId);
if (myContact.Email) {
await syncContact(myContact, config.dest, trigger.events.length);
const foundContacts = await dest.getByObjectName('myContact').where(`Email='${myContact.Email}'`);
if (foundContacts.length === 1) {
await dest.replaceObjectNameByObjectId('myContact', foundContacts[0].Id, myContact);
console.log(`${foundContacts[0].Id} updated`);
} else {
const newContact = await dest.createByObjectName('myContact', myContact);
console.log(`${newContact.Id} created`);
}
}
}
}
Expand Down
Loading