220 lines
		
	
	
		
			6.1 KiB
		
	
	
	
		
			Go
		
	
	
		
			Executable File
		
	
	
			
		
		
	
	
			220 lines
		
	
	
		
			6.1 KiB
		
	
	
	
		
			Go
		
	
	
		
			Executable File
		
	
	
package application
 | 
						|
 | 
						|
import (
 | 
						|
	"context"
 | 
						|
	"encoding/json"
 | 
						|
	"fmt"
 | 
						|
	"sort"
 | 
						|
	"strings"
 | 
						|
	"time"
 | 
						|
 | 
						|
	groupsmanagement "git.coopgo.io/coopgo-platform/groups-management/grpcapi"
 | 
						|
	groupstorage "git.coopgo.io/coopgo-platform/groups-management/storage"
 | 
						|
	mobilityaccounts "git.coopgo.io/coopgo-platform/mobility-accounts/grpcapi"
 | 
						|
	mobilityaccountsstorage "git.coopgo.io/coopgo-platform/mobility-accounts/storage"
 | 
						|
	"github.com/google/uuid"
 | 
						|
	"github.com/rs/zerolog/log"
 | 
						|
	"google.golang.org/protobuf/types/known/structpb"
 | 
						|
)
 | 
						|
 | 
						|
type GroupsResult struct {
 | 
						|
	Groups []groupstorage.Group
 | 
						|
}
 | 
						|
 | 
						|
type CreateGroupModuleResult struct {
 | 
						|
	GroupID string
 | 
						|
}
 | 
						|
 | 
						|
type GroupModuleCreateDataResult struct {
 | 
						|
	GroupTypes []string
 | 
						|
}
 | 
						|
 | 
						|
type DisplayGroupModuleResult struct {
 | 
						|
	GroupID              string
 | 
						|
	Accounts             []any
 | 
						|
	CacheID              string
 | 
						|
	Searched             bool
 | 
						|
	Beneficiary          any
 | 
						|
	Group                groupstorage.Group
 | 
						|
	AccountsBeneficiaire []mobilityaccountsstorage.Account
 | 
						|
}
 | 
						|
 | 
						|
var Addres any
 | 
						|
 | 
						|
type BeneficiariesGroupForm struct {
 | 
						|
	FirstName   string     `json:"first_name" validate:"required"`
 | 
						|
	LastName    string     `json:"last_name" validate:"required"`
 | 
						|
	Email       string     `json:"email" validate:"required,email"`
 | 
						|
	Birthdate   *time.Time `json:"birthdate"`
 | 
						|
	PhoneNumber string     `json:"phone_number" validate:"required,phoneNumber"`
 | 
						|
	Address     any        `json:"address,omitempty"`
 | 
						|
	Gender      string     `json:"gender"`
 | 
						|
}
 | 
						|
 | 
						|
type GroupsModuleByName []groupstorage.Group
 | 
						|
 | 
						|
func (a GroupsModuleByName) Len() int { return len(a) }
 | 
						|
func (a GroupsModuleByName) Less(i, j int) bool {
 | 
						|
	return strings.Compare(a[i].Data["name"].(string), a[j].Data["name"].(string)) < 0
 | 
						|
}
 | 
						|
func (a GroupsModuleByName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
 | 
						|
 | 
						|
func (h *ApplicationHandler) GetGroups(ctx context.Context) (*GroupsResult, error) {
 | 
						|
	request := &groupsmanagement.GetGroupsRequest{
 | 
						|
		Namespaces: []string{"parcoursmob_groups"},
 | 
						|
	}
 | 
						|
 | 
						|
	resp, err := h.services.GRPC.GroupsManagement.GetGroups(ctx, request)
 | 
						|
	if err != nil {
 | 
						|
		return nil, fmt.Errorf("failed to get groups: %w", err)
 | 
						|
	}
 | 
						|
 | 
						|
	var groups = []groupstorage.Group{}
 | 
						|
 | 
						|
	for _, group := range resp.Groups {
 | 
						|
		g := group.ToStorageType()
 | 
						|
		groups = append(groups, g)
 | 
						|
	}
 | 
						|
 | 
						|
	sort.Sort(GroupsModuleByName(groups))
 | 
						|
 | 
						|
	return &GroupsResult{
 | 
						|
		Groups: groups,
 | 
						|
	}, nil
 | 
						|
}
 | 
						|
 | 
						|
func (h *ApplicationHandler) GetGroupModuleCreateData(ctx context.Context) (*GroupModuleCreateDataResult, error) {
 | 
						|
	groupTypes := h.config.GetStringSlice("modules.groups.group_types")
 | 
						|
	return &GroupModuleCreateDataResult{
 | 
						|
		GroupTypes: groupTypes,
 | 
						|
	}, nil
 | 
						|
}
 | 
						|
 | 
						|
func (h *ApplicationHandler) CreateGroupModule(ctx context.Context, name, groupType, description, address string) (*CreateGroupModuleResult, error) {
 | 
						|
	if name == "" {
 | 
						|
		return nil, fmt.Errorf("name is required")
 | 
						|
	}
 | 
						|
	if groupType == "" {
 | 
						|
		return nil, fmt.Errorf("type is required")
 | 
						|
	}
 | 
						|
 | 
						|
	var addressData any
 | 
						|
	if address != "" {
 | 
						|
		if err := json.Unmarshal([]byte(address), &addressData); err != nil {
 | 
						|
			return nil, fmt.Errorf("failed to parse address: %w", err)
 | 
						|
		}
 | 
						|
		Addres = addressData
 | 
						|
	}
 | 
						|
 | 
						|
	groupID := uuid.NewString()
 | 
						|
 | 
						|
	dataMap := map[string]any{
 | 
						|
		"name":        name,
 | 
						|
		"type":        groupType,
 | 
						|
		"description": description,
 | 
						|
		"address":     Addres,
 | 
						|
	}
 | 
						|
 | 
						|
	data, err := structpb.NewValue(dataMap)
 | 
						|
	if err != nil {
 | 
						|
		return nil, fmt.Errorf("failed to create data structure: %w", err)
 | 
						|
	}
 | 
						|
 | 
						|
	request := &groupsmanagement.AddGroupRequest{
 | 
						|
		Group: &groupsmanagement.Group{
 | 
						|
			Id:        groupID,
 | 
						|
			Namespace: "parcoursmob_groups",
 | 
						|
			Data:      data.GetStructValue(),
 | 
						|
		},
 | 
						|
	}
 | 
						|
 | 
						|
	_, err = h.services.GRPC.GroupsManagement.AddGroup(ctx, request)
 | 
						|
	if err != nil {
 | 
						|
		return nil, fmt.Errorf("failed to add group: %w", err)
 | 
						|
	}
 | 
						|
 | 
						|
	return &CreateGroupModuleResult{
 | 
						|
		GroupID: groupID,
 | 
						|
	}, nil
 | 
						|
}
 | 
						|
 | 
						|
func filterAccountBySearch(searchFilter string, a *mobilityaccounts.Account) bool {
 | 
						|
	if searchFilter != "" {
 | 
						|
		name := a.Data.AsMap()["first_name"].(string) + " " + a.Data.AsMap()["last_name"].(string)
 | 
						|
		if !strings.Contains(strings.ToLower(name), strings.ToLower(searchFilter)) {
 | 
						|
			return false
 | 
						|
		}
 | 
						|
	}
 | 
						|
	return true
 | 
						|
}
 | 
						|
func (h *ApplicationHandler) DisplayGroupModule(ctx context.Context, groupID string, searchFilter string, currentUserGroup groupstorage.Group) (*DisplayGroupModuleResult, error) {
 | 
						|
	request := &groupsmanagement.GetGroupRequest{
 | 
						|
		Id: groupID,
 | 
						|
	}
 | 
						|
 | 
						|
	resp, err := h.services.GRPC.GroupsManagement.GetGroup(ctx, request)
 | 
						|
	if err != nil {
 | 
						|
		return nil, fmt.Errorf("failed to get group: %w", err)
 | 
						|
	}
 | 
						|
 | 
						|
	var accounts = []any{}
 | 
						|
 | 
						|
	accountsRequest := &mobilityaccounts.GetAccountsBatchRequest{
 | 
						|
		Accountids: resp.Group.Members,
 | 
						|
	}
 | 
						|
 | 
						|
	accountsResp, err := h.services.GRPC.MobilityAccounts.GetAccountsBatch(ctx, accountsRequest)
 | 
						|
	if err != nil {
 | 
						|
		log.Warn().Err(err).Msg("failed to get accounts batch")
 | 
						|
	} else {
 | 
						|
		for _, account := range accountsResp.Accounts {
 | 
						|
			if filterAccountBySearch(searchFilter, account) {
 | 
						|
				a := account.ToStorageType()
 | 
						|
				accounts = append(accounts, a)
 | 
						|
			}
 | 
						|
		}
 | 
						|
	}
 | 
						|
 | 
						|
	cacheID := uuid.NewString()
 | 
						|
	h.cache.PutWithTTL(cacheID, accounts, 1*time.Hour)
 | 
						|
 | 
						|
	// Get beneficiaries in current user's group
 | 
						|
	accountsBeneficiaire, err := h.services.GetBeneficiariesInGroup(currentUserGroup)
 | 
						|
	if err != nil {
 | 
						|
		return nil, fmt.Errorf("failed to get beneficiaries in group: %w", err)
 | 
						|
	}
 | 
						|
 | 
						|
	return &DisplayGroupModuleResult{
 | 
						|
		GroupID:              resp.Group.ToStorageType().ID,
 | 
						|
		Accounts:             accounts,
 | 
						|
		CacheID:              cacheID,
 | 
						|
		Searched:             false,
 | 
						|
		Beneficiary:          nil,
 | 
						|
		Group:                resp.Group.ToStorageType(),
 | 
						|
		AccountsBeneficiaire: accountsBeneficiaire,
 | 
						|
	}, nil
 | 
						|
}
 | 
						|
 | 
						|
func (h *ApplicationHandler) SubscribeBeneficiaryToGroup(ctx context.Context, groupID string, beneficiaryID string) error {
 | 
						|
	beneficiaryRequest := &mobilityaccounts.GetAccountRequest{
 | 
						|
		Id: beneficiaryID,
 | 
						|
	}
 | 
						|
 | 
						|
	beneficiaryResp, err := h.services.GRPC.MobilityAccounts.GetAccount(ctx, beneficiaryRequest)
 | 
						|
	if err != nil {
 | 
						|
		return fmt.Errorf("failed to get beneficiary: %w", err)
 | 
						|
	}
 | 
						|
 | 
						|
	subscribe := &groupsmanagement.SubscribeRequest{
 | 
						|
		Groupid:  groupID,
 | 
						|
		Memberid: beneficiaryResp.Account.Id,
 | 
						|
	}
 | 
						|
 | 
						|
	_, err = h.services.GRPC.GroupsManagement.Subscribe(ctx, subscribe)
 | 
						|
	if err != nil {
 | 
						|
		return fmt.Errorf("failed to subscribe beneficiary to group: %w", err)
 | 
						|
	}
 | 
						|
 | 
						|
	return nil
 | 
						|
}
 |