All files marketing.ts

60% Statements 3/5
100% Branches 0/0
60% Functions 3/5
60% Lines 3/5

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130                                                                                                                                                                                                                  1x                     1x           1x              
import type { ApiClient, BusinessId } from "./api";
 
export type BenefitType = "COUPON" | "RED_PACKET" | "SUBSIDY";
 
export interface RegionRestriction {
  level: "PROVINCE" | "CITY" | "DISTRICT";
  regionCode: string;
}
 
export interface Benefit {
  benefitNo: string;
  userId: BusinessId;
  ruleCode: string;
  benefitType: BenefitType;
  thresholdAmount: string | number;
  discountAmount: string | number;
  status: string;
  lockedOrderNo: string | null;
  redeemedOrderNo: string | null;
  validFrom: string;
  validUntil: string;
  regions: RegionRestriction[];
}
 
export interface DeliveryRegion {
  provinceCode: string;
  cityCode: string;
  districtCode: string;
}
 
export interface PricingPreviewLine {
  lineNo: number;
  skuId: BusinessId;
  lineAmount: string;
}
 
export interface PricingPreviewInput {
  originalAmount: string;
  deliveryRegion: DeliveryRegion;
  lines: PricingPreviewLine[];
  benefitNos: string[];
}
 
export interface DiscountAllocation {
  lineNo: number;
  skuId: BusinessId;
  benefitNo: string;
  ruleCode: string;
  benefitType: BenefitType;
  discountAmount: string | number;
}
 
export interface AppliedBenefit {
  benefitNo: string;
  ruleCode: string;
  benefitType: BenefitType;
  discountAmount: string | number;
  allocations: DiscountAllocation[];
}
 
export interface PricingPreview {
  originalAmount: string | number;
  couponDiscount: string | number;
  redPacketDiscount: string | number;
  subsidyDiscount: string | number;
  discountAmount: string | number;
  payableAmount: string | number;
  appliedBenefits: AppliedBenefit[];
  calculatedAt: string;
}
 
export interface MarketingRule {
  ruleCode: string;
  name: string;
  benefitType: BenefitType;
  thresholdAmount: string | number;
  discountAmount: string | number;
  stackOrder: number;
  validFrom: string;
  validUntil: string;
  status: string;
  regions: RegionRestriction[];
  version: number;
}
 
export interface CreateMarketingRuleInput {
  ruleCode: string;
  name: string;
  benefitType: BenefitType;
  thresholdAmount: string;
  discountAmount: string;
  stackOrder: number;
  validFrom: string;
  validUntil: string;
  regions: RegionRestriction[];
}
 
export interface MarketingApi {
  benefits(): Promise<Benefit[]>;
  previewPricing(input: PricingPreviewInput): Promise<PricingPreview>;
  createRule(input: CreateMarketingRuleInput): Promise<MarketingRule>;
  grantBenefit(userId: BusinessId, ruleCode: string, grantKey: string): Promise<Benefit>;
}
 
export function createMarketingApi(client: ApiClient): MarketingApi {
  return {
    benefits() {
      return client.request<Benefit[]>("/api/v1/marketing/benefits");
    },
    previewPricing(input) {
      return client.request<PricingPreview>("/api/v1/marketing/pricing-previews", {
        method: "POST",
        body: JSON.stringify(input),
      });
    },
    createRule(input) {
      return client.request<MarketingRule>("/api/v1/marketing/admin/rules", {
        method: "POST",
        body: JSON.stringify(input),
      });
    },
    grantBenefit(userId, ruleCode, grantKey) {
      return client.request<Benefit>("/api/v1/marketing/admin/benefits", {
        method: "POST",
        body: JSON.stringify({ userId, ruleCode, grantKey }),
      });
    },
  };
}