|
| 1 | +/* |
| 2 | + * This file is part of node-crisp-api |
| 3 | + * |
| 4 | + * Copyright (c) 2026 Crisp IM SAS |
| 5 | + * All rights belong to Crisp IM SAS |
| 6 | + */ |
| 7 | + |
| 8 | +/************************************************************************** |
| 9 | + * IMPORTS |
| 10 | + ***************************************************************************/ |
| 11 | + |
| 12 | +// PROJECT: RESOURCES |
| 13 | +import BaseResource from "./BaseResource"; |
| 14 | + |
| 15 | +/************************************************************************** |
| 16 | + * TYPES |
| 17 | + ***************************************************************************/ |
| 18 | + |
| 19 | +export type PlanSubscription = { |
| 20 | + id?: string; |
| 21 | + name?: string; |
| 22 | + price?: number; |
| 23 | + since?: string; |
| 24 | + trialing?: boolean; |
| 25 | + trial_end?: string; |
| 26 | + trial_end_date?: string; |
| 27 | + bill_period?: PlanSubscriptionBillPeriod; |
| 28 | + bill_valid_until?: string; |
| 29 | + active?: boolean; |
| 30 | + sandbox?: boolean; |
| 31 | + website?: PlanSubscriptionWebsite; |
| 32 | + coupon_redeemed?: boolean; |
| 33 | + card_id?: string; |
| 34 | + owner?: PlanSubscriptionOwner; |
| 35 | +} |
| 36 | + |
| 37 | +export type PlanSubscriptionBillPeriod = "monthly" | "yearly"; |
| 38 | + |
| 39 | +export type PlanSubscriptionWebsite = { |
| 40 | + id?: string; |
| 41 | + name?: string; |
| 42 | + domain?: string; |
| 43 | + logo?: string; |
| 44 | +} |
| 45 | + |
| 46 | +export type PlanSubscriptionOwner = { |
| 47 | + user_id?: string; |
| 48 | + email?: string; |
| 49 | + first_name?: string; |
| 50 | + last_name?: string; |
| 51 | +} |
| 52 | + |
| 53 | +export type PlanSubscriptionCoupon = { |
| 54 | + code?: string; |
| 55 | + policy?: PlanSubscriptionCouponPolicy; |
| 56 | + redeem_limit?: number; |
| 57 | + expire_at?: string; |
| 58 | +} |
| 59 | + |
| 60 | +export type PlanSubscriptionCouponPolicy = { |
| 61 | + rebate_percent?: number; |
| 62 | + trial_days?: number; |
| 63 | +} |
| 64 | + |
| 65 | +/************************************************************************** |
| 66 | + * CLASSES |
| 67 | + ***************************************************************************/ |
| 68 | + |
| 69 | +/** |
| 70 | + * Crisp PlanSubscription Resource |
| 71 | + */ |
| 72 | +class PlanSubscriptionService extends BaseResource { |
| 73 | + /** |
| 74 | + * List All Active Plan Subscriptions |
| 75 | + */ |
| 76 | + listAllActiveSubscriptions(): Promise<PlanSubscription[]> { |
| 77 | + return this.crisp.get( |
| 78 | + this.crisp.prepareRestUrl(["plans", "subscription"]) |
| 79 | + ); |
| 80 | + }; |
| 81 | + |
| 82 | + /** |
| 83 | + * Get Plan Subscription For A Website |
| 84 | + */ |
| 85 | + getPlanSubscriptionForWebsite(websiteID: string): Promise<PlanSubscription> { |
| 86 | + return this.crisp.get( |
| 87 | + this.crisp.prepareRestUrl(["plans", "subscription", websiteID]) |
| 88 | + ); |
| 89 | + }; |
| 90 | + |
| 91 | + /** |
| 92 | + * Subscribe Website To Plan |
| 93 | + */ |
| 94 | + subscribeWebsiteToPlan(websiteID: string, planID: string) { |
| 95 | + return this.crisp.post( |
| 96 | + this.crisp.prepareRestUrl(["plans", "subscription", websiteID]), |
| 97 | + |
| 98 | + null, |
| 99 | + |
| 100 | + { |
| 101 | + plan_id: planID |
| 102 | + } |
| 103 | + ); |
| 104 | + }; |
| 105 | + |
| 106 | + /** |
| 107 | + * Unsubscribe Plan From Website |
| 108 | + */ |
| 109 | + unsubscribePlanFromWebsite(websiteID: string) { |
| 110 | + return this.crisp.delete( |
| 111 | + this.crisp.prepareRestUrl(["plans", "subscription", websiteID]) |
| 112 | + ); |
| 113 | + }; |
| 114 | + |
| 115 | + /** |
| 116 | + * Change Bill Period For Website Plan Subscription |
| 117 | + */ |
| 118 | + changeBillPeriodForWebsitePlanSubscription( |
| 119 | + websiteID: string, |
| 120 | + period: PlanSubscriptionBillPeriod |
| 121 | + ) { |
| 122 | + return this.crisp.patch( |
| 123 | + this.crisp.prepareRestUrl([ |
| 124 | + "plans", "subscription", websiteID, "bill", "period" |
| 125 | + ]), |
| 126 | + |
| 127 | + null, |
| 128 | + |
| 129 | + { |
| 130 | + period |
| 131 | + } |
| 132 | + ); |
| 133 | + }; |
| 134 | + |
| 135 | + /** |
| 136 | + * Check Coupon Availability For Website Plan Subscription |
| 137 | + */ |
| 138 | + checkCouponAvailabilityForWebsitePlanSubscription( |
| 139 | + websiteID: string, |
| 140 | + code: string |
| 141 | + ): Promise<PlanSubscriptionCoupon> { |
| 142 | + return this.crisp.get( |
| 143 | + this.crisp.prepareRestUrl(["plans", "subscription", websiteID, "coupon"]), |
| 144 | + |
| 145 | + { |
| 146 | + code |
| 147 | + } |
| 148 | + ); |
| 149 | + }; |
| 150 | + |
| 151 | + /** |
| 152 | + * Redeem Coupon For Website Plan Subscription |
| 153 | + */ |
| 154 | + redeemCouponForWebsitePlanSubscription(websiteID: string, code: string) { |
| 155 | + return this.crisp.patch( |
| 156 | + this.crisp.prepareRestUrl(["plans", "subscription", websiteID, "coupon"]), |
| 157 | + |
| 158 | + null, |
| 159 | + |
| 160 | + { |
| 161 | + code |
| 162 | + } |
| 163 | + ); |
| 164 | + }; |
| 165 | +} |
| 166 | + |
| 167 | +/************************************************************************** |
| 168 | + * EXPORTS |
| 169 | + ***************************************************************************/ |
| 170 | + |
| 171 | +export default PlanSubscriptionService; |
0 commit comments