Gift cards are automatically added to gift card profile
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { IdMap } from "medusa-test-utils"
|
||||
import { request } from "../../../../../helpers/test-request"
|
||||
import { ProductServiceMock } from "../../../../../services/__mocks__/product"
|
||||
import { ShippingProfileServiceMock } from "../../../../../services/__mocks__/shipping-profile"
|
||||
|
||||
describe("POST /admin/products", () => {
|
||||
describe("successful creation", () => {
|
||||
@@ -38,8 +39,80 @@ describe("POST /admin/products", () => {
|
||||
description: "Test Description",
|
||||
tags: "hi,med,dig",
|
||||
handle: "test-product",
|
||||
is_giftcard: false,
|
||||
})
|
||||
})
|
||||
|
||||
it("calls shipping profile default", () => {
|
||||
expect(ShippingProfileServiceMock.retrieveDefault).toHaveBeenCalledTimes(
|
||||
1
|
||||
)
|
||||
expect(ShippingProfileServiceMock.retrieveDefault).toHaveBeenCalledWith()
|
||||
})
|
||||
})
|
||||
|
||||
describe("successful creation of gift card product", () => {
|
||||
let subject
|
||||
|
||||
beforeAll(async () => {
|
||||
jest.clearAllMocks()
|
||||
subject = await request("POST", "/admin/products", {
|
||||
payload: {
|
||||
title: "Gift Card",
|
||||
description: "make someone happy",
|
||||
handle: "test-gift-card",
|
||||
is_giftcard: true,
|
||||
options: [{ title: "Denominations" }],
|
||||
variants: [
|
||||
{
|
||||
title: "100 USD",
|
||||
prices: [
|
||||
{
|
||||
currency_code: "USD",
|
||||
amount: 100,
|
||||
},
|
||||
],
|
||||
options: [
|
||||
{
|
||||
value: "100",
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
adminSession: {
|
||||
jwt: {
|
||||
userId: IdMap.getId("admin_user"),
|
||||
},
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
it("calls service createDraft", () => {
|
||||
expect(ProductServiceMock.createDraft).toHaveBeenCalledTimes(1)
|
||||
expect(ProductServiceMock.createDraft).toHaveBeenCalledWith({
|
||||
title: "Gift Card",
|
||||
description: "make someone happy",
|
||||
options: [{ title: "Denominations" }],
|
||||
handle: "test-gift-card",
|
||||
is_giftcard: true,
|
||||
})
|
||||
})
|
||||
|
||||
it("calls profile service", () => {
|
||||
expect(
|
||||
ShippingProfileServiceMock.retrieveGiftCardDefault
|
||||
).toHaveBeenCalledTimes(1)
|
||||
expect(
|
||||
ShippingProfileServiceMock.retrieveGiftCardDefault
|
||||
).toHaveBeenCalledWith()
|
||||
|
||||
expect(ShippingProfileServiceMock.addProduct).toHaveBeenCalledTimes(1)
|
||||
expect(ShippingProfileServiceMock.addProduct).toHaveBeenCalledWith(
|
||||
IdMap.getId("giftCardProfile"),
|
||||
undefined
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("invalid data returns error details", () => {
|
||||
|
||||
@@ -5,6 +5,7 @@ export default async (req, res) => {
|
||||
title: Validator.string().required(),
|
||||
description: Validator.string(),
|
||||
tags: Validator.string(),
|
||||
is_giftcard: Validator.boolean().default(false),
|
||||
options: Validator.array().items({
|
||||
title: Validator.string().required(),
|
||||
}),
|
||||
@@ -71,8 +72,13 @@ export default async (req, res) => {
|
||||
}
|
||||
|
||||
// Add to default shipping profile
|
||||
const { _id } = await shippingProfileService.retrieveDefault()
|
||||
await shippingProfileService.addProduct(_id, newProduct._id)
|
||||
if (value.is_giftcard) {
|
||||
const { _id } = await shippingProfileService.retrieveGiftCardDefault()
|
||||
await shippingProfileService.addProduct(_id, newProduct._id)
|
||||
} else {
|
||||
const { _id } = await shippingProfileService.retrieveDefault()
|
||||
await shippingProfileService.addProduct(_id, newProduct._id)
|
||||
}
|
||||
|
||||
newProduct = await productService.decorate(
|
||||
newProduct,
|
||||
@@ -90,6 +96,7 @@ export default async (req, res) => {
|
||||
)
|
||||
res.json({ product: newProduct })
|
||||
} catch (err) {
|
||||
console.log(err)
|
||||
throw err
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ export default async (req, res) => {
|
||||
const schema = Validator.object().keys({
|
||||
variant_id: Validator.string().required(),
|
||||
quantity: Validator.number().required(),
|
||||
metadata: Validator.object().optional(),
|
||||
})
|
||||
|
||||
const { value, error } = schema.validate(req.body)
|
||||
@@ -21,7 +22,8 @@ export default async (req, res) => {
|
||||
const lineItem = await lineItemService.generate(
|
||||
value.variant_id,
|
||||
cart.region_id,
|
||||
value.quantity
|
||||
value.quantity,
|
||||
value.metadata
|
||||
)
|
||||
await cartService.addLineItem(cart._id, lineItem)
|
||||
|
||||
|
||||
@@ -22,5 +22,7 @@ export default async ({ container }) => {
|
||||
fulfillment_providers: fulfilIds,
|
||||
payment_providers: payIds,
|
||||
})
|
||||
|
||||
await profileService.createDefault()
|
||||
await profileService.createGiftCardDefault()
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ class DiscountModel extends BaseModel {
|
||||
static schema = {
|
||||
code: { type: String, required: true, unique: true },
|
||||
is_dynamic: { type: Boolean, default: false },
|
||||
is_giftcard: { type: Boolean, default: false },
|
||||
discount_rule: { type: DiscountRule, required: true },
|
||||
usage_count: { type: Number, default: 0 },
|
||||
disabled: { type: Boolean, default: false },
|
||||
|
||||
@@ -14,6 +14,7 @@ class ProductModel extends BaseModel {
|
||||
description: { type: String, default: "" },
|
||||
tags: { type: String, default: "" },
|
||||
handle: { type: String, unique: true, sparse: true },
|
||||
is_giftcard: { type: Boolean, default: false },
|
||||
images: { type: [String], default: [] },
|
||||
thumbnail: { type: String, default: "" },
|
||||
options: { type: [OptionSchema], default: [] },
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
export const MiddlewareServiceMock = {
|
||||
usePostAuthentication: jest.fn(),
|
||||
usePreAuthentication: jest.fn(),
|
||||
getRouters: jest.fn().mockReturnValue([]),
|
||||
}
|
||||
|
||||
const mock = jest.fn().mockImplementation(() => {
|
||||
|
||||
@@ -31,7 +31,11 @@ export const products = {
|
||||
|
||||
export const ProductServiceMock = {
|
||||
createDraft: jest.fn().mockImplementation(data => {
|
||||
return Promise.resolve(products.product1)
|
||||
if (data.title === "Test Product") {
|
||||
return Promise.resolve(products.product1)
|
||||
}
|
||||
|
||||
return Promise.resolve({ ...data })
|
||||
}),
|
||||
publish: jest.fn().mockImplementation(_ => {
|
||||
return Promise.resolve({
|
||||
|
||||
@@ -31,6 +31,9 @@ export const ShippingProfileServiceMock = {
|
||||
}
|
||||
return Promise.resolve()
|
||||
}),
|
||||
retrieveGiftCardDefault: jest.fn().mockImplementation(data => {
|
||||
return Promise.resolve({ _id: IdMap.getId("giftCardProfile") })
|
||||
}),
|
||||
retrieveDefault: jest.fn().mockImplementation(data => {
|
||||
return Promise.resolve({ _id: IdMap.getId("default_shipping_profile") })
|
||||
}),
|
||||
|
||||
@@ -97,6 +97,32 @@ class ShippingProfileService extends BaseService {
|
||||
return profile
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the default gift card profile
|
||||
* @return the shipping profile for gift cards
|
||||
*/
|
||||
async retrieveGiftCardProfile() {
|
||||
return await this.profileModel_
|
||||
.findOne({ name: "default_gift_card_profile" })
|
||||
.catch(err => {
|
||||
throw new MedusaError(MedusaError.Types.DB_ERROR, err.message)
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a default shipping profile, for gift cards if unless it already
|
||||
* exists.
|
||||
* @return {Promise<ShippingProfile>} the shipping profile
|
||||
*/
|
||||
async createGiftCardDefault() {
|
||||
const profile = await this.retrieveGiftCardProfile()
|
||||
if (!profile) {
|
||||
return this.profileModel_.create({ name: "default_gift_card_profile" })
|
||||
}
|
||||
|
||||
return profile
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new shipping profile.
|
||||
* @param {ShippingProfile} profile - the shipping profile to create from
|
||||
|
||||
Reference in New Issue
Block a user