feat(medusa): tracking links (#177)

* fix: creates tracking links on fulfillments

* fix: typo
This commit is contained in:
Sebastian Rindom
2021-02-18 16:59:19 +01:00
committed by GitHub
parent 24529bd025
commit 99ad43bf47
18 changed files with 371 additions and 26 deletions
@@ -0,0 +1,211 @@
import WebshipperFulfillmentService from "../webshipper-fulfillment"
describe("WebshipperFulfillmentService", () => {
const orderService = {
createShipment: jest.fn(),
}
const swapService = {
createShipment: jest.fn(),
}
const claimService = {
createShipment: jest.fn(),
}
describe("handleWebhook", () => {
beforeEach(() => {
jest.clearAllMocks()
})
it("creates an order shipment", async () => {
const webshipper = new WebshipperFulfillmentService(
{
orderService,
claimService,
swapService,
},
{}
)
webshipper.retrieveRelationship = () => {
return {
data: {
attributes: {
ext_ref: "order_test.ful_test",
},
},
}
}
const body = {
data: {
attributes: {
tracking_links: [
{
url: "https://test/1134",
number: "12324245345",
},
{
url: "https://test/1234",
number: "12324245345",
},
],
},
relationships: {
order: {
id: "order",
},
},
},
}
await webshipper.handleWebhook("", body)
expect(claimService.createShipment).toHaveBeenCalledTimes(0)
expect(swapService.createShipment).toHaveBeenCalledTimes(0)
expect(orderService.createShipment).toHaveBeenCalledTimes(1)
expect(orderService.createShipment).toHaveBeenCalledWith(
"order_test",
"ful_test",
[
{
url: "https://test/1134",
tracking_number: "12324245345",
},
{
url: "https://test/1234",
tracking_number: "12324245345",
},
]
)
})
it("creates a claim shipment", async () => {
const webshipper = new WebshipperFulfillmentService(
{
orderService,
claimService,
swapService,
},
{}
)
webshipper.retrieveRelationship = () => {
return {
data: {
attributes: {
ext_ref: "claim_test.ful_test",
},
},
}
}
const body = {
data: {
attributes: {
tracking_links: [
{
url: "https://test/1134",
number: "12324245345",
},
{
url: "https://test/1234",
number: "12324245345",
},
],
},
relationships: {
order: {
id: "order",
},
},
},
}
await webshipper.handleWebhook("", body)
expect(orderService.createShipment).toHaveBeenCalledTimes(0)
expect(swapService.createShipment).toHaveBeenCalledTimes(0)
expect(claimService.createShipment).toHaveBeenCalledTimes(1)
expect(claimService.createShipment).toHaveBeenCalledWith(
"claim_test",
"ful_test",
[
{
url: "https://test/1134",
tracking_number: "12324245345",
},
{
url: "https://test/1234",
tracking_number: "12324245345",
},
]
)
})
it("creates a swap shipment", async () => {
const webshipper = new WebshipperFulfillmentService(
{
orderService,
claimService,
swapService,
},
{}
)
webshipper.retrieveRelationship = () => {
return {
data: {
attributes: {
ext_ref: "swap_test.ful_test",
},
},
}
}
const body = {
data: {
attributes: {
tracking_links: [
{
url: "https://test/1134",
number: "12324245345",
},
{
url: "https://test/1234",
number: "12324245345",
},
],
},
relationships: {
order: {
id: "order",
},
},
},
}
await webshipper.handleWebhook("", body)
expect(orderService.createShipment).toHaveBeenCalledTimes(0)
expect(claimService.createShipment).toHaveBeenCalledTimes(0)
expect(swapService.createShipment).toHaveBeenCalledTimes(1)
expect(swapService.createShipment).toHaveBeenCalledWith(
"swap_test",
"ful_test",
[
{
url: "https://test/1134",
tracking_number: "12324245345",
},
{
url: "https://test/1234",
tracking_number: "12324245345",
},
]
)
})
})
})
@@ -363,9 +363,10 @@ class WebshipperFulfillmentService extends FulfillmentService {
body.data.relationships.order
)
if (wsOrder.data && wsOrder.data.attributes.ext_ref) {
const trackingNumbers = body.data.attributes.tracking_links.map(
(l) => l.number
)
const trackingLinks = body.data.attributes.tracking_links.map((l) => ({
url: l.url,
tracking_number: l.number,
}))
const [orderId, fulfillmentIndex] = wsOrder.data.attributes.ext_ref.split(
"."
)
@@ -375,7 +376,7 @@ class WebshipperFulfillmentService extends FulfillmentService {
return this.swapService_.createShipment(
orderId,
fulfillmentIndex,
trackingNumbers
trackingLinks
)
} else {
const swap = await this.swapService_.retrieve(orderId.substring(1), {
@@ -385,21 +386,21 @@ class WebshipperFulfillmentService extends FulfillmentService {
return this.swapService_.createShipment(
swap.id,
fulfillment.id,
trackingNumbers
trackingLinks
)
}
} else if (orderId.charAt(0).toLowerCase() === "c") {
return this.claimService_.createShipment(
orderId,
fulfillmentIndex,
trackingNumbers
trackingLinks
)
} else {
if (fulfillmentIndex.startsWith("ful")) {
return this.orderService_.createShipment(
orderId,
fulfillmentIndex,
trackingNumbers
trackingLinks
)
} else {
const order = await this.orderService_.retrieve(orderId, {
@@ -411,7 +412,7 @@ class WebshipperFulfillmentService extends FulfillmentService {
return this.orderService_.createShipment(
order.id,
fulfillment.id,
trackingNumbers
trackingLinks
)
}
}