feat(medusa): tracking links (#177)
* fix: creates tracking links on fulfillments * fix: typo
This commit is contained in:
1 parent
24529bd025
commit
99ad43bf47
18 files changed
+371
-26
No files matched your search
@@ -0,0 +1,19 @@
|
|||||||
|
import { NotificationService } from "medusa-interfaces";
|
||||||
|
|
||||||
|
class TestNotiService extends NotificationService {
|
||||||
|
static identifier = "test-not";
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
async sendNotification() {
|
||||||
|
return Promise.resolve();
|
||||||
|
}
|
||||||
|
|
||||||
|
async resendNotification() {
|
||||||
|
return Promise.resolve();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default TestNotiService;
|
||||||
+211
@@ -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
|
body.data.relationships.order
|
||||||
)
|
)
|
||||||
if (wsOrder.data && wsOrder.data.attributes.ext_ref) {
|
if (wsOrder.data && wsOrder.data.attributes.ext_ref) {
|
||||||
const trackingNumbers = body.data.attributes.tracking_links.map(
|
const trackingLinks = body.data.attributes.tracking_links.map((l) => ({
|
||||||
(l) => l.number
|
url: l.url,
|
||||||
)
|
tracking_number: l.number,
|
||||||
|
}))
|
||||||
const [orderId, fulfillmentIndex] = wsOrder.data.attributes.ext_ref.split(
|
const [orderId, fulfillmentIndex] = wsOrder.data.attributes.ext_ref.split(
|
||||||
"."
|
"."
|
||||||
)
|
)
|
||||||
@@ -375,7 +376,7 @@ class WebshipperFulfillmentService extends FulfillmentService {
|
|||||||
return this.swapService_.createShipment(
|
return this.swapService_.createShipment(
|
||||||
orderId,
|
orderId,
|
||||||
fulfillmentIndex,
|
fulfillmentIndex,
|
||||||
trackingNumbers
|
trackingLinks
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
const swap = await this.swapService_.retrieve(orderId.substring(1), {
|
const swap = await this.swapService_.retrieve(orderId.substring(1), {
|
||||||
@@ -385,21 +386,21 @@ class WebshipperFulfillmentService extends FulfillmentService {
|
|||||||
return this.swapService_.createShipment(
|
return this.swapService_.createShipment(
|
||||||
swap.id,
|
swap.id,
|
||||||
fulfillment.id,
|
fulfillment.id,
|
||||||
trackingNumbers
|
trackingLinks
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
} else if (orderId.charAt(0).toLowerCase() === "c") {
|
} else if (orderId.charAt(0).toLowerCase() === "c") {
|
||||||
return this.claimService_.createShipment(
|
return this.claimService_.createShipment(
|
||||||
orderId,
|
orderId,
|
||||||
fulfillmentIndex,
|
fulfillmentIndex,
|
||||||
trackingNumbers
|
trackingLinks
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
if (fulfillmentIndex.startsWith("ful")) {
|
if (fulfillmentIndex.startsWith("ful")) {
|
||||||
return this.orderService_.createShipment(
|
return this.orderService_.createShipment(
|
||||||
orderId,
|
orderId,
|
||||||
fulfillmentIndex,
|
fulfillmentIndex,
|
||||||
trackingNumbers
|
trackingLinks
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
const order = await this.orderService_.retrieve(orderId, {
|
const order = await this.orderService_.retrieve(orderId, {
|
||||||
@@ -411,7 +412,7 @@ class WebshipperFulfillmentService extends FulfillmentService {
|
|||||||
return this.orderService_.createShipment(
|
return this.orderService_.createShipment(
|
||||||
order.id,
|
order.id,
|
||||||
fulfillment.id,
|
fulfillment.id,
|
||||||
trackingNumbers
|
trackingLinks
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,6 +10,8 @@ const defaultRelations = [
|
|||||||
"shipping_methods",
|
"shipping_methods",
|
||||||
"payments",
|
"payments",
|
||||||
"fulfillments",
|
"fulfillments",
|
||||||
|
"fulfillments.tracking_links",
|
||||||
|
"fulfillments.items",
|
||||||
"returns",
|
"returns",
|
||||||
"gift_cards",
|
"gift_cards",
|
||||||
"gift_card_transactions",
|
"gift_card_transactions",
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ export default async (req, res) => {
|
|||||||
await claimService.createShipment(
|
await claimService.createShipment(
|
||||||
claim_id,
|
claim_id,
|
||||||
value.fulfillment_id,
|
value.fulfillment_id,
|
||||||
value.tracking_numbers
|
value.tracking_numbers.map(n => ({ tracking_number: n }))
|
||||||
)
|
)
|
||||||
|
|
||||||
const order = await orderService.retrieve(id, {
|
const order = await orderService.retrieve(id, {
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ export default async (req, res) => {
|
|||||||
await orderService.createShipment(
|
await orderService.createShipment(
|
||||||
id,
|
id,
|
||||||
value.fulfillment_id,
|
value.fulfillment_id,
|
||||||
value.tracking_numbers
|
value.tracking_numbers.map(n => ({ tracking_number: n }))
|
||||||
)
|
)
|
||||||
|
|
||||||
const order = await orderService.retrieve(id, {
|
const order = await orderService.retrieve(id, {
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ export default async (req, res) => {
|
|||||||
await swapService.createShipment(
|
await swapService.createShipment(
|
||||||
swap_id,
|
swap_id,
|
||||||
value.fulfillment_id,
|
value.fulfillment_id,
|
||||||
value.tracking_numbers
|
value.tracking_numbers.map(n => ({ tracking_number: n }))
|
||||||
)
|
)
|
||||||
|
|
||||||
const order = await orderService.retrieve(id, {
|
const order = await orderService.retrieve(id, {
|
||||||
|
|||||||
@@ -188,6 +188,8 @@ export const defaultRelations = [
|
|||||||
"shipping_methods",
|
"shipping_methods",
|
||||||
"payments",
|
"payments",
|
||||||
"fulfillments",
|
"fulfillments",
|
||||||
|
"fulfillments.tracking_links",
|
||||||
|
"fulfillments.items",
|
||||||
"returns",
|
"returns",
|
||||||
"gift_cards",
|
"gift_cards",
|
||||||
"gift_card_transactions",
|
"gift_card_transactions",
|
||||||
@@ -271,6 +273,7 @@ export const allowedRelations = [
|
|||||||
"shipping_methods",
|
"shipping_methods",
|
||||||
"payments",
|
"payments",
|
||||||
"fulfillments",
|
"fulfillments",
|
||||||
|
"fulfillments.tracking_links",
|
||||||
"returns",
|
"returns",
|
||||||
"claims",
|
"claims",
|
||||||
"swaps",
|
"swaps",
|
||||||
|
|||||||
@@ -0,0 +1,16 @@
|
|||||||
|
import {MigrationInterface, QueryRunner} from "typeorm";
|
||||||
|
|
||||||
|
export class trackingLinks1613656135167 implements MigrationInterface {
|
||||||
|
name = 'trackingLinks1613656135167'
|
||||||
|
|
||||||
|
public async up(queryRunner: QueryRunner): Promise<void> {
|
||||||
|
await queryRunner.query(`CREATE TABLE "tracking_link" ("id" character varying NOT NULL, "url" character varying, "tracking_number" character varying NOT NULL, "fulfillment_id" character varying NOT NULL, "created_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "updated_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "deleted_at" TIMESTAMP WITH TIME ZONE, "metadata" jsonb, "idempotency_key" character varying, CONSTRAINT "PK_fcfd77feb9012ec2126d7c0bfb6" PRIMARY KEY ("id"))`);
|
||||||
|
await queryRunner.query(`ALTER TABLE "tracking_link" ADD CONSTRAINT "FK_471e9e4c96e02ba209a307db32b" FOREIGN KEY ("fulfillment_id") REFERENCES "fulfillment"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async down(queryRunner: QueryRunner): Promise<void> {
|
||||||
|
await queryRunner.query(`ALTER TABLE "tracking_link" DROP CONSTRAINT "FK_471e9e4c96e02ba209a307db32b"`);
|
||||||
|
await queryRunner.query(`DROP TABLE "tracking_link"`);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -22,6 +22,7 @@ import { FulfillmentProvider } from "./fulfillment-provider"
|
|||||||
import { FulfillmentItem } from "./fulfillment-item"
|
import { FulfillmentItem } from "./fulfillment-item"
|
||||||
import { Swap } from "./swap"
|
import { Swap } from "./swap"
|
||||||
import { ClaimOrder } from "./claim-order"
|
import { ClaimOrder } from "./claim-order"
|
||||||
|
import { TrackingLink } from "./tracking-link"
|
||||||
|
|
||||||
@Entity()
|
@Entity()
|
||||||
export class Fulfillment {
|
export class Fulfillment {
|
||||||
@@ -76,6 +77,13 @@ export class Fulfillment {
|
|||||||
)
|
)
|
||||||
items: FulfillmentItem[]
|
items: FulfillmentItem[]
|
||||||
|
|
||||||
|
@OneToMany(
|
||||||
|
() => TrackingLink,
|
||||||
|
tl => tl.fulfillment,
|
||||||
|
{ cascade: ["insert"] }
|
||||||
|
)
|
||||||
|
tracking_links: TrackingLink[]
|
||||||
|
|
||||||
@Column({ type: "jsonb", default: [] })
|
@Column({ type: "jsonb", default: [] })
|
||||||
tracking_numbers: string[]
|
tracking_numbers: string[]
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,63 @@
|
|||||||
|
import {
|
||||||
|
Entity,
|
||||||
|
Index,
|
||||||
|
BeforeInsert,
|
||||||
|
Column,
|
||||||
|
DeleteDateColumn,
|
||||||
|
CreateDateColumn,
|
||||||
|
UpdateDateColumn,
|
||||||
|
PrimaryColumn,
|
||||||
|
OneToOne,
|
||||||
|
OneToMany,
|
||||||
|
ManyToOne,
|
||||||
|
ManyToMany,
|
||||||
|
JoinColumn,
|
||||||
|
JoinTable,
|
||||||
|
} from "typeorm"
|
||||||
|
import { ulid } from "ulid"
|
||||||
|
|
||||||
|
import { Fulfillment } from "./fulfillment"
|
||||||
|
|
||||||
|
@Entity()
|
||||||
|
export class TrackingLink {
|
||||||
|
@PrimaryColumn()
|
||||||
|
id: string
|
||||||
|
|
||||||
|
@Column({ nullable: true })
|
||||||
|
url: string
|
||||||
|
|
||||||
|
@Column()
|
||||||
|
tracking_number: string
|
||||||
|
|
||||||
|
@Column()
|
||||||
|
fulfillment_id: string
|
||||||
|
|
||||||
|
@ManyToOne(
|
||||||
|
() => Fulfillment,
|
||||||
|
ful => ful.tracking_links
|
||||||
|
)
|
||||||
|
@JoinColumn({ name: "fulfillment_id" })
|
||||||
|
fulfillment: Fulfillment
|
||||||
|
|
||||||
|
@CreateDateColumn({ type: "timestamptz" })
|
||||||
|
created_at: Date
|
||||||
|
|
||||||
|
@UpdateDateColumn({ type: "timestamptz" })
|
||||||
|
updated_at: Date
|
||||||
|
|
||||||
|
@DeleteDateColumn({ type: "timestamptz" })
|
||||||
|
deleted_at: Date
|
||||||
|
|
||||||
|
@Column({ type: "jsonb", nullable: true })
|
||||||
|
metadata: any
|
||||||
|
|
||||||
|
@Column({ nullable: true })
|
||||||
|
idempotency_key: string
|
||||||
|
|
||||||
|
@BeforeInsert()
|
||||||
|
private beforeInsert() {
|
||||||
|
if (this.id) return
|
||||||
|
const id = ulid()
|
||||||
|
this.id = `tlink_${id}`
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
import { EntityRepository, Repository } from "typeorm"
|
||||||
|
import { TrackingLink } from "../models/tracking-link"
|
||||||
|
|
||||||
|
@EntityRepository(TrackingLink)
|
||||||
|
export class TrackingLinkRepository extends Repository<TrackingLink> {}
|
||||||
@@ -95,6 +95,7 @@ describe("FulfillmentService", () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
describe("createShipment", () => {
|
describe("createShipment", () => {
|
||||||
|
const trackingLinkRepository = MockRepository({ create: c => c })
|
||||||
const fulfillmentRepository = MockRepository({
|
const fulfillmentRepository = MockRepository({
|
||||||
findOne: () => Promise.resolve({ id: IdMap.getId("fulfillment") }),
|
findOne: () => Promise.resolve({ id: IdMap.getId("fulfillment") }),
|
||||||
})
|
})
|
||||||
@@ -102,6 +103,7 @@ describe("FulfillmentService", () => {
|
|||||||
const fulfillmentService = new FulfillmentService({
|
const fulfillmentService = new FulfillmentService({
|
||||||
manager: MockManager,
|
manager: MockManager,
|
||||||
fulfillmentRepository,
|
fulfillmentRepository,
|
||||||
|
trackingLinkRepository,
|
||||||
})
|
})
|
||||||
|
|
||||||
const now = new Date()
|
const now = new Date()
|
||||||
@@ -113,14 +115,17 @@ describe("FulfillmentService", () => {
|
|||||||
it("calls order model functions", async () => {
|
it("calls order model functions", async () => {
|
||||||
await fulfillmentService.createShipment(
|
await fulfillmentService.createShipment(
|
||||||
IdMap.getId("fulfillment"),
|
IdMap.getId("fulfillment"),
|
||||||
["1234", "2345"],
|
[{ tracking_number: "1234" }, { tracking_number: "2345" }],
|
||||||
{}
|
{}
|
||||||
)
|
)
|
||||||
|
|
||||||
expect(fulfillmentRepository.save).toHaveBeenCalledTimes(1)
|
expect(fulfillmentRepository.save).toHaveBeenCalledTimes(1)
|
||||||
expect(fulfillmentRepository.save).toHaveBeenCalledWith({
|
expect(fulfillmentRepository.save).toHaveBeenCalledWith({
|
||||||
id: IdMap.getId("fulfillment"),
|
id: IdMap.getId("fulfillment"),
|
||||||
tracking_numbers: ["1234", "2345"],
|
tracking_links: [
|
||||||
|
{ tracking_number: "1234" },
|
||||||
|
{ tracking_number: "2345" },
|
||||||
|
],
|
||||||
metadata: {},
|
metadata: {},
|
||||||
shipped_at: now,
|
shipped_at: now,
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1182,14 +1182,14 @@ describe("OrderService", () => {
|
|||||||
await orderService.createShipment(
|
await orderService.createShipment(
|
||||||
IdMap.getId("test"),
|
IdMap.getId("test"),
|
||||||
IdMap.getId("fulfillment"),
|
IdMap.getId("fulfillment"),
|
||||||
["1234", "2345"],
|
[{ tracking_number: "1234" }, { tracking_number: "2345" }],
|
||||||
{}
|
{}
|
||||||
)
|
)
|
||||||
|
|
||||||
expect(fulfillmentService.createShipment).toHaveBeenCalledTimes(1)
|
expect(fulfillmentService.createShipment).toHaveBeenCalledTimes(1)
|
||||||
expect(fulfillmentService.createShipment).toHaveBeenCalledWith(
|
expect(fulfillmentService.createShipment).toHaveBeenCalledWith(
|
||||||
IdMap.getId("fulfillment"),
|
IdMap.getId("fulfillment"),
|
||||||
["1234", "2345"],
|
[{ tracking_number: "1234" }, { tracking_number: "2345" }],
|
||||||
{}
|
{}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -418,7 +418,7 @@ class ClaimService extends BaseService {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
async createShipment(id, fulfillmentId, trackingNumbers, metadata = []) {
|
async createShipment(id, fulfillmentId, trackingLinks, metadata = []) {
|
||||||
return this.atomicPhase_(async manager => {
|
return this.atomicPhase_(async manager => {
|
||||||
const claim = await this.retrieve(id, {
|
const claim = await this.retrieve(id, {
|
||||||
relations: ["additional_items"],
|
relations: ["additional_items"],
|
||||||
@@ -426,7 +426,7 @@ class ClaimService extends BaseService {
|
|||||||
|
|
||||||
const shipment = await this.fulfillmentService_
|
const shipment = await this.fulfillmentService_
|
||||||
.withTransaction(manager)
|
.withTransaction(manager)
|
||||||
.createShipment(fulfillmentId, trackingNumbers, metadata)
|
.createShipment(fulfillmentId, trackingLinks, metadata)
|
||||||
|
|
||||||
claim.fulfillment_status = "shipped"
|
claim.fulfillment_status = "shipped"
|
||||||
|
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ class FulfillmentService extends BaseService {
|
|||||||
manager,
|
manager,
|
||||||
totalsService,
|
totalsService,
|
||||||
fulfillmentRepository,
|
fulfillmentRepository,
|
||||||
|
trackingLinkRepository,
|
||||||
shippingProfileService,
|
shippingProfileService,
|
||||||
lineItemService,
|
lineItemService,
|
||||||
fulfillmentProviderService,
|
fulfillmentProviderService,
|
||||||
@@ -26,6 +27,9 @@ class FulfillmentService extends BaseService {
|
|||||||
/** @private @const {FulfillmentRepository} */
|
/** @private @const {FulfillmentRepository} */
|
||||||
this.fulfillmentRepository_ = fulfillmentRepository
|
this.fulfillmentRepository_ = fulfillmentRepository
|
||||||
|
|
||||||
|
/** @private @const {TrackingLinkRepository} */
|
||||||
|
this.trackingLinkRepository_ = trackingLinkRepository
|
||||||
|
|
||||||
/** @private @const {ShippingProfileService} */
|
/** @private @const {ShippingProfileService} */
|
||||||
this.shippingProfileService_ = shippingProfileService
|
this.shippingProfileService_ = shippingProfileService
|
||||||
|
|
||||||
@@ -44,6 +48,7 @@ class FulfillmentService extends BaseService {
|
|||||||
const cloned = new FulfillmentService({
|
const cloned = new FulfillmentService({
|
||||||
manager: transactionManager,
|
manager: transactionManager,
|
||||||
totalsService: this.totalsService_,
|
totalsService: this.totalsService_,
|
||||||
|
trackingLinkRepository: this.trackingLinkRepository_,
|
||||||
fulfillmentRepository: this.fulfillmentRepository_,
|
fulfillmentRepository: this.fulfillmentRepository_,
|
||||||
shippingProfileService: this.shippingProfileService_,
|
shippingProfileService: this.shippingProfileService_,
|
||||||
lineItemService: this.lineItemService_,
|
lineItemService: this.lineItemService_,
|
||||||
@@ -235,15 +240,18 @@ class FulfillmentService extends BaseService {
|
|||||||
* Creates a shipment by marking a fulfillment as shipped. Adds
|
* Creates a shipment by marking a fulfillment as shipped. Adds
|
||||||
* tracking numbers and potentially more metadata.
|
* tracking numbers and potentially more metadata.
|
||||||
* @param {Order} fulfillmentId - the fulfillment to ship
|
* @param {Order} fulfillmentId - the fulfillment to ship
|
||||||
* @param {string[]} trackingNumbers - tracking numbers for the shipment
|
* @param {TrackingLink[]} trackingNumbers - tracking numbers for the shipment
|
||||||
* @param {object} metadata - potential metadata to add
|
* @param {object} metadata - potential metadata to add
|
||||||
* @return {Fulfillment} the shipped fulfillment
|
* @return {Fulfillment} the shipped fulfillment
|
||||||
*/
|
*/
|
||||||
async createShipment(fulfillmentId, trackingNumbers, metadata) {
|
async createShipment(fulfillmentId, trackingLinks, metadata) {
|
||||||
return this.atomicPhase_(async manager => {
|
return this.atomicPhase_(async manager => {
|
||||||
const fulfillmentRepository = manager.getCustomRepository(
|
const fulfillmentRepository = manager.getCustomRepository(
|
||||||
this.fulfillmentRepository_
|
this.fulfillmentRepository_
|
||||||
)
|
)
|
||||||
|
const trackingLinkRepo = manager.getCustomRepository(
|
||||||
|
this.trackingLinkRepository_
|
||||||
|
)
|
||||||
|
|
||||||
const fulfillment = await this.retrieve(fulfillmentId, {
|
const fulfillment = await this.retrieve(fulfillmentId, {
|
||||||
relations: ["items"],
|
relations: ["items"],
|
||||||
@@ -251,7 +259,11 @@ class FulfillmentService extends BaseService {
|
|||||||
|
|
||||||
const now = new Date()
|
const now = new Date()
|
||||||
fulfillment.shipped_at = now
|
fulfillment.shipped_at = now
|
||||||
fulfillment.tracking_numbers = trackingNumbers
|
|
||||||
|
fulfillment.tracking_links = trackingLinks.map(tl =>
|
||||||
|
trackingLinkRepo.create(tl)
|
||||||
|
)
|
||||||
|
|
||||||
fulfillment.metadata = {
|
fulfillment.metadata = {
|
||||||
...fulfillment.metadata,
|
...fulfillment.metadata,
|
||||||
...metadata,
|
...metadata,
|
||||||
|
|||||||
@@ -553,13 +553,13 @@ class OrderService extends BaseService {
|
|||||||
* have been created in regards to the shipment.
|
* have been created in regards to the shipment.
|
||||||
* @param {string} orderId - the id of the order that has been shipped
|
* @param {string} orderId - the id of the order that has been shipped
|
||||||
* @param {string} fulfillmentId - the fulfillment that has now been shipped
|
* @param {string} fulfillmentId - the fulfillment that has now been shipped
|
||||||
* @param {Array<String>} trackingNumbers - array of tracking numebers
|
* @param {TrackingLink[]} trackingLinks - array of tracking numebers
|
||||||
* associated with the shipment
|
* associated with the shipment
|
||||||
* @param {Dictionary<String, String>} metadata - optional metadata to add to
|
* @param {Dictionary<String, String>} metadata - optional metadata to add to
|
||||||
* the fulfillment
|
* the fulfillment
|
||||||
* @return {order} the resulting order following the update.
|
* @return {order} the resulting order following the update.
|
||||||
*/
|
*/
|
||||||
async createShipment(orderId, fulfillmentId, trackingNumbers, metadata = {}) {
|
async createShipment(orderId, fulfillmentId, trackingLinks, metadata = {}) {
|
||||||
return this.atomicPhase_(async manager => {
|
return this.atomicPhase_(async manager => {
|
||||||
const order = await this.retrieve(orderId, { relations: ["items"] })
|
const order = await this.retrieve(orderId, { relations: ["items"] })
|
||||||
const shipment = await this.fulfillmentService_.retrieve(fulfillmentId)
|
const shipment = await this.fulfillmentService_.retrieve(fulfillmentId)
|
||||||
@@ -573,7 +573,7 @@ class OrderService extends BaseService {
|
|||||||
|
|
||||||
const shipmentRes = await this.fulfillmentService_
|
const shipmentRes = await this.fulfillmentService_
|
||||||
.withTransaction(manager)
|
.withTransaction(manager)
|
||||||
.createShipment(fulfillmentId, trackingNumbers, metadata)
|
.createShipment(fulfillmentId, trackingLinks, metadata)
|
||||||
|
|
||||||
order.fulfillment_status = "shipped"
|
order.fulfillment_status = "shipped"
|
||||||
for (const item of order.items) {
|
for (const item of order.items) {
|
||||||
|
|||||||
@@ -671,12 +671,12 @@ class SwapService extends BaseService {
|
|||||||
* @param {string} swapId - the id of the swap that has been shipped.
|
* @param {string} swapId - the id of the swap that has been shipped.
|
||||||
* @param {string} fulfillmentId - the id of the specific fulfillment that
|
* @param {string} fulfillmentId - the id of the specific fulfillment that
|
||||||
* has been shipped
|
* has been shipped
|
||||||
* @param {Array<string>} trackingNumbers - the tracking numbers associated
|
* @param {TrackingLink[]} trackingLinks - the tracking numbers associated
|
||||||
* with the shipment
|
* with the shipment
|
||||||
* @param {object} metadata - optional metadata to attach to the shipment.
|
* @param {object} metadata - optional metadata to attach to the shipment.
|
||||||
* @returns {Promise<Swap>} the updated swap with new fulfillments and status.
|
* @returns {Promise<Swap>} the updated swap with new fulfillments and status.
|
||||||
*/
|
*/
|
||||||
async createShipment(swapId, fulfillmentId, trackingNumbers, metadata = {}) {
|
async createShipment(swapId, fulfillmentId, trackingLinks, metadata = {}) {
|
||||||
return this.atomicPhase_(async manager => {
|
return this.atomicPhase_(async manager => {
|
||||||
const swap = await this.retrieve(swapId, {
|
const swap = await this.retrieve(swapId, {
|
||||||
relations: ["additional_items"],
|
relations: ["additional_items"],
|
||||||
@@ -685,7 +685,7 @@ class SwapService extends BaseService {
|
|||||||
// Update the fulfillment to register
|
// Update the fulfillment to register
|
||||||
const shipment = await this.fulfillmentService_
|
const shipment = await this.fulfillmentService_
|
||||||
.withTransaction(manager)
|
.withTransaction(manager)
|
||||||
.createShipment(fulfillmentId, trackingNumbers, metadata)
|
.createShipment(fulfillmentId, trackingLinks, metadata)
|
||||||
|
|
||||||
swap.fulfillment_status = "shipped"
|
swap.fulfillment_status = "shipped"
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user