2024-05-24 17:44:24 +00:00
|
|
|
package presenters
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/emochka2007/block-accounting/internal/interface/rest/domain"
|
|
|
|
"github.com/emochka2007/block-accounting/internal/interface/rest/domain/hal"
|
|
|
|
"github.com/emochka2007/block-accounting/internal/pkg/ctxmeta"
|
|
|
|
"github.com/emochka2007/block-accounting/internal/pkg/models"
|
2024-05-28 22:46:30 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2024-05-24 17:44:24 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ParticipantsPresenter interface {
|
|
|
|
ResponseListParticipants(
|
|
|
|
ctx context.Context,
|
|
|
|
participants []models.OrganizationParticipant,
|
|
|
|
) ([]byte, error)
|
2024-05-24 22:45:56 +00:00
|
|
|
ResponseParticipant(
|
|
|
|
ctx context.Context,
|
|
|
|
participant models.OrganizationParticipant,
|
|
|
|
) ([]byte, error)
|
2024-05-26 18:20:14 +00:00
|
|
|
ResponseParticipantsHal(
|
|
|
|
ctx context.Context,
|
|
|
|
participants []models.OrganizationParticipant,
|
|
|
|
) (*hal.Resource, error)
|
|
|
|
ResponseParticipantHal(
|
|
|
|
ctx context.Context,
|
|
|
|
participant models.OrganizationParticipant,
|
|
|
|
) (*hal.Resource, error)
|
2024-05-24 17:44:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type participantsPresenter struct{}
|
|
|
|
|
|
|
|
func NewParticipantsPresenter() ParticipantsPresenter {
|
|
|
|
return new(participantsPresenter)
|
|
|
|
}
|
|
|
|
|
2024-05-26 18:20:14 +00:00
|
|
|
func (p *participantsPresenter) ResponseParticipantHal(
|
2024-05-24 17:44:24 +00:00
|
|
|
ctx context.Context,
|
|
|
|
participant models.OrganizationParticipant,
|
|
|
|
) (*hal.Resource, error) {
|
|
|
|
domainParticipant := &domain.Participant{
|
|
|
|
ID: participant.Id().String(),
|
|
|
|
Name: participant.ParticipantName(),
|
|
|
|
Position: participant.Position(),
|
|
|
|
CreatedAt: participant.CreatedDate().UnixMilli(),
|
|
|
|
UpdatedAt: participant.UpdatedDate().UnixMilli(),
|
|
|
|
}
|
|
|
|
|
|
|
|
if !participant.DeletedDate().IsZero() {
|
|
|
|
domainParticipant.DeletedAt = participant.DeletedDate().UnixMilli()
|
|
|
|
}
|
|
|
|
|
|
|
|
if user := participant.GetUser(); user != nil {
|
|
|
|
if user.Credentails != nil {
|
|
|
|
domainParticipant.Credentials = &domain.UserParticipantCredentials{
|
|
|
|
Email: user.Credentails.Email,
|
|
|
|
Phone: user.Credentails.Phone,
|
|
|
|
Telegram: user.Credentails.Telegram,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-26 09:26:53 +00:00
|
|
|
domainParticipant.Position = user.Position()
|
2024-05-28 22:46:30 +00:00
|
|
|
domainParticipant.PublicKey = common.Bytes2Hex(user.PublicKey())
|
2024-05-24 17:44:24 +00:00
|
|
|
domainParticipant.IsUser = true
|
|
|
|
domainParticipant.IsAdmin = user.IsAdmin()
|
|
|
|
domainParticipant.IsOwner = user.IsOwner()
|
2024-05-25 20:38:47 +00:00
|
|
|
domainParticipant.IsActive = user.Activated
|
2024-05-24 17:44:24 +00:00
|
|
|
|
2024-05-26 09:26:53 +00:00
|
|
|
} else if employee := participant.GetEmployee(); employee != nil {
|
|
|
|
domainParticipant.Name = employee.EmployeeName
|
|
|
|
domainParticipant.Position = employee.Position()
|
2024-05-24 17:44:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
organizationID, err := ctxmeta.OrganizationId(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error fetch organization id from context. %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
r := hal.NewResource(
|
|
|
|
domainParticipant,
|
2024-05-26 09:26:53 +00:00
|
|
|
"/organizations/"+organizationID.String()+"/participants/"+domainParticipant.ID,
|
2024-05-24 17:44:24 +00:00
|
|
|
hal.WithType("participant"),
|
|
|
|
)
|
|
|
|
|
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|
2024-05-26 18:20:14 +00:00
|
|
|
func (p *participantsPresenter) ResponseParticipantsHal(
|
2024-05-24 17:44:24 +00:00
|
|
|
ctx context.Context,
|
|
|
|
participants []models.OrganizationParticipant,
|
|
|
|
) (*hal.Resource, error) {
|
|
|
|
resources := make([]*hal.Resource, len(participants))
|
|
|
|
|
|
|
|
for i, pt := range participants {
|
2024-05-26 18:20:14 +00:00
|
|
|
r, err := p.ResponseParticipantHal(ctx, pt)
|
2024-05-24 17:44:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error map participant to hal resource. %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resources[i] = r
|
|
|
|
}
|
|
|
|
|
|
|
|
organizationID, err := ctxmeta.OrganizationId(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error fetch organization id from context. %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
r := hal.NewResource(
|
|
|
|
map[string][]*hal.Resource{
|
|
|
|
"participants": resources,
|
|
|
|
},
|
|
|
|
"/organizations/"+organizationID.String()+"/participants",
|
|
|
|
hal.WithType("participants"),
|
|
|
|
)
|
|
|
|
|
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *participantsPresenter) ResponseListParticipants(
|
|
|
|
ctx context.Context,
|
|
|
|
participants []models.OrganizationParticipant,
|
|
|
|
) ([]byte, error) {
|
2024-05-26 18:20:14 +00:00
|
|
|
r, err := p.ResponseParticipantsHal(ctx, participants)
|
2024-05-24 17:44:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error map participants to hal. %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
out, err := json.Marshal(r)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error marshal organization create response. %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return out, nil
|
|
|
|
}
|
2024-05-24 22:45:56 +00:00
|
|
|
|
|
|
|
func (p *participantsPresenter) ResponseParticipant(
|
|
|
|
ctx context.Context,
|
|
|
|
participant models.OrganizationParticipant,
|
|
|
|
) ([]byte, error) {
|
2024-05-26 18:20:14 +00:00
|
|
|
r, err := p.ResponseParticipantHal(ctx, participant)
|
2024-05-24 22:45:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error map participant to hal resource. %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
out, err := json.Marshal(r)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error marshal organization create response. %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return out, nil
|
|
|
|
}
|