Files
lincodex/backend/internal/usecase/notification/invitation.go
T

123 lines
5.7 KiB
Go

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
}