feat(accounts): add consent settings and organization management
This commit is contained in:
1 parent
24638d46d2
commit
391af7a1ef
83 files changed
+10198
-613
No files matched your search
@@ -5,6 +5,7 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
@@ -59,6 +60,8 @@ type organizationRepository struct {
|
||||
|
||||
var _ outbound.OrganizationRepository = organizationRepository{}
|
||||
|
||||
const maxOrganizationCreationSnapshotBytes = 65536
|
||||
|
||||
func (repository organizationRepository) CreateWithOwner(ctx context.Context, organization domain.Organization, owner domain.Membership) error {
|
||||
const location = "organizationRepository.CreateWithOwner"
|
||||
switch {
|
||||
@@ -100,6 +103,158 @@ func (repository organizationRepository) CreateWithOwner(ctx context.Context, or
|
||||
return nil
|
||||
}
|
||||
|
||||
func (repository organizationRepository) SaveCreationResult(
|
||||
ctx context.Context,
|
||||
scope [outbound.IdempotencyDigestSize]byte,
|
||||
result outbound.OrganizationCreationResult,
|
||||
) error {
|
||||
const location = "organizationRepository.SaveCreationResult"
|
||||
if scope == ([outbound.IdempotencyDigestSize]byte{}) || result.Organization.IsZero() || result.Owner.IsZero() ||
|
||||
result.Owner.OrganizationID() != result.Organization.ID() || result.Owner.Role() != domain.OrganizationRoleOwner ||
|
||||
!result.Owner.GrantsAccess() {
|
||||
return fmt.Errorf("%s - scope and active owner result must be set: %w", location, domain.ErrInvalidOrganization)
|
||||
}
|
||||
snapshot := creationSnapshotFromDomain(result)
|
||||
encoded, err := json.Marshal(snapshot)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s - encode domain snapshot", location)
|
||||
}
|
||||
if len(encoded) > maxOrganizationCreationSnapshotBytes {
|
||||
return fmt.Errorf("%s - domain snapshot exceeds its storage bound: %w", location, domain.ErrInvalidOrganization)
|
||||
}
|
||||
if err := repository.queries.SaveOrganizationCreationResult(ctx, sqlc.SaveOrganizationCreationResultParams{
|
||||
ScopeHash: scope[:],
|
||||
Snapshot: encoded,
|
||||
}); err != nil {
|
||||
return fmt.Errorf("%s - store domain snapshot: %w", location, mapOrganizationError(err))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (repository organizationRepository) GetCreationResult(
|
||||
ctx context.Context,
|
||||
scope [outbound.IdempotencyDigestSize]byte,
|
||||
) (outbound.OrganizationCreationResult, error) {
|
||||
const location = "organizationRepository.GetCreationResult"
|
||||
if scope == ([outbound.IdempotencyDigestSize]byte{}) {
|
||||
return outbound.OrganizationCreationResult{}, fmt.Errorf("%s - scope must be set: %w", location, domain.ErrInvalidOrganization)
|
||||
}
|
||||
encoded, err := repository.queries.GetOrganizationCreationResult(ctx, scope[:])
|
||||
if err != nil {
|
||||
return outbound.OrganizationCreationResult{}, fmt.Errorf("%s - read domain snapshot: %w", location, mapOrganizationNoRows(err, outbound.ErrOrganizationCreationResultNotFound))
|
||||
}
|
||||
if len(encoded) > maxOrganizationCreationSnapshotBytes {
|
||||
return outbound.OrganizationCreationResult{}, fmt.Errorf("%s - stored domain snapshot exceeds its bound", location)
|
||||
}
|
||||
var snapshot organizationCreationSnapshot
|
||||
if err := json.Unmarshal(encoded, &snapshot); err != nil {
|
||||
// The snapshot contains company profile data and representative names.
|
||||
return outbound.OrganizationCreationResult{}, fmt.Errorf("%s - stored domain snapshot is invalid", location)
|
||||
}
|
||||
result, err := snapshot.domainResult()
|
||||
if err != nil {
|
||||
return outbound.OrganizationCreationResult{}, fmt.Errorf("%s - restore domain snapshot: %w", location, err)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
type organizationCreationSnapshot struct {
|
||||
Organization organizationCreationOrganizationSnapshot `json:"organization"`
|
||||
Owner organizationCreationMembershipSnapshot `json:"owner"`
|
||||
}
|
||||
|
||||
type organizationCreationOrganizationSnapshot struct {
|
||||
ID uuid.UUID `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
Sphere string `json:"sphere"`
|
||||
Website string `json:"website"`
|
||||
Email string `json:"email"`
|
||||
Phone string `json:"phone"`
|
||||
Telegram string `json:"telegram"`
|
||||
Representatives []organizationCreationRepresentativeSnapshot `json:"representatives"`
|
||||
VerificationLevel string `json:"verification_level"`
|
||||
VerificationBasis string `json:"verification_basis"`
|
||||
VerificationChangedAt time.Time `json:"verification_changed_at"`
|
||||
Version int64 `json:"version"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
}
|
||||
|
||||
type organizationCreationRepresentativeSnapshot struct {
|
||||
Name string `json:"name"`
|
||||
JobTitle string `json:"job_title"`
|
||||
}
|
||||
|
||||
type organizationCreationMembershipSnapshot struct {
|
||||
ID uuid.UUID `json:"id"`
|
||||
OrganizationID uuid.UUID `json:"organization_id"`
|
||||
UserID uuid.UUID `json:"user_id"`
|
||||
Role string `json:"role"`
|
||||
State string `json:"state"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
}
|
||||
|
||||
func creationSnapshotFromDomain(result outbound.OrganizationCreationResult) organizationCreationSnapshot {
|
||||
organization := result.Organization
|
||||
profile := organization.Profile()
|
||||
contacts := profile.Contacts()
|
||||
representatives := profile.Representatives()
|
||||
snapshotRepresentatives := make([]organizationCreationRepresentativeSnapshot, 0, len(representatives))
|
||||
for _, representative := range representatives {
|
||||
snapshotRepresentatives = append(snapshotRepresentatives, organizationCreationRepresentativeSnapshot{
|
||||
Name: representative.Name(), JobTitle: representative.JobTitle(),
|
||||
})
|
||||
}
|
||||
verification := organization.Verification()
|
||||
owner := result.Owner
|
||||
return organizationCreationSnapshot{
|
||||
Organization: organizationCreationOrganizationSnapshot{
|
||||
ID: organization.ID(), Name: profile.Name(), Description: profile.Description(), Sphere: profile.Sphere(),
|
||||
Website: contacts.Website(), Email: contacts.Email(), Phone: contacts.Phone(), Telegram: contacts.Telegram(),
|
||||
Representatives: snapshotRepresentatives, VerificationLevel: string(verification.Level()),
|
||||
VerificationBasis: string(verification.Basis()), VerificationChangedAt: verification.ChangedAt(),
|
||||
Version: organization.Version(), CreatedAt: organization.CreatedAt(), UpdatedAt: organization.UpdatedAt(),
|
||||
},
|
||||
Owner: organizationCreationMembershipSnapshot{
|
||||
ID: owner.ID(), OrganizationID: owner.OrganizationID(), UserID: owner.UserID(), Role: string(owner.Role()),
|
||||
State: string(owner.State()), CreatedAt: owner.CreatedAt(), UpdatedAt: owner.UpdatedAt(),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (snapshot organizationCreationSnapshot) domainResult() (outbound.OrganizationCreationResult, error) {
|
||||
stored := snapshot.Organization
|
||||
representatives := make([]domain.StoredRepresentative, 0, len(stored.Representatives))
|
||||
for _, representative := range stored.Representatives {
|
||||
representatives = append(representatives, domain.StoredRepresentative{
|
||||
Name: representative.Name, JobTitle: representative.JobTitle,
|
||||
})
|
||||
}
|
||||
organization, err := domain.RestoreOrganization(domain.StoredOrganization{
|
||||
ID: stored.ID, Name: stored.Name, Description: stored.Description, Sphere: stored.Sphere,
|
||||
Website: stored.Website, Email: stored.Email, Phone: stored.Phone, Telegram: stored.Telegram,
|
||||
Representatives: representatives, VerificationLevel: stored.VerificationLevel, VerificationBasis: stored.VerificationBasis,
|
||||
VerificationChangedAt: stored.VerificationChangedAt, Version: stored.Version, CreatedAt: stored.CreatedAt, UpdatedAt: stored.UpdatedAt,
|
||||
})
|
||||
if err != nil {
|
||||
return outbound.OrganizationCreationResult{}, err
|
||||
}
|
||||
owner := snapshot.Owner
|
||||
membership, err := domain.RestoreMembership(domain.StoredMembership{
|
||||
ID: owner.ID, OrganizationID: owner.OrganizationID, UserID: owner.UserID,
|
||||
Role: owner.Role, State: owner.State, CreatedAt: owner.CreatedAt, UpdatedAt: owner.UpdatedAt,
|
||||
})
|
||||
if err != nil {
|
||||
return outbound.OrganizationCreationResult{}, err
|
||||
}
|
||||
if membership.OrganizationID() != organization.ID() || membership.Role() != domain.OrganizationRoleOwner || !membership.GrantsAccess() {
|
||||
return outbound.OrganizationCreationResult{}, domain.ErrInvalidMembership
|
||||
}
|
||||
return outbound.OrganizationCreationResult{Organization: organization, Owner: membership}, nil
|
||||
}
|
||||
|
||||
func (repository organizationRepository) insertRepresentatives(
|
||||
ctx context.Context,
|
||||
organizationID uuid.UUID,
|
||||
|
||||
Reference in new issue
Block a user