1use serde::{
2 Deserialize, Deserializer, Serialize, Serializer,
3 de::{self, Visitor},
4};
5use std::fmt;
6
7#[derive(Debug, Clone, PartialEq, Eq)]
8pub enum EventKey {
9 Account(AccountEventKey),
10 Sync(SyncEventKey),
11}
12
13impl EventKey {
14 pub fn as_str(&self) -> &'static str {
15 match self {
16 EventKey::Account(key) => key.as_str(),
17 EventKey::Sync(key) => key.as_str(),
18 }
19 }
20
21}
94
95impl Serialize for EventKey {
97 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
98 where
99 S: Serializer,
100 {
101 serializer.serialize_str(self.as_str())
102 }
103}
104
105impl<'de> Deserialize<'de> for EventKey {
107 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
108 where
109 D: Deserializer<'de>,
110 {
111 struct EventKeyVisitor;
112
113 impl<'de> Visitor<'de> for EventKeyVisitor {
114 type Value = EventKey;
115
116 fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
117 formatter.write_str("a valid event key string")
118 }
119
120 fn visit_str<E>(self, value: &str) -> Result<EventKey, E>
121 where
122 E: de::Error,
123 {
124 match value {
125 "account.welcome" => Ok(keys::ACCOUNT_WELCOME),
126 "account.tier_upgrade" => Ok(keys::ACCOUNT_TIER_UPGRADE),
127 "sync.connection.finished" => Ok(keys::SYNC_CONNECTION_FINISHED),
128 "sync.connection.added" => Ok(keys::SYNC_CONNECTION_ADDED),
129 "sync.disputer.pending_charges.payment" => Ok(keys::SYNC_DISPUTER_PENDING_CHARGES_PAYMENT),
130 "sync.disputer.pending_charges.limit_reached" => {
131 Ok(keys::SYNC_DISPUTER_PENDING_CHARGES_LIMIT_REACHED)
132 }
133 "sync.disputer.pending_charges.added" => Ok(keys::SYNC_DISPUTER_PENDING_CHARGES_ADDED),
134 "sync.disputer.disputes.created" => Ok(keys::SYNC_DISPUTER_DISPUTES_CREATED),
135 "sync.disputer.disputes.won" => Ok(keys::SYNC_DISPUTER_DISPUTES_WON),
136 "sync.disputer.disputes.lost" => Ok(keys::SYNC_DISPUTER_DISPUTES_LOST),
137 "sync.disputer.disputes.warning_closed" => Ok(keys::SYNC_DISPUTER_DISPUTES_WARNING_CLOSED),
138 "sync.disputer.disputes.missing_evidence_reminder" => Ok(keys::SYNC_DISPUTER_DISPUTES_MISSING_EVIDENCE_REMINDER),
139 "sync.disputer.evidences.submitted" => Ok(keys::SYNC_DISPUTER_EVIDENCES_SUBMITTED),
140 "sync.disputer.evidences.received" => Ok(keys::SYNC_DISPUTER_EVIDENCES_RECEIVED),
141 "sync.connection.not_created.nudge_day_1" => Ok(keys::SYNC_CONNECTION_NUDGE_DAY1),
142 "sync.connection.not_created.nudge_day_2" => Ok(keys::SYNC_CONNECTION_NUDGE_DAY2),
144 "sync.connection.not_created.nudge_day_4" => Ok(keys::SYNC_CONNECTION_NUDGE_DAY4),
145 "sync.connection.not_created.nudge_day_7" => Ok(keys::SYNC_CONNECTION_NUDGE_DAY7),
146 "sync.connection.not_created.nudge_day_30" => Ok(keys::SYNC_CONNECTION_NUDGE_DAY30),
147
148 "sync.response.deadline.approaching" => Ok(keys::SYNC_RESPONSE_DEADLINE_APPROACHING),
151
152 "account.existing_chargebacks" => Ok(keys::ACCOUNT_EXISTING_CHARGEBACKS),
162 "account.high_chargeback_ratio_warning"=> Ok(keys::ACCOUNT_HIGH_CHARGEBACK_RATIO_WARNING),
163 "account.provider_disconnected" => Ok(keys::ACCOUNT_PROVIDER_DISCONNECTED),
164
165 "billing.payment_failed" => Ok(keys::BILLING_PAYMENT_FAILED),
167 "billing.invoice_ready" => Ok(keys::BILLING_INVOICE_READY),
168
169 "welcome" => Ok(keys::BILLING_WELCOME),
171 "payment_succeeded" => Ok(keys::BILLING_PAYMENT_SUCCEEDED),
172 "payment_failed" => Ok(keys::BILLING_PAYMENT_FAILED_2),
173 "overage_invoice_created" => Ok(keys::BILLING_OVERAGE_INVOICE_CREATED),
174 "upcoming_invoice" => Ok(keys::BILLING_UPCOMING_INVOICE),
175 "trial_expired" => Ok(keys::BILLING_TRIAL_EXPIRED),
176 "trial_ending" => Ok(keys::BILLING_TRIAL_ENDING),
177 "trial_started" => Ok(keys::BILLING_TRIAL_STARTED),
178 "plan_downgraded" => Ok(keys::BILLING_PLAN_DOWNGRADED),
179 "plan_upgraded" => Ok(keys::BILLING_PLAN_UPGRADED),
180 "plan_renewed" => Ok(keys::BILLING_PLAN_RENEWED),
181 "plan_cancelled" => Ok(keys::BILLING_PLAN_CANCELLED),
182 "plan_purchased" => Ok(keys::BILLING_PLAN_PURCHASED),
183
184 unknown => {
185 eprintln!("Unknown event key '{}', expected a known key", unknown);
187 Err(de::Error::custom(format!("Unknown event key: {}", unknown)))
188 }
189 }
190 }
191 }
192
193 deserializer.deserialize_str(EventKeyVisitor)
194 }
195}
196
197#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
198pub enum AccountEventKey {
199 Welcome,
200 TierUpgrade,
201
202 ExistingChargebacks,
204 HighChargebackRatioWarning,
205 ProviderDisconnected,
206
207 BillingPaymentFailed,
209 BillingInvoiceReady,
210
211 BillingWelcome,
213 BillingPaymentSucceeded,
214 BillingPaymentFailed2,
215 BillingOverageInvoiceCreated,
216 BillingUpcomingInvoice,
217 BillingTrialExpired,
218 BillingTrialEnding,
219 BillingTrialStarted,
220 BillingPlanDowngraded,
221 BillingPlanUpgraded,
222 BillingPlanRenewed,
223 BillingPlanCancelled,
224 BillingPlanPurchased,
225}
226
227impl AccountEventKey {
228 pub fn as_str(&self) -> &'static str {
229 match self {
230 AccountEventKey::Welcome => "account.welcome",
231 AccountEventKey::TierUpgrade => "account.tier_upgrade",
232 AccountEventKey::ExistingChargebacks => "account.existing_chargebacks",
233 AccountEventKey::HighChargebackRatioWarning => "account.high_chargeback_ratio_warning",
234 AccountEventKey::ProviderDisconnected => "account.provider_disconnected",
235 AccountEventKey::BillingPaymentFailed => "billing.payment_failed",
236 AccountEventKey::BillingInvoiceReady => "billing.invoice_ready",
237 AccountEventKey::BillingWelcome => "welcome",
238 AccountEventKey::BillingPaymentSucceeded => "payment_succeeded",
239 AccountEventKey::BillingPaymentFailed2 => "payment_failed",
240 AccountEventKey::BillingOverageInvoiceCreated => "overage_invoice_created",
241 AccountEventKey::BillingUpcomingInvoice => "upcoming_invoice",
242 AccountEventKey::BillingTrialExpired => "trial_expired",
243 AccountEventKey::BillingTrialEnding => "trial_ending",
244 AccountEventKey::BillingTrialStarted => "trial_started",
245 AccountEventKey::BillingPlanDowngraded => "plan_downgraded",
246 AccountEventKey::BillingPlanUpgraded => "plan_upgraded",
247 AccountEventKey::BillingPlanRenewed => "plan_renewed",
248 AccountEventKey::BillingPlanCancelled => "plan_cancelled",
249 AccountEventKey::BillingPlanPurchased => "plan_purchased",
250 }
251 }
252}
253
254#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
255pub enum SyncEventKey {
256 Connection(ConnectionEventKey),
257 Disputer(DisputerEventKey),
258
259 ConnectionNudge(ConnectionNudgeKey),
261
262 Response(ResponseEventKey),
265 }
268
269impl SyncEventKey {
270 pub fn as_str(&self) -> &'static str {
271 match self {
272 SyncEventKey::Connection(key) => key.as_str(),
273 SyncEventKey::Disputer(key) => key.as_str(),
274 SyncEventKey::ConnectionNudge(key) => key.as_str(),
275 SyncEventKey::Response(key) => key.as_str(),
277 }
280 }
281}
282
283#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
284pub enum ConnectionEventKey {
285 Finished,
286 Added,
287}
288
289impl ConnectionEventKey {
290 pub fn as_str(&self) -> &'static str {
291 match self {
292 ConnectionEventKey::Finished => "sync.connection.finished",
293 ConnectionEventKey::Added => "sync.connection.added",
294 }
295 }
296}
297
298#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
299pub enum DisputerEventKey {
300 PendingCharges(PendingChargeEventKey),
301 Disputes(DisputeEventKey),
302 Evidences(EvidencesEventKey),
303}
304
305impl DisputerEventKey {
306 pub fn as_str(&self) -> &'static str {
307 match self {
308 DisputerEventKey::PendingCharges(key) => key.as_str(),
309 DisputerEventKey::Disputes(key) => key.as_str(),
310 DisputerEventKey::Evidences(key) => key.as_str(),
311 }
312 }
313}
314
315#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
316pub enum DisputeEventKey {
317 Created,
318 Won,
319 Lost,
320 WarningClosed,
321 MissingEvidenceReminder
322}
323
324impl DisputeEventKey {
325 pub fn as_str(&self) -> &'static str {
326 match self {
327 DisputeEventKey::Created => "sync.disputer.disputes.created",
328 DisputeEventKey::Won => "sync.disputer.disputes.won",
329 DisputeEventKey::Lost => "sync.disputer.disputes.lost",
330 DisputeEventKey::WarningClosed => "sync.disputer.disputes.warning_closed",
331 DisputeEventKey::MissingEvidenceReminder => "sync.disputer.disputes.missing_evidence_reminder",
332 }
333 }
334}
335
336#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
337pub enum PendingChargeEventKey {
338 Payment,
339 LimitReached,
340 Added,
341}
342
343impl PendingChargeEventKey {
344 pub fn as_str(&self) -> &'static str {
345 match self {
346 PendingChargeEventKey::Payment => "sync.disputer.pending_charges.payment",
347 PendingChargeEventKey::LimitReached => "sync.disputer.pending_charges.limit_reached",
348 PendingChargeEventKey::Added => "sync.disputer.pending_charges.added",
349 }
350 }
351}
352
353#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
354pub enum EvidencesEventKey {
355 Submitted,
356 Received,
357}
358
359impl EvidencesEventKey {
360 pub fn as_str(&self) -> &'static str {
361 match self {
362 EvidencesEventKey::Submitted => "sync.disputer.evidences.submitted",
363 EvidencesEventKey::Received => "sync.disputer.evidences.received",
364 }
365 }
366}
367
368#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
370pub enum ConnectionNudgeKey {
371 Day1,
372 Day2,
373 Day4,
374 Day7,
375 Day30,
376}
377
378impl ConnectionNudgeKey {
379 pub fn as_str(&self) -> &'static str {
380 match self {
381 ConnectionNudgeKey::Day1 => "sync.connection.not_created.nudge_day_1",
382 ConnectionNudgeKey::Day2 => "sync.connection.not_created.nudge_day_2",
383 ConnectionNudgeKey::Day4 => "sync.connection.not_created.nudge_day_4",
384 ConnectionNudgeKey::Day7 => "sync.connection.not_created.nudge_day_7",
385 ConnectionNudgeKey::Day30 => "sync.connection.not_created.nudge_day_30",
386 }
387 }
388}
389
390#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
405pub enum ResponseEventKey {
406 DeadlineApproaching,
407}
408impl ResponseEventKey {
409 pub fn as_str(&self) -> &'static str {
410 match self {
411 ResponseEventKey::DeadlineApproaching => "sync.response.deadline.approaching",
412 }
413 }
414}
415
416pub mod keys {
447 use super::*;
448
449 pub const ACCOUNT_WELCOME: EventKey = EventKey::Account(AccountEventKey::Welcome);
450 pub const ACCOUNT_TIER_UPGRADE: EventKey = EventKey::Account(AccountEventKey::TierUpgrade);
451
452 pub const SYNC_CONNECTION_FINISHED: EventKey =
453 EventKey::Sync(SyncEventKey::Connection(ConnectionEventKey::Finished));
454
455 pub const SYNC_CONNECTION_ADDED: EventKey = EventKey::Sync(SyncEventKey::Connection(ConnectionEventKey::Added));
456
457 pub const SYNC_DISPUTER_PENDING_CHARGES_PAYMENT: EventKey = EventKey::Sync(SyncEventKey::Disputer(
458 DisputerEventKey::PendingCharges(PendingChargeEventKey::Payment),
459 ));
460 pub const SYNC_DISPUTER_PENDING_CHARGES_LIMIT_REACHED: EventKey = EventKey::Sync(SyncEventKey::Disputer(
461 DisputerEventKey::PendingCharges(PendingChargeEventKey::LimitReached),
462 ));
463 pub const SYNC_DISPUTER_PENDING_CHARGES_ADDED: EventKey = EventKey::Sync(SyncEventKey::Disputer(
464 DisputerEventKey::PendingCharges(PendingChargeEventKey::Added),
465 ));
466 pub const SYNC_DISPUTER_DISPUTES_CREATED: EventKey = EventKey::Sync(SyncEventKey::Disputer(
467 DisputerEventKey::Disputes(DisputeEventKey::Created),
468 ));
469 pub const SYNC_DISPUTER_DISPUTES_WON: EventKey =
470 EventKey::Sync(SyncEventKey::Disputer(DisputerEventKey::Disputes(DisputeEventKey::Won)));
471 pub const SYNC_DISPUTER_DISPUTES_LOST: EventKey = EventKey::Sync(SyncEventKey::Disputer(
472 DisputerEventKey::Disputes(DisputeEventKey::Lost),
473 ));
474 pub const SYNC_DISPUTER_DISPUTES_WARNING_CLOSED: EventKey = EventKey::Sync(SyncEventKey::Disputer(
475 DisputerEventKey::Disputes(DisputeEventKey::WarningClosed),
476 ));
477 pub const SYNC_DISPUTER_DISPUTES_MISSING_EVIDENCE_REMINDER: EventKey = EventKey::Sync(SyncEventKey::Disputer(
478 DisputerEventKey::Disputes(DisputeEventKey::MissingEvidenceReminder),
479 ));
480 pub const SYNC_DISPUTER_EVIDENCES_SUBMITTED: EventKey = EventKey::Sync(SyncEventKey::Disputer(
481 DisputerEventKey::Evidences(EvidencesEventKey::Submitted),
482 ));
483 pub const SYNC_DISPUTER_EVIDENCES_RECEIVED: EventKey = EventKey::Sync(SyncEventKey::Disputer(
484 DisputerEventKey::Evidences(EvidencesEventKey::Received),
485 ));
486
487 pub const SYNC_CONNECTION_NUDGE_DAY1: EventKey = EventKey::Sync(SyncEventKey::ConnectionNudge(ConnectionNudgeKey::Day1));
488
489 pub const SYNC_CONNECTION_NUDGE_DAY2: EventKey =
490 EventKey::Sync(SyncEventKey::ConnectionNudge(ConnectionNudgeKey::Day2));
491 pub const SYNC_CONNECTION_NUDGE_DAY4: EventKey =
492 EventKey::Sync(SyncEventKey::ConnectionNudge(ConnectionNudgeKey::Day4));
493 pub const SYNC_CONNECTION_NUDGE_DAY7: EventKey =
494 EventKey::Sync(SyncEventKey::ConnectionNudge(ConnectionNudgeKey::Day7));
495 pub const SYNC_CONNECTION_NUDGE_DAY30: EventKey =
496 EventKey::Sync(SyncEventKey::ConnectionNudge(ConnectionNudgeKey::Day30));
497
498 pub const SYNC_RESPONSE_DEADLINE_APPROACHING: EventKey =
502 EventKey::Sync(SyncEventKey::Response(ResponseEventKey::DeadlineApproaching));
503
504 pub const ACCOUNT_EXISTING_CHARGEBACKS: EventKey =
516 EventKey::Account(AccountEventKey::ExistingChargebacks);
517 pub const ACCOUNT_HIGH_CHARGEBACK_RATIO_WARNING: EventKey =
518 EventKey::Account(AccountEventKey::HighChargebackRatioWarning);
519 pub const ACCOUNT_PROVIDER_DISCONNECTED: EventKey =
520 EventKey::Account(AccountEventKey::ProviderDisconnected);
521
522 pub const BILLING_PAYMENT_FAILED: EventKey =
524 EventKey::Account(AccountEventKey::BillingPaymentFailed);
525 pub const BILLING_INVOICE_READY: EventKey =
526 EventKey::Account(AccountEventKey::BillingInvoiceReady);
527
528 pub const BILLING_WELCOME: EventKey =
530 EventKey::Account(AccountEventKey::BillingWelcome);
531 pub const BILLING_PAYMENT_SUCCEEDED: EventKey =
532 EventKey::Account(AccountEventKey::BillingPaymentSucceeded);
533 pub const BILLING_PAYMENT_FAILED_2: EventKey =
534 EventKey::Account(AccountEventKey::BillingPaymentFailed2);
535 pub const BILLING_OVERAGE_INVOICE_CREATED: EventKey =
536 EventKey::Account(AccountEventKey::BillingOverageInvoiceCreated);
537 pub const BILLING_UPCOMING_INVOICE: EventKey =
538 EventKey::Account(AccountEventKey::BillingUpcomingInvoice);
539 pub const BILLING_TRIAL_EXPIRED: EventKey =
540 EventKey::Account(AccountEventKey::BillingTrialExpired);
541 pub const BILLING_TRIAL_ENDING: EventKey =
542 EventKey::Account(AccountEventKey::BillingTrialEnding);
543 pub const BILLING_TRIAL_STARTED: EventKey =
544 EventKey::Account(AccountEventKey::BillingTrialStarted);
545 pub const BILLING_PLAN_DOWNGRADED: EventKey =
546 EventKey::Account(AccountEventKey::BillingPlanDowngraded);
547 pub const BILLING_PLAN_UPGRADED: EventKey =
548 EventKey::Account(AccountEventKey::BillingPlanUpgraded);
549 pub const BILLING_PLAN_RENEWED: EventKey =
550 EventKey::Account(AccountEventKey::BillingPlanRenewed);
551 pub const BILLING_PLAN_CANCELLED: EventKey =
552 EventKey::Account(AccountEventKey::BillingPlanCancelled);
553 pub const BILLING_PLAN_PURCHASED: EventKey =
554 EventKey::Account(AccountEventKey::BillingPlanPurchased);
555
556}