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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
pub mod create;
pub mod get;
pub mod update;
pub mod delete;
pub mod list;
pub mod enable;
pub mod disable;

use std::collections::BTreeMap;
use serde_json::Value;

#[derive(Default, PartialEq, Debug, Serialize, Deserialize)]
pub struct VouchersList {
    pub vouchers: Vec<Voucher>
}

#[derive(Default, PartialEq, Debug, Serialize, Deserialize)]
pub struct Voucher {
    pub object: Option<String>,
    pub created_at: Option<String>,
    pub code: Option<String>,
    pub campaign: Option<String>,
    pub category: Option<String>,
    #[serde(rename = "type")]
    pub voucher_type: Option<VoucherType>,
    pub is_referral_code: Option<bool>,
    pub start_date: Option<String>,
    pub expiration_date: Option<String>,
    pub publish: Option<Publish>,
    pub redemption: Option<Redemption>,
    pub active: Option<bool>,
    pub additional_info: Option<String>,
    pub metadata: Option<BTreeMap<String, Value>>,
    pub discount: Option<Discount>,
    pub gift: Option<Gift>,
    pub assets: Option<Assets>,
    pub referrer_id: Option<String>,
}

impl Voucher {
    pub fn new() -> Voucher {
        Voucher::default()
    }

    pub fn code(mut self, code: String) -> Voucher {
        self.code = Some(code);
        self
    }

    pub fn category(mut self, category: String) -> Voucher {
        self.category = Some(category);
        self
    }

    pub fn voucher_type(mut self, voucher_type: VoucherType) -> Voucher {
        self.voucher_type = Some(voucher_type);
        self
    }

    pub fn start_date(mut self, start_date: String) -> Voucher {
        self.start_date = Some(start_date);
        self
    }

    pub fn expiration_date(mut self, expiration_date: String) -> Voucher {
        self.expiration_date = Some(expiration_date);
        self
    }

    pub fn discount(mut self, discount_type: DiscountType, amount: u32) -> Voucher {
        self.discount = Some(Discount::new(discount_type, amount));
        self
    }

    pub fn build(self) -> Voucher {
        self
    }
}

#[derive(Hash, Eq, PartialEq, Debug, Serialize, Deserialize)]
#[allow(non_camel_case_types)]
pub enum VoucherType {
    DISCOUNT_VOUCHER,
    GIFT_VOUCHER,
}

#[derive(Hash, Eq, PartialEq, Debug, Serialize, Deserialize)]
pub struct Publish {
    count: u32,
    entries: Option<Vec<PublishEntry>>,
}

#[derive(Hash, Eq, PartialEq, Debug, Serialize, Deserialize)]
pub struct PublishEntry {
    customer_id: String,
    customer: String,
    channel: String,
    published_at: String,
}

#[derive(Hash, Eq, PartialEq, Debug, Serialize, Deserialize)]
pub struct Redemption {
    redeemed_quantity: u32,
    redemption_entries: Option<Vec<RedemptionEntry>>,
    redeemed_amount: Option<u32>,
    quantity: Option<u32>,
}

#[derive(Hash, Eq, PartialEq, Debug, Serialize, Deserialize)]
pub struct RedemptionEntry {
    id: String,
}

#[derive(Hash, Eq, PartialEq, Debug, Serialize, Deserialize)]
pub struct Discount {
    #[serde(rename = "type")]
    pub discount_type: DiscountType,
    pub amount_off: Option<u32>,
    pub percent_off: Option<u32>
}

impl Discount {
    pub fn new(discount_type: DiscountType, amount: u32) -> Discount {
        if discount_type == DiscountType::PERCENT {
            return Discount {
                discount_type: discount_type,
                amount_off: None,
                percent_off: Some(amount)
            }
        }

        Discount {
            discount_type: discount_type,
            amount_off: Some(amount),
            percent_off: None
        }
    }
}

#[derive(Hash, Eq, PartialEq, Debug, Serialize, Deserialize)]
pub enum DiscountType {
    AMOUNT,
    PERCENT,
    UNIT,
}

#[derive(Hash, Eq, PartialEq, Debug, Serialize, Deserialize)]
pub struct Gift {
    amount: usize,
    balance: usize,
}

impl Gift {
    pub fn new(amount: usize, balance: usize) -> Gift {
        Gift {
            amount: amount,
            balance: balance,
        }
    }
}

#[derive(Hash, Eq, PartialEq, Debug, Serialize, Deserialize)]
pub struct Assets {
    qr: QrCode,
}

#[derive(Hash, Eq, PartialEq, Debug, Serialize, Deserialize)]
pub struct QrCode {
    id: String,
    url: String,
}