what: - adds a status column to promotion table - introduce active promotion query - scope revert, register and compute actions to active promotions - admin to create and update promotion with statuses RESOLVES CMRC-845 RESOLVES CMRC-846 RESOLVES CMRC-847 RESOLVES CMRC-848 RESOLVES CMRC-849 RESOLVES CMRC-850
50 lines
1.6 KiB
TypeScript
50 lines
1.6 KiB
TypeScript
import { HttpTypes } from "@medusajs/types"
|
|
import { i18n } from "../components/utilities/i18n"
|
|
|
|
export enum PromotionStatus {
|
|
SCHEDULED = "SCHEDULED",
|
|
EXPIRED = "EXPIRED",
|
|
ACTIVE = "ACTIVE",
|
|
INACTIVE = "INACTIVE",
|
|
DRAFT = "DRAFT",
|
|
}
|
|
|
|
export type StatusColors = "grey" | "orange" | "green" | "red" | "grey"
|
|
export type StatusMap = Record<string, [StatusColors, string]>
|
|
export const promotionStatusMap: StatusMap = {
|
|
[PromotionStatus.ACTIVE]: ["green", i18n.t("statuses.active")],
|
|
[PromotionStatus.INACTIVE]: ["red", i18n.t("statuses.inactive")],
|
|
[PromotionStatus.DRAFT]: ["grey", i18n.t("statuses.draft")],
|
|
[PromotionStatus.SCHEDULED]: [
|
|
"orange",
|
|
`${i18n.t("promotions.fields.campaign")} ${i18n.t("statuses.scheduled").toLowerCase()}`,
|
|
],
|
|
[PromotionStatus.EXPIRED]: [
|
|
"red",
|
|
`${i18n.t("promotions.fields.campaign")} ${i18n.t("statuses.expired").toLowerCase()}`,
|
|
],
|
|
}
|
|
|
|
export const getPromotionStatus = (promotion: HttpTypes.AdminPromotion) => {
|
|
const date = new Date()
|
|
const campaign = promotion.campaign
|
|
|
|
if (!campaign) {
|
|
return promotionStatusMap[promotion.status!.toUpperCase()]
|
|
}
|
|
|
|
if (campaign.starts_at && new Date(campaign.starts_at!) > date) {
|
|
return promotionStatusMap[PromotionStatus.SCHEDULED]
|
|
}
|
|
|
|
const campaignBudget = campaign.budget
|
|
const overBudget =
|
|
campaignBudget && campaignBudget.used! > campaignBudget.limit!
|
|
|
|
if ((campaign.ends_at && new Date(campaign.ends_at) < date) || overBudget) {
|
|
return promotionStatusMap[PromotionStatus.EXPIRED]
|
|
}
|
|
|
|
return promotionStatusMap[promotion.status!.toUpperCase()]
|
|
}
|