Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
langalex committed Nov 30, 2023
1 parent 216d3a6 commit 2fd8c15
Show file tree
Hide file tree
Showing 13 changed files with 304 additions and 373 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,6 @@ typings/
# next.js build output
.next
lib

# vscode
.vscode
44 changes: 12 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,42 +1,22 @@
# ts
# Cobot Zapier Integration

This Zapier integration project is generated by the `zapier init` CLI command.
This is an integration between cobot.me and zapier.com.

These are what you normally do next:
It is based on the [Zapier platform CLI](https://github.com/zapier/zapier-platform/tree/main/packages/cli).

## Commands

```bash
# Install dependencies
npm install # or you can use yarn

# Run tests
zapier test

# Register the integration on Zapier if you haven't
zapier register "App Title"

# Or you can link to an existing integration on Zapier
zapier link

# Push it to Zapier
zapier push
```
yarn test

Find out more on the latest docs: https://github.com/zapier/zapier-platform/blob/main/packages/cli/README.md.

# The "typescript" Template

This example is mainly a proof-of-concept for using features not yet available
in Node.

In `package.json`, we've define some commonly-used scripts:

```bash
# Watch and compile as you edit code
npm run watch
# compile TS to JS
yarn build

# There's also a non-watch compile command
npm run build
# Run Zapier validations
yarn validate

# To push to Zapier, make sure you compile first
npm run build && zapier push
# Push to Zapier
yarn push
```
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
"main": "index.js",
"scripts": {
"test": "jest",
"validate": "npm run build && zapier validate",
"build": "npm run clean && tsc",
"push": "jest && npm run build && zapier push",
"validate": "yarn build && zapier validate",
"build": "yarn clean && tsc",
"push": "jest && yarn build && zapier push",
"clean": "rimraf ./lib ./build",
"watch": "npm run clean && tsc --watch"
"watch": "yarn clean && tsc --watch"
},
"dependencies": {
"lodash": "^4.17.21",
Expand Down
98 changes: 41 additions & 57 deletions src/authentication.ts
Original file line number Diff line number Diff line change
@@ -1,104 +1,88 @@
import { get } from 'lodash';
import { Bundle, HttpRequestOptions, ZObject, } from 'zapier-platform-core';
import { get } from "lodash";
import { Bundle, HttpRequestOptions, ZObject } from "zapier-platform-core";
import { AuthData } from "./types/kontentBundle";

const BASE_URL = `https://www.cobot.me`
const AUTHORIZE_URL = `${BASE_URL}/oauth/authorize`
const ACCESS_TOKEN_URL = `${BASE_URL}/oauth/access_token`
const REFRESH_TOKEN_URL = `${BASE_URL}/oauth/refresh_token`
const TEST_AUTH_URL = `${BASE_URL}/api/user`
const scopes = 'read read_admins read_bookings read_external_bookings read_spaces read_user write_activities write_subscriptions'
const BASE_URL = `https://www.cobot.me`;
const AUTHORIZE_URL = `${BASE_URL}/oauth/authorize`;
const ACCESS_TOKEN_URL = `${BASE_URL}/oauth/access_token`;
const TEST_AUTH_URL = `${BASE_URL}/api/user`;
const scopes =
"read read_admins read_bookings read_external_bookings read_spaces read_user write_activities write_subscriptions";

const getAccessToken = async (z:ZObject, bundle:Bundle) => {
const getAccessToken = async (
z: ZObject,
bundle: Bundle
): Promise<AuthData> => {
const response = await z.request({
url: ACCESS_TOKEN_URL,
method: 'POST',
method: "POST",
body: {
client_id: process.env.CLIENT_ID,
client_secret: process.env.CLIENT_SECRET,
grant_type: 'authorization_code',
grant_type: "authorization_code",
code: bundle.inputData.code,
},
headers: { 'content-type': 'application/x-www-form-urlencoded' },
headers: { "content-type": "application/x-www-form-urlencoded" },
});

const userData = await z.request({
const userData = await z.request({
url: TEST_AUTH_URL,
method: 'GET',
method: "GET",
headers: {
Authorization: 'Bearer ' + response.data.access_token
}
});

const subdomain = get(userData, 'data.admin_of.0.space_subdomain')
const subdomains = get(userData, 'data.admin_of').map((x: { space_subdomain: any; space_name: any; }) => ({id:x.space_subdomain, name: x.space_name}))
const space = get(userData, 'data.admin_of.0.space_name')
const membershipId = get(userData, 'data.memberships.0.id')
const memberships = get(userData, 'data.memberships')

return {
access_token: response.data.access_token,
refresh_token: 'refresh_token',
subdomain,
space,
membershipId,
memberships,
subdomains
};
};

const refreshAccessToken = async (z:ZObject, bundle:Bundle) => {
const response = await z.request({
url: REFRESH_TOKEN_URL,
method: 'POST',
body: {
client_id: process.env.CLIENT_ID,
client_secret: process.env.CLIENT_SECRET,
grant_type: 'refresh_token',
refresh_token: bundle.authData.refresh_token,
Authorization: "Bearer " + response.data.access_token,
},
headers: { 'content-type': 'application/x-www-form-urlencoded' },
});

const adminOf = get(userData, "data.admin_of").map(
(x: { space_subdomain: string; space_name: string }) => ({
subdomain: x.space_subdomain,
name: x.space_name,
})
);

return {
access_token: response.data.access_token,
refresh_token: response.data.refresh_token,
adminOf,
};
};

const includeBearerToken = (request:HttpRequestOptions, z:ZObject, bundle:Bundle) => {
const includeBearerToken = (
request: HttpRequestOptions,
z: ZObject,
bundle: Bundle
) => {
if (bundle.authData.access_token && request.headers) {
request.headers.Authorization = `Bearer ${bundle.authData.access_token}`;
}

return request;
};

const test = (z:ZObject, bundle:Bundle) => z.request({ url: TEST_AUTH_URL });
const test = (z: ZObject, bundle: Bundle) => z.request({ url: TEST_AUTH_URL });

export default {
config: {
type: 'oauth2',
type: "oauth2",
oauth2Config: {
authorizeUrl: {
url: AUTHORIZE_URL,
params: {
client_id: '{{process.env.CLIENT_ID}}',
state: '{{bundle.inputData.state}}',
redirect_uri: '{{bundle.inputData.redirect_uri}}',
response_type: 'code',
scope:scopes
client_id: "{{process.env.CLIENT_ID}}",
state: "{{bundle.inputData.state}}",
redirect_uri: "{{bundle.inputData.redirect_uri}}",
response_type: "code",
scope: scopes,
},
},
getAccessToken,
refreshAccessToken,
autoRefresh: true,
autoRefresh: false,
},

fields: [],

test,

connectionLabel: '{{data.email}}',
connectionLabel: "{{data.email}}",
},
befores: [includeBearerToken],
afters: [],
Expand Down
78 changes: 38 additions & 40 deletions src/creates/activity.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,48 @@
import { Bundle, ZObject } from 'zapier-platform-core';
import { Field } from '../fields/field';
import { getSourceIdsField } from '../fields/getSourceIdsField';
import { KontentBundle } from '../types/kontentBundle';
import { ElementFields } from '../fields/elements/getItemElementFields';
import { createActivity } from '../utils/api';
import { getSubdomainField } from '../fields/getSudomainsField';
import { Field } from "../fields/field";
import { ElementFields } from "../fields/elements/getItemElementFields";
import { createActivity } from "../utils/api";
import { getSubdomainField } from "../fields/getSudomainsField";

const textField: Field = {
label: 'Text',
key: 'text',
type: 'text',
label: "Text",
key: "text",
type: "text",
required: true,
helpText: 'Any text. Can include html link tags. Other tags are removed. URLs are converted into links.'
}
helpText:
"The text to post on the dashboard. Can include html link tags. Other tags are removed. URLs are converted into links.",
};

const levelChoiceField: Field = {
label: 'Level',
key: 'level',
type: 'string',
label: "Level",
key: "level",
type: "string",
choices: [
{ sample: 'ERROR', value: 'ERROR', label: 'ERROR'},
{ sample: 'WARN', value: 'WARN', label: 'WARN'},
{ sample: "ERROR", value: "ERROR", label: "ERROR" },
{ sample: "WARN", value: "WARN", label: "WARN" },
],
helpText: 'can be ERROR, WARN or empty'
}
helpText: "can be ERROR, WARN or empty",
};

const channelMultiChoiceField: Field = {
label: 'Channels',
key: 'channels',
type: 'string',
label: "Channels",
key: "channels",
type: "string",
choices: [
{ sample: 'admin', value: 'admin', label: 'admin'},
{ sample: 'membership', value: 'membership', label: 'membership'},
{ sample: "admin", value: "admin", label: "admin" },
{ sample: "membership", value: "membership", label: "membership" },
],
default: 'admin',
default: "admin",
list: true,
}
helpText: "Whether to post an activity on the admin or member dashboard.",
};

export default {
key: 'create_activity',
noun: 'Create Activity',
key: "create_activity",
noun: "Create Activity",

display: {
label: 'Create Activity',
description: 'Creates an Activity.',
label: "Create Activity",
description: "Creates an Activity.",
},

operation: {
Expand All @@ -53,16 +52,15 @@ export default {
textField,
levelChoiceField,
channelMultiChoiceField,
getSourceIdsField(),
],
sample: {
"created_at": "2013/05/04 12:00:00 +0000",
"type": "text",
"channels": ["admin", "membership"],
"level": "ERROR",
"attributes": {
"text": "Some text about your activity."
}
created_at: "2013/05/04 12:00:00 +0000",
type: "text",
channels: ["admin", "membership"],
level: "ERROR",
attributes: {
text: "Some text about your activity.",
},
},
},
};
Expand All @@ -72,5 +70,5 @@ export type InputData = Readonly<{
text: string;
level: string;
channels: string[];
source_ids?: string[];
}> & ElementFields;
}> &
ElementFields;
11 changes: 0 additions & 11 deletions src/fields/getSourceIdsField.ts

This file was deleted.

25 changes: 11 additions & 14 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import { version as platformVersion } from 'zapier-platform-core';
import auth from './authentication';
import { version as platformVersion } from "zapier-platform-core";
import auth from "./authentication";

import ActivityCreate from './creates/activity'
import ActivityCreate from "./creates/activity";

import getMemberships from './triggers/dropdowns/getMemberships';
import triggerBookingCreated from "./triggers/triggerBookingCreated";
import triggerBookingWillBegin from "./triggers/triggerBookingWillBegin";
import triggerMembershipConfirmed from "./triggers/triggerMembershipConfirmed";
import triggerMembershipPlanChanged from "./triggers/triggerMembershipPlanChanged";
import triggerExternalBooking from "./triggers/triggerExternalBooking";
import getSubdomains from "./triggers/dropdowns/getSubdomains";

import triggerBookingCreated from './triggers/triggerBookingCreated';
import triggerBookingWillBegin from './triggers/triggerBookingWillBegin';
import triggerMembershipConfirmed from './triggers/triggerMembershipConfirmed';
import triggerMembershipPlanChanged from './triggers/triggerMembershipPlanChanged';
import triggerExternalBooking from './triggers/triggerExternalBooking';
import getSubdomains from './triggers/dropdowns/getSubdomains';

const { version } = require('../package.json');
const { version } = require("../package.json");

export default {
version,
Expand All @@ -34,8 +32,7 @@ export default {
[triggerMembershipPlanChanged.key]: triggerMembershipPlanChanged,
[triggerExternalBooking.key]: triggerExternalBooking,
// Lists for dropdowns
[getMemberships.key]: getMemberships,
[getSubdomains.key]: getSubdomains
[getSubdomains.key]: getSubdomains,
},

creates: {
Expand Down
Loading

0 comments on commit 2fd8c15

Please sign in to comment.