| | | 1 | | using ArturRios.Heimdall.Domain.Entities; |
| | | 2 | | using Microsoft.EntityFrameworkCore; |
| | | 3 | | using Microsoft.EntityFrameworkCore.Metadata.Builders; |
| | | 4 | | |
| | | 5 | | namespace ArturRios.Heimdall.Data.EntityMaps; |
| | | 6 | | |
| | | 7 | | internal static class GoogleUserDbMap |
| | | 8 | | { |
| | | 9 | | public static void Configure(this EntityTypeBuilder<GoogleUser> googleUser) |
| | | 10 | | { |
| | 3 | 11 | | googleUser.ToTable("google_user"); |
| | | 12 | | |
| | 3 | 13 | | googleUser.HasKey(x => x.Id); |
| | | 14 | | |
| | 3 | 15 | | googleUser.Property(x => x.PublicId).IsRequired(); |
| | 3 | 16 | | googleUser.HasIndex(x => x.PublicId).IsUnique(); |
| | | 17 | | |
| | 3 | 18 | | googleUser.Property(x => x.GoogleId).IsRequired(); |
| | | 19 | | |
| | 3 | 20 | | googleUser.Property(x => x.Email).IsRequired(); |
| | | 21 | | |
| | 3 | 22 | | googleUser.Property(x => x.EmailVerified).HasDefaultValue(false); |
| | 3 | 23 | | googleUser.Property(x => x.IsDeleted).HasDefaultValue(false); |
| | | 24 | | |
| | | 25 | | // NFR-20's retention window is measured from DeletedAt, so the anonymisation pass reads it |
| | | 26 | | // on every run: indexed, and filtered to the rows that can possibly be due, which is a small |
| | | 27 | | // fraction of the table. |
| | 3 | 28 | | googleUser.Property(x => x.DeletedAt); |
| | 3 | 29 | | googleUser.Property(x => x.DeletionKind); |
| | 3 | 30 | | googleUser.Property(x => x.AnonymisedAt); |
| | | 31 | | |
| | | 32 | | // NFR-23: the basis is required and defaults to Unrecorded, so a row can never be silent |
| | | 33 | | // about it — a null would be indistinguishable from "nobody has looked at this yet". |
| | 3 | 34 | | googleUser.Property(x => x.LegalBasis).HasDefaultValue(0); |
| | 3 | 35 | | googleUser.Property(x => x.PrivacyNoticeVersion); |
| | 3 | 36 | | googleUser.Property(x => x.BasisRecordedAt); |
| | | 37 | | |
| | | 38 | | // NFR-24: indexed because every listing filters on it, and the restricted set is a small |
| | | 39 | | // fraction of the table. |
| | 3 | 40 | | googleUser.Property(x => x.ProcessingRestrictedAt); |
| | 3 | 41 | | googleUser.Property(x => x.RestrictionGround); |
| | 3 | 42 | | googleUser.Property(x => x.RestrictionLiftNotifiedAt); |
| | | 43 | | |
| | 3 | 44 | | googleUser.HasIndex(x => x.ProcessingRestrictedAt) |
| | 3 | 45 | | .HasFilter("processing_restricted_at IS NOT NULL"); |
| | | 46 | | |
| | 3 | 47 | | googleUser.HasIndex(x => x.DeletedAt) |
| | 3 | 48 | | .HasFilter("is_deleted = true AND anonymised_at IS NULL"); |
| | | 49 | | |
| | 3 | 50 | | googleUser.Property(x => x.CreatedAt).HasDefaultValueSql("now()"); |
| | 3 | 51 | | googleUser.Property(x => x.UpdatedAt).HasDefaultValueSql("now()"); |
| | | 52 | | |
| | | 53 | | // GoogleId (Google's 'sub' claim) is unique within the scope (FR-GO-08). |
| | 3 | 54 | | googleUser.HasIndex(x => new { x.ScopeId, x.GoogleId }).IsUnique(); |
| | | 55 | | |
| | | 56 | | // Email is unique within the scope among Google Users (FR-GO-07), case-insensitively — the |
| | | 57 | | // application compares addresses with LOWER(), so an index over the raw column would accept |
| | | 58 | | // a pair the handler had already refused and enforce a rule nobody wrote. The expression |
| | | 59 | | // form is applied in the migration's raw SQL: EF cannot model an index over an expression, |
| | | 60 | | // so this call reserves the name and the migration replaces its definition. |
| | | 61 | | // |
| | | 62 | | // The joint uniqueness with User persons' emails spans two tables and cannot be a unique |
| | | 63 | | // index at all; it stays in the application layer (CreateUserCommandHandler, |
| | | 64 | | // UpdatePersonCommandHandler, GoogleSignInCommandHandler). |
| | 3 | 65 | | googleUser.HasIndex(x => new { x.ScopeId, x.Email }) |
| | 3 | 66 | | .IsUnique() |
| | 3 | 67 | | .HasDatabaseName("ix_google_user_scope_id_email"); |
| | | 68 | | |
| | | 69 | | // A Google User belongs to exactly one scope (FR-GO-06); hard-deleting the scope cascades |
| | | 70 | | // to its Google Users (NFR-14). |
| | 3 | 71 | | googleUser.HasOne(x => x.Scope) |
| | 3 | 72 | | .WithMany(s => s.GoogleUsers) |
| | 3 | 73 | | .HasForeignKey(x => x.ScopeId) |
| | 3 | 74 | | .IsRequired() |
| | 3 | 75 | | .OnDelete(DeleteBehavior.Cascade); |
| | 3 | 76 | | } |
| | | 77 | | } |