feat(invitations): send invitations into the candidate inbox atomically
This commit is contained in:
1 parent
fc35e27af5
commit
1a067221f7
46 files changed
+3276
-113
No files matched your search
@@ -28,13 +28,14 @@ func NewInboxContentResolver(resolvers map[domain.NotificationInboxSourceType]In
|
||||
}
|
||||
|
||||
// DefaultInboxSourceResolvers lists the source types that have a resolver
|
||||
// today. The synthetic notice resolver is registered only when
|
||||
// syntheticEnabled is true (development and browser-test stands); a stored
|
||||
// synthetic row is then listed without content. The invitation module adds
|
||||
// domain.NotificationInboxSourceInvitation with its own resolver; the
|
||||
// handler, the table, and the API need no change.
|
||||
// today: the delivered invitation, and the synthetic notice. The synthetic
|
||||
// notice resolver is registered only when syntheticEnabled is true
|
||||
// (development and browser-test stands); otherwise a stored synthetic row is
|
||||
// listed without content.
|
||||
func DefaultInboxSourceResolvers(syntheticEnabled bool) map[domain.NotificationInboxSourceType]InboxSourceResolver {
|
||||
resolvers := map[domain.NotificationInboxSourceType]InboxSourceResolver{}
|
||||
resolvers := map[domain.NotificationInboxSourceType]InboxSourceResolver{
|
||||
domain.NotificationInboxSourceInvitation: ResolveInvitationNotice,
|
||||
}
|
||||
if syntheticEnabled {
|
||||
resolvers[domain.NotificationInboxSourceOrganization] = ResolveSyntheticNotice
|
||||
}
|
||||
|
||||
@@ -274,12 +274,63 @@ func TestSyntheticNoticeResolverFollowsTheRecipientStanding(t *testing.T) {
|
||||
OccurredAt: time.Date(2026, time.October, 8, 12, 0, 0, 0, time.UTC),
|
||||
})
|
||||
if _, err := resolvers.Resolve(context.Background(), unit, invitation); !errors.Is(err, ErrInboxContentUnavailable) {
|
||||
t.Fatalf("Resolve(source without a resolver) error = %v, want ErrInboxContentUnavailable", err)
|
||||
t.Fatalf("Resolve(invitation the recipient cannot read) error = %v, want ErrInboxContentUnavailable", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveInvitationNoticeNamesTheSenderOnlyWhileVisible(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
fixture := newInboxFixture(t, domain.ScenarioCandidate)
|
||||
delivered := fixture.deliveredInvitation(t)
|
||||
entry := fixtureEntry(t, domain.NotificationInboxEntryInput{
|
||||
EventID: uuid.New(), RecipientUserID: fixture.user.ID, OrganizationID: fixture.organizationID,
|
||||
EventType: domain.NotificationInboxInvitationDelivered, SchemaVersion: 1,
|
||||
SourceType: domain.NotificationInboxSourceInvitation, SourceID: delivered.ID(),
|
||||
OccurredAt: time.Date(2026, time.October, 8, 12, 0, 0, 0, time.UTC),
|
||||
})
|
||||
resolvers := NewInboxContentResolver(DefaultInboxSourceResolvers(false))
|
||||
content, err := resolvers.Resolve(context.Background(), fixture.unit(), entry)
|
||||
if err != nil {
|
||||
t.Fatalf("Resolve(delivered invitation) error = %v", err)
|
||||
}
|
||||
if content.Title() != invitationNoticeTitle || content.LinkPath() != candidateInvitationsPath ||
|
||||
!strings.Contains(content.Summary(), "«Север»") || !strings.Contains(content.Summary(), "«Inbox role»") {
|
||||
t.Fatalf("invitation content = %q / %q / %q", content.Title(), content.Summary(), content.LinkPath())
|
||||
}
|
||||
if strings.Contains(content.Summary(), fixture.user.ID.String()) {
|
||||
t.Fatal("invitation content names the candidate")
|
||||
}
|
||||
|
||||
// Another recipient and another organization context find nothing.
|
||||
foreign := fixtureEntry(t, domain.NotificationInboxEntryInput{
|
||||
EventID: uuid.New(), RecipientUserID: uuid.New(), OrganizationID: fixture.organizationID,
|
||||
EventType: domain.NotificationInboxInvitationDelivered, SchemaVersion: 1,
|
||||
SourceType: domain.NotificationInboxSourceInvitation, SourceID: delivered.ID(),
|
||||
OccurredAt: time.Date(2026, time.October, 8, 12, 0, 0, 0, time.UTC),
|
||||
})
|
||||
if _, err := ResolveInvitationNotice(context.Background(), fixture.unit(), foreign); !errors.Is(err, ErrInboxContentUnavailable) {
|
||||
t.Fatalf("ResolveInvitationNotice(other recipient) error = %v, want ErrInboxContentUnavailable", err)
|
||||
}
|
||||
otherContext := fixtureEntry(t, domain.NotificationInboxEntryInput{
|
||||
EventID: uuid.New(), RecipientUserID: fixture.user.ID, OrganizationID: uuid.New(),
|
||||
EventType: domain.NotificationInboxInvitationDelivered, SchemaVersion: 1,
|
||||
SourceType: domain.NotificationInboxSourceInvitation, SourceID: delivered.ID(),
|
||||
OccurredAt: time.Date(2026, time.October, 8, 12, 0, 0, 0, time.UTC),
|
||||
})
|
||||
if _, err := ResolveInvitationNotice(context.Background(), fixture.unit(), otherContext); !errors.Is(err, ErrInboxContentUnavailable) {
|
||||
t.Fatalf("ResolveInvitationNotice(other organization) error = %v, want ErrInboxContentUnavailable", err)
|
||||
}
|
||||
// A hidden organization is no longer named.
|
||||
fixture.repository.organizationName = ""
|
||||
if _, err := ResolveInvitationNotice(context.Background(), fixture.unit(), entry); !errors.Is(err, ErrInboxContentUnavailable) {
|
||||
t.Fatalf("ResolveInvitationNotice(hidden sender) error = %v, want ErrInboxContentUnavailable", err)
|
||||
}
|
||||
}
|
||||
|
||||
type inboxFixture struct {
|
||||
service *InboxService
|
||||
invitations map[uuid.UUID]domain.Invitation
|
||||
repository *memoryInboxRepository
|
||||
resolver *memoryInboxResolver
|
||||
users map[uuid.UUID]domain.User
|
||||
@@ -298,6 +349,7 @@ func newInboxFixtureWithClock(t *testing.T, scenario domain.Scenario, clock func
|
||||
t.Helper()
|
||||
verifiedAt := time.Date(2026, time.October, 1, 10, 0, 0, 0, time.UTC)
|
||||
fixture := &inboxFixture{
|
||||
invitations: map[uuid.UUID]domain.Invitation{},
|
||||
repository: &memoryInboxRepository{organizationName: "Север"},
|
||||
resolver: &memoryInboxResolver{unavailable: map[uuid.UUID]bool{}},
|
||||
users: map[uuid.UUID]domain.User{},
|
||||
@@ -316,6 +368,39 @@ func newInboxFixtureWithClock(t *testing.T, scenario domain.Scenario, clock func
|
||||
|
||||
func (fixture *inboxFixture) unit() outbound.UnitOfWork { return inboxTestUnit{fixture: fixture} }
|
||||
|
||||
// deliveredInvitation stores a delivered invitation of the fixture
|
||||
// organization addressed to the fixture user.
|
||||
func (fixture *inboxFixture) deliveredInvitation(t *testing.T) domain.Invitation {
|
||||
t.Helper()
|
||||
policyID, err := domain.NewPolicyVersionID()
|
||||
if err != nil {
|
||||
t.Fatalf("NewPolicyVersionID() error = %v", err)
|
||||
}
|
||||
none := domain.InvitationPackageItem{Status: domain.InvitationPackageNotProvided}
|
||||
at := time.Date(2026, time.October, 8, 11, 0, 0, 0, time.UTC)
|
||||
draft, err := domain.NewInvitationDraft(domain.NewInvitationDraftInput{
|
||||
ID: uuid.New(), OrganizationID: fixture.organizationID, CandidateID: fixture.user.ID, ActorID: uuid.New(),
|
||||
PolicyVersionID: policyID, At: at,
|
||||
Content: domain.InvitationDraftContent{ResponsePeriod: 7, Terms: domain.InvitationTerms{
|
||||
RoleTitle: "Inbox role", Specialization: domain.SpecializationBackend, Grade: domain.GradeMiddle,
|
||||
OfferType: domain.InvitationOfferFullTime, Tasks: []string{"Read the inbox"}, Team: "Inbox team",
|
||||
TechStack: []string{"Go"}, WorkFormats: []domain.NeedWorkFormat{domain.NeedWorkFormatRemote},
|
||||
CommunicationMethod: domain.InvitationCommunicationMethod{Channel: domain.InvitationChannelEmail},
|
||||
Salary: domain.SalaryRange{SalaryFrom: 100_000, SalaryTo: 140_000, Currency: "RUB", Period: "month", TaxBasis: "net"},
|
||||
Package: domain.InvitationPackage{Bonus: none, Equipment: none, Training: none, HealthInsurance: none},
|
||||
}},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("NewInvitationDraft() error = %v", err)
|
||||
}
|
||||
delivered, err := draft.Deliver(at.Add(time.Minute))
|
||||
if err != nil {
|
||||
t.Fatalf("Deliver() error = %v", err)
|
||||
}
|
||||
fixture.invitations[delivered.ID()] = delivered
|
||||
return delivered
|
||||
}
|
||||
|
||||
func (fixture *inboxFixture) entryInput() domain.NotificationInboxEntryInput {
|
||||
return fixtureEntryInput(fixture.user.ID, fixture.organizationID)
|
||||
}
|
||||
@@ -379,6 +464,25 @@ func (unit inboxTestUnit) Organizations() outbound.OrganizationRepository {
|
||||
func (unit inboxTestUnit) NotificationInbox() outbound.NotificationInboxRepository {
|
||||
return unit.fixture.repository
|
||||
}
|
||||
func (unit inboxTestUnit) Invitations() outbound.InvitationRepository {
|
||||
return inboxTestInvitations{fixture: unit.fixture}
|
||||
}
|
||||
|
||||
type inboxTestInvitations struct {
|
||||
outbound.InvitationRepository
|
||||
|
||||
fixture *inboxFixture
|
||||
}
|
||||
|
||||
// GetForCandidate answers like the real repository: only a delivered
|
||||
// invitation addressed to the candidate is found.
|
||||
func (repository inboxTestInvitations) GetForCandidate(_ context.Context, candidateID, invitationID uuid.UUID) (domain.Invitation, error) {
|
||||
invitation, ok := repository.fixture.invitations[invitationID]
|
||||
if !ok || invitation.CandidateID() != candidateID || !invitation.IsDelivered() {
|
||||
return domain.Invitation{}, outbound.ErrInvitationNotFound
|
||||
}
|
||||
return invitation, nil
|
||||
}
|
||||
|
||||
type inboxTestUsers struct {
|
||||
outbound.UserRepository
|
||||
|
||||
@@ -0,0 +1,122 @@
|
||||
package notification
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"lincodex/backend/internal/domain"
|
||||
"lincodex/backend/internal/port/outbound"
|
||||
"lincodex/backend/internal/usecase/notification/mailtemplate"
|
||||
)
|
||||
|
||||
const (
|
||||
// invitationAggregateType is the aggregate of invitation.delivered, which
|
||||
// is also the source of the inbox content.
|
||||
invitationAggregateType = "invitation"
|
||||
// candidateInvitationsPath is the inbox of invitations in the candidate
|
||||
// cabinet. Both the notification and the email link to it.
|
||||
candidateInvitationsPath = "/candidate/invitations"
|
||||
|
||||
invitationNoticeTitle = "Новое приглашение"
|
||||
// invitationNoticeSummary has one slot for the organization name and one
|
||||
// for the role.
|
||||
invitationNoticeSummary = "Организация «%s» отправила вам приглашение на роль «%s». Условия и срок ответа указаны в разделе «Приглашения»."
|
||||
|
||||
// The email names neither the organization nor the terms: the candidate
|
||||
// reads them in the cabinet, where a hiding or a later change applies.
|
||||
// It never holds a contact or an identifier of the candidate.
|
||||
invitationMailSubject = "Новое приглашение от работодателя"
|
||||
invitationMailBody = "Работодатель отправил вам приглашение на платформе Lincodex. Условия, организация и срок ответа указаны в разделе «Приглашения» личного кабинета кандидата."
|
||||
)
|
||||
|
||||
// InvitationNotifier records the notifications of a delivered invitation in
|
||||
// the transaction of the send: the invitation.delivered event that the inbox
|
||||
// consumer turns into the candidate's notification, and a mail request. The
|
||||
// invitation is delivered when that transaction commits; neither the
|
||||
// notification nor the email, nor any retry of them, changes its state or
|
||||
// deadline.
|
||||
type InvitationNotifier struct {
|
||||
mail *MailRequester
|
||||
}
|
||||
|
||||
// NewInvitationNotifier fails at startup without a mail requester.
|
||||
func NewInvitationNotifier(mail *MailRequester) (*InvitationNotifier, error) {
|
||||
if mail == nil {
|
||||
return nil, errors.New("notification.NewInvitationNotifier - mail requester must be set")
|
||||
}
|
||||
return &InvitationNotifier{mail: mail}, nil
|
||||
}
|
||||
|
||||
// InvitationDelivered appends the inbox event and requests the email through
|
||||
// unit, which must be the transaction that delivered the invitation. The
|
||||
// event payload names the recipient and the organization only, as the inbox
|
||||
// contract requires; it never leaves the internal event bus.
|
||||
func (notifier *InvitationNotifier) InvitationDelivered(ctx context.Context, unit outbound.UnitOfWork, invitation domain.Invitation) error {
|
||||
const location = "InvitationNotifier.InvitationDelivered"
|
||||
if invitation.State() != domain.InvitationDelivered || !invitation.IsDelivered() {
|
||||
return fmt.Errorf("%s - the invitation must be delivered: %w", location, domain.ErrInvalidInvitation)
|
||||
}
|
||||
payload, err := json.Marshal(inboxEventPayload{RecipientUserID: invitation.CandidateID(), OrganizationID: invitation.OrganizationID()})
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s - encode payload: %w", location, err)
|
||||
}
|
||||
event, err := domain.NewDomainEvent(domain.DomainEventInput{
|
||||
EventType: string(domain.NotificationInboxInvitationDelivered),
|
||||
SchemaVersion: inboxPayloadSchemaVersion,
|
||||
AggregateType: invitationAggregateType,
|
||||
AggregateID: invitation.ID(),
|
||||
AggregateVersion: invitation.Version(),
|
||||
Payload: payload,
|
||||
})
|
||||
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)
|
||||
}
|
||||
if _, err := notifier.mail.RequestNotification(ctx, unit, NotificationRequest{
|
||||
RecipientUserID: invitation.CandidateID(),
|
||||
Notification: mailtemplate.NotificationData{
|
||||
Subject: invitationMailSubject, Body: invitationMailBody, LinkPath: candidateInvitationsPath,
|
||||
},
|
||||
}); err != nil {
|
||||
return fmt.Errorf("%s - request email: %w", location, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ResolveInvitationNotice resolves the notification of a delivered
|
||||
// invitation from the current invitation and the recipient's current
|
||||
// standing. The recipient must be the candidate it was delivered to, and the
|
||||
// organization is named only while the candidate has not hidden it;
|
||||
// otherwise the entry stays in the list without content.
|
||||
func ResolveInvitationNotice(ctx context.Context, unit outbound.UnitOfWork, entry domain.NotificationInboxEntry) (InboxContent, error) {
|
||||
const location = "notification.ResolveInvitationNotice"
|
||||
invitation, err := unit.Invitations().GetForCandidate(ctx, entry.RecipientUserID(), entry.SourceID())
|
||||
if errors.Is(err, outbound.ErrInvitationNotFound) {
|
||||
return InboxContent{}, ErrInboxContentUnavailable
|
||||
}
|
||||
if err != nil {
|
||||
return InboxContent{}, fmt.Errorf("%s - read invitation: %w", location, err)
|
||||
}
|
||||
if invitation.OrganizationID() != entry.OrganizationID() {
|
||||
return InboxContent{}, ErrInboxContentUnavailable
|
||||
}
|
||||
name, err := unit.NotificationInbox().OrganizationNameForRecipient(ctx, entry.RecipientUserID(), entry.OrganizationID())
|
||||
if errors.Is(err, outbound.ErrNotificationInboxSourceNotVisible) {
|
||||
return InboxContent{}, ErrInboxContentUnavailable
|
||||
}
|
||||
if err != nil {
|
||||
return InboxContent{}, fmt.Errorf("%s - read sender organization: %w", location, err)
|
||||
}
|
||||
content, err := NewInboxContent(invitationNoticeTitle,
|
||||
fmt.Sprintf(invitationNoticeSummary, name, invitation.Snapshot().Terms.RoleTitle), candidateInvitationsPath)
|
||||
if err != nil {
|
||||
// A stored name or role that does not fit the presentation is not
|
||||
// shown.
|
||||
return InboxContent{}, ErrInboxContentUnavailable
|
||||
}
|
||||
return content, nil
|
||||
}
|
||||
Reference in new issue
Block a user