feat: add candidate profile, questionnaire and company forms
This commit is contained in:
1 parent
3a61a8e8bb
commit
f81cd0ae09
68 files changed
+9517
-54
No files matched your search
@@ -0,0 +1,152 @@
|
||||
//go:build e2e
|
||||
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"lincodex/backend/internal/adapter/secondary/postgres/sqlc"
|
||||
"lincodex/backend/internal/domain"
|
||||
"lincodex/backend/internal/port/outbound"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrCompanyFixtureScope = errors.New("company fixture scope rejected")
|
||||
companyFixtureEmail = regexp.MustCompile(`^company-[a-f0-9]{8}@example\.test$`)
|
||||
)
|
||||
|
||||
func isCompanyFixtureEmployer(user domain.User) bool {
|
||||
return user.Scenario == domain.ScenarioEmployer && user.EmailVerified() &&
|
||||
companyFixtureEmail.MatchString(user.Email.Address())
|
||||
}
|
||||
|
||||
// RevokeCompanyFixtureMembership removes the synthetic owner membership used
|
||||
// by the isolated browser suite. It is available only in e2e-tagged builds.
|
||||
func RevokeCompanyFixtureMembership(
|
||||
ctx context.Context,
|
||||
pool *pgxpool.Pool,
|
||||
organizationID, userID, successorUserID uuid.UUID,
|
||||
) error {
|
||||
const location = "postgres.RevokeCompanyFixtureMembership"
|
||||
if pool == nil || organizationID == uuid.Nil || userID == uuid.Nil || successorUserID == uuid.Nil ||
|
||||
userID == successorUserID {
|
||||
return fmt.Errorf("%s - invalid fixture identity: %w", location, ErrCompanyFixtureScope)
|
||||
}
|
||||
|
||||
tx, err := pool.BeginTx(ctx, transactionOptions)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s - begin transaction: %w", location, sanitizeConnectError(err))
|
||||
}
|
||||
finished := false
|
||||
defer func() {
|
||||
if !finished {
|
||||
_ = rollback(ctx, tx)
|
||||
}
|
||||
}()
|
||||
|
||||
unit := &UnitOfWork{tx: tx, queries: sqlc.New(tx)}
|
||||
membership, err := validateCompanyFixtureAccounts(ctx, unit, location, organizationID, userID, successorUserID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
changedAt := time.Now().UTC().Truncate(time.Microsecond)
|
||||
if changedAt.Before(membership.UpdatedAt()) {
|
||||
changedAt = membership.UpdatedAt()
|
||||
}
|
||||
if err := addCompanyFixtureSuccessor(ctx, unit, location, organizationID, successorUserID, changedAt); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := removeCompanyFixtureMembership(ctx, tx, location, organizationID, userID, changedAt); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
finished = true
|
||||
if err := tx.Commit(ctx); err != nil {
|
||||
return fmt.Errorf("%s - commit transaction: %w", location, markTransactionConflict(err))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateCompanyFixtureAccounts(
|
||||
ctx context.Context,
|
||||
unit *UnitOfWork,
|
||||
location string,
|
||||
organizationID, userID, successorUserID uuid.UUID,
|
||||
) (domain.Membership, error) {
|
||||
user, err := unit.Users().GetByID(ctx, userID)
|
||||
if err != nil {
|
||||
return domain.Membership{}, fmt.Errorf("%s - read fixture account: %w", location, err)
|
||||
}
|
||||
if !isCompanyFixtureEmployer(user) {
|
||||
return domain.Membership{}, fmt.Errorf("%s - account is outside the fixture scope: %w", location, ErrCompanyFixtureScope)
|
||||
}
|
||||
successor, err := unit.Users().GetByID(ctx, successorUserID)
|
||||
if err != nil {
|
||||
return domain.Membership{}, fmt.Errorf("%s - read successor account: %w", location, err)
|
||||
}
|
||||
if !isCompanyFixtureEmployer(successor) {
|
||||
return domain.Membership{}, fmt.Errorf("%s - successor account is outside the fixture scope: %w", location, ErrCompanyFixtureScope)
|
||||
}
|
||||
membership, err := unit.Memberships().Get(ctx, organizationID, userID)
|
||||
if err != nil {
|
||||
return domain.Membership{}, fmt.Errorf("%s - read fixture membership: %w", location, err)
|
||||
}
|
||||
if membership.Role() != domain.OrganizationRoleOwner || membership.State() != domain.MembershipStateActive {
|
||||
return domain.Membership{}, fmt.Errorf("%s - membership is outside the fixture scope: %w", location, ErrCompanyFixtureScope)
|
||||
}
|
||||
if _, err := unit.Memberships().Get(ctx, organizationID, successorUserID); err == nil {
|
||||
return domain.Membership{}, fmt.Errorf("%s - successor already belongs to the organization: %w", location, ErrCompanyFixtureScope)
|
||||
} else if !errors.Is(err, outbound.ErrMembershipNotFound) {
|
||||
return domain.Membership{}, fmt.Errorf("%s - check successor membership: %w", location, err)
|
||||
}
|
||||
return membership, nil
|
||||
}
|
||||
|
||||
func addCompanyFixtureSuccessor(
|
||||
ctx context.Context,
|
||||
unit *UnitOfWork,
|
||||
location string,
|
||||
organizationID, successorUserID uuid.UUID,
|
||||
changedAt time.Time,
|
||||
) error {
|
||||
successorMembership, err := domain.NewOwnerMembership(organizationID, successorUserID, changedAt)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s - create successor owner membership: %w", location, err)
|
||||
}
|
||||
if err := unit.Memberships().Insert(ctx, successorMembership); err != nil {
|
||||
return fmt.Errorf("%s - add successor owner membership: %w", location, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func removeCompanyFixtureMembership(
|
||||
ctx context.Context,
|
||||
tx pgx.Tx,
|
||||
location string,
|
||||
organizationID, userID uuid.UUID,
|
||||
changedAt time.Time,
|
||||
) error {
|
||||
tag, err := tx.Exec(ctx, `
|
||||
UPDATE memberships
|
||||
SET state = 'removed', updated_at = $3
|
||||
WHERE organization_id = $1
|
||||
AND user_id = $2
|
||||
AND role = 'owner'
|
||||
AND state = 'active'
|
||||
`, organizationID, userID, changedAt)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s - remove fixture membership: %w", location, err)
|
||||
}
|
||||
if tag.RowsAffected() != 1 {
|
||||
return fmt.Errorf("%s - membership changed concurrently: %w", location, outbound.ErrTransactionConflict)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
//go:build e2e
|
||||
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"lincodex/backend/internal/domain"
|
||||
)
|
||||
|
||||
func TestIsCompanyFixtureEmployerRequiresVerifiedSyntheticEmployer(t *testing.T) {
|
||||
verifiedAt := time.Date(2026, time.October, 7, 12, 0, 0, 0, time.UTC)
|
||||
tests := []struct {
|
||||
name string
|
||||
email string
|
||||
scenario domain.Scenario
|
||||
verified bool
|
||||
want bool
|
||||
}{
|
||||
{name: "verified fixture employer", email: "company-abcdef12@example.test", scenario: domain.ScenarioEmployer, verified: true, want: true},
|
||||
{name: "unverified employer", email: "company-abcdef12@example.test", scenario: domain.ScenarioEmployer},
|
||||
{name: "candidate account", email: "company-abcdef12@example.test", scenario: domain.ScenarioCandidate, verified: true},
|
||||
{name: "ordinary employer", email: "employer@example.test", scenario: domain.ScenarioEmployer, verified: true},
|
||||
{name: "fixture-like address with wrong suffix", email: "company-abcdef12@example.org", scenario: domain.ScenarioEmployer, verified: true},
|
||||
}
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
address, err := domain.NewEmailAddress(test.email)
|
||||
if err != nil {
|
||||
t.Fatalf("NewEmailAddress() error = %v", err)
|
||||
}
|
||||
user := domain.User{Email: address, Scenario: test.scenario}
|
||||
if test.verified {
|
||||
user.EmailVerifiedAt = &verifiedAt
|
||||
}
|
||||
if got := isCompanyFixtureEmployer(user); got != test.want {
|
||||
t.Fatalf("isCompanyFixtureEmployer() = %t, want %t", got, test.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user