From b9d6f95dbd32c096e59057797fd0cf479ff23c7b Mon Sep 17 00:00:00 2001 From: Zakaria El Asri <33696020+zakariaelas@users.noreply.github.com> Date: Thu, 16 Dec 2021 15:10:41 +0100 Subject: [PATCH] feat: add returns admin endpoints to medusa-js (#935) * add: returns admin endpoint to medusa-js * fix: http verbs * fix duplicate import * fix: type for cancel * fix: conflicting import Co-authored-by: Sebastian Rindom --- .../medusa-js/src/resources/admin/index.ts | 2 + .../medusa-js/src/resources/admin/returns.ts | 50 +++++++++++++++++++ packages/medusa/src/api/index.js | 1 + .../routes/admin/returns/receive-return.ts | 2 +- 4 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 packages/medusa-js/src/resources/admin/returns.ts diff --git a/packages/medusa-js/src/resources/admin/index.ts b/packages/medusa-js/src/resources/admin/index.ts index 64afafd509..609962a3af 100644 --- a/packages/medusa-js/src/resources/admin/index.ts +++ b/packages/medusa-js/src/resources/admin/index.ts @@ -7,6 +7,7 @@ import AdminDraftOrdersResource from "./draft-orders" import AdminGiftCardsResource from "./gift-cards" import AdminInvitesResource from "./invites" import AdminNotesResource from "./notes" +import AdminReturnsResource from "./returns" import AdminOrdersResource from "./orders" import AdminReturnReasonsResource from "./return-reasons" import AdminVariantsResource from "./variants" @@ -24,6 +25,7 @@ class Admin extends BaseResource { public giftCards = new AdminGiftCardsResource(this.client) public invites = new AdminInvitesResource(this.client) public notes = new AdminNotesResource(this.client) + public returns = new AdminReturnsResource(this.client) public orders = new AdminOrdersResource(this.client) public returnReasons = new AdminReturnReasonsResource(this.client) public variants = new AdminVariantsResource(this.client) diff --git a/packages/medusa-js/src/resources/admin/returns.ts b/packages/medusa-js/src/resources/admin/returns.ts new file mode 100644 index 0000000000..6b11e0b9de --- /dev/null +++ b/packages/medusa-js/src/resources/admin/returns.ts @@ -0,0 +1,50 @@ +import { + AdminGetReturnsParams, + AdminReturnsCancelRes, + AdminReturnsListRes, + AdminReturnsRes, +} from "@medusajs/medusa" +import { ResponsePromise } from "../../typings" +import BaseResource from "../base" + +class AdminReturnsResource extends BaseResource { + /** + * @description cancels a return + * @param id id of return to cancel + * @returns the order for which the return was canceled + */ + cancel(id: string): ResponsePromise { + const path = `/admin/returns/${id}/cancel` + return this.client.request("POST", path) + } + + /** + * @description receive a return + * @param id id of the return to receive. + * @returns the return + */ + receive(id: string): ResponsePromise { + const path = `/admin/returns/${id}/receive` + return this.client.request("POST", path) + } + + /** + * @description lists returns matching a query + * @param query query for searching returns + * @returns a list of returns matching the query + */ + list(query?: AdminGetReturnsParams): ResponsePromise { + let path = `/admin/returns/` + + if (query) { + const queryString = Object.entries(query).map(([key, value]) => { + return typeof value !== "undefined" ? `${key}=${value}` : "" + }) + path = `/admin/returns?${queryString.join("&")}` + } + + return this.client.request("GET", path) + } +} + +export default AdminReturnsResource diff --git a/packages/medusa/src/api/index.js b/packages/medusa/src/api/index.js index d7027378cf..627e69d83f 100644 --- a/packages/medusa/src/api/index.js +++ b/packages/medusa/src/api/index.js @@ -31,6 +31,7 @@ export * from "./routes/admin/orders" export * from "./routes/admin/variants" export * from "./routes/admin/return-reasons" export * from "./routes/admin/swaps" +export * from "./routes/admin/returns" export * from "./routes/admin/shipping-options" export * from "./routes/admin/regions" diff --git a/packages/medusa/src/api/routes/admin/returns/receive-return.ts b/packages/medusa/src/api/routes/admin/returns/receive-return.ts index f15fcd741e..df2795f1e1 100644 --- a/packages/medusa/src/api/routes/admin/returns/receive-return.ts +++ b/packages/medusa/src/api/routes/admin/returns/receive-return.ts @@ -94,7 +94,7 @@ export default async (req, res) => { res.status(200).json({ return: receivedReturn }) } -export class Item { +class Item { @IsString() item_id: string