**What** - Discounts details page - Edit discount details - Edit discount configurations - `ListSummary` component **NOTE** - conditions edit form will be implemented in a separate PR - edit details from is missing metadata component which will be added later --- https://github.com/medusajs/medusa/assets/16856471/c878af4a-48c2-4c45-b824-662784c7a139
33 lines
786 B
TypeScript
33 lines
786 B
TypeScript
import { Discount } from "@medusajs/medusa"
|
|
import { end, parse } from "iso8601-duration"
|
|
|
|
export enum PromotionStatus {
|
|
SCHEDULED = "SCHEDULED",
|
|
EXPIRED = "EXPIRED",
|
|
ACTIVE = "ACTIVE",
|
|
DISABLED = "DISABLED",
|
|
}
|
|
|
|
export const getDiscountStatus = (discount: Discount) => {
|
|
if (discount.is_disabled) {
|
|
return PromotionStatus.DISABLED
|
|
}
|
|
|
|
const date = new Date()
|
|
if (new Date(discount.starts_at) > date) {
|
|
return PromotionStatus.SCHEDULED
|
|
}
|
|
|
|
if (
|
|
(discount.ends_at && new Date(discount.ends_at) < date) ||
|
|
(discount.valid_duration &&
|
|
date >
|
|
end(parse(discount.valid_duration), new Date(discount.starts_at))) ||
|
|
discount.usage_count === discount.usage_limit
|
|
) {
|
|
return PromotionStatus.EXPIRED
|
|
}
|
|
|
|
return PromotionStatus.ACTIVE
|
|
}
|