Files

84 lines
3.6 KiB
Go

package notification
import (
"context"
"errors"
"fmt"
"lincodex/backend/internal/domain"
"lincodex/backend/internal/port/outbound"
)
// InboxSourceResolver resolves the content of the entries of one source
// type from the current state of that source, for the entry's recipient. It
// returns ErrInboxContentUnavailable when the source is gone or the recipient
// may not see it now.
type InboxSourceResolver func(ctx context.Context, unit outbound.UnitOfWork, entry domain.NotificationInboxEntry) (InboxContent, error)
// sourceContentResolver dispatches by source type. A source type without a
// resolver is unavailable, never guessed, so a new event type shows nothing
// until its resolver is registered.
type sourceContentResolver map[domain.NotificationInboxSourceType]InboxSourceResolver
var _ InboxContentResolver = sourceContentResolver(nil)
// NewInboxContentResolver combines the resolvers of the source types.
func NewInboxContentResolver(resolvers map[domain.NotificationInboxSourceType]InboxSourceResolver) InboxContentResolver {
return sourceContentResolver(resolvers)
}
// DefaultInboxSourceResolvers lists the source types that have a resolver
// 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{
domain.NotificationInboxSourceInvitation: ResolveInvitationNotice,
}
if syntheticEnabled {
resolvers[domain.NotificationInboxSourceOrganization] = ResolveSyntheticNotice
}
return resolvers
}
func (resolvers sourceContentResolver) Resolve(
ctx context.Context,
unit outbound.UnitOfWork,
entry domain.NotificationInboxEntry,
) (InboxContent, error) {
resolve, ok := resolvers[entry.SourceType()]
if !ok {
return InboxContent{}, ErrInboxContentUnavailable
}
return resolve(ctx, unit, entry)
}
const (
syntheticNoticeTitle = "Тестовое уведомление"
// syntheticNoticeSummary has one verb slot for the organization name.
syntheticNoticeSummary = "Организация «%s» отправила проверочное уведомление платформы. Оно создано для проверки доставки и не требует действий."
)
// ResolveSyntheticNotice resolves a synthetic notice. The text is fixed and
// names the sending organization by its current name, which is read under the
// recipient's current standing: a candidate sees it until they hide the
// organization, an employer while they are an active member. Afterwards the
// entry stays in the list without content.
func ResolveSyntheticNotice(ctx context.Context, unit outbound.UnitOfWork, entry domain.NotificationInboxEntry) (InboxContent, error) {
const location = "notification.ResolveSyntheticNotice"
name, err := unit.NotificationInbox().OrganizationNameForRecipient(ctx, entry.RecipientUserID(), entry.SourceID())
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(syntheticNoticeTitle, fmt.Sprintf(syntheticNoticeSummary, name), "")
if err != nil {
// A stored name that does not fit the presentation is not shown.
return InboxContent{}, ErrInboxContentUnavailable
}
return content, nil
}