diff --git a/components/amazon_selling_partner/actions/check-fba-inventory-levels/check-fba-inventory-levels.mjs b/components/amazon_selling_partner/actions/check-fba-inventory-levels/check-fba-inventory-levels.mjs new file mode 100644 index 0000000000000..a526bab484958 --- /dev/null +++ b/components/amazon_selling_partner/actions/check-fba-inventory-levels/check-fba-inventory-levels.mjs @@ -0,0 +1,55 @@ +import amazonSellingPartner from "../../amazon_selling_partner.app.mjs"; + +export default { + key: "amazon_selling_partner-check-fba-inventory-levels", + name: "Check FBA Inventory Levels", + description: "Retrieves inventory summaries from Amazon fulfillment centers to monitor stock availability. [See the documentation](https://developer-docs.amazon.com/sp-api/reference/getinventorysummaries)", + version: "0.0.1", + type: "action", + props: { + amazonSellingPartner, + marketplaceId: { + propDefinition: [ + amazonSellingPartner, + "marketplaceId", + ], + }, + details: { + type: "boolean", + label: "Details", + description: "Set to `true` to return inventory summaries with additional summarized inventory details and quantities. Otherwise, returns inventory summaries only (default value).", + optional: true, + }, + startDateTime: { + type: "string", + label: "Start Date Time", + description: "A start date and time in ISO8601 format. If specified, all inventory summaries that have changed since then are returned. You must specify a date and time that is no earlier than 18 months prior to the date and time when you call the API. Note: Changes in inboundWorkingQuantity, inboundShippedQuantity and inboundReceivingQuantity are not detected.", + optional: true, + }, + sellerSkus: { + type: "string[]", + label: "Seller SKUs", + description: "A list of seller SKUs for which to return inventory summaries. You may specify up to 50 SKUs.", + optional: true, + }, + }, + async run({ $ }) { + const { payload } = await this.amazonSellingPartner.getInventorySummaries({ + $, + params: { + marketplaceIds: this.marketplaceId, + granularityType: "Marketplace", + granularityId: this.marketplaceId, + details: this.details, + startDateTime: this.startDateTime, + sellerSkus: this.sellerSkus?.length + ? this.sellerSkus.join(",") + : undefined, + }, + }); + $.export("$summary", `Fetched ${payload.inventorySummaries.length} inventory summar${payload.inventorySummaries.length === 1 + ? "y" + : "ies"}`); + return payload.inventorySummaries || []; + }, +}; diff --git a/components/amazon_selling_partner/actions/fetch-orders-by-date-range/fetch-orders-by-date-range.mjs b/components/amazon_selling_partner/actions/fetch-orders-by-date-range/fetch-orders-by-date-range.mjs new file mode 100644 index 0000000000000..458d5f328e61f --- /dev/null +++ b/components/amazon_selling_partner/actions/fetch-orders-by-date-range/fetch-orders-by-date-range.mjs @@ -0,0 +1,63 @@ +import amazonSellingPartner from "../../amazon_selling_partner.app.mjs"; + +export default { + key: "amazon_selling_partner-fetch-orders-by-date-range", + name: "Fetch Orders by Date Range", + description: "Retrieves a list of orders based on a specified date range, buyer email, or order ID. [See the documentation](https://developer-docs.amazon.com/sp-api/reference/getorders)", + version: "0.0.1", + type: "action", + props: { + amazonSellingPartner, + marketplaceId: { + propDefinition: [ + amazonSellingPartner, + "marketplaceId", + ], + }, + createdAfter: { + type: "string", + label: "Created After", + description: "Fetch orders created after this ISO date.", + }, + createdBefore: { + type: "string", + label: "Created Before", + description: "Fetch orders created before this ISO date.", + optional: true, + }, + buyerEmail: { + type: "string", + label: "Buyer Email", + description: "The email address of a buyer", + optional: true, + }, + amazonOrderId: { + propDefinition: [ + amazonSellingPartner, + "orderId", + (c) => ({ + marketplaceId: c.marketplaceId, + }), + ], + label: "Seller Order ID", + description: "An order identifier that is specified by the seller. Used to select only the orders that match the order identifier. If SellerOrderId is specified, then FulfillmentChannels, OrderStatuses, PaymentMethod, LastUpdatedAfter, LastUpdatedBefore, and BuyerEmail cannot be specified.", + optional: true, + }, + }, + async run({ $ }) { + const { payload } = await this.amazonSellingPartner.listOrders({ + $, + params: { + MarketplaceIds: this.marketplaceId, + CreatedAfter: this.createdAfter, + CreatedBefore: this.createdBefore, + BuyerEmail: this.buyerEmail, + SellerOrderId: this.amazonOrderId, + }, + }); + $.export("$summary", `Fetched ${payload.Orders.length} order${payload.Orders.length === 1 + ? "" + : "s"}`); + return payload.Orders || []; + }, +}; diff --git a/components/amazon_selling_partner/actions/generate-sales-inventory-reports/generate-sales-inventory-reports.mjs b/components/amazon_selling_partner/actions/generate-sales-inventory-reports/generate-sales-inventory-reports.mjs new file mode 100644 index 0000000000000..57a05b0214fc5 --- /dev/null +++ b/components/amazon_selling_partner/actions/generate-sales-inventory-reports/generate-sales-inventory-reports.mjs @@ -0,0 +1,71 @@ +import amazonSellingPartner from "../../amazon_selling_partner.app.mjs"; + +export default { + key: "amazon_selling_partner-generate-sales-inventory-reports", + name: "Generate Sales & Inventory Reports", + description: "Requests reports on sales, inventory, and fulfillment performance. [See the documentation](https://developer-docs.amazon.com/sp-api/reference/getreports)", + version: "0.0.1", + type: "action", + props: { + amazonSellingPartner, + reportTypes: { + type: "string[]", + label: "Report Types", + description: "A list of report types used to filter reports. Refer to [Report Type Values](https://developer-docs.amazon.com/sp-api/docs/report-type-values) for more information.", + }, + marketplaceId: { + propDefinition: [ + amazonSellingPartner, + "marketplaceId", + ], + optional: true, + }, + processingStatuses: { + type: "string[]", + label: "Processing Statuses", + description: "A list of processing statuses used to filter reports", + options: [ + "CANCELLED", + "DONE", + "FATAL", + "IN_PROGRESS", + "IN_QUEUE", + ], + optional: true, + }, + createdSince: { + type: "string", + label: "Created Since", + description: "The earliest report creation date and time for reports to include in the response, in ISO 8601 date time format. The default is 90 days ago. Reports are retained for a maximum of 90 days.", + optional: true, + }, + createdUntil: { + type: "string", + label: "Created Until", + description: "The latest report creation date and time for reports to include in the response, in ISO 8601 date time format. The default is now.", + optional: true, + }, + }, + async run({ $ }) { + const reports = await this.amazonSellingPartner.getPaginatedResources({ + fn: this.amazonSellingPartner.listReports, + params: { + reportTypes: this.reportTypes + ? this.reportTypes.join(",") + : undefined, + marketplaceIds: this.marketplaceId, + processingStatuses: this.processingStatuses + ? this.processingStatuses.join(",") + : undefined, + createdSince: this.createdSince, + createdUntil: this.createdUntil, + }, + resourceKey: "reports", + hasPayload: false, + }); + $.export("$summary", `Fetched ${reports.length} report${reports.length === 1 + ? "" + : "s"}`); + return reports; + }, +}; diff --git a/components/amazon_selling_partner/actions/get-order-details/get-order-details.mjs b/components/amazon_selling_partner/actions/get-order-details/get-order-details.mjs new file mode 100644 index 0000000000000..852bff998ce68 --- /dev/null +++ b/components/amazon_selling_partner/actions/get-order-details/get-order-details.mjs @@ -0,0 +1,35 @@ +import amazonSellingPartner from "../../amazon_selling_partner.app.mjs"; + +export default { + key: "amazon_selling_partner-get-order-details", + name: "Get Order Details", + description: "Fetches detailed information about a specific order using its order ID. [See the documentation](https://developer-docs.amazon.com/sp-api/reference/getorder)", + version: "0.0.1", + type: "action", + props: { + amazonSellingPartner, + marketplaceId: { + propDefinition: [ + amazonSellingPartner, + "marketplaceId", + ], + }, + amazonOrderId: { + propDefinition: [ + amazonSellingPartner, + "orderId", + (c) => ({ + marketplaceId: c.marketplaceId, + }), + ], + }, + }, + async run({ $ }) { + const response = await this.amazonSellingPartner.getOrder({ + $, + orderId: this.amazonOrderId, + }); + $.export("$summary", `Fetched order details for ${this.amazonOrderId}`); + return response; + }, +}; diff --git a/components/amazon_selling_partner/actions/list-inbound-shipments/list-inbound-shipments.mjs b/components/amazon_selling_partner/actions/list-inbound-shipments/list-inbound-shipments.mjs new file mode 100644 index 0000000000000..4d44cf7263b62 --- /dev/null +++ b/components/amazon_selling_partner/actions/list-inbound-shipments/list-inbound-shipments.mjs @@ -0,0 +1,54 @@ +import amazonSellingPartner from "../../amazon_selling_partner.app.mjs"; + +export default { + key: "amazon_selling_partner-list-inbound-shipments", + name: "List Inbound Shipments", + description: "Fetches inbound shipment details to track stock movement and replenishment. [See the documentation](https://developer-docs.amazon.com/sp-api/reference/getshipments)", + version: "0.0.1", + type: "action", + props: { + amazonSellingPartner, + marketplaceId: { + propDefinition: [ + amazonSellingPartner, + "marketplaceId", + ], + }, + status: { + propDefinition: [ + amazonSellingPartner, + "status", + ], + }, + lastUpdatedAfter: { + type: "string", + label: "Last Updated After", + description: "A date used for selecting inbound shipments that were last updated after (or at) a specified time. The selection includes updates made by Amazon and by the seller.", + optional: true, + }, + lastUpdatedBefore: { + type: "string", + label: "Last Updated Before", + description: "A date used for selecting inbound shipments that were last updated before (or at) a specified time. The selection includes updates made by Amazon and by the seller.", + optional: true, + }, + }, + async run({ $ }) { + const shipments = await this.amazonSellingPartner.getPaginatedResources({ + fn: this.amazonSellingPartner.listInboundShipments, + params: { + MarketplaceId: this.marketplaceId, + ShipmentStatusList: this.status?.length + ? this.status.join(",") + : undefined, + LastUpdatedAfter: this.lastUpdatedAfter, + LastUpdatedBefore: this.lastUpdatedBefore, + }, + resourceKey: "ShipmentData", + }); + $.export("$summary", `Fetched ${shipments.length} shipment${shipments.length === 1 + ? "" + : "s"}`); + return shipments; + }, +}; diff --git a/components/amazon_selling_partner/actions/optimize-product-pricing/optimize-product-pricing.mjs b/components/amazon_selling_partner/actions/optimize-product-pricing/optimize-product-pricing.mjs new file mode 100644 index 0000000000000..a812b22afe0e9 --- /dev/null +++ b/components/amazon_selling_partner/actions/optimize-product-pricing/optimize-product-pricing.mjs @@ -0,0 +1,58 @@ +import amazonSellingPartner from "../../amazon_selling_partner.app.mjs"; + +export default { + key: "amazon_selling_partner-optimize-product-pricing", + name: "Optimize Product Pricing", + description: "Retrieves pricing data to adjust product prices dynamically based on market trends. [See the documentation](https://developer-docs.amazon.com/sp-api/reference/getcompetitivepricing)", + version: "0.0.1", + type: "action", + props: { + amazonSellingPartner, + marketplaceId: { + propDefinition: [ + amazonSellingPartner, + "marketplaceId", + ], + }, + itemType: { + type: "string", + label: "Item Type", + description: "Indicates whether ASIN values or seller SKU values are used to identify items. If you specify Asin, the information in the response will be dependent on the list of Asins you provide in the Asins parameter. If you specify Sku, the information in the response will be dependent on the list of Skus you provide in the Skus parameter.", + options: [ + "Asin", + "Sku", + ], + }, + values: { + type: "string[]", + label: "Values", + description: "A list of ASINs or seller SKUs. You may specify up to 20 identifiers.", + }, + customerType: { + type: "string", + label: "Customer Type", + description: "Filters the offer listings based on customer type", + options: [ + "Consumer", + "Business", + ], + }, + }, + async run({ $ }) { + const { payload } = await this.amazonSellingPartner.listProductPricing({ + $, + params: { + MarketplaceId: this.marketplaceId, + ItemType: this.itemType, + [this.itemType === "Asin" + ? "Asins" + : "Skus"]: this.values.join(","), + CustomerType: this.customerType, + }, + }); + if (payload[0]?.Product) { + $.export("$summary", "Fetched pricing information"); + } + return payload; + }, +}; diff --git a/components/amazon_selling_partner/actions/retrieve-sales-performance-reports/retrieve-sales-performance-reports.mjs b/components/amazon_selling_partner/actions/retrieve-sales-performance-reports/retrieve-sales-performance-reports.mjs new file mode 100644 index 0000000000000..73f3935c0f730 --- /dev/null +++ b/components/amazon_selling_partner/actions/retrieve-sales-performance-reports/retrieve-sales-performance-reports.mjs @@ -0,0 +1,103 @@ +import amazonSellingPartner from "../../amazon_selling_partner.app.mjs"; +import { ConfigurationError } from "@pipedream/platform"; + +export default { + key: "amazon_selling_partner-retrieve-sales-performance-reports", + name: "Retrieve Sales Performance Reports", + description: "Fetches sales reports for visualization in third-party dashboarding tools. [See the documentation](https://developer-docs.amazon.com/sp-api/reference/getordermetrics)", + version: "0.0.1", + type: "action", + props: { + amazonSellingPartner, + marketplaceId: { + propDefinition: [ + amazonSellingPartner, + "marketplaceId", + ], + }, + interval: { + type: "string", + label: "Interval", + description: "A time interval used for selecting order metrics. This takes the form of two dates separated by two hyphens (first date is inclusive; second date is exclusive). Dates are in ISO8601 format and must represent absolute time (either Z notation or offset notation). Example: `2018-09-01T00:00:00-07:00--2018-09-04T00:00:00-07:00` requests order metrics for Sept 1st, 2nd and 3rd in the -07:00 zone.", + }, + granularity: { + type: "string", + label: "Granularity", + description: "The granularity of the grouping of order metrics, based on a unit of time", + options: [ + "Hour", + "Day", + "Week", + "Month", + "Year", + "Total", + ], + }, + granularityTimeZone: { + type: "string", + label: "Granularity Time Zone", + description: "An IANA-compatible time zone for determining the day boundary. Required when specifying a granularity value greater than Hour.", + optional: true, + }, + buyerType: { + type: "string", + label: "Buyer Type", + description: "Filters the results by the buyer type that you specify, B2B (business to business) or B2C (business to customer). Example: B2B, if you want the response to include order metrics for only B2B buyers.", + options: [ + "All", + "B2B", + "B2C", + ], + optional: true, + }, + fulfillmentNetwork: { + type: "string", + label: "Fulfillment Network", + description: "Filters the results by the fulfillment network that you specify, MFN (merchant fulfillment network) or AFN (Amazon fulfillment network). Do not include this filter if you want the response to include order metrics for all fulfillment networks. Example: AFN, if you want the response to include order metrics for only Amazon fulfillment network.", + optional: true, + }, + firstDayOfWeek: { + type: "string", + label: "First Day of Week", + description: "Specifies the day that the week starts on when granularity=Week, either Monday or Sunday. Default: Monday. Example: Sunday, if you want the week to start on a Sunday.", + options: [ + "Monday", + "Sunday", + ], + optional: true, + }, + asin: { + type: "string", + label: "ASIN", + description: "Filters the results by the ASIN that you specify. Specifying both ASIN and SKU returns an error. Do not include this filter if you want the response to include order metrics for all ASINs. Example: B0792R1RSN, if you want the response to include order metrics for only ASIN B0792R1RSN.", + optional: true, + }, + sku: { + type: "string", + label: "SKU", + description: "Filters the results by the SKU that you specify. Specifying both ASIN and SKU returns an error. Do not include this filter if you want the response to include order metrics for all SKUs. Example: TestSKU, if you want the response to include order metrics for only SKU TestSKU.", + optional: true, + }, + }, + async run({ $ }) { + if (this.asin && this.sku) { + throw new ConfigurationError("Cannot specify both ASIN and SKU parameters"); + } + const { payload } = await this.amazonSellingPartner.getOrderMetrics({ + $, + params: { + marketplaceIds: this.marketplaceId, + interval: this.interval, + granularity: this.granularity, + granularityTimeZone: this.granularityTimeZone, + buyerType: this.buyerType, + fulfillmentNetwork: this.fulfillmentNetwork, + firstDayOfWeek: this.firstDayOfWeek, + asin: this.asin, + sku: this.sku, + }, + }); + $.export("$summary", "Fetched sales performance reports"); + return payload; + }, +}; diff --git a/components/amazon_selling_partner/amazon_selling_partner.app.mjs b/components/amazon_selling_partner/amazon_selling_partner.app.mjs index d0cea946e286b..380c6993ef36c 100644 --- a/components/amazon_selling_partner/amazon_selling_partner.app.mjs +++ b/components/amazon_selling_partner/amazon_selling_partner.app.mjs @@ -1,11 +1,168 @@ +import { axios } from "@pipedream/platform"; + export default { type: "app", app: "amazon_selling_partner", - propDefinitions: {}, + propDefinitions: { + marketplaceId: { + type: "string", + label: "Marketplace ID", + description: "The Amazon Marketplace ID", + async options() { + const { payload } = await this.listMarketplaces(); + return payload?.map(({ marketplace }) => ({ + label: marketplace.name, + value: marketplace.id, + })) || []; + }, + }, + orderId: { + type: "string", + label: "Order ID", + description: "The ID of an order", + async options({ + marketplaceId, prevContext, + }) { + const { payload } = await this.listOrders({ + params: { + MarketplaceIds: marketplaceId, + NextToken: prevContext?.next, + LastUpdatedAfter: new Date(new Date().setMonth(new Date().getMonth() - 1)) + .toISOString(), // 1 month ago + }, + }); + const { + Orders: orders, NextToken: next, + } = payload; + return { + options: orders?.map(({ AmazonOrderId }) => ({ + label: AmazonOrderId, + value: AmazonOrderId, + })) || [], + context: { + next, + }, + }; + }, + }, + status: { + type: "string[]", + label: "Status", + description: "Filter inbound shipments by status", + options: [ + "WORKING", + "SHIPPED", + "RECEIVING", + "CANCELLED", + "DELETED", + "CLOSED", + "ERROR", + "IN_TRANSIT", + "DELIVERED", + "CHECKED_IN", + ], + }, + }, methods: { - // this.$auth contains connected account data - authKeys() { - console.log(Object.keys(this.$auth)); + _baseUrl() { + return "https://sellingpartnerapi-na.amazon.com"; + }, + _makeRequest({ + $ = this, path, ...opts + }) { + return axios($, { + url: `${this._baseUrl()}${path}`, + headers: { + "x-amz-access-token": this.$auth.oauth_access_token, + }, + ...opts, + }); + }, + getOrder({ + orderId, ...opts + }) { + return this._makeRequest({ + path: `/orders/${orderId}`, + ...opts, + }); + }, + getOrderMetrics(opts = {}) { + return this._makeRequest({ + path: "/sales/v1/orderMetrics", + ...opts, + }); + }, + getInventorySummaries(opts = {}) { + return this._makeRequest({ + path: "/fba/inventory/v1/summaries", + ...opts, + }); + }, + listMarketplaces(opts = {}) { + return this._makeRequest({ + path: "/sellers/v1/marketplaceParticipations", + ...opts, + }); + }, + listOrders(opts = {}) { + return this._makeRequest({ + path: "/orders/v0/orders", + ...opts, + }); + }, + listInboundShipments(opts = {}) { + return this._makeRequest({ + path: "/fba/inbound/v0/shipments", + ...opts, + }); + }, + listReports(opts = {}) { + return this._makeRequest({ + path: "/reports/2021-06-30/reports", + ...opts, + }); + }, + listProductPricing(opts = {}) { + return this._makeRequest({ + path: "/products/pricing/v0/competitivePrice", + ...opts, + }); + }, + async *paginate({ + fn, params, resourceKey, hasPayload = true, max, + }) { + let hasMore, count = 0; + do { + const response = await fn({ + params, + }); + const payload = hasPayload + ? response.payload + : response; + const items = payload[resourceKey] || []; + for (const item of items) { + yield item; + if (max && ++count >= max) { + return; + } + } + hasMore = false; + if (payload?.NextToken) { + params.NextToken = payload.NextToken; + hasMore = true; + } + if (payload?.nextToken) { + params.nextToken = payload.nextToken; + hasMore = true; + } + } while (hasMore); + }, + async getPaginatedResources(opts = {}) { + const results = []; + for await (const item of this.paginate(opts)) { + results.push(item); + } + return results; }, }, }; diff --git a/components/amazon_selling_partner/package.json b/components/amazon_selling_partner/package.json index ddab104783ce3..d7d933e238deb 100644 --- a/components/amazon_selling_partner/package.json +++ b/components/amazon_selling_partner/package.json @@ -1,6 +1,6 @@ { "name": "@pipedream/amazon_selling_partner", - "version": "0.0.1", + "version": "0.1.0", "description": "Pipedream Amazon Selling Partner Components", "main": "amazon_selling_partner.app.mjs", "keywords": [ @@ -11,5 +11,8 @@ "author": "Pipedream (https://pipedream.com/)", "publishConfig": { "access": "public" + }, + "dependencies": { + "@pipedream/platform": "^3.1.0" } } diff --git a/components/amazon_selling_partner/sources/common/base.mjs b/components/amazon_selling_partner/sources/common/base.mjs new file mode 100644 index 0000000000000..37101c8d1d3a4 --- /dev/null +++ b/components/amazon_selling_partner/sources/common/base.mjs @@ -0,0 +1,29 @@ +import amazonSellingPartner from "../../amazon_selling_partner.app.mjs"; +import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform"; + +export default { + props: { + amazonSellingPartner, + db: "$.service.db", + timer: { + type: "$.interface.timer", + default: { + intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL, + }, + }, + marketplaceId: { + propDefinition: [ + amazonSellingPartner, + "marketplaceId", + ], + }, + }, + methods: { + _getLastTs() { + return this.db.get("lastTs") || new Date(Date.now() - 1000 * 60 * 60).toISOString(); + }, + _setLastTs(ts) { + this.db.set("lastTs", ts); + }, + }, +}; diff --git a/components/amazon_selling_partner/sources/new-inbound-shipment-to-fba-created/new-inbound-shipment-to-fba-created.mjs b/components/amazon_selling_partner/sources/new-inbound-shipment-to-fba-created/new-inbound-shipment-to-fba-created.mjs new file mode 100644 index 0000000000000..4fd735fa63ed6 --- /dev/null +++ b/components/amazon_selling_partner/sources/new-inbound-shipment-to-fba-created/new-inbound-shipment-to-fba-created.mjs @@ -0,0 +1,60 @@ +import common from "../common/base.mjs"; + +export default { + ...common, + key: "amazon_selling_partner-new-inbound-shipment-to-fba-created", + name: "New Inbound Shipment to FBA Created", + description: "Emit new event when a new inbound shipment to FBA is created. [See the documentation](https://developer-docs.amazon.com/sp-api/reference/getshipments)", + version: "0.0.1", + type: "source", + dedupe: "unique", + props: { + ...common.props, + status: { + propDefinition: [ + common.props.amazonSellingPartner, + "status", + ], + }, + }, + methods: { + ...common.methods, + generateMeta(shipment) { + return { + id: shipment.ShipmentId, + summary: `New Inbound Shipment: ${shipment.ShipmentId}`, + ts: Date.parse(shipment.LastUpdatedDate), + }; + }, + }, + async run() { + const lastTs = this._getLastTs(); + let maxTs = lastTs; + + const shipments = await this.amazonSellingPartner.getPaginatedResources({ + fn: this.amazonSellingPartner.listInboundShipments, + params: { + MarketplaceIds: this.marketplaceId, + LastUpdatedAfter: lastTs, + ShipmentStatusList: this.status?.length + ? this.status.join(",") + : undefined, + }, + resourceKey: "ShipmentData", + }); + + if (!shipments?.length) { + return; + } + + for (const shipment of shipments) { + if (Date.parse(shipment.LastUpdatedDate) > Date.parse(maxTs)) { + maxTs = shipment.LastUpdatedDate; + } + const meta = this.generateMeta(shipment); + this.$emit(shipment, meta); + } + + this._setLastTs(maxTs); + }, +}; diff --git a/components/amazon_selling_partner/sources/new-order-created/new-order-created.mjs b/components/amazon_selling_partner/sources/new-order-created/new-order-created.mjs new file mode 100644 index 0000000000000..93f4abedbdb23 --- /dev/null +++ b/components/amazon_selling_partner/sources/new-order-created/new-order-created.mjs @@ -0,0 +1,48 @@ +import common from "../common/base.mjs"; + +export default { + ...common, + key: "amazon_selling_partner-new-order-created", + name: "New Order Created", + description: "Emit new event when a new order is created in Amazon Seller Central. [See the documentation](https://developer-docs.amazon.com/sp-api/reference/getorders)", + version: "0.0.1", + type: "source", + dedupe: "unique", + methods: { + ...common.methods, + generateMeta(order) { + return { + id: order.AmazonOrderId, + summary: `New Order: ${order.AmazonOrderId}`, + ts: Date.parse(order.PurchaseDate), + }; + }, + }, + async run() { + const lastTs = this._getLastTs(); + let maxTs = lastTs; + + const orders = await this.amazonSellingPartner.getPaginatedResources({ + fn: this.amazonSellingPartner.listOrders, + params: { + MarketplaceIds: this.marketplaceId, + CreatedAfter: lastTs, + }, + resourceKey: "Orders", + }); + + if (!orders?.length) { + return; + } + + for (const order of orders) { + if (Date.parse(order.PurchaseDate) > Date.parse(maxTs)) { + maxTs = order.PurchaseDate; + } + const meta = this.generateMeta(order); + this.$emit(order, meta); + } + + this._setLastTs(maxTs); + }, +}; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 1726721f05b06..fa3c6b1c4c8c1 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -771,7 +771,11 @@ importers: components/amazon_redshift: {} - components/amazon_selling_partner: {} + components/amazon_selling_partner: + dependencies: + '@pipedream/platform': + specifier: ^3.1.0 + version: 3.1.0 components/amazon_ses: dependencies: @@ -13305,8 +13309,7 @@ importers: components/switchboard: {} - components/sylius: - specifiers: {} + components/sylius: {} components/symbl_ai: dependencies: @@ -35948,7 +35951,7 @@ snapshots: '@pipedream/canva@0.2.3': dependencies: - '@pipedream/platform': 3.0.3 + '@pipedream/platform': 3.1.0 transitivePeerDependencies: - debug @@ -35967,7 +35970,7 @@ snapshots: '@pipedream/docusign@0.2.1': dependencies: - '@pipedream/platform': 3.0.3 + '@pipedream/platform': 3.1.0 transitivePeerDependencies: - debug