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 "success-fee-invoice" => Ok(keys::BILLING_SUCCESS_FEE_INVOICE),
184
185 unknown => {
186 eprintln!("Unknown event key '{}', expected a known key", unknown);
188 Err(de::Error::custom(format!("Unknown event key: {}", unknown)))
189 }
190 }
191 }
192 }
193
194 deserializer.deserialize_str(EventKeyVisitor)
195 }
196}
197
198#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
199pub enum AccountEventKey {
200 Welcome,
201 TierUpgrade,
202
203 ExistingChargebacks,
205 HighChargebackRatioWarning,
206 ProviderDisconnected,
207
208 BillingPaymentFailed,
210 BillingInvoiceReady,
211
212 BillingWelcome,
214 BillingPaymentSucceeded,
215 BillingPaymentFailed2,
216 BillingOverageInvoiceCreated,
217 BillingUpcomingInvoice,
218 BillingTrialExpired,
219 BillingTrialEnding,
220 BillingTrialStarted,
221 BillingPlanDowngraded,
222 BillingPlanUpgraded,
223 BillingPlanRenewed,
224 BillingPlanCancelled,
225 BillingPlanPurchased,
226 BillingSuccessFeeInvoice,
227}
228
229impl AccountEventKey {
230 pub fn as_str(&self) -> &'static str {
231 match self {
232 AccountEventKey::Welcome => "account.welcome",
233 AccountEventKey::TierUpgrade => "account.tier_upgrade",
234 AccountEventKey::ExistingChargebacks => "account.existing_chargebacks",
235 AccountEventKey::HighChargebackRatioWarning => "account.high_chargeback_ratio_warning",
236 AccountEventKey::ProviderDisconnected => "account.provider_disconnected",
237 AccountEventKey::BillingPaymentFailed => "billing.payment_failed",
238 AccountEventKey::BillingInvoiceReady => "billing.invoice_ready",
239 AccountEventKey::BillingWelcome => "welcome",
240 AccountEventKey::BillingPaymentSucceeded => "payment_succeeded",
241 AccountEventKey::BillingPaymentFailed2 => "payment_failed",
242 AccountEventKey::BillingOverageInvoiceCreated => "overage_invoice_created",
243 AccountEventKey::BillingUpcomingInvoice => "upcoming_invoice",
244 AccountEventKey::BillingTrialExpired => "trial_expired",
245 AccountEventKey::BillingTrialEnding => "trial_ending",
246 AccountEventKey::BillingTrialStarted => "trial_started",
247 AccountEventKey::BillingPlanDowngraded => "plan_downgraded",
248 AccountEventKey::BillingPlanUpgraded => "plan_upgraded",
249 AccountEventKey::BillingPlanRenewed => "plan_renewed",
250 AccountEventKey::BillingPlanCancelled => "plan_cancelled",
251 AccountEventKey::BillingPlanPurchased => "plan_purchased",
252 AccountEventKey::BillingSuccessFeeInvoice => "success-fee-invoice",
253 }
254 }
255}
256
257#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
258pub enum SyncEventKey {
259 Connection(ConnectionEventKey),
260 Disputer(DisputerEventKey),
261
262 ConnectionNudge(ConnectionNudgeKey),
264
265 Response(ResponseEventKey),
268 }
271
272impl SyncEventKey {
273 pub fn as_str(&self) -> &'static str {
274 match self {
275 SyncEventKey::Connection(key) => key.as_str(),
276 SyncEventKey::Disputer(key) => key.as_str(),
277 SyncEventKey::ConnectionNudge(key) => key.as_str(),
278 SyncEventKey::Response(key) => key.as_str(),
280 }
283 }
284}
285
286#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
287pub enum ConnectionEventKey {
288 Finished,
289 Added,
290}
291
292impl ConnectionEventKey {
293 pub fn as_str(&self) -> &'static str {
294 match self {
295 ConnectionEventKey::Finished => "sync.connection.finished",
296 ConnectionEventKey::Added => "sync.connection.added",
297 }
298 }
299}
300
301#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
302pub enum DisputerEventKey {
303 PendingCharges(PendingChargeEventKey),
304 Disputes(DisputeEventKey),
305 Evidences(EvidencesEventKey),
306}
307
308impl DisputerEventKey {
309 pub fn as_str(&self) -> &'static str {
310 match self {
311 DisputerEventKey::PendingCharges(key) => key.as_str(),
312 DisputerEventKey::Disputes(key) => key.as_str(),
313 DisputerEventKey::Evidences(key) => key.as_str(),
314 }
315 }
316}
317
318#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
319pub enum DisputeEventKey {
320 Created,
321 Won,
322 Lost,
323 WarningClosed,
324 MissingEvidenceReminder
325}
326
327impl DisputeEventKey {
328 pub fn as_str(&self) -> &'static str {
329 match self {
330 DisputeEventKey::Created => "sync.disputer.disputes.created",
331 DisputeEventKey::Won => "sync.disputer.disputes.won",
332 DisputeEventKey::Lost => "sync.disputer.disputes.lost",
333 DisputeEventKey::WarningClosed => "sync.disputer.disputes.warning_closed",
334 DisputeEventKey::MissingEvidenceReminder => "sync.disputer.disputes.missing_evidence_reminder",
335 }
336 }
337}
338
339#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
340pub enum PendingChargeEventKey {
341 Payment,
342 LimitReached,
343 Added,
344}
345
346impl PendingChargeEventKey {
347 pub fn as_str(&self) -> &'static str {
348 match self {
349 PendingChargeEventKey::Payment => "sync.disputer.pending_charges.payment",
350 PendingChargeEventKey::LimitReached => "sync.disputer.pending_charges.limit_reached",
351 PendingChargeEventKey::Added => "sync.disputer.pending_charges.added",
352 }
353 }
354}
355
356#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
357pub enum EvidencesEventKey {
358 Submitted,
359 Received,
360}
361
362impl EvidencesEventKey {
363 pub fn as_str(&self) -> &'static str {
364 match self {
365 EvidencesEventKey::Submitted => "sync.disputer.evidences.submitted",
366 EvidencesEventKey::Received => "sync.disputer.evidences.received",
367 }
368 }
369}
370
371#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
373pub enum ConnectionNudgeKey {
374 Day1,
375 Day2,
376 Day4,
377 Day7,
378 Day30,
379}
380
381impl ConnectionNudgeKey {
382 pub fn as_str(&self) -> &'static str {
383 match self {
384 ConnectionNudgeKey::Day1 => "sync.connection.not_created.nudge_day_1",
385 ConnectionNudgeKey::Day2 => "sync.connection.not_created.nudge_day_2",
386 ConnectionNudgeKey::Day4 => "sync.connection.not_created.nudge_day_4",
387 ConnectionNudgeKey::Day7 => "sync.connection.not_created.nudge_day_7",
388 ConnectionNudgeKey::Day30 => "sync.connection.not_created.nudge_day_30",
389 }
390 }
391}
392
393#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
408pub enum ResponseEventKey {
409 DeadlineApproaching,
410}
411impl ResponseEventKey {
412 pub fn as_str(&self) -> &'static str {
413 match self {
414 ResponseEventKey::DeadlineApproaching => "sync.response.deadline.approaching",
415 }
416 }
417}
418
419pub mod keys {
450 use super::*;
451
452 pub const ACCOUNT_WELCOME: EventKey = EventKey::Account(AccountEventKey::Welcome);
453 pub const ACCOUNT_TIER_UPGRADE: EventKey = EventKey::Account(AccountEventKey::TierUpgrade);
454
455 pub const SYNC_CONNECTION_FINISHED: EventKey =
456 EventKey::Sync(SyncEventKey::Connection(ConnectionEventKey::Finished));
457
458 pub const SYNC_CONNECTION_ADDED: EventKey = EventKey::Sync(SyncEventKey::Connection(ConnectionEventKey::Added));
459
460 pub const SYNC_DISPUTER_PENDING_CHARGES_PAYMENT: EventKey = EventKey::Sync(SyncEventKey::Disputer(
461 DisputerEventKey::PendingCharges(PendingChargeEventKey::Payment),
462 ));
463 pub const SYNC_DISPUTER_PENDING_CHARGES_LIMIT_REACHED: EventKey = EventKey::Sync(SyncEventKey::Disputer(
464 DisputerEventKey::PendingCharges(PendingChargeEventKey::LimitReached),
465 ));
466 pub const SYNC_DISPUTER_PENDING_CHARGES_ADDED: EventKey = EventKey::Sync(SyncEventKey::Disputer(
467 DisputerEventKey::PendingCharges(PendingChargeEventKey::Added),
468 ));
469 pub const SYNC_DISPUTER_DISPUTES_CREATED: EventKey = EventKey::Sync(SyncEventKey::Disputer(
470 DisputerEventKey::Disputes(DisputeEventKey::Created),
471 ));
472 pub const SYNC_DISPUTER_DISPUTES_WON: EventKey =
473 EventKey::Sync(SyncEventKey::Disputer(DisputerEventKey::Disputes(DisputeEventKey::Won)));
474 pub const SYNC_DISPUTER_DISPUTES_LOST: EventKey = EventKey::Sync(SyncEventKey::Disputer(
475 DisputerEventKey::Disputes(DisputeEventKey::Lost),
476 ));
477 pub const SYNC_DISPUTER_DISPUTES_WARNING_CLOSED: EventKey = EventKey::Sync(SyncEventKey::Disputer(
478 DisputerEventKey::Disputes(DisputeEventKey::WarningClosed),
479 ));
480 pub const SYNC_DISPUTER_DISPUTES_MISSING_EVIDENCE_REMINDER: EventKey = EventKey::Sync(SyncEventKey::Disputer(
481 DisputerEventKey::Disputes(DisputeEventKey::MissingEvidenceReminder),
482 ));
483 pub const SYNC_DISPUTER_EVIDENCES_SUBMITTED: EventKey = EventKey::Sync(SyncEventKey::Disputer(
484 DisputerEventKey::Evidences(EvidencesEventKey::Submitted),
485 ));
486 pub const SYNC_DISPUTER_EVIDENCES_RECEIVED: EventKey = EventKey::Sync(SyncEventKey::Disputer(
487 DisputerEventKey::Evidences(EvidencesEventKey::Received),
488 ));
489
490 pub const SYNC_CONNECTION_NUDGE_DAY1: EventKey = EventKey::Sync(SyncEventKey::ConnectionNudge(ConnectionNudgeKey::Day1));
491
492 pub const SYNC_CONNECTION_NUDGE_DAY2: EventKey =
493 EventKey::Sync(SyncEventKey::ConnectionNudge(ConnectionNudgeKey::Day2));
494 pub const SYNC_CONNECTION_NUDGE_DAY4: EventKey =
495 EventKey::Sync(SyncEventKey::ConnectionNudge(ConnectionNudgeKey::Day4));
496 pub const SYNC_CONNECTION_NUDGE_DAY7: EventKey =
497 EventKey::Sync(SyncEventKey::ConnectionNudge(ConnectionNudgeKey::Day7));
498 pub const SYNC_CONNECTION_NUDGE_DAY30: EventKey =
499 EventKey::Sync(SyncEventKey::ConnectionNudge(ConnectionNudgeKey::Day30));
500
501 pub const SYNC_RESPONSE_DEADLINE_APPROACHING: EventKey =
505 EventKey::Sync(SyncEventKey::Response(ResponseEventKey::DeadlineApproaching));
506
507 pub const ACCOUNT_EXISTING_CHARGEBACKS: EventKey =
519 EventKey::Account(AccountEventKey::ExistingChargebacks);
520 pub const ACCOUNT_HIGH_CHARGEBACK_RATIO_WARNING: EventKey =
521 EventKey::Account(AccountEventKey::HighChargebackRatioWarning);
522 pub const ACCOUNT_PROVIDER_DISCONNECTED: EventKey =
523 EventKey::Account(AccountEventKey::ProviderDisconnected);
524
525 pub const BILLING_PAYMENT_FAILED: EventKey =
527 EventKey::Account(AccountEventKey::BillingPaymentFailed);
528 pub const BILLING_INVOICE_READY: EventKey =
529 EventKey::Account(AccountEventKey::BillingInvoiceReady);
530
531 pub const BILLING_WELCOME: EventKey =
533 EventKey::Account(AccountEventKey::BillingWelcome);
534 pub const BILLING_PAYMENT_SUCCEEDED: EventKey =
535 EventKey::Account(AccountEventKey::BillingPaymentSucceeded);
536 pub const BILLING_PAYMENT_FAILED_2: EventKey =
537 EventKey::Account(AccountEventKey::BillingPaymentFailed2);
538 pub const BILLING_OVERAGE_INVOICE_CREATED: EventKey =
539 EventKey::Account(AccountEventKey::BillingOverageInvoiceCreated);
540 pub const BILLING_UPCOMING_INVOICE: EventKey =
541 EventKey::Account(AccountEventKey::BillingUpcomingInvoice);
542 pub const BILLING_TRIAL_EXPIRED: EventKey =
543 EventKey::Account(AccountEventKey::BillingTrialExpired);
544 pub const BILLING_TRIAL_ENDING: EventKey =
545 EventKey::Account(AccountEventKey::BillingTrialEnding);
546 pub const BILLING_TRIAL_STARTED: EventKey =
547 EventKey::Account(AccountEventKey::BillingTrialStarted);
548 pub const BILLING_PLAN_DOWNGRADED: EventKey =
549 EventKey::Account(AccountEventKey::BillingPlanDowngraded);
550 pub const BILLING_PLAN_UPGRADED: EventKey =
551 EventKey::Account(AccountEventKey::BillingPlanUpgraded);
552 pub const BILLING_PLAN_RENEWED: EventKey =
553 EventKey::Account(AccountEventKey::BillingPlanRenewed);
554 pub const BILLING_PLAN_CANCELLED: EventKey =
555 EventKey::Account(AccountEventKey::BillingPlanCancelled);
556 pub const BILLING_PLAN_PURCHASED: EventKey =
557 EventKey::Account(AccountEventKey::BillingPlanPurchased);
558 pub const BILLING_SUCCESS_FEE_INVOICE: EventKey =
559 EventKey::Account(AccountEventKey::BillingSuccessFeeInvoice);
560
561}