Skip to content

Commit 307085b

Browse files
authored
New Components - channable (#17630)
* new components * pnpm-lock.yaml * update * pnpm-lock.yaml
1 parent 59b859c commit 307085b

File tree

10 files changed

+345
-13
lines changed

10 files changed

+345
-13
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import channable from "../../channable.app.mjs";
2+
3+
export default {
4+
key: "channable-list-stock-updates",
5+
name: "List Stock Updates",
6+
description: "List stock updates for a company and project. [See the documentation](https://api.channable.com/v1/docs#tag/stock_updates/operation/get_stock_updates_companies__company_id__projects__project_id__offers_get)",
7+
version: "0.0.1",
8+
type: "action",
9+
props: {
10+
channable,
11+
search: {
12+
type: "string",
13+
label: "Search",
14+
description: "A text based search query",
15+
optional: true,
16+
},
17+
startDate: {
18+
type: "string",
19+
label: "Start Date",
20+
description: "The start date of the stock updates",
21+
optional: true,
22+
},
23+
endDate: {
24+
type: "string",
25+
label: "End Date",
26+
description: "The end date of the stock updates",
27+
optional: true,
28+
},
29+
max: {
30+
type: "integer",
31+
label: "Max",
32+
description: "The maximum number of stock updates to return",
33+
default: 100,
34+
optional: true,
35+
},
36+
},
37+
async run({ $ }) {
38+
const stockUpdates = await this.channable.getPaginatedResources({
39+
fn: this.channable.listStockUpdates,
40+
args: {
41+
$,
42+
params: {
43+
search: this.search,
44+
start_date: this.startDate,
45+
end_date: this.endDate,
46+
},
47+
max: this.max,
48+
},
49+
resourceKey: "offers",
50+
});
51+
$.export("$summary", `Found ${stockUpdates.length} stock update${stockUpdates.length === 1
52+
? ""
53+
: "s"}`);
54+
return stockUpdates;
55+
},
56+
};
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import channable from "../../channable.app.mjs";
2+
3+
export default {
4+
key: "channable-update-stock-update",
5+
name: "Update Stock Update",
6+
description: "Update a stock update for a company and project. [See the documentation](https://api.channable.com/v1/docs#tag/stock_updates/operation/stock_updates_update_companies__company_id__projects__project_id__stock_updates_post)",
7+
version: "0.0.1",
8+
type: "action",
9+
props: {
10+
channable,
11+
stockUpdateId: {
12+
propDefinition: [
13+
channable,
14+
"stockUpdateId",
15+
],
16+
},
17+
stock: {
18+
type: "integer",
19+
label: "Stock",
20+
description: "Whole new stock value for the item, not a delta",
21+
},
22+
title: {
23+
type: "string",
24+
label: "Title",
25+
description: "The title of the stock update",
26+
},
27+
gtin: {
28+
type: "string",
29+
label: "GTIN",
30+
description: "The GTIN of the item",
31+
},
32+
},
33+
async run({ $ }) {
34+
const response = await this.channable.updateStockUpdate({
35+
$,
36+
data: [
37+
{
38+
id: this.stockUpdateId,
39+
stock: this.stock,
40+
title: this.title,
41+
gtin: this.gtin,
42+
},
43+
],
44+
});
45+
$.export("$summary", `Updated stock update ${this.stockUpdateId}`);
46+
return response;
47+
},
48+
};
Lines changed: 88 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,95 @@
1+
import { axios } from "@pipedream/platform";
2+
13
export default {
24
type: "app",
35
app: "channable",
4-
propDefinitions: {},
6+
propDefinitions: {
7+
stockUpdateId: {
8+
type: "string",
9+
label: "Stock Update ID",
10+
description: "The ID of a stock update",
11+
async options({ page }) {
12+
const { offers } = await this.listStockUpdates({
13+
params: {
14+
limit: 100,
15+
offset: page * 100,
16+
},
17+
});
18+
return offers?.map((offer) => ({
19+
label: offer.label,
20+
value: offer.id,
21+
})) || [];
22+
},
23+
},
24+
},
525
methods: {
6-
// this.$auth contains connected account data
7-
authKeys() {
8-
console.log(Object.keys(this.$auth));
26+
_baseUrl() {
27+
return "https://api.channable.com/v1";
28+
},
29+
_companyId() {
30+
return this.$auth.company_id;
31+
},
32+
_projectId() {
33+
return this.$auth.project_id;
34+
},
35+
_makeRequest({
36+
$ = this, path, ...opts
37+
}) {
38+
return axios($, {
39+
url: `${this._baseUrl()}${path}`,
40+
headers: {
41+
Authorization: `Bearer ${this.$auth.api_token}`,
42+
},
43+
...opts,
44+
});
45+
},
46+
listStockUpdates(opts = {}) {
47+
return this._makeRequest({
48+
path: `/companies/${this._companyId()}/projects/${this._projectId()}/offers`,
49+
...opts,
50+
});
51+
},
52+
updateStockUpdate(opts = {}) {
53+
return this._makeRequest({
54+
path: `/companies/${this._companyId()}/projects/${this._projectId()}/stock_updates`,
55+
method: "POST",
56+
...opts,
57+
});
58+
},
59+
async *paginate({
60+
fn, args, resourceKey, max,
61+
}) {
62+
args = {
63+
...args,
64+
params: {
65+
...args?.params,
66+
limit: 100,
67+
offset: 0,
68+
},
69+
};
70+
let total, count = 0;
71+
do {
72+
const response = await fn(args);
73+
const items = response[resourceKey];
74+
total = items?.length;
75+
if (!total) {
76+
return;
77+
}
78+
for (const item of items) {
79+
yield item;
80+
if (max && ++count >= max) {
81+
return;
82+
}
83+
}
84+
args.params.offset += args.params.limit;
85+
} while (total === args.params.limit);
86+
},
87+
async getPaginatedResources(opts) {
88+
const resources = [];
89+
for await (const resource of this.paginate(opts)) {
90+
resources.push(resource);
91+
}
92+
return resources;
993
},
1094
},
1195
};

components/channable/package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/channable",
3-
"version": "0.0.1",
3+
"version": "0.1.0",
44
"description": "Pipedream Channable Components",
55
"main": "channable.app.mjs",
66
"keywords": [
@@ -11,5 +11,8 @@
1111
"author": "Pipedream <[email protected]> (https://pipedream.com/)",
1212
"publishConfig": {
1313
"access": "public"
14+
},
15+
"dependencies": {
16+
"@pipedream/platform": "^3.1.0"
1417
}
15-
}
18+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import channable from "../../channable.app.mjs";
2+
import {
3+
DEFAULT_POLLING_SOURCE_TIMER_INTERVAL, ConfigurationError,
4+
} from "@pipedream/platform";
5+
6+
export default {
7+
props: {
8+
channable,
9+
db: "$.service.db",
10+
timer: {
11+
type: "$.interface.timer",
12+
default: {
13+
intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL,
14+
},
15+
},
16+
},
17+
methods: {
18+
_getLastTs() {
19+
return this.db.get("lastTs");
20+
},
21+
_setLastTs(ts) {
22+
this.db.set("lastTs", ts);
23+
},
24+
getResourceKey() {
25+
return "offers";
26+
},
27+
async processEvent(max) {
28+
const lastTs = this._getLastTs();
29+
let maxTs = lastTs;
30+
const tsField = this.getTsField();
31+
32+
const results = await this.channable.paginate({
33+
fn: this.getResourceFn(),
34+
args: {
35+
params: {
36+
last_modified_after: lastTs,
37+
},
38+
},
39+
resourceKey: this.getResourceKey(),
40+
});
41+
42+
let items = [];
43+
for await (const result of results) {
44+
const ts = result[tsField];
45+
if (!maxTs || Date.parse(ts) > Date.parse(maxTs)) {
46+
maxTs = ts;
47+
}
48+
items.push(result);
49+
}
50+
51+
if (!items.length) {
52+
return;
53+
}
54+
55+
this._setLastTs(maxTs);
56+
57+
if (max && items.length > max) {
58+
items = items.slice(0, max);
59+
}
60+
61+
items.forEach((item) => {
62+
const meta = this.generateMeta(item);
63+
this.$emit(item, meta);
64+
});
65+
},
66+
getResourceFn() {
67+
throw new ConfigurationError("getResourceFn must be implemented");
68+
},
69+
getTsField() {
70+
throw new ConfigurationError("getTsField must be implemented");
71+
},
72+
generateMeta() {
73+
throw new ConfigurationError("generateMeta must be implemented");
74+
},
75+
},
76+
hooks: {
77+
async deploy() {
78+
await this.processEvent(25);
79+
},
80+
},
81+
async run() {
82+
await this.processEvent();
83+
},
84+
};
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import common from "../common/base.mjs";
2+
3+
export default {
4+
...common,
5+
key: "channable-new-stock-update-created",
6+
name: "New Stock Update Created",
7+
description: "Emit new event when a new stock update is created. [See the documentation](https://api.channable.com/v1/docs#tag/stock_updates/operation/get_stock_updates_companies__company_id__projects__project_id__offers_get)",
8+
version: "0.0.1",
9+
type: "source",
10+
dedupe: "unique",
11+
methods: {
12+
...common.methods,
13+
getResourceFn() {
14+
return this.channable.listStockUpdates;
15+
},
16+
getTsField() {
17+
return "created";
18+
},
19+
generateMeta(item) {
20+
return {
21+
id: item.id,
22+
summary: `New stock update created: ${item.id}`,
23+
ts: Date.parse(item.created),
24+
};
25+
},
26+
},
27+
};
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import common from "../common/base.mjs";
2+
3+
export default {
4+
...common,
5+
key: "channable-stock-update-updated",
6+
name: "Stock Update Updated",
7+
description: "Emit new event when a stock update is updated. [See the documentation](https://api.channable.com/v1/docs#tag/stock_updates/operation/get_stock_updates_companies__company_id__projects__project_id__offers_get)",
8+
version: "0.0.1",
9+
type: "source",
10+
dedupe: "unique",
11+
methods: {
12+
...common.methods,
13+
getResourceFn() {
14+
return this.channable.listStockUpdates;
15+
},
16+
getTsField() {
17+
return "modified";
18+
},
19+
generateMeta(item) {
20+
const ts = Date.parse(item.modified);
21+
return {
22+
id: `${item.id}-${ts}`,
23+
summary: `Stock update updated: ${item.id}`,
24+
ts,
25+
};
26+
},
27+
},
28+
};

components/memento_database/memento_database.app.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ export default {
88
console.log(Object.keys(this.$auth));
99
},
1010
},
11-
};
11+
};

components/robopost/robopost.app.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ export default {
88
console.log(Object.keys(this.$auth));
99
},
1010
},
11-
};
11+
};

0 commit comments

Comments
 (0)