2049 timebox 1j zapier integration 4 define and implement a first trigger for zapier app (#2132)

* Add create company trigger

* Refactor

* Add operation in subscribe

* Add create hook api endpoint

* Add import of hook module

* Add a test for hook subscribe

* Add delete hook api endpoint

* Add delete hook test

* Add findMany hook route

---------

Co-authored-by: Charles Bochet <charles@twenty.com>
This commit is contained in:
martmull
2023-10-19 22:48:34 +02:00
committed by GitHub
parent 5ce8b4c73c
commit e9092162e0
17 changed files with 413 additions and 62 deletions

View File

@ -1,26 +1,16 @@
import { Bundle, ZObject } from 'zapier-platform-core';
import handleQueryParams from '../utils/handleQueryParams';
import requestDb from '../utils/requestDb';
const perform = async (z: ZObject, bundle: Bundle) => {
const response = await z.request({
body: {
query: `
mutation CreateCompany {
createOneCompany(
data:{${handleQueryParams(bundle.inputData)}}
)
{id}
}`,
},
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
Authorization: `Bearer ${bundle.authData.apiKey}`,
},
method: 'POST',
url: `${process.env.SERVER_BASE_URL}/graphql`,
});
return response.json;
const query = `
mutation CreateCompany {
createOneCompany(
data:{${handleQueryParams(bundle.inputData)}}
)
{id}
}`;
return await requestDb(z, bundle, query);
};
export default {
display: {

View File

@ -1,26 +1,16 @@
import { Bundle, ZObject } from 'zapier-platform-core';
import handleQueryParams from '../utils/handleQueryParams';
import requestDb from '../utils/requestDb';
const perform = async (z: ZObject, bundle: Bundle) => {
const response = await z.request({
body: {
query: `
mutation CreatePerson {
createOnePerson(
data:{${handleQueryParams(bundle.inputData)}}
)
{id}
}`,
},
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
Authorization: `Bearer ${bundle.authData.apiKey}`,
},
method: 'POST',
url: `${process.env.SERVER_BASE_URL}/graphql`,
});
return response.json;
const query = `
mutation CreatePerson {
createOnePerson(
data:{${handleQueryParams(bundle.inputData)}}
)
{id}
}`;
return await requestDb(z, bundle, query);
};
export default {
display: {

View File

@ -2,13 +2,17 @@ const { version } = require('../package.json');
import { version as platformVersion } from 'zapier-platform-core';
import createPerson from './creates/create_person';
import createCompany from './creates/create_company';
import company from './triggers/company';
import authentication from './authentication';
import 'dotenv/config'
import 'dotenv/config';
export default {
version,
platformVersion,
authentication: authentication,
triggers: {
[company.key]: company,
},
creates: {
[createPerson.key]: createPerson,
[createCompany.key]: createCompany,

View File

@ -7,33 +7,22 @@ import {
ZObject,
} from 'zapier-platform-core';
import getBundle from '../utils/getBundle';
import handleQueryParams from '../utils/handleQueryParams';
import requestDb from '../utils/requestDb';
const appTester = createAppTester(App);
tools.env.inject();
const generateKey = async (z: ZObject, bundle: Bundle) => {
const options = {
url: `${process.env.SERVER_BASE_URL}/graphql`,
method: 'POST',
headers: {
Authorization: `Bearer ${bundle.authData.apiKey}`,
},
body: {
query: `mutation
CreateApiKey {
createOneApiKey(data:{
name:"${bundle.inputData.name}",
expiresAt: "${bundle.inputData.expiresAt}"
}) {token}}`,
},
} satisfies HttpRequestOptions;
return z.request(options).then((response) => {
const results = response.json;
return results.data.createOneApiKey.token;
});
const query = `
mutation CreateApiKey {
createOneApiKey(
data:{${handleQueryParams(bundle.inputData)}}
)
{token}
}`;
return (await requestDb(z, bundle, query)).data.createOneApiKey.token;
};
const apiKey = String(process.env.API_KEY);
describe('custom auth', () => {
it('passes authentication and returns json', async () => {
const bundle = getBundle();

View File

@ -0,0 +1,90 @@
import { Bundle, createAppTester, ZObject } from 'zapier-platform-core';
import App from '../../index';
import getBundle from '../../utils/getBundle';
import requestDb from '../../utils/requestDb';
const appTester = createAppTester(App);
describe('triggers.company', () => {
test('should succeed to subscribe', async () => {
const bundle = getBundle({});
bundle.targetUrl = 'https://test.com';
const result = await appTester(
App.triggers.company.operation.performSubscribe,
bundle,
);
expect(result).toBeDefined();
expect(result.id).toBeDefined();
const checkDbResult = await appTester(
(z: ZObject, bundle: Bundle) =>
requestDb(
z,
bundle,
`query findManyHook {findManyHook(where: {id: {equals: "${result.id}"}}){id operation}}`,
),
bundle,
);
expect(checkDbResult.data.findManyHook.length).toEqual(1);
expect(checkDbResult.data.findManyHook[0].operation).toEqual(
'createOneCompany',
);
});
test('should succeed to unsubscribe', async () => {
const bundle = getBundle({});
bundle.targetUrl = 'https://test.com';
const result = await appTester(
App.triggers.company.operation.performSubscribe,
bundle,
);
const unsubscribeBundle = getBundle({});
unsubscribeBundle.subscribeData = { id: result.id };
const unsubscribeResult = await appTester(
App.triggers.company.operation.performUnsubscribe,
unsubscribeBundle,
);
expect(unsubscribeResult).toBeDefined();
expect(unsubscribeResult.id).toEqual(result.id);
const checkDbResult = await appTester(
(z: ZObject, bundle: Bundle) =>
requestDb(
z,
bundle,
`query findManyHook {findManyHook(where: {id: {equals: "${result.id}"}}){id}}`,
),
bundle,
);
expect(checkDbResult.data.findManyHook.length).toEqual(0);
});
test('should load company from hook', async () => {
const bundle = {
cleanedRequest: {
id: 'd6ccb1d1-a90b-4822-a992-a0dd946592c9',
name: '',
domainName: '',
createdAt: '2023-10-19 10:10:12.490',
address: '',
employees: null,
linkedinUrl: null,
xUrl: null,
annualRecurringRevenue: null,
idealCustomerProfile: false,
},
};
const results = await appTester(
App.triggers.company.operation.perform,
bundle,
);
expect(results.length).toEqual(1);
const company = results[0];
expect(company.id).toEqual('d6ccb1d1-a90b-4822-a992-a0dd946592c9');
});
it('should load companies from list', async () => {
const bundle = getBundle({});
const results = await appTester(
App.triggers.company.operation.performList,
bundle,
);
expect(results.length).toBeGreaterThan(1);
const firstCompany = results[0];
expect(firstCompany.id).toBeDefined();
});
});

View File

@ -0,0 +1,74 @@
import { Bundle, ZObject } from 'zapier-platform-core';
import requestDb from '../utils/requestDb';
import handleQueryParams from '../utils/handleQueryParams';
const performSubscribe = async (z: ZObject, bundle: Bundle) => {
const data = { targetUrl: bundle.targetUrl, operation: 'createOneCompany' };
const result = await requestDb(
z,
bundle,
`mutation createOneHook {createOneHook(data:{${handleQueryParams(
data,
)}}) {id}}`,
);
return result.data.createOneHook;
};
const performUnsubscribe = async (z: ZObject, bundle: Bundle) => {
const data = { id: bundle.subscribeData?.id };
const result = await requestDb(
z,
bundle,
`mutation deleteOneHook {deleteOneHook(where:{${handleQueryParams(
data,
)}}) {id}}`,
);
return result.data.deleteOneHook;
};
const perform = (z: ZObject, bundle: Bundle) => {
return [bundle.cleanedRequest];
};
const performList = async (z: ZObject, bundle: Bundle) => {
const results = await requestDb(
z,
bundle,
`query FindManyCompany {findManyCompany {
id
name
domainName
createdAt
address
employees
linkedinUrl
xUrl
annualRecurringRevenue
idealCustomerProfile
}}`,
);
return results.data.findManyCompany;
};
export default {
key: 'company',
noun: 'Company',
display: {
label: 'New Company',
description: 'Triggers when a new company is created.',
},
operation: {
inputFields: [],
type: 'hook',
performSubscribe,
performUnsubscribe,
perform,
performList,
sample: {
id: 'f75f6b2e-9442-4c72-aa95-47d8e5ec8cb3',
createdAt: '2023-10-19 07:37:25.306',
workspaceId: 'c8b070fc-c969-4ca5-837a-e7c3735734d2',
},
outputFields: [
{ key: 'id', label: 'ID' },
{ key: 'createdAt', label: 'Created At' },
{ key: 'workspaceId', label: 'Workspace ID' },
],
},
};

View File

@ -1,7 +1,20 @@
const getBundle = (inputData?: object) => {
import { Bundle } from 'zapier-platform-core';
const getBundle = (inputData?: { [x: string]: any }): Bundle => {
return {
authData: { apiKey: String(process.env.API_KEY) },
inputData,
inputData: inputData || {},
cleanedRequest: {},
inputDataRaw: {},
meta: {
isBulkRead: false,
isFillingDynamicDropdown: false,
isLoadingSample: false,
isPopulatingDedupe: false,
isTestingAuth: false,
limit: 1,
page: 1,
},
};
};
export default getBundle;

View File

@ -5,6 +5,8 @@ const requestDb = async (z: ZObject, bundle: Bundle, query: string) => {
url: `${process.env.SERVER_BASE_URL}/graphql`,
method: 'POST',
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
Authorization: `Bearer ${bundle.authData.apiKey}`,
},
body: {