diff --git a/components/wildberries/actions/common/constants.ts b/components/wildberries/actions/common/constants.ts new file mode 100644 index 0000000000000..fc42da2b6446f --- /dev/null +++ b/components/wildberries/actions/common/constants.ts @@ -0,0 +1,44 @@ +export default { + STICKERS_REQUEST_TYPE: [ + "code128", + "qr", + ], + ORDER_STATUSES: [ + { + label: "New order", + value: "0", + }, + { + label: "Accepted the order", + value: "1", + }, + { + label: "Assembly task completed", + value: "2", + }, + { + label: "Assembly order rejected", + value: "3", + }, + { + label: "On delivery by courier", + value: "5", + }, + { + label: "The client received the goods (courier delivery and pickup)", + value: "6", + }, + { + label: "The client did not accept the goods (courier delivery and pickup)", + value: "7", + }, + { + label: "Goods for pickup from the store accepted for work", + value: "8", + }, + { + label: "Product for self-pickup from the store is ready for pickup", + value: "9", + }, + ], +}; diff --git a/components/wildberries/actions/list-order-stickers/list-order-stickers.ts b/components/wildberries/actions/list-order-stickers/list-order-stickers.ts new file mode 100644 index 0000000000000..3934e0dd32d8a --- /dev/null +++ b/components/wildberries/actions/list-order-stickers/list-order-stickers.ts @@ -0,0 +1,42 @@ +import app from "../../app/wildberries.app"; +import constants from "../common/constants"; +import { defineAction } from "@pipedream/types"; + +export default defineAction({ + name: "List Order Stickers", + description: "List order stickers. [See docs here](https://suppliers-api.wildberries.ru/swagger/index.html#/Marketplace/post_api_v2_orders_stickers)", + key: "wildberries-list-order-stickers", + version: "0.0.1", + type: "action", + props: { + app, + orderIds: { + propDefinition: [ + app, + "orderIds", + ], + }, + type: { + label: "Type", + type: "string", + description: "Sticker type, default: `code128`.", + default: "code128", + options: constants.STICKERS_REQUEST_TYPE, + }, + asPdf: { + type: "boolean", + label: "List as PDF", + description: "Set true for use the PDF API [See docs here](https://suppliers-api.wildberries.ru/swagger/index.html#/Marketplace/post_api_v2_orders_stickers_pdf).", + default: false, + }, + }, + async run({ $ }) { + const params = { + orderIds: this.orderIds, + type: this.type, + }; + const response = await this.app.listOrderStickers($, params, this.asPdf); + $.export("$summary", `Successfully listed stickers for ${this.orderIds.length} orders.`); + return response; + }, +}); diff --git a/components/wildberries/actions/list-orders/list-orders.ts b/components/wildberries/actions/list-orders/list-orders.ts new file mode 100644 index 0000000000000..5f9bfd8b0336d --- /dev/null +++ b/components/wildberries/actions/list-orders/list-orders.ts @@ -0,0 +1,67 @@ +import app from "../../app/wildberries.app"; +import { defineAction } from "@pipedream/types"; + +export default defineAction({ + name: "List Orders", + description: "Returns a list of orders. [See docs here](https://suppliers-api.wildberries.ru/swagger/index.html#/Marketplace/get_api_v2_orders)", + key: "wildberries-list-orders", + version: "0.0.1", + type: "action", + props: { + app, + dateStart: { + type: "string", + label: "Starting date", + description: "Starting date for querying in [RFC3339](https://www.rfc-editor.org/rfc/rfc3339) format.", + }, + dateEnd: { + type: "string", + label: "Ending date", + description: "Ending date for querying in [RFC3339](https://www.rfc-editor.org/rfc/rfc3339) format.", + optional: true, + }, + status: { + propDefinition: [ + app, + "status", + ], + optional: true, + description: "Select by status", + }, + take: { + type: "integer", + label: "Take", + description: "How many records to return at a time.", + default: 10, + }, + skip: { + type: "integer", + label: "Skip", + description: "How many records to skip.", + default: 0, + }, + orderId: { + propDefinition: [ + app, + "orderId", + ], + description: "Select by order id", + optional: true, + }, + }, + async run({ $ }) { + const params = { + date_start: this.dateStart, + date_end: this.dateEnd, + status: typeof (this.status) !== "undefined" ? + parseInt(this.status) : + null, + take: this.take, + skip: this.skip, + id: this.orderId, + }; + const response = await this.app.listOrders($, params); + $.export("$summary", `Successfully fetched ${response.orders.length} orders`); + return response; + }, +}); diff --git a/components/wildberries/actions/update-order-status/update-order-status.ts b/components/wildberries/actions/update-order-status/update-order-status.ts new file mode 100644 index 0000000000000..da498b9dd4b00 --- /dev/null +++ b/components/wildberries/actions/update-order-status/update-order-status.ts @@ -0,0 +1,46 @@ +import app from "../../app/wildberries.app"; +import { defineAction } from "@pipedream/types"; + +export default defineAction({ + name: "Update Order Status", + description: "Update a order status. [See docs here](https://suppliers-api.wildberries.ru/swagger/index.html#/Marketplace/put_api_v2_orders)", + key: "wildberries-update-order-status", + version: "0.0.1", + type: "action", + props: { + app, + orderId: { + propDefinition: [ + app, + "orderId", + ], + }, + status: { + propDefinition: [ + app, + "status", + ], + }, + sgtin: { + type: "any", + label: "SGTIN", + description: "Array required only for pharmaceutical products when they are transferred to status `Customer received the goods`.\n\n**Example:** `[{ code: string, numerator: integer, denominator: integer, sid: integer }]`\n\n[See docs here](https://suppliers-api.wildberries.ru/swagger/index.html#/Marketplace/put_api_v2_orders)", + optional: true, + }, + }, + async run({ $ }) { + const params = { + orderId: this.orderId, + status: parseInt(this.status), + sgtin: this.sgtin, + }; + if (this.sgtin && !Array.isArray(this.sgtin)) { + params.sgtin = [ + this.sgtin, + ]; + } + const response = await this.app.updateOrderStatus($, params); + $.export("$summary", `Successfully updated the status for order: ${this.orderId}`); + return response; + }, +}); diff --git a/components/wildberries/app/wildberries.app.ts b/components/wildberries/app/wildberries.app.ts index 2e31c59b0e260..37890edfda053 100644 --- a/components/wildberries/app/wildberries.app.ts +++ b/components/wildberries/app/wildberries.app.ts @@ -1,13 +1,90 @@ import { defineApp } from "@pipedream/types"; +import { axios } from "@pipedream/platform"; +import constants from "../actions/common/constants"; export default defineApp({ type: "app", app: "wildberries", - propDefinitions: {}, + propDefinitions: { + orderId: { + type: "integer", + label: "Order id", + description: "Set the Order Id.", + }, + orderIds: { + type: "integer[]", + label: "Order ids", + description: "Array of order ids.\n\n**Example:**`[8423848, 6436344]`", + }, + status: { + label: "Status", + type: "string", + description: "Set the new status of the order.", + options: constants.ORDER_STATUSES, + }, + }, methods: { - // this.$auth contains connected account data - authKeys() { - console.log(Object.keys(this.$auth)); + _getBaseUrl() { + return "https://suppliers-api.wildberries.ru/api/v2"; + }, + _getHeaders() { + return { + "content-type": "application/json", + "Authorization": `Bearer ${this.$auth.api_key}`, + }; + }, + _getRequestParams(opts) { + return { + ...opts, + url: this._getBaseUrl() + opts.path, + headers: this._getHeaders(), + }; + }, + async listOrders($ = this, params) { + const response = await axios($, this._getRequestParams({ + method: "GET", + path: "/orders", + params: this.filterEmptyValues(params), + })); + return response; + }, + async updateOrderStatus($ = this, params) { + const response = await axios($, this._getRequestParams({ + method: "PUT", + path: "/orders", + data: [ + this.filterEmptyValues(params), + ], + })); + return response; + }, + async listOrderStickers($ = this, params, asPdf) { + let path = "/orders/stickers"; + if (asPdf) { + path += "/pdf"; + } + const response = await axios($, this._getRequestParams({ + method: "POST", + path, + data: this.filterEmptyValues(params), + })); + return response; + }, + filterEmptyValues(obj) { + return Object.entries(obj) + .reduce((reduction, + [ + key, + value, + ]) => { + if (value === undefined || value === null) { + return reduction; + } + return { + ...reduction, + [key]: value, + }; + }, {}); }, }, -}); \ No newline at end of file +}); diff --git a/components/wildberries/package.json b/components/wildberries/package.json index dce6577b43783..17f36118a0161 100644 --- a/components/wildberries/package.json +++ b/components/wildberries/package.json @@ -7,11 +7,16 @@ "pipedream", "wildberries" ], - "files": ["dist"], + "files": [ + "dist" + ], "homepage": "https://pipedream.com/apps/wildberries", "author": "Pipedream (https://pipedream.com/)", "license": "MIT", "publishConfig": { "access": "public" + }, + "dependencies": { + "@pipedream/platform": "^1.1.1" } -} \ No newline at end of file +} diff --git a/components/wildberries/tsconfig.json b/components/wildberries/tsconfig.json new file mode 100644 index 0000000000000..335fa6f1cf6ec --- /dev/null +++ b/components/wildberries/tsconfig.json @@ -0,0 +1,21 @@ +{ + "compilerOptions": { + "lib": ["es2020"], + "module": "ES2020", + "target": "ES2020", + "moduleResolution": "node", + "listEmittedFiles": true, // Used as a part of the build task, since we need to pass emitted files to our post-build script + "composite": true, + "outDir": "dist", + "allowSyntheticDefaultImports": true, + }, + "allowJs": true, + "include": [ + "app", + "actions", + "sources" + ], + "exclude": [ + "dist", + ] + } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 0393e6c58ffcb..bf72c6e1dbcc2 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -2092,7 +2092,10 @@ importers: specifiers: {} components/wildberries: - specifiers: {} + specifiers: + '@pipedream/platform': ^1.1.1 + dependencies: + '@pipedream/platform': 1.1.1 components/wistia: specifiers: