Skip to content

Commit c337052

Browse files
committed
wip
1 parent 6d66e61 commit c337052

File tree

10 files changed

+394
-5
lines changed

10 files changed

+394
-5
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import oto from "../../oto.app.mjs";
2+
3+
export default {
4+
key: "oto-create-product",
5+
name: "Create Product",
6+
description: "Creates a new product. [See the documentation](https://apis.tryoto.com/#21b289bc-04c1-49b1-993e-23e928d57f56)",
7+
version: "0.0.{{ts}}",
8+
type: "action",
9+
props: {
10+
oto,
11+
},
12+
async run({ $ }) {
13+
const response = await this.oto.createProduct({});
14+
$.export("$summary", "");
15+
return response;
16+
},
17+
};
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import oto from "../../oto.app.mjs";
2+
3+
export default {
4+
key: "oto-list-orders",
5+
name: "List Orders",
6+
description: "Retrieves a list of orders. [See the documentation](https://apis.tryoto.com/#c2e94027-5214-456d-b653-0a66c038e3a4)",
7+
version: "0.0.{{ts}}",
8+
type: "action",
9+
props: {
10+
oto,
11+
status: {
12+
propDefinition: [
13+
oto,
14+
"status",
15+
],
16+
},
17+
minDate: {
18+
type: "string",
19+
label: "Min Date",
20+
description: "Starting \"Order Creation Date\" of your orders in \"yyyy-mm-dd\" format",
21+
optional: true,
22+
},
23+
maxDate: {
24+
type: "string",
25+
label: "Max Date",
26+
description: "Ending \"Order Creation Date\" of your orders in \"yyyy-mm-dd\" format",
27+
optional: true,
28+
},
29+
maxResults: {
30+
type: "integer",
31+
label: "Max Results",
32+
description: "The maximum number of orders to return",
33+
default: 100,
34+
optional: true,
35+
},
36+
},
37+
async run({ $ }) {
38+
const results = this.oto.paginate({
39+
fn: this.oto.listOrders,
40+
args: {
41+
$,
42+
params: {
43+
minDate: this.minDate,
44+
maxDate: this.maxDate,
45+
status: this.status,
46+
},
47+
},
48+
resourceKey: "orders",
49+
max: this.maxResults,
50+
});
51+
52+
const orders = [];
53+
for await (const order of results) {
54+
orders.push(order);
55+
}
56+
57+
$.export("$summary", `Successfully retrieved ${orders.length} order${orders.length === 1
58+
? ""
59+
: "s"}`);
60+
return orders;
61+
},
62+
};
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import oto from "../../oto.app.mjs";
2+
3+
export default {
4+
key: "oto-track-shipment",
5+
name: "Track Shipment",
6+
description: "track a shipment by providing the tracking number and delivery company name. [See the documentation](https://apis.tryoto.com/#3b8d84ec-7769-41d4-adf2-6d2d8c1189a4)",
7+
version: "0.0.{{ts}}",
8+
type: "action",
9+
props: {
10+
oto,
11+
trackingNumber: {
12+
type: "string",
13+
label: "Tracking Number",
14+
description: "The shipment/ tracking number that you wanna track",
15+
},
16+
deliveryCompanyName: {
17+
type: "string",
18+
label: "Delivery Company Name",
19+
description: "The name of the delivery company",
20+
},
21+
brandName: {
22+
propDefinition: [
23+
oto,
24+
"brandName",
25+
],
26+
},
27+
},
28+
async run({ $ }) {
29+
const response = await this.oto.trackShipment({
30+
$,
31+
data: {
32+
trackingNumber: this.trackingNumber,
33+
deliveryCompanyName: this.deliveryCompanyName,
34+
brandName: this.brandName,
35+
statusHistory: true,
36+
},
37+
});
38+
$.export("$summary", `Successfully tracked shipment with tracking number: ${this.trackingNumber}`);
39+
return response;
40+
},
41+
};
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
const DEFAULT_LIMIT = 100;
2+
3+
const STATUSES = [
4+
"new",
5+
"paymentConfirmed",
6+
"waitingAddressConfirmation",
7+
"addressConfirmed",
8+
"needConfirmation",
9+
"paymentTypeConfirmed",
10+
"codOrderConfirmed",
11+
"orderConfirmed",
12+
"pickupFromStore",
13+
"interDepotTransfer",
14+
"canceled",
15+
"deleted",
16+
"readyForCollection",
17+
"branchAssigned",
18+
"assignedToWarehouse",
19+
"shipmentOnHoldWarehouse",
20+
"shipmentOnHoldToCancel",
21+
"notAvailableBR",
22+
"notAvailableWH",
23+
"picked",
24+
"packed",
25+
"searchingDriver",
26+
"shipmentCreated",
27+
"goingToPickup",
28+
"arrivedPickup",
29+
"pickedUp",
30+
"arrivedDestinationTerminal",
31+
"arrivedTerminal",
32+
"departedTerminal",
33+
"inTransit",
34+
"arrivedOriginTerminal",
35+
"outForDelivery",
36+
"arrivedDestination",
37+
"shipmentInProgress",
38+
"undeliveredAttempt",
39+
"shipmentOnHold",
40+
"delivered",
41+
"returned",
42+
"returnProcessing",
43+
"returnShipmentProcessing",
44+
"reverseShipmentProcessing",
45+
"reverseShipmentCreated",
46+
"reverseShipmentCanceled",
47+
"reverseGoingToPickup",
48+
"reversePickedUp",
49+
"reverseOutForDelivery",
50+
"reverseArrivedTerminal",
51+
"reverseDepartedTerminal",
52+
"reverseArrivedDestinationTerminal",
53+
"reverseUndeliveredAttempt",
54+
"reverseShipmentOnHold",
55+
"reverseReturned",
56+
"reverseConfirmReturn",
57+
"shipmentCanceled",
58+
"lostOrDamaged",
59+
"confirmedReturn",
60+
"approved",
61+
"rejected",
62+
"reverseShipmentCanceled",
63+
"reverseConfirmReturn",
64+
"returnReverseComment",
65+
];
66+
67+
export default {
68+
DEFAULT_LIMIT,
69+
STATUSES,
70+
};

components/oto/oto.app.mjs

Lines changed: 100 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,107 @@
1+
import { axios } from "@pipedream/platform";
2+
import constants from "./common/constants.mjs";
3+
14
export default {
25
type: "app",
36
app: "oto",
4-
propDefinitions: {},
7+
propDefinitions: {
8+
brandName: {
9+
type: "string",
10+
label: "Brand Name",
11+
description: "The brand name associated with the shipment",
12+
optional: true,
13+
async options() {
14+
const { clientStores } = await this.listBrands();
15+
return clientStores?.map(({ storeName }) => storeName) || [];
16+
},
17+
},
18+
status: {
19+
type: "string",
20+
label: "Status",
21+
description: "The status of an order",
22+
options: constants.STATUSES,
23+
optional: true,
24+
},
25+
},
526
methods: {
6-
// this.$auth contains connected account data
7-
authKeys() {
8-
console.log(Object.keys(this.$auth));
27+
_baseUrl() {
28+
return `${this.$auth.api_url}/rest/v2`;
29+
},
30+
_makeRequest({
31+
$ = this, path, ...otherOpts
32+
}) {
33+
return axios($, {
34+
...otherOpts,
35+
url: `${this._baseUrl()}${path}`,
36+
headers: {
37+
Authorization: `Bearer ${this.$auth.oauth_access_token}`,
38+
},
39+
});
40+
},
41+
createWebhook(opts = {}) {
42+
return this._makeRequest({
43+
method: "POST",
44+
path: "/webhook",
45+
...opts,
46+
});
47+
},
48+
deleteWebhook(opts = {}) {
49+
return this._makeRequest({
50+
method: "DELETE",
51+
path: "/webhook",
52+
...opts,
53+
});
54+
},
55+
listOrders(opts = {}) {
56+
return this._makeRequest({
57+
path: "/orders",
58+
...opts,
59+
});
60+
},
61+
listBrands(opts = {}) {
62+
return this._makeRequest({
63+
path: "/getBrandList",
64+
...opts,
65+
});
66+
},
67+
createProduct(opts = {}) {
68+
return this._makeRequest({
69+
method: "POST",
70+
path: "/createProduct",
71+
...opts,
72+
});
73+
},
74+
trackShipment(opts = {}) {
75+
return this._makeRequest({
76+
method: "POST",
77+
path: "/trackShipment",
78+
...opts,
79+
});
80+
},
81+
async *paginate({
82+
fn, args, resourceKey, max,
83+
}) {
84+
args = {
85+
...args,
86+
params: {
87+
...args?.params,
88+
perPage: constants.DEFAULT_LIMIT,
89+
page: 1,
90+
},
91+
};
92+
let total, count = 0;
93+
do {
94+
const response = await fn(args);
95+
const items = response[resourceKey];
96+
for (const item of items) {
97+
yield item;
98+
if (max && ++count >= max) {
99+
return;
100+
}
101+
}
102+
total = items?.length;
103+
args.params.page++;
104+
} while (total === args.params.perPage);
9105
},
10106
},
11107
};

components/oto/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@
1212
"publishConfig": {
1313
"access": "public"
1414
}
15-
}
15+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import oto from "../../oto.app.mjs";
2+
3+
export default {
4+
props: {
5+
oto,
6+
db: "$.service.db",
7+
http: "$.interface.http",
8+
},
9+
hooks: {
10+
async activate() {
11+
const { id } = await this.oto.createWebhook({
12+
data: {
13+
url: this.http.endpoint,
14+
method: "POST",
15+
webhookType: this.getWebhookType(),
16+
},
17+
});
18+
this._setHookId(id);
19+
},
20+
async deactivate() {
21+
const id = this._getHookId();
22+
if (id) {
23+
await this.oto.deleteWebhook({
24+
params: {
25+
id,
26+
},
27+
});
28+
}
29+
},
30+
},
31+
methods: {
32+
_getHookId() {
33+
return this.db.get("hookId");
34+
},
35+
_setHookId(hookId) {
36+
this.db.set("hookId", hookId);
37+
},
38+
getWebhookType() {
39+
throw new Error("getWebhookType is not implemented");
40+
},
41+
generateMeta() {
42+
return {};
43+
},
44+
},
45+
async run(event) {
46+
const { body } = event;
47+
if (!body) {
48+
return;
49+
}
50+
console.log(body);
51+
},
52+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import common from "../common/base.mjs";
2+
3+
export default {
4+
...common,
5+
key: "oto-new-order-instant",
6+
name: "New Order (Instant)",
7+
description: "Emit new event when a new order is placed. [See the documentation](https://apis.tryoto.com/#9671ca1f-7d06-43fc-8ee9-cf9c336b088d)",
8+
version: "0.0.{{ts}}",
9+
type: "source",
10+
dedupe: "unique",
11+
methods: {
12+
...common.methods,
13+
getWebhookType() {
14+
return "newOrders";
15+
},
16+
},
17+
};

0 commit comments

Comments
 (0)