136 lines
5.2 KiB
Go
136 lines
5.2 KiB
Go
package consent
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"lincodex/backend/internal/domain"
|
|
"lincodex/backend/internal/port/outbound"
|
|
)
|
|
|
|
type SettingsRevocationInput struct {
|
|
SubjectID uuid.UUID
|
|
Purpose domain.ConsentPurpose
|
|
OrganizationID uuid.UUID
|
|
}
|
|
|
|
type RevocationResult struct {
|
|
Records []domain.ConsentRecord
|
|
}
|
|
|
|
func (service *Service) RevokeInSettingsInNewTransaction(ctx context.Context, input SettingsRevocationInput) (RevocationResult, error) {
|
|
const location = "Service.RevokeInSettingsInNewTransaction"
|
|
if input.Purpose == domain.ConsentPurposePersonalData {
|
|
return RevocationResult{}, fmt.Errorf("%s - c1 uses the account deletion request: %w", location, domain.ErrInvalidConsent)
|
|
}
|
|
if input.Purpose == domain.ConsentPurposeContactDisclosure && input.OrganizationID == uuid.Nil {
|
|
return RevocationResult{}, fmt.Errorf("%s - c4 organization is required: %w", location, domain.ErrInvalidConsent)
|
|
}
|
|
if input.Purpose != domain.ConsentPurposeContactDisclosure && input.OrganizationID != uuid.Nil {
|
|
return RevocationResult{}, fmt.Errorf("%s - organization scope is only valid for c4: %w", location, domain.ErrInvalidConsent)
|
|
}
|
|
if _, err := domain.ParseConsentPurpose(string(input.Purpose)); err != nil {
|
|
return RevocationResult{}, fmt.Errorf("%s - validate purpose: %w", location, err)
|
|
}
|
|
revocation, err := domain.NewConsentRevocation(domain.ConsentRevocationInput{
|
|
SubjectID: input.SubjectID, Purpose: input.Purpose, OrganizationID: input.OrganizationID,
|
|
Source: domain.ConsentRevocationSourceConsentSettings, RevokedAt: service.clock(),
|
|
})
|
|
if err != nil {
|
|
return RevocationResult{}, fmt.Errorf("%s - create revocation: %w", location, err)
|
|
}
|
|
var result RevocationResult
|
|
err = service.inNewTransaction(ctx, func(ctx context.Context, unit outbound.UnitOfWork) error {
|
|
records, err := unit.ConsentLedger().Revoke(ctx, revocation)
|
|
if err != nil {
|
|
return fmt.Errorf("%s - revoke consent ledger: %w", location, err)
|
|
}
|
|
if len(records) == 0 {
|
|
result = RevocationResult{}
|
|
return nil
|
|
}
|
|
if err := unit.ConsentRevocationEffects().Apply(ctx, revocation, records); err != nil {
|
|
return fmt.Errorf("%s - apply revocation effects: %w", location, err)
|
|
}
|
|
result = RevocationResult{Records: records}
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
return RevocationResult{}, fmt.Errorf("%s - run transaction: %w", location, err)
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func (service *Service) RequestAccountDeletionInNewTransaction(ctx context.Context, subjectID uuid.UUID) (domain.AccountDeletionRequest, error) {
|
|
const location = "Service.RequestAccountDeletionInNewTransaction"
|
|
request, err := domain.NewAccountDeletionRequest(subjectID, service.clock())
|
|
if err != nil {
|
|
return domain.AccountDeletionRequest{}, fmt.Errorf("%s - create request: %w", location, err)
|
|
}
|
|
revocation, err := domain.NewConsentRevocation(domain.ConsentRevocationInput{
|
|
SubjectID: subjectID, Purpose: domain.ConsentPurposePersonalData,
|
|
Source: domain.ConsentRevocationSourceConsentSettings, RevokedAt: request.RequestedAt(),
|
|
})
|
|
if err != nil {
|
|
return domain.AccountDeletionRequest{}, fmt.Errorf("%s - create c1 revocation: %w", location, err)
|
|
}
|
|
var result domain.AccountDeletionRequest
|
|
err = service.inNewTransaction(ctx, func(ctx context.Context, unit outbound.UnitOfWork) error {
|
|
storedRequest, created, err := unit.AccountDeletionRequests().CreateOrGet(ctx, request)
|
|
if err != nil {
|
|
return fmt.Errorf("%s - store account deletion request: %w", location, err)
|
|
}
|
|
records, err := unit.ConsentLedger().Revoke(ctx, revocation)
|
|
if err != nil {
|
|
return fmt.Errorf("%s - revoke c1 consent: %w", location, err)
|
|
}
|
|
if len(records) > 0 {
|
|
if err := unit.ConsentRevocationEffects().Apply(ctx, revocation, records); err != nil {
|
|
return fmt.Errorf("%s - apply c1 revocation effects: %w", location, err)
|
|
}
|
|
}
|
|
if created {
|
|
if err := appendDeletionRequestedEvent(ctx, unit, storedRequest); err != nil {
|
|
return fmt.Errorf("%s - append deletion request event: %w", location, err)
|
|
}
|
|
}
|
|
result = storedRequest
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
return domain.AccountDeletionRequest{}, fmt.Errorf("%s - run transaction: %w", location, err)
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
type accountDeletionRequestedPayload struct {
|
|
RequestID uuid.UUID `json:"request_id"`
|
|
SubjectID uuid.UUID `json:"subject_id"`
|
|
RequestedAt time.Time `json:"requested_at"`
|
|
}
|
|
|
|
func appendDeletionRequestedEvent(ctx context.Context, unit outbound.UnitOfWork, request domain.AccountDeletionRequest) error {
|
|
const location = "appendDeletionRequestedEvent"
|
|
body, err := json.Marshal(accountDeletionRequestedPayload{
|
|
RequestID: request.ID(), SubjectID: request.SubjectID(), RequestedAt: request.RequestedAt(),
|
|
})
|
|
if err != nil {
|
|
return fmt.Errorf("%s - marshal payload: %w", location, err)
|
|
}
|
|
event, err := domain.NewDomainEvent(domain.DomainEventInput{
|
|
EventType: "privacy.account_deletion_requested", SchemaVersion: 1,
|
|
AggregateType: "account_deletion_request", AggregateID: request.ID(), AggregateVersion: 1, Payload: body,
|
|
})
|
|
if err != nil {
|
|
return fmt.Errorf("%s - build event: %w", location, err)
|
|
}
|
|
if err := unit.Outbox().Append(ctx, event); err != nil {
|
|
return fmt.Errorf("%s - append event: %w", location, err)
|
|
}
|
|
return nil
|
|
}
|