|
| 1 | +using Snowcloak.API.Dto.Account; |
| 2 | + |
| 3 | +namespace Snowcloak.Core.Accounts; |
| 4 | + |
| 5 | +public sealed record AccountEntitlements( |
| 6 | + bool IsLinked = false, |
| 7 | + bool IsPayingPatron = false, |
| 8 | + bool HasBenefits = false, |
| 9 | + bool IsCompetitionWinner = false, |
| 10 | + bool IsTestOverride = false, |
| 11 | + bool IsCreatorForCampaign = false, |
| 12 | + string PatreonUserId = "") |
| 13 | +{ |
| 14 | + public static AccountEntitlements Empty { get; } = new(); |
| 15 | +} |
| 16 | + |
| 17 | +public static class AccountEntitlementMapper |
| 18 | +{ |
| 19 | + public static AccountEntitlements FromPatreonStatus(PatreonStatusReplyDto? dto) |
| 20 | + { |
| 21 | + return dto == null |
| 22 | + ? AccountEntitlements.Empty |
| 23 | + : FromDto(dto.Entitlements, dto.IsLinked, dto.IsPayingPatron, dto.HasBenefits, |
| 24 | + dto.IsCompetitionWinner, dto.IsTestOverride, dto.IsCreatorForCampaign, dto.PatreonUserId); |
| 25 | + } |
| 26 | + |
| 27 | + public static AccountEntitlements FromPatreonLinkPoll(PatreonLinkPollReplyDto? dto) |
| 28 | + { |
| 29 | + return dto == null |
| 30 | + ? AccountEntitlements.Empty |
| 31 | + : FromDto(dto.Entitlements, dto.IsLinked, dto.IsPayingPatron, dto.HasBenefits, |
| 32 | + dto.IsCompetitionWinner, dto.IsTestOverride, dto.IsCreatorForCampaign, null); |
| 33 | + } |
| 34 | + |
| 35 | + public static AccountEntitlements FromDto(AccountEntitlementsDto? dto) |
| 36 | + { |
| 37 | + return FromDto(dto, false, false, false, false, false, false, null); |
| 38 | + } |
| 39 | + |
| 40 | + public static AccountEntitlements FromDto(AccountEntitlementsDto? dto, bool fallbackIsLinked, |
| 41 | + bool fallbackIsPayingPatron, bool fallbackHasBenefits, bool fallbackIsCompetitionWinner, |
| 42 | + bool fallbackIsTestOverride, bool fallbackIsCreatorForCampaign, string? fallbackPatreonUserId) |
| 43 | + { |
| 44 | + var hasFallback = fallbackIsLinked |
| 45 | + || fallbackIsPayingPatron |
| 46 | + || fallbackHasBenefits |
| 47 | + || fallbackIsCompetitionWinner |
| 48 | + || fallbackIsTestOverride |
| 49 | + || fallbackIsCreatorForCampaign |
| 50 | + || !string.IsNullOrWhiteSpace(fallbackPatreonUserId); |
| 51 | + |
| 52 | + if (dto != null && (HasValues(dto) || !hasFallback)) |
| 53 | + { |
| 54 | + return new AccountEntitlements( |
| 55 | + dto.IsLinked, |
| 56 | + dto.IsPayingPatron, |
| 57 | + dto.HasBenefits, |
| 58 | + dto.IsCompetitionWinner, |
| 59 | + dto.IsTestOverride, |
| 60 | + dto.IsCreatorForCampaign, |
| 61 | + dto.PatreonUserId ?? string.Empty); |
| 62 | + } |
| 63 | + |
| 64 | + return new AccountEntitlements( |
| 65 | + fallbackIsLinked, |
| 66 | + fallbackIsPayingPatron, |
| 67 | + fallbackHasBenefits, |
| 68 | + fallbackIsCompetitionWinner, |
| 69 | + fallbackIsTestOverride, |
| 70 | + fallbackIsCreatorForCampaign, |
| 71 | + fallbackPatreonUserId ?? string.Empty); |
| 72 | + } |
| 73 | + |
| 74 | + private static bool HasValues(AccountEntitlementsDto dto) |
| 75 | + { |
| 76 | + return dto.IsLinked |
| 77 | + || dto.IsPayingPatron |
| 78 | + || dto.HasBenefits |
| 79 | + || dto.IsCompetitionWinner |
| 80 | + || dto.IsTestOverride |
| 81 | + || dto.IsCreatorForCampaign |
| 82 | + || !string.IsNullOrWhiteSpace(dto.PatreonUserId); |
| 83 | + } |
| 84 | +} |
0 commit comments