Skip to content

Commit b57dba8

Browse files
committed
[Components] motive #15221
Sources - New Hours Of Service Violation (Instant) - New Safety Event (Instant) Actions - Find User Details
1 parent 6494014 commit b57dba8

File tree

11 files changed

+347
-309
lines changed

11 files changed

+347
-309
lines changed

components/motive/actions/find-user-details/find-user-details.mjs

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,30 @@
1+
import { ConfigurationError } from "@pipedream/platform";
2+
import { clearObj } from "../../common/util.mjs";
13
import motive from "../../motive.app.mjs";
2-
import { axios } from "@pipedream/platform";
34

45
export default {
56
key: "motive-find-user-details",
67
name: "Find User Details",
78
description: "Retrieve user details based on specific criteria. [See the documentation](https://developer.gomotive.com/reference/users-lookup)",
8-
version: "0.0.{{ts}}",
9+
version: "0.0.1",
910
type: "action",
1011
props: {
1112
motive,
13+
alert: {
14+
type: "alert",
15+
alertType: "info",
16+
content: "If you provide more than one prop, only one will be considered, the others will be ignored.",
17+
},
1218
username: {
13-
propDefinition: [
14-
motive,
15-
"username",
16-
],
19+
type: "string",
20+
label: "Username",
21+
description: "Username to retrieve user details",
1722
optional: true,
1823
},
1924
email: {
20-
propDefinition: [
21-
motive,
22-
"email",
23-
],
25+
type: "string",
26+
label: "Email",
27+
description: "Email to retrieve user details",
2428
optional: true,
2529
},
2630
driverCompanyId: {
@@ -30,35 +34,41 @@ export default {
3034
],
3135
optional: true,
3236
},
33-
phone: {
34-
propDefinition: [
35-
motive,
36-
"phone",
37-
],
38-
optional: true,
39-
},
4037
},
4138
async run({ $ }) {
39+
let response;
40+
const criteria = [];
41+
4242
const {
43-
username, email, driverCompanyId, phone,
43+
username, email, driverCompanyId,
4444
} = this;
45-
if (!username && !email && !driverCompanyId && !phone) {
46-
throw new Error("At least one of 'username', 'email', 'driverCompanyId', or 'phone' must be provided.");
45+
if (!username && !email && !driverCompanyId) {
46+
throw new Error("At least one of 'Username', 'Email', 'Driver Company Id' must be provided.");
4747
}
4848

49-
const response = await this.motive.retrieveUserDetails({
50-
username,
51-
email,
52-
driverCompanyId,
53-
phone,
54-
});
55-
56-
const criteria = [];
5749
if (username) criteria.push(`username: ${username}`);
5850
if (email) criteria.push(`email: ${email}`);
5951
if (driverCompanyId) criteria.push(`driverCompanyId: ${driverCompanyId}`);
60-
if (phone) criteria.push(`phone: ${phone}`);
6152

53+
try {
54+
response = await this.motive.retrieveUserDetails({
55+
$,
56+
params: clearObj({
57+
username,
58+
email,
59+
driver_company_id: driverCompanyId,
60+
}),
61+
});
62+
63+
} catch ({ response: { data } }) {
64+
if (data.error_message === "user not found") {
65+
response = {
66+
user: null,
67+
};
68+
} else {
69+
throw new ConfigurationError(data.error_message);
70+
}
71+
}
6272
$.export("$summary", `Successfully retrieved user details for ${criteria.join(", ")}`);
6373
return response;
6474
},

components/motive/common/util.mjs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
export const clearObj = (obj) => {
2+
return Object.entries(obj)
3+
.filter(([
4+
_,
5+
v,
6+
]) => (v != null && v != "" && _ != "$emit"))
7+
.reduce((acc, [
8+
k,
9+
v,
10+
]) => ({
11+
...acc,
12+
[k]: (!Array.isArray(v) && v === Object(v))
13+
? clearObj(v)
14+
: v,
15+
}), {});
16+
};

components/motive/motive.app.mjs

Lines changed: 51 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -3,124 +3,96 @@ import { axios } from "@pipedream/platform";
33
export default {
44
type: "app",
55
app: "motive",
6-
version: "0.0.{{ts}}",
76
propDefinitions: {
87
driverId: {
98
type: "string",
109
label: "Driver ID",
1110
description: "Filter to a specific driver",
12-
optional: true,
1311
},
1412
safetyCategory: {
1513
type: "string",
1614
label: "Safety Category",
1715
description: "Filter to a specific safety category",
18-
optional: true,
19-
},
20-
username: {
21-
type: "string",
22-
label: "Username",
23-
description: "Username to retrieve user details",
24-
optional: true,
25-
},
26-
email: {
27-
type: "string",
28-
label: "Email",
29-
description: "Email to retrieve user details",
30-
optional: true,
3116
},
3217
driverCompanyId: {
3318
type: "string",
3419
label: "Driver Company ID",
3520
description: "Driver company ID to retrieve user details",
36-
optional: true,
37-
},
38-
phone: {
39-
type: "string",
40-
label: "Phone",
41-
description: "Phone number to retrieve user details",
42-
optional: true,
21+
async options({ page }) {
22+
const { users } = await this.listUsers({
23+
params: {
24+
page_no: page + 1,
25+
role: "driver",
26+
},
27+
});
28+
29+
return users.map(({ user }) => ({
30+
label: user.email || `${user.first_name} ${user.last_name}`,
31+
value: user.driver_company_id,
32+
}));
33+
},
4334
},
4435
},
4536
methods: {
46-
// this.$auth contains connected account data
47-
authKeys() {
48-
console.log(Object.keys(this.$auth));
49-
},
5037
_baseUrl() {
51-
return "https://api.gomotive.com";
38+
return "https://api.gomotive.com/v1";
39+
},
40+
_headers() {
41+
return {
42+
Authorization: `Bearer ${this.$auth.oauth_access_token}`,
43+
};
5244
},
53-
async _makeRequest(opts = {}) {
54-
const {
55-
$, method = "GET", path = "/", headers, ...otherOpts
56-
} = opts;
45+
_makeRequest({
46+
$ = this, path, ...opts
47+
}) {
5748
return axios($, {
58-
...otherOpts,
59-
method,
60-
url: `${this._baseUrl()}${path}`,
61-
headers: {
62-
...headers,
63-
"user-agent": "@PipedreamHQ/pipedream v0.1",
64-
"Authorization": `Bearer ${this.$auth.api_token}`,
65-
},
49+
url: this._baseUrl() + path,
50+
headers: this._headers(),
51+
...opts,
6652
});
6753
},
68-
async listHosViolations(opts = {}) {
69-
const params = {};
70-
if (opts.driverId) params.driver_id = opts.driverId;
54+
listHosViolations(opts = {}) {
7155
return this._makeRequest({
7256
method: "GET",
7357
path: "/hos_violations",
74-
params,
7558
...opts,
7659
});
7760
},
78-
async listDriverPerformanceEvents(opts = {}) {
79-
const params = {};
80-
if (opts.safetyCategory) params.safety_category = opts.safetyCategory;
61+
listUsers(opts = {}) {
62+
return this._makeRequest({
63+
path: "/users",
64+
...opts,
65+
});
66+
},
67+
createWebhook(opts = {}) {
68+
return this._makeRequest({
69+
method: "POST",
70+
path: "/company_webhooks",
71+
...opts,
72+
});
73+
},
74+
updateWebhook({
75+
webhookId, ...opts
76+
}) {
77+
return this._makeRequest({
78+
method: "PUT",
79+
path: `/company_webhooks/${webhookId}`,
80+
...opts,
81+
});
82+
},
83+
listDriverPerformanceEvents(opts = {}) {
8184
return this._makeRequest({
8285
method: "GET",
8386
path: "/driver_performance_events",
84-
params,
8587
...opts,
8688
});
8789
},
88-
async retrieveUserDetails(opts = {}) {
89-
const {
90-
username, email, driverCompanyId, phone, ...otherOpts
91-
} = opts;
92-
const queryParams = {};
93-
if (username) queryParams.username = username;
94-
if (email) queryParams.email = email;
95-
if (driverCompanyId) queryParams.driver_company_id = driverCompanyId;
96-
if (phone) queryParams.phone = phone;
97-
90+
retrieveUserDetails(opts = {}) {
9891
return this._makeRequest({
9992
method: "GET",
10093
path: "/users/lookup",
101-
params: queryParams,
102-
...otherOpts,
94+
...opts,
10395
});
10496
},
105-
async paginate(fn, ...args) {
106-
const results = [];
107-
let hasMore = true;
108-
let page = 1;
109-
110-
while (hasMore) {
111-
const response = await fn({
112-
...args,
113-
page,
114-
});
115-
if (!Array.isArray(response) || response.length === 0) {
116-
hasMore = false;
117-
} else {
118-
results.push(...response);
119-
page += 1;
120-
}
121-
}
122-
123-
return results;
124-
},
12597
},
12698
};

components/motive/package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/motive",
3-
"version": "0.0.1",
3+
"version": "0.1.0",
44
"description": "Pipedream Motive Components",
55
"main": "motive.app.mjs",
66
"keywords": [
@@ -11,5 +11,9 @@
1111
"author": "Pipedream <[email protected]> (https://pipedream.com/)",
1212
"publishConfig": {
1313
"access": "public"
14+
},
15+
"dependencies": {
16+
"@pipedream/platform": "^3.0.3",
17+
"uuid": "^11.0.4"
1418
}
1519
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { v4 as uuid } from "uuid";
2+
import motive from "../../motive.app.mjs";
3+
4+
export default {
5+
props: {
6+
motive,
7+
http: "$.interface.http",
8+
db: "$.service.db",
9+
},
10+
methods: {
11+
_getHookId() {
12+
return this.db.get("hookId");
13+
},
14+
_setHookId(hookId) {
15+
this.db.set("hookId", hookId);
16+
},
17+
filterEvent() {
18+
return true;
19+
},
20+
},
21+
hooks: {
22+
async deploy() {
23+
const response = await this.motive.createWebhook({
24+
data: {
25+
url: this.http.endpoint,
26+
secret: uuid(),
27+
actions: this.getActions(),
28+
format: "json",
29+
enabled: true,
30+
webhook_type: "POST",
31+
},
32+
});
33+
this._setHookId(response.company_webhook.id);
34+
},
35+
async activate() {
36+
await this.motive.updateWebhook({
37+
webhookId: this._getHookId(),
38+
data: {
39+
enabled: true,
40+
},
41+
});
42+
},
43+
async deactivate() {
44+
await this.motive.updateWebhook({
45+
webhookId: this._getHookId(),
46+
data: {
47+
enabled: false,
48+
},
49+
});
50+
},
51+
},
52+
async run({ body }) {
53+
if (this.filterEvent(body)) {
54+
this.$emit(body, {
55+
id: body.id,
56+
summary: this.getSummary(body),
57+
ts: Date.parse(body.end_time || new Date()),
58+
});
59+
}
60+
},
61+
};

0 commit comments

Comments
 (0)