| | | 1 | | using System.Linq.Expressions; |
| | | 2 | | using ArturRios.Data.Relational.Core.Entities; |
| | | 3 | | using ArturRios.Heimdall.Domain.Enums; |
| | | 4 | | |
| | | 5 | | namespace ArturRios.Heimdall.Domain.Entities; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// An invitation to co-own a scope (UC-21, FR-SC-14): a Scope Admin asked for an existing administrator |
| | | 9 | | /// to become an owner, and the ownership starts only when that person accepts it, signed in as |
| | | 10 | | /// themselves (UC-60). It ends exactly once — accepted, declined, withdrawn or voided — or runs out at |
| | | 11 | | /// <see cref="ExpiresAt" />. |
| | | 12 | | /// </summary> |
| | | 13 | | /// <remarks> |
| | | 14 | | /// At most one invitation per scope and invitee is unended at a time, which a partial unique index |
| | | 15 | | /// holds. An expired row still counts until something closes it, so opening a new one first closes an |
| | | 16 | | /// expired one with <see cref="EndedAt" /> at its expiry and no <see cref="Outcome" />. |
| | | 17 | | /// </remarks> |
| | | 18 | | public class ScopeOwnerInvitation : Entity<long> |
| | | 19 | | { |
| | | 20 | | /// <summary> |
| | | 21 | | /// How the scope's owners withdraw it (UC-58) and the invitee answers it (UC-60, UC-61). Random, so it |
| | | 22 | | /// discloses nothing about the people involved. |
| | | 23 | | /// </summary> |
| | 596 | 24 | | public Guid PublicId { get; set; } = Guid.NewGuid(); |
| | | 25 | | |
| | | 26 | | /// <summary>Foreign key to the scope the invitee is asked to co-own (internal Id).</summary> |
| | 266 | 27 | | public long ScopeId { get; set; } |
| | | 28 | | |
| | | 29 | | /// <summary>Foreign key to the invited administrator (internal Id); only they may answer.</summary> |
| | 294 | 30 | | public long InviteePersonId { get; set; } |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Foreign key to the administrator who invited (internal Id). Cleared when they are anonymised, and |
| | | 34 | | /// set to null by the database when they are hard-deleted. |
| | | 35 | | /// </summary> |
| | 199 | 36 | | public long? InvitedByPersonId { get; set; } |
| | | 37 | | |
| | 196 | 38 | | public DateTime CreatedAt { get; set; } |
| | | 39 | | |
| | | 40 | | /// <summary>When it stops being acceptable (<c>HEIMDALL_SCOPE_INVITATION_LIFETIME_IN_SECONDS</c> after creation).</ |
| | 298 | 41 | | public DateTime ExpiresAt { get; set; } |
| | | 42 | | |
| | | 43 | | /// <summary>When it ended; <c>null</c> while it has not.</summary> |
| | 303 | 44 | | public DateTime? EndedAt { get; set; } |
| | | 45 | | |
| | | 46 | | /// <summary>How it ended; <c>null</c> while open, and for one that ended by expiry.</summary> |
| | 185 | 47 | | public ScopeOwnerInvitationOutcomes? Outcome { get; set; } |
| | | 48 | | |
| | 88 | 49 | | public Scope Scope { get; set; } = null!; |
| | | 50 | | |
| | 125 | 51 | | public Person Invitee { get; set; } = null!; |
| | | 52 | | |
| | 114 | 53 | | public Person? InvitedBy { get; set; } |
| | | 54 | | |
| | | 55 | | /// <summary> |
| | | 56 | | /// Open at <paramref name="now" />: not ended, and its expiry still ahead. An expression, so a query |
| | | 57 | | /// and a conditional write ask exactly the same question. |
| | | 58 | | /// </summary> |
| | | 59 | | public static Expression<Func<ScopeOwnerInvitation, bool>> OpenAt(DateTime now) => |
| | 136 | 60 | | invitation => invitation.EndedAt == null && invitation.ExpiresAt > now; |
| | | 61 | | |
| | | 62 | | /// <summary> |
| | | 63 | | /// Whether a person may still accept an invitation (UC-60): not logically deleted, not |
| | | 64 | | /// processing-restricted, not anonymised, and a Scope Admin or System Admin. The one definition of |
| | | 65 | | /// the rule: the PostgreSQL acceptance writes it into its conditional <c>UPDATE</c>, and code that |
| | | 66 | | /// holds a person in memory compiles it. The scope must be live too, which this does not cover. |
| | | 67 | | /// </summary> |
| | 35 | 68 | | public static Expression<Func<Person, bool>> EligibleInvitee { get; } = person => |
| | 3 | 69 | | !person.IsDeleted && person.ProcessingRestrictedAt == null && person.AnonymisedAt == null && |
| | 3 | 70 | | (person.RoleId == (long)Roles.ScopeAdmin || person.RoleId == (long)Roles.SystemAdmin); |
| | | 71 | | } |