All files / src/models subscription.js

55.56% Statements 5/9
100% Branches 0/0
42.86% Functions 3/7
55.56% Lines 5/9
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                          6x   6x                                           6x                               2x               2x                                                      
import Model from 'model';
 
/**
 * The `Subscription` model
 */
export default class Subscription extends Model {
  static STATUS_ACTIVE = 'active';
  static STATUS_PENDING = 'pending'; // Waiting for a valid mandate.
  static STATUS_CANCELED = 'canceled';
  static STATUS_SUSPENDED = 'suspended'; // Active, but mandate became invalid.
  static STATUS_COMPLETED = 'completed';
 
  constructor(props) {
    super(props);
 
    const defaults = {
      resource: 'subscription',
      id: null,
      mode: null,
      createdAt: null,
      status: null,
      amount: {
        currency: null,
        value: null,
      },
      times: null,
      interval: null,
      startDate: null,
      description: null,
      method: null,
      canceledAt: null,
      webhookUrl: null,
      _links: {
        customer: null,
      },
    };
 
    Object.assign(this, defaults, props);
  }
 
  /**
   * Get the webhook url
   * @returns {boolean|string}
   */
  getWebhookUrl() {
    return this.webhookUrl;
  }
 
  /**
   * If the subscription is active
   * @returns {boolean}
   */
  isActive() {
    return this.status === Subscription.STATUS_ACTIVE;
  }
 
  /**
   * If the subscription is pending
   * @returns {boolean}
   */
  isPending() {
    return this.status === Subscription.STATUS_PENDING;
  }
 
  /**
   * If the subscription is completed
   * @returns {boolean}
   */
  isCompleted() {
    return this.status === Subscription.STATUS_COMPLETED;
  }
 
  /**
   * If the subscription is suspended
   * @returns {boolean}
   */
  isSuspended() {
    return this.status === Subscription.STATUS_SUSPENDED;
  }
 
  /**
   * If the subscription is canceled
   * @returns {boolean}
   */
  isCanceled() {
    return !!this.canceledAt;
  }
}