feat: Add discountable flag to product (#329)

This commit is contained in:
Oliver Windall Juhl
2021-08-05 10:40:12 +02:00
committed by GitHub
parent 821d8be733
commit 6053c4a8dd
16 changed files with 1118 additions and 45 deletions

View File

@@ -28,7 +28,6 @@ describe("/admin/discounts", () => {
describe("POST /admin/discounts", () => {
beforeEach(async () => {
const manager = dbConnection.manager;
try {
await adminSeeder(dbConnection);
} catch (err) {
@@ -105,8 +104,9 @@ describe("/admin/discounts", () => {
});
describe("testing for soft-deletion + uniqueness on discount codes", () => {
const manager = dbConnection.manager;
let manager;
beforeEach(async () => {
manager = dbConnection.manager;
try {
await adminSeeder(dbConnection);
await manager.insert(DiscountRule, {
@@ -120,6 +120,8 @@ describe("/admin/discounts", () => {
id: "test-discount",
code: "TESTING",
rule_id: "test-discount-rule",
is_dynamic: false,
is_disabled: false,
});
} catch (err) {
throw err;
@@ -127,6 +129,7 @@ describe("/admin/discounts", () => {
});
afterEach(async () => {
manager = dbConnection.manager;
await manager.query(`DELETE FROM "discount"`);
await manager.query(`DELETE FROM "discount_rule"`);
await manager.query(`DELETE FROM "user"`);
@@ -173,7 +176,7 @@ describe("/admin/discounts", () => {
expect(response.status).toEqual(200);
expect(response.data.discount).toEqual(
expect.objectContaining({
code: "HELLOWORLD",
code: "TESTING",
usage_limit: 10,
})
);

View File

@@ -92,6 +92,8 @@ describe("/admin/products", () => {
expect(response.data.product).toEqual(
expect.objectContaining({
title: "Test product",
discountable: true,
is_giftcard: false,
handle: "test-product",
images: expect.arrayContaining([
expect.objectContaining({
@@ -148,6 +150,43 @@ describe("/admin/products", () => {
);
});
it("creates a giftcard", async () => {
const api = useApi();
const payload = {
title: "Test Giftcard",
is_giftcard: true,
description: "test-giftcard-description",
options: [{ title: "Denominations" }],
variants: [
{
title: "Test variant",
prices: [{ currency_code: "usd", amount: 100 }],
options: [{ value: "100" }],
},
],
};
const response = await api
.post("/admin/products", payload, {
headers: {
Authorization: "Bearer test_token",
},
})
.catch((err) => {
console.log(err);
});
expect(response.status).toEqual(200);
expect(response.data.product).toEqual(
expect.objectContaining({
title: "Test Giftcard",
discountable: false,
})
);
});
it("updates a product (update tags, delete collection, delete type, replaces images)", async () => {
const api = useApi();

View File

@@ -18,8 +18,12 @@ describe("/store/carts", () => {
await manager.query(`DELETE FROM "discount"`);
await manager.query(`DELETE FROM "discount_rule"`);
await manager.query(`DELETE FROM "shipping_method"`);
await manager.query(`DELETE FROM "shipping_option"`);
await manager.query(`DELETE FROM "line_item"`);
await manager.query(`DELETE FROM "cart"`);
await manager.query(`DELETE FROM "money_amount"`);
await manager.query(`DELETE FROM "product_variant"`);
await manager.query(`DELETE FROM "product"`);
await manager.query(`DELETE FROM "shipping_option"`);
await manager.query(`DELETE FROM "address"`);
await manager.query(`DELETE FROM "customer"`);
await manager.query(
@@ -233,6 +237,44 @@ describe("/store/carts", () => {
expect(cartWithShippingMethod.status).toEqual(200);
});
it("adds a giftcard to cart, but ensures discount only applied to discountable items", async () => {
const api = useApi();
// Add standard line item to cart
await api.post(
"/store/carts/test-cart/line-items",
{
variant_id: "test-variant",
quantity: 1,
},
{ withCredentials: true }
);
// Add gift card to cart
await api.post(
"/store/carts/test-cart/line-items",
{
variant_id: "giftcard-denom",
quantity: 1,
},
{ withCredentials: true }
);
// Add a 10% discount to the cart
const cartWithGiftcard = await api.post(
"/store/carts/test-cart",
{
discounts: [{ code: "10PERCENT" }],
},
{ withCredentials: true }
);
// Ensure that the discount is only applied to the standard item
expect(cartWithGiftcard.data.cart.total).toBe(1900); // 1000 (giftcard) + 900 (standard item with 10% discount)
expect(cartWithGiftcard.data.cart.discount_total).toBe(100);
expect(cartWithGiftcard.status).toEqual(200);
});
it("adds no more than 1 shipping method per shipping profile", async () => {
const api = useApi();
const addShippingMethod = async (option_id) => {

View File

@@ -8,6 +8,9 @@ const {
ShippingOption,
ShippingMethod,
Address,
ProductVariant,
Product,
MoneyAmount,
} = require("@medusajs/medusa");
module.exports = async (connection, data = {}) => {
@@ -17,6 +20,10 @@ module.exports = async (connection, data = {}) => {
type: "default",
});
const gcProfile = await manager.findOne(ShippingProfile, {
type: "gift_card",
});
await manager.insert(Address, {
id: "test-general-address",
first_name: "superman",
@@ -47,9 +54,27 @@ module.exports = async (connection, data = {}) => {
freeDisc.regions = [r];
freeDisc.rule = freeRule;
await manager.save(freeDisc);
const tenPercentRule = manager.create(DiscountRule, {
id: "tenpercent-rule",
description: "Ten percent rule",
type: "percentage",
value: 10,
allocation: "total",
});
const tenPercent = manager.create(Discount, {
id: "10Percent",
code: "10PERCENT",
is_dynamic: false,
is_disabled: false,
});
tenPercent.regions = [r];
tenPercent.rule = tenPercentRule;
await manager.save(tenPercent);
const d = await manager.create(Discount, {
id: "test-discount",
code: "CREATED",
@@ -100,6 +125,17 @@ module.exports = async (connection, data = {}) => {
data: {},
});
await manager.insert(ShippingOption, {
id: "gc-option",
name: "Digital copy",
provider_id: "test-ful",
region_id: "test-region",
profile_id: gcProfile.id,
price_type: "flat_rate",
amount: 0,
data: {},
});
await manager.insert(ShippingOption, {
id: "test-option-2",
name: "test-option-2",
@@ -111,6 +147,62 @@ module.exports = async (connection, data = {}) => {
data: {},
});
await manager.insert(Product, {
id: "giftcard-product",
title: "Giftcard",
is_giftcard: true,
discountable: false,
profile_id: gcProfile.id,
options: [{ id: "denom", title: "Denomination" }],
});
await manager.insert(ProductVariant, {
id: "giftcard-denom",
title: "1000",
product_id: "giftcard-product",
inventory_quantity: 1,
options: [
{
option_id: "denom",
value: "1000",
},
],
});
await manager.insert(Product, {
id: "test-product",
title: "test product",
profile_id: defaultProfile.id,
options: [{ id: "test-option", title: "Size" }],
});
await manager.insert(ProductVariant, {
id: "test-variant",
title: "test variant",
product_id: "test-product",
inventory_quantity: 1,
options: [
{
option_id: "test-option",
value: "Size",
},
],
});
const ma = manager.create(MoneyAmount, {
variant_id: "test-variant",
currency_code: "usd",
amount: 1000,
});
await manager.save(ma);
const ma2 = manager.create(MoneyAmount, {
variant_id: "giftcard-denom",
currency_code: "usd",
amount: 1000,
});
await manager.save(ma2);
const cart = manager.create(Cart, {
id: "test-cart",
customer_id: "some-customer",

View File

@@ -8,7 +8,7 @@
"build": "babel src -d dist --extensions \".ts,.js\""
},
"dependencies": {
"@medusajs/medusa": "^1.1.32",
"@medusajs/medusa": "1.1.33-dev-1627995051381",
"medusa-interfaces": "^1.1.18",
"typeorm": "^0.2.31"
},

File diff suppressed because it is too large Load Diff

View File

@@ -35,6 +35,7 @@ describe("POST /admin/products", () => {
expect(ProductServiceMock.create).toHaveBeenCalledTimes(1)
expect(ProductServiceMock.create).toHaveBeenCalledWith({
title: "Test Product",
discountable: true,
description: "Test Description",
tags: [{ id: "test", value: "test" }],
handle: "test-product",
@@ -92,6 +93,7 @@ describe("POST /admin/products", () => {
expect(ProductServiceMock.create).toHaveBeenCalledTimes(1)
expect(ProductServiceMock.create).toHaveBeenCalledWith({
title: "Gift Card",
discountable: true,
description: "make someone happy",
options: [{ title: "Denominations" }],
handle: "test-gift-card",

View File

@@ -36,6 +36,7 @@ describe("GET /admin/products/:id", () => {
"description",
"handle",
"is_giftcard",
"discountable",
"thumbnail",
"profile_id",
"collection_id",

View File

@@ -23,6 +23,9 @@ import { defaultRelations, defaultFields } from "."
* is_giftcard:
* description: A flag to indicate if the Product represents a Gift Card. Purchasing Products with this flag set to `true` will result in a Gift Card being created.
* type: boolean
* discountable:
* description: A flag to indicate if discounts can be applied to the LineItems generated from this Product
* type: boolean
* images:
* description: Images of the Product.
* type: array
@@ -184,6 +187,7 @@ export default async (req, res) => {
subtitle: Validator.string().allow(""),
description: Validator.string().allow(""),
is_giftcard: Validator.boolean().default(false),
discountable: Validator.boolean().default(true),
images: Validator.array()
.items(Validator.string())
.optional(),

View File

@@ -69,6 +69,7 @@ export const defaultFields = [
"description",
"handle",
"is_giftcard",
"discountable",
"thumbnail",
"profile_id",
"collection_id",
@@ -93,6 +94,7 @@ export const allowedFields = [
"description",
"handle",
"is_giftcard",
"discountable",
"thumbnail",
"profile_id",
"collection_id",

View File

@@ -25,6 +25,9 @@ import { defaultRelations, defaultFields } from "."
* is_giftcard:
* description: A flag to indicate if the Product represents a Gift Card. Purchasing Products with this flag set to `true` will result in a Gift Card being created.
* type: boolean
* discountable:
* description: A flag to indicate if discounts can be applied to the LineItems generated from this Product
* type: boolean
* images:
* description: Images of the Product.
* type: array
@@ -189,6 +192,7 @@ export default async (req, res) => {
.optional()
.allow(null, ""),
description: Validator.string().optional(),
discountable: Validator.boolean().optional(),
type: Validator.object()
.keys({
id: Validator.string().optional(),

View File

@@ -0,0 +1,14 @@
import {MigrationInterface, QueryRunner} from "typeorm";
export class addDiscountableToProduct1627995307200 implements MigrationInterface {
name = 'addDiscountableToProduct1627995307200'
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "product" ADD "discountable" boolean NOT NULL DEFAULT true`);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "product" DROP COLUMN "discountable"`);
}
}

View File

@@ -136,6 +136,9 @@ export class Product {
})
tags: ProductTag[]
@Column({ default: true })
discountable: boolean
@CreateDateColumn({ type: "timestamptz" })
created_at: Date
@@ -184,6 +187,9 @@ export class Product {
* is_giftcard:
* description: "Whether the Product represents a Gift Card. Products that represent Gift Cards will automatically generate a redeemable Gift Card code once they are purchased."
* type: boolean
* discountable:
* description: "Whether the Product can be discounted. Discounts will not apply to Line Items of this Product when this flag is set to `false`.
* type: boolean
* images:
* description: "Images of the Product"
* type: array

View File

@@ -30,6 +30,7 @@ describe("LineItemService", () => {
title: "Test product",
thumbnail: "",
is_giftcard: true,
discountable: false,
},
}
}

View File

@@ -122,7 +122,7 @@ class LineItemService extends BaseService {
thumbnail: variant.product.thumbnail,
variant_id: variant.id,
quantity: quantity || 1,
allow_discounts: !variant.product.is_giftcard,
allow_discounts: variant.product.discountable,
is_giftcard: variant.product.is_giftcard,
metadata: config?.metadata || {},
should_merge: shouldMerge,

View File

@@ -288,6 +288,11 @@ class ProductService extends BaseService {
rest.thumbnail = images[0]
}
// if product is a giftcard, we should disallow discounts
if (rest.is_giftcard) {
rest.discountable = false
}
let product = productRepo.create(rest)
if (images && images.length) {