fix: adjustments based on feedback

rename RMAShippingOption to CustomShippingOption
update models and relations
update unit and integration tests
update services
This commit is contained in:
zakariaelas
2021-10-06 14:17:36 +01:00
parent db83448d18
commit 52be911e50
29 changed files with 467 additions and 604 deletions
@@ -45,6 +45,17 @@ import { defaultFields, defaultRelations } from "./"
* quantity:
* description: The quantity of the Product Variant to ship.
* type: integer
* custom_shipping_options:
* description: The custom shipping options to potentially create a Shipping Method from.
* type: array
* items:
* properties:
* option_id:
* description: The id of the Shipping Option to override with a custom price.
* type: string
* price:
* description: The custom price of the Shipping Option.
* type: integer
* no_notification:
* description: If set to true no notification will be send related to this Swap.
* type: boolean
@@ -81,18 +92,16 @@ export default async (req, res) => {
.optional(),
})
.optional(),
rma_shipping_options: Validator.array()
.items({
option_id: Validator.string().optional(),
price: Validator.number()
.integer()
.optional(),
})
.default([]),
additional_items: Validator.array().items({
variant_id: Validator.string().required(),
quantity: Validator.number().required(),
}),
custom_shipping_options: Validator.array()
.items({
option_id: Validator.string().required(),
price: Validator.number().required(),
})
.default([]),
no_notification: Validator.boolean().optional(),
allow_backorder: Validator.boolean().default(true),
})
@@ -150,7 +159,6 @@ export default async (req, res) => {
value.return_items,
value.additional_items,
value.return_shipping,
value.rma_shipping_options,
{
idempotency_key: idempotencyKey.idempotency_key,
no_notification: value.no_notification,
@@ -158,7 +166,9 @@ export default async (req, res) => {
}
)
await swapService.withTransaction(manager).createCart(swap.id)
await swapService
.withTransaction(manager)
.createCart(swap.id, value.custom_shipping_options)
const returnOrder = await returnService
.withTransaction(manager)
.retrieveBySwap(swap.id)
@@ -23,9 +23,9 @@ describe("POST /store/carts/:id/shipping-methods", () => {
jest.clearAllMocks()
})
it("calls CartService addRMAMethod", () => {
expect(CartServiceMock.addRMAMethod).toHaveBeenCalledTimes(1)
expect(CartServiceMock.addRMAMethod).toHaveBeenCalledWith(
it("calls CartService addShippingMethod", () => {
expect(CartServiceMock.addShippingMethod).toHaveBeenCalledTimes(1)
expect(CartServiceMock.addShippingMethod).toHaveBeenCalledWith(
IdMap.getId("fr-cart"),
IdMap.getId("freeShipping"),
{}
@@ -45,7 +45,7 @@ describe("POST /store/carts/:id/shipping-methods", () => {
})
})
describe("successfully adds a RMA shipping method", () => {
describe("successfully adds a shipping method", () => {
let subject
beforeAll(async () => {
@@ -65,9 +65,9 @@ describe("POST /store/carts/:id/shipping-methods", () => {
jest.clearAllMocks()
})
it("calls CartService addRMAMethod", () => {
expect(CartServiceMock.addRMAMethod).toHaveBeenCalledTimes(1)
expect(CartServiceMock.addRMAMethod).toHaveBeenCalledWith(
it("calls CartService addShippingMethod", () => {
expect(CartServiceMock.addShippingMethod).toHaveBeenCalledTimes(1)
expect(CartServiceMock.addShippingMethod).toHaveBeenCalledWith(
IdMap.getId("swap-cart"),
IdMap.getId("freeShipping"),
{}
@@ -112,9 +112,9 @@ describe("POST /store/carts/:id/shipping-methods", () => {
jest.clearAllMocks()
})
it("calls CartService addRMAMethod", () => {
expect(CartServiceMock.addRMAMethod).toHaveBeenCalledTimes(1)
expect(CartServiceMock.addRMAMethod).toHaveBeenCalledWith(
it("calls CartService addShippingMethod", () => {
expect(CartServiceMock.addShippingMethod).toHaveBeenCalledTimes(1)
expect(CartServiceMock.addShippingMethod).toHaveBeenCalledWith(
IdMap.getId("fr-cart"),
IdMap.getId("freeShipping"),
{
@@ -45,7 +45,7 @@ export default async (req, res) => {
await manager.transaction(async m => {
const txCartService = cartService.withTransaction(m)
await txCartService.addRMAMethod(id, value.option_id, value.data)
await txCartService.addShippingMethod(id, value.option_id, value.data)
const updated = await txCartService.retrieve(id, {
relations: ["payment_sessions"],
@@ -4,7 +4,7 @@ import { carts, CartServiceMock } from "../../../../../services/__mocks__/cart"
import { ShippingProfileServiceMock } from "../../../../../services/__mocks__/shipping-profile"
describe("GET /store/shipping-options", () => {
describe("retrieves shipping options when cart type is not swap and not claim", () => {
describe("retrieves shipping options", () => {
let subject
beforeAll(async () => {
@@ -29,6 +29,7 @@ describe("GET /store/shipping-options", () => {
"items",
"items.variant",
"items.variant.product",
"custom_shipping_options",
],
}
)
@@ -53,54 +54,4 @@ describe("GET /store/shipping-options", () => {
)
})
})
describe("retrieves shipping options when cart type is swap", () => {
let subject
beforeAll(async () => {
subject = await request(
"GET",
`/store/shipping-options/${IdMap.getId("swap-cart")}`
)
})
afterAll(() => {
jest.clearAllMocks()
})
it("calls CartService retrieve", () => {
expect(CartServiceMock.retrieve).toHaveBeenCalledTimes(1)
expect(CartServiceMock.retrieve).toHaveBeenCalledWith(
IdMap.getId("swap-cart"),
{
select: ["subtotal"],
relations: [
"region",
"items",
"items.variant",
"items.variant.product",
],
}
)
})
it("calls ShippingProfileService fetchRMAOptions", () => {
expect(ShippingProfileServiceMock.fetchRMAOptions).toHaveBeenCalledTimes(
1
)
expect(ShippingProfileServiceMock.fetchRMAOptions).toHaveBeenCalledWith(
carts.testSwapCart
)
})
it("returns 200", () => {
expect(subject.status).toEqual(200)
})
it("returns the RMAshippingOptions", () => {
expect(subject.body.shipping_options[0].id).toEqual(
IdMap.getId("cartRMAShippingOption")
)
})
})
})
@@ -37,15 +37,16 @@ export default async (req, res) => {
const cart = await cartService.retrieve(value.cart_id, {
select: ["subtotal"],
relations: ["region", "items", "items.variant", "items.variant.product"],
relations: [
"region",
"items",
"items.variant",
"items.variant.product",
"custom_shipping_options",
],
})
let options
if (cart.type === "swap" || cart.type === "claim") {
options = await shippingProfileService.fetchRMAOptions(cart)
} else {
options = await shippingProfileService.fetchCartOptions(cart)
}
const options = await shippingProfileService.fetchCartOptions(cart)
res.status(200).json({ shipping_options: options })
} catch (err) {
@@ -130,7 +130,6 @@ export default async (req, res) => {
value.return_items,
value.additional_items,
returnShipping,
[],
{
idempotency_key: idempotencyKey.idempotency_key,
no_notification: true,