feat: Add discountable flag to product (#329)
This commit is contained in:
1 parent
821d8be733
commit
6053c4a8dd
16 files changed
+1118
-45
No files matched your search
@@ -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",
|
||||
|
||||
@@ -36,6 +36,7 @@ describe("GET /admin/products/:id", () => {
|
||||
"description",
|
||||
"handle",
|
||||
"is_giftcard",
|
||||
"discountable",
|
||||
"thumbnail",
|
||||
"profile_id",
|
||||
"collection_id",
|
||||
|
||||
@@ -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(),
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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(),
|
||||
|
||||
@@ -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"`);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -30,6 +30,7 @@ describe("LineItemService", () => {
|
||||
title: "Test product",
|
||||
thumbnail: "",
|
||||
is_giftcard: true,
|
||||
discountable: false,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in new issue
Block a user