feat(invitations): accept invitations with C4 consent and contact grants; share the E0 quota across holdings

This commit is contained in:
shira committed 2026-10-09 18:03:35 +03:00
1 parent a5990d3fc6
commit 676b3c6e24
59 files changed
+4926 -194

No files matched your search

@@ -24,6 +24,7 @@ var organizationConstraintErrors = map[constraintViolation]error{
{uniqueViolationCode, "memberships_user_id_organization_id_key"}: outbound.ErrMembershipExists,
{foreignKeyViolationCode, "memberships_organization_id_fkey"}: outbound.ErrOrganizationNotFound,
{foreignKeyViolationCode, "memberships_user_id_fkey"}: outbound.ErrUserNotFound,
{foreignKeyViolationCode, "holdings_founder_account_id_fkey"}: outbound.ErrUserNotFound,
{foreignKeyViolationCode, "organization_representatives_organization_id_fkey"}: outbound.ErrOrganizationNotFound,
}
@@ -72,10 +73,14 @@ func (repository organizationRepository) CreateWithOwner(ctx context.Context, or
case owner.OrganizationID() != organization.ID() || owner.Role() != domain.OrganizationRoleOwner || !owner.GrantsAccess():
return fmt.Errorf("%s - owner must be the active owner membership of the organization: %w", location, domain.ErrInvalidMembership)
}
holdingID, err := repository.ensureFounderHolding(ctx, owner.UserID(), organization.CreatedAt())
if err != nil {
return fmt.Errorf("%s - join founder holding: %w", location, err)
}
profile := organization.Profile()
contacts := profile.Contacts()
verification := organization.Verification()
err := repository.queries.InsertOrganization(ctx, sqlc.InsertOrganizationParams{
err = repository.queries.InsertOrganization(ctx, sqlc.InsertOrganizationParams{
ID: pgUUID(organization.ID()),
Name: profile.Name(),
Description: pgOptionalText(profile.Description()),
@@ -90,6 +95,7 @@ func (repository organizationRepository) CreateWithOwner(ctx context.Context, or
Version: organization.Version(),
CreatedAt: pgTime(organization.CreatedAt()),
UpdatedAt: pgTime(organization.UpdatedAt()),
HoldingID: pgUUID(holdingID),
})
if err != nil {
return fmt.Errorf("%s - insert organization: %w", location, mapOrganizationError(err))
@@ -103,6 +109,37 @@ func (repository organizationRepository) CreateWithOwner(ctx context.Context, or
return nil
}
// ensureFounderHolding returns the holding of the founder account and creates
// it in the caller's transaction when the account has none. The insert ignores
// an existing holding, so a concurrent creation of the same account waits for
// the other transaction and then reads the holding that was stored.
func (repository organizationRepository) ensureFounderHolding(
ctx context.Context,
founderID uuid.UUID,
createdAt time.Time,
) (uuid.UUID, error) {
const location = "organizationRepository.ensureFounderHolding"
holding, err := domain.NewHolding(founderID, createdAt)
if err != nil {
return uuid.Nil, fmt.Errorf("%s - build holding: %w", location, err)
}
if err := repository.queries.InsertHoldingIfAbsent(ctx, sqlc.InsertHoldingIfAbsentParams{
ID: pgUUID(holding.ID()),
FounderAccountID: pgUUID(holding.FounderAccountID()),
CreatedAt: pgTime(holding.CreatedAt()),
}); err != nil {
return uuid.Nil, fmt.Errorf("%s - insert holding: %w", location, mapOrganizationError(err))
}
stored, err := repository.queries.GetHoldingIDByFounder(ctx, pgUUID(founderID))
if err != nil {
return uuid.Nil, fmt.Errorf("%s - read stored holding: %w", location, mapOrganizationError(err))
}
if !stored.Valid {
return uuid.Nil, fmt.Errorf("%s - stored holding has no identifier", location)
}
return uuid.UUID(stored.Bytes), nil
}
func (repository organizationRepository) SaveCreationResult(
ctx context.Context,
scope [outbound.IdempotencyDigestSize]byte,