feat(profile): persist candidate edits and enforce company access

This commit is contained in:
TakuyaYagam1 committed 2026-10-07 14:15:16 +03:00
1 parent 3ab6bd91a3
commit 73c7caa225
67 files changed
+5286 -229

No files matched your search

@@ -252,6 +252,9 @@ func (service *Service) Update(ctx context.Context, command UpdateCommand) (doma
if command.ActorID == uuid.Nil || command.OrganizationID == uuid.Nil {
return domain.Organization{}, fmt.Errorf("%s - actor and organization IDs must be set: %w", location, ErrForbidden)
}
if err := matchCompanyContext(ctx, command.ActorID, command.OrganizationID, location); err != nil {
return domain.Organization{}, err
}
if command.ExpectedVersion < 1 {
return domain.Organization{}, fmt.Errorf("%s - expected version must be positive: %w", location, domain.ErrInvalidOrganization)
}
@@ -299,13 +302,16 @@ func (service *Service) Update(ctx context.Context, command UpdateCommand) (doma
return result, nil
}
// Get reads an organization only for an active member of the employer
// account. The membership is checked in the same transaction as the account.
// Get reads an organization only for its active owner. The membership is
// checked in the same transaction as the account.
func (service *Service) Get(ctx context.Context, actorID, organizationID uuid.UUID) (domain.Organization, error) {
const location = "organization.Service.Get"
if actorID == uuid.Nil || organizationID == uuid.Nil {
return domain.Organization{}, fmt.Errorf("%s - actor and organization IDs must be set: %w", location, ErrForbidden)
}
if err := matchCompanyContext(ctx, actorID, organizationID, location); err != nil {
return domain.Organization{}, err
}
var result domain.Organization
err := service.transactor.WithinTransaction(ctx, func(ctx context.Context, unit outbound.UnitOfWork) error {
if err := requireEmployer(ctx, unit, actorID, location); err != nil {
@@ -318,6 +324,9 @@ func (service *Service) Get(ctx context.Context, actorID, organizationID uuid.UU
if !membership.GrantsAccess() {
return fmt.Errorf("%s - require active membership: %w", location, outbound.ErrOrganizationNotFound)
}
if membership.Role() != domain.OrganizationRoleOwner {
return fmt.Errorf("%s - require active owner: %w", location, outbound.ErrOrganizationManagerRequired)
}
result = organization
return nil
})
@@ -327,6 +336,82 @@ func (service *Service) Get(ctx context.Context, actorID, organizationID uuid.UU
return result, nil
}
// AuthorizeCompany verifies the current verified employer account, active
// membership, and owner role before issuing a request-scoped capability.
func (service *Service) AuthorizeCompany(ctx context.Context, actorID, organizationID uuid.UUID) (CompanyAccess, error) {
const location = "organization.Service.AuthorizeCompany"
if actorID == uuid.Nil || organizationID == uuid.Nil {
return CompanyAccess{}, fmt.Errorf("%s - actor and organization IDs must be set: %w", location, ErrForbidden)
}
var access CompanyAccess
err := service.transactor.WithinTransaction(ctx, func(ctx context.Context, unit outbound.UnitOfWork) error {
if err := requireEmployer(ctx, unit, actorID, location); err != nil {
return fmt.Errorf("%s - authorize company actor: %w", location, err)
}
_, membership, err := unit.Organizations().GetForMember(ctx, organizationID, actorID)
if err != nil {
return fmt.Errorf("%s - read active company membership: %w", location, err)
}
if !membership.GrantsAccess() {
return fmt.Errorf("%s - require active membership: %w", location, outbound.ErrOrganizationNotFound)
}
if membership.Role() != domain.OrganizationRoleOwner {
return fmt.Errorf("%s - require active owner: %w", location, outbound.ErrOrganizationManagerRequired)
}
access = CompanyAccess{actorID: actorID, organizationID: organizationID, membershipID: membership.ID()}
return nil
})
if err != nil {
return CompanyAccess{}, fmt.Errorf("%s - run transaction: %w", location, err)
}
return access, nil
}
// ListMyOrganizations returns active organization memberships for one
// verified employer. The actor always comes from the authenticated session.
func (service *Service) ListMyOrganizations(ctx context.Context, command OrganizationListCommand) (outbound.OrganizationPage, error) {
const location = "organization.Service.ListMyOrganizations"
if command.ActorID == uuid.Nil {
return outbound.OrganizationPage{}, fmt.Errorf("%s - actor ID must be set: %w", location, ErrForbidden)
}
if command.Limit < 1 || command.Limit > outbound.MaxOrganizationPageSize {
return outbound.OrganizationPage{}, fmt.Errorf("%s - limit must be 1 to %d: %w", location, outbound.MaxOrganizationPageSize, domain.ErrInvalidMembership)
}
var result outbound.OrganizationPage
err := service.transactor.WithinTransaction(ctx, func(ctx context.Context, unit outbound.UnitOfWork) error {
if err := requireEmployer(ctx, unit, command.ActorID, location); err != nil {
return fmt.Errorf("%s - authorize list actor: %w", location, err)
}
page, err := unit.Organizations().ListForUser(ctx, outbound.OrganizationListQuery{
UserID: command.ActorID, After: command.After, Limit: command.Limit,
})
if err != nil {
return fmt.Errorf("%s - list active memberships: %w", location, err)
}
result = page
return nil
})
if err != nil {
return outbound.OrganizationPage{}, fmt.Errorf("%s - run transaction: %w", location, err)
}
return result, nil
}
// OrganizationListCommand selects a page of the actor's own memberships.
type OrganizationListCommand struct {
ActorID uuid.UUID
After *outbound.OrganizationListPosition
Limit int32
}
func matchCompanyContext(ctx context.Context, actorID, organizationID uuid.UUID, location string) error {
access, ok := CompanyAccessFromContext(ctx)
if ok && !access.Matches(actorID, organizationID) {
return fmt.Errorf("%s - company context does not match the operation: %w", location, ErrForbidden)
}
return nil
}
func applyPatch(current domain.OrganizationProfile, patch ProfilePatch) (domain.OrganizationProfile, error) {
contacts := current.Contacts()
input := domain.OrganizationProfileInput{