Skip to content

Commit bb85311

Browse files
committed
allocadence init
1 parent 8a8e3e5 commit bb85311

File tree

6 files changed

+461
-4
lines changed

6 files changed

+461
-4
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import allocadence from "../../allocadence.app.mjs";
2+
import { axios } from "@pipedream/platform";
3+
4+
export default {
5+
key: "allocadence-create-customer-order",
6+
name: "Create Customer Order",
7+
description: "Creates a new customer order. [See the documentation](https://docs.allocadence.com/)",
8+
version: "0.0.1",
9+
type: "action",
10+
props: {
11+
allocadence,
12+
customerInformation: {
13+
propDefinition: [
14+
allocadence,
15+
"customerInformation",
16+
],
17+
},
18+
productDetails: {
19+
propDefinition: [
20+
allocadence,
21+
"productDetails",
22+
],
23+
},
24+
shippingAddress: {
25+
propDefinition: [
26+
allocadence,
27+
"shippingAddress",
28+
],
29+
},
30+
specialInstructions: {
31+
propDefinition: [
32+
allocadence,
33+
"specialInstructions",
34+
{
35+
optional: true,
36+
},
37+
],
38+
},
39+
},
40+
async run({ $ }) {
41+
const response = await this.allocadence.createCustomerOrder({
42+
customerInformation: this.customerInformation,
43+
productDetails: this.productDetails,
44+
shippingAddress: this.shippingAddress,
45+
specialInstructions: this.specialInstructions,
46+
});
47+
48+
$.export("$summary", `Successfully created customer order with ID ${response.id}`);
49+
return response;
50+
},
51+
};
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import allocadence from "../../allocadence.app.mjs";
2+
3+
export default {
4+
key: "allocadence-create-purchase-order",
5+
name: "Create Purchase Order",
6+
description: "Generates a new purchase order. [See the documentation](https://docs.allocadence.com/)",
7+
version: "0.0.{{ts}}",
8+
type: "action",
9+
props: {
10+
allocadence,
11+
supplierDetails: {
12+
propDefinition: [
13+
allocadence,
14+
"supplierDetails",
15+
],
16+
},
17+
productDetails: {
18+
propDefinition: [
19+
allocadence,
20+
"productDetails",
21+
],
22+
},
23+
deliveryAddress: {
24+
propDefinition: [
25+
allocadence,
26+
"deliveryAddress",
27+
],
28+
},
29+
orderDeadline: {
30+
propDefinition: [
31+
allocadence,
32+
"orderDeadline",
33+
],
34+
},
35+
additionalInstructions: {
36+
propDefinition: [
37+
allocadence,
38+
"additionalInstructions",
39+
],
40+
},
41+
},
42+
async run({ $ }) {
43+
const response = await this.allocadence.createPurchaseOrder({
44+
supplierDetails: this.supplierDetails,
45+
productDetails: this.productDetails,
46+
deliveryAddress: this.deliveryAddress,
47+
orderDeadline: this.orderDeadline,
48+
additionalInstructions: this.additionalInstructions,
49+
});
50+
51+
$.export("$summary", `Successfully created purchase order with ID ${response.id}`);
52+
return response;
53+
},
54+
};
Lines changed: 137 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,145 @@
1+
import { axios } from "@pipedream/platform";
2+
13
export default {
24
type: "app",
35
app: "allocadence",
4-
propDefinitions: {},
6+
propDefinitions: {
7+
customerId: {
8+
type: "string",
9+
label: "Customer ID",
10+
description: "The ID of the customer who placed the order",
11+
},
12+
productId: {
13+
type: "string",
14+
label: "Product ID",
15+
description: "The ID of the product",
16+
},
17+
orderDetails: {
18+
type: "string",
19+
label: "Order Details",
20+
description: "Optional details about the customer order",
21+
optional: true,
22+
},
23+
deliveryMode: {
24+
type: "string",
25+
label: "Delivery Mode",
26+
description: "Optional delivery mode for the order",
27+
optional: true,
28+
},
29+
supplierId: {
30+
type: "string",
31+
label: "Supplier ID",
32+
description: "The ID of the supplier for purchase orders",
33+
},
34+
quantity: {
35+
type: "integer",
36+
label: "Quantity",
37+
description: "Optional quantity for the purchase order",
38+
optional: true,
39+
},
40+
deliveryDate: {
41+
type: "string",
42+
label: "Delivery Date",
43+
description: "Optional delivery date for the purchase order",
44+
optional: true,
45+
},
46+
customerInformation: {
47+
type: "string",
48+
label: "Customer Information",
49+
description: "Information about the customer",
50+
},
51+
productDetails: {
52+
type: "string",
53+
label: "Product Details",
54+
description: "Details about the product",
55+
},
56+
shippingAddress: {
57+
type: "string",
58+
label: "Shipping Address",
59+
description: "Address where the order will be shipped to",
60+
},
61+
specialInstructions: {
62+
type: "string",
63+
label: "Special Instructions",
64+
description: "Optional special instructions for the order",
65+
optional: true,
66+
},
67+
supplierDetails: {
68+
type: "string",
69+
label: "Supplier Details",
70+
description: "Details about the supplier",
71+
},
72+
deliveryAddress: {
73+
type: "string",
74+
label: "Delivery Address",
75+
description: "The address where the purchase order will be delivered",
76+
},
77+
orderDeadline: {
78+
type: "string",
79+
label: "Order Deadline",
80+
description: "Optional deadline for the order",
81+
optional: true,
82+
},
83+
additionalInstructions: {
84+
type: "string",
85+
label: "Additional Instructions",
86+
description: "Optional additional instructions for the purchase order",
87+
optional: true,
88+
},
89+
},
590
methods: {
6-
// this.$auth contains connected account data
91+
_baseUrl() {
92+
return "https://api.allocadence.com";
93+
},
94+
async _makeRequest(opts = {}) {
95+
const {
96+
$ = this, method = "GET", path = "/", headers, ...otherOpts
97+
} = opts;
98+
return axios($, {
99+
...otherOpts,
100+
method,
101+
url: this._baseUrl() + path,
102+
headers: {
103+
...headers,
104+
Authorization: `Bearer ${this.$auth.api_token}`,
105+
},
106+
});
107+
},
108+
async createCustomerOrder(opts = {}) {
109+
const {
110+
customerInformation, productDetails, shippingAddress, specialInstructions, ...extraOpts
111+
} = opts;
112+
return this._makeRequest({
113+
method: "POST",
114+
path: "/customer-orders",
115+
data: {
116+
customerInformation,
117+
productDetails,
118+
shippingAddress,
119+
specialInstructions,
120+
...extraOpts,
121+
},
122+
});
123+
},
124+
async createPurchaseOrder(opts = {}) {
125+
const {
126+
supplierDetails, productDetails, deliveryAddress, orderDeadline, additionalInstructions, ...extraOpts
127+
} = opts;
128+
return this._makeRequest({
129+
method: "POST",
130+
path: "/purchase-orders",
131+
data: {
132+
supplierDetails,
133+
productDetails,
134+
deliveryAddress,
135+
orderDeadline,
136+
additionalInstructions,
137+
...extraOpts,
138+
},
139+
});
140+
},
7141
authKeys() {
8142
console.log(Object.keys(this.$auth));
9143
},
10144
},
11-
};
145+
};

components/allocadence/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: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import allocadence from "../../allocadence.app.mjs";
2+
import {
3+
axios, DEFAULT_POLLING_SOURCE_TIMER_INTERVAL,
4+
} from "@pipedream/platform";
5+
6+
export default {
7+
key: "allocadence-new-customer-order",
8+
name: "New Customer Order Created",
9+
description: "Emit new event when a new customer order is created. [See the documentation](https://docs.allocadence.com/)",
10+
version: "0.0.{{ts}}",
11+
type: "source",
12+
dedupe: "unique",
13+
props: {
14+
allocadence,
15+
db: "$.service.db",
16+
customerId: {
17+
propDefinition: [
18+
allocadence,
19+
"customerId",
20+
],
21+
},
22+
productId: {
23+
propDefinition: [
24+
allocadence,
25+
"productId",
26+
],
27+
},
28+
orderDetails: {
29+
propDefinition: [
30+
allocadence,
31+
"orderDetails",
32+
],
33+
optional: true,
34+
},
35+
deliveryMode: {
36+
propDefinition: [
37+
allocadence,
38+
"deliveryMode",
39+
],
40+
optional: true,
41+
},
42+
timer: {
43+
type: "$.interface.timer",
44+
default: {
45+
intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL,
46+
},
47+
},
48+
},
49+
methods: {
50+
_getLastOrderCreatedTime() {
51+
return this.db.get("lastOrderCreatedTime") || 0;
52+
},
53+
_setLastOrderCreatedTime(time) {
54+
this.db.set("lastOrderCreatedTime", time);
55+
},
56+
async _getNewCustomerOrders() {
57+
const lastOrderCreatedTime = this._getLastOrderCreatedTime();
58+
const orders = await this.allocadence.createCustomerOrder({
59+
customerInformation: this.customerId,
60+
productDetails: this.productId,
61+
specialInstructions: this.orderDetails,
62+
deliveryMode: this.deliveryMode,
63+
});
64+
65+
const newOrders = orders.filter((order) => new Date(order.createdDate).getTime() > lastOrderCreatedTime);
66+
return newOrders;
67+
},
68+
},
69+
hooks: {
70+
async deploy() {
71+
const newOrders = await this._getNewCustomerOrders();
72+
73+
for (const order of newOrders.slice(-50)) {
74+
this.$emit(order, {
75+
id: order.id,
76+
summary: `New Customer Order: ${order.orderNumber}`,
77+
ts: new Date(order.createdDate).getTime(),
78+
});
79+
}
80+
81+
if (newOrders.length) {
82+
const latestOrderDate = newOrders[newOrders.length - 1].createdDate;
83+
this._setLastOrderCreatedTime(new Date(latestOrderDate).getTime());
84+
}
85+
},
86+
async activate() {
87+
const orders = await this._getNewCustomerOrders();
88+
if (orders.length) {
89+
const latestOrderDate = orders[orders.length - 1].createdDate;
90+
this._setLastOrderCreatedTime(new Date(latestOrderDate).getTime());
91+
}
92+
},
93+
async deactivate() {
94+
this.db.set("lastOrderCreatedTime", null);
95+
},
96+
},
97+
async run() {
98+
const newOrders = await this._getNewCustomerOrders();
99+
100+
for (const order of newOrders) {
101+
this.$emit(order, {
102+
id: order.id,
103+
summary: `New Customer Order: ${order.orderNumber}`,
104+
ts: new Date(order.createdDate).getTime(),
105+
});
106+
}
107+
108+
if (newOrders.length) {
109+
const latestOrderDate = newOrders[newOrders.length - 1].createdDate;
110+
this._setLastOrderCreatedTime(new Date(latestOrderDate).getTime());
111+
}
112+
},
113+
};

0 commit comments

Comments
 (0)