Skip to content

Commit 1725ae6

Browse files
authored
New Components - lawmatics (#18271)
* new components * pnpm-lock.yaml * update contactId prop label
1 parent 99d800d commit 1725ae6

File tree

7 files changed

+328
-8
lines changed

7 files changed

+328
-8
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import lawmatics from "../../lawmatics.app.mjs";
2+
import { parseObject } from "../../common/utils.mjs";
3+
4+
export default {
5+
key: "lawmatics-create-contact",
6+
name: "Create Contact",
7+
description: "Create a new contact. [See the documentation](https://docs.lawmatics.com/#714b9275-b769-4195-9954-2095479b9993)",
8+
version: "0.0.1",
9+
type: "action",
10+
props: {
11+
lawmatics,
12+
firstName: {
13+
propDefinition: [
14+
lawmatics,
15+
"firstName",
16+
],
17+
},
18+
lastName: {
19+
propDefinition: [
20+
lawmatics,
21+
"lastName",
22+
],
23+
},
24+
email: {
25+
propDefinition: [
26+
lawmatics,
27+
"email",
28+
],
29+
},
30+
phone: {
31+
propDefinition: [
32+
lawmatics,
33+
"phone",
34+
],
35+
},
36+
notes: {
37+
propDefinition: [
38+
lawmatics,
39+
"notes",
40+
],
41+
},
42+
},
43+
async run({ $ }) {
44+
const { data } = await this.lawmatics.createContact({
45+
$,
46+
data: {
47+
first_name: this.firstName,
48+
last_name: this.lastName,
49+
email: this.email,
50+
phone: this.phone,
51+
notes: parseObject(this.notes),
52+
},
53+
});
54+
$.export("$summary", `Successfully created contact: ${data.id}`);
55+
return data;
56+
},
57+
};
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import lawmatics from "../../lawmatics.app.mjs";
2+
import { parseObject } from "../../common/utils.mjs";
3+
4+
export default {
5+
key: "lawmatics-update-contact",
6+
name: "Update Contact",
7+
description: "Update a contact. [See the documentation](https://docs.lawmatics.com/#4734312c-854c-46c7-b8f9-962f775f1ab2)",
8+
version: "0.0.1",
9+
type: "action",
10+
props: {
11+
lawmatics,
12+
contactId: {
13+
propDefinition: [
14+
lawmatics,
15+
"contactId",
16+
],
17+
},
18+
firstName: {
19+
propDefinition: [
20+
lawmatics,
21+
"firstName",
22+
],
23+
},
24+
lastName: {
25+
propDefinition: [
26+
lawmatics,
27+
"lastName",
28+
],
29+
},
30+
email: {
31+
propDefinition: [
32+
lawmatics,
33+
"email",
34+
],
35+
},
36+
phone: {
37+
propDefinition: [
38+
lawmatics,
39+
"phone",
40+
],
41+
},
42+
notes: {
43+
propDefinition: [
44+
lawmatics,
45+
"notes",
46+
],
47+
},
48+
},
49+
async run({ $ }) {
50+
const { data } = await this.lawmatics.updateContact({
51+
$,
52+
contactId: this.contactId,
53+
data: {
54+
first_name: this.firstName,
55+
last_name: this.lastName,
56+
email: this.email,
57+
phone: this.phone,
58+
notes: parseObject(this.notes),
59+
},
60+
});
61+
$.export("$summary", `Successfully updated contact: ${data.id}`);
62+
return data;
63+
},
64+
};

components/lawmatics/common/utils.mjs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
export const parseObject = (obj) => {
2+
if (!obj) return undefined;
3+
4+
if (typeof obj === "string") {
5+
try {
6+
return JSON.parse(obj);
7+
} catch (error) {
8+
return obj;
9+
}
10+
}
11+
12+
if (Array.isArray(obj)) {
13+
return obj.map(parseObject);
14+
}
15+
16+
if (typeof obj === "object") {
17+
return Object.fromEntries(Object.entries(obj).map(([
18+
key,
19+
value,
20+
]) => [
21+
key,
22+
parseObject(value),
23+
]));
24+
}
25+
26+
return obj;
27+
};
Lines changed: 106 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,113 @@
1+
import { axios } from "@pipedream/platform";
2+
13
export default {
24
type: "app",
35
app: "lawmatics",
4-
propDefinitions: {},
6+
propDefinitions: {
7+
contactId: {
8+
type: "string",
9+
label: "Contact ID",
10+
description: "The ID of the contact",
11+
async options({ page }) {
12+
const { data } = await this.listContacts({
13+
params: {
14+
page: page + 1,
15+
},
16+
});
17+
return data.map(({
18+
id: value, attributes: { email },
19+
}) => ({
20+
value,
21+
label: email,
22+
}));
23+
},
24+
},
25+
firstName: {
26+
type: "string",
27+
label: "First Name",
28+
description: "The first name of the contact",
29+
},
30+
lastName: {
31+
type: "string",
32+
label: "Last Name",
33+
description: "The last name of the contact",
34+
},
35+
email: {
36+
type: "string",
37+
label: "Email",
38+
description: "The email of the contact",
39+
},
40+
phone: {
41+
type: "string",
42+
label: "Phone",
43+
description: "The phone number of the contact",
44+
optional: true,
45+
},
46+
notes: {
47+
type: "string[]",
48+
label: "Notes",
49+
description: "An array of notes for the contact. Each note is an object with properties `name` and `body`. Example: `[{ name: 'Note 1', body: 'This is a note' }, { name: 'Note 2', body: 'This is another note' }]`",
50+
optional: true,
51+
},
52+
},
553
methods: {
6-
// this.$auth contains connected account data
7-
authKeys() {
8-
console.log(Object.keys(this.$auth));
54+
_baseUrl() {
55+
return "https://api.lawmatics.com/v1";
56+
},
57+
_makeRequest({
58+
$ = this, path, ...opts
59+
}) {
60+
return axios($, {
61+
url: `${this._baseUrl()}${path}`,
62+
headers: {
63+
Authorization: `Bearer ${this.$auth.oauth_access_token}`,
64+
},
65+
...opts,
66+
});
67+
},
68+
listContacts(opts = {}) {
69+
return this._makeRequest({
70+
path: "/contacts",
71+
...opts,
72+
});
73+
},
74+
createContact(opts = {}) {
75+
return this._makeRequest({
76+
path: "/contacts",
77+
method: "post",
78+
...opts,
79+
});
80+
},
81+
updateContact({
82+
contactId, ...opts
83+
}) {
84+
return this._makeRequest({
85+
path: `/contacts/${contactId}`,
86+
method: "put",
87+
...opts,
88+
});
89+
},
90+
async *paginate({
91+
fn, params, max,
92+
}) {
93+
params = {
94+
...params,
95+
page: 1,
96+
};
97+
let totalPages, count = 0;
98+
do {
99+
const { data } = await fn({
100+
params,
101+
});
102+
for (const item of data) {
103+
yield item;
104+
if (max && ++count >= max) {
105+
return;
106+
}
107+
}
108+
totalPages = data?.meta?.total_pages;
109+
params.page++;
110+
} while (params.page <= totalPages);
9111
},
10112
},
11113
};

components/lawmatics/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/lawmatics",
3-
"version": "0.6.0",
3+
"version": "0.7.0",
44
"description": "Pipedream lawmatics Components",
55
"main": "lawmatics.app.mjs",
66
"keywords": [
@@ -13,6 +13,6 @@
1313
"access": "public"
1414
},
1515
"dependencies": {
16-
"@pipedream/platform": "^3.0.0"
16+
"@pipedream/platform": "^3.1.0"
1717
}
1818
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import lawmatics from "../../lawmatics.app.mjs";
2+
import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform";
3+
4+
export default {
5+
key: "lawmatics-new-contact-added",
6+
name: "New Contact Added",
7+
description: "Emit new event when a contact is added. [See the documentation](https://docs.lawmatics.com/#555909b6-ebb0-4b8b-9e56-1dceb1abb7b7)",
8+
version: "0.0.1",
9+
type: "source",
10+
props: {
11+
lawmatics,
12+
db: "$.service.db",
13+
timer: {
14+
type: "$.interface.timer",
15+
default: {
16+
intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL,
17+
},
18+
},
19+
},
20+
methods: {
21+
_getLastTs() {
22+
return this.db.get("lastTs") || 0;
23+
},
24+
_setLastTs(ts) {
25+
this.db.set("lastTs", ts);
26+
},
27+
generateMeta(contact) {
28+
return {
29+
id: contact.id,
30+
summary: `${contact.attributes.first_name} ${contact.attributes.last_name} ${contact.attributes.email}`.trim() || contact.id,
31+
ts: Date.parse(contact.attributes.created_at),
32+
};
33+
},
34+
async processEvents(max) {
35+
const lastTs = this._getLastTs();
36+
let newLastTs;
37+
38+
const contacts = await this.lawmatics.paginate({
39+
fn: this.lawmatics.listContacts,
40+
params: {
41+
sort_by: "created_at",
42+
sort_order: "desc",
43+
},
44+
max,
45+
});
46+
47+
for await (const contact of contacts) {
48+
if (Date.parse(contact.attributes.created_at) <= lastTs) {
49+
break;
50+
}
51+
this.$emit(contact, this.generateMeta(contact));
52+
if (!newLastTs) {
53+
newLastTs = Date.parse(contact.attributes.created_at);
54+
}
55+
}
56+
57+
if (newLastTs) {
58+
this._setLastTs(newLastTs);
59+
}
60+
},
61+
},
62+
hooks: {
63+
async deploy() {
64+
await this.processEvents(25);
65+
},
66+
},
67+
async run() {
68+
await this.processEvents();
69+
},
70+
};

pnpm-lock.yaml

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)