File size: 2,875 Bytes
83a59ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package requests

import (
	"encoding/json"
	"strings"
	"time"

	"github.com/NdoleStudio/httpsms/pkg/entities"
	"github.com/google/uuid"
	"github.com/lib/pq"
)

// ContactItem is a single contact in a create request.
type ContactItem struct {
	Name         string            `json:"name" example:"Alice Smith"`
	Emails       []string          `json:"emails,omitempty"`
	PhoneNumbers []string          `json:"phone_numbers"`
	Properties   map[string]string `json:"properties,omitempty"`
}

// ContactStoreRequest creates one or many contacts.
type ContactStoreRequest struct {
	request
	Contacts []ContactItem `json:"contacts"`
}

// UnmarshalJSON accepts either a JSON array of contacts or {"contacts":[...]}.
func (input *ContactStoreRequest) UnmarshalJSON(data []byte) error {
	trimmed := strings.TrimSpace(string(data))
	if strings.HasPrefix(trimmed, "[") {
		var items []ContactItem
		if err := json.Unmarshal(data, &items); err != nil {
			return err
		}
		input.Contacts = items
		return nil
	}

	type alias ContactStoreRequest
	var wrapper alias
	if err := json.Unmarshal(data, &wrapper); err != nil {
		return err
	}

	input.Contacts = wrapper.Contacts
	return nil
}

// Sanitize trims and normalizes each contact item.
func (input ContactStoreRequest) Sanitize() ContactStoreRequest {
	for index := range input.Contacts {
		input.Contacts[index] = SanitizeContactItem(input.Contacts[index])
	}
	return input
}

// ToContacts converts the request into persistable entities.Contact records.
func (input *ContactStoreRequest) ToContacts(userID entities.UserID) []*entities.Contact {
	now := time.Now().UTC()
	contacts := make([]*entities.Contact, 0, len(input.Contacts))
	for _, item := range input.Contacts {
		properties := item.Properties
		if properties == nil {
			properties = map[string]string{}
		}

		contacts = append(contacts, &entities.Contact{
			ID:           uuid.New(),
			UserID:       userID,
			Name:         item.Name,
			Emails:       pq.StringArray(item.Emails),
			PhoneNumbers: pq.StringArray(item.PhoneNumbers),
			Properties:   entities.ContactProperties(properties),
			CreatedAt:    now,
			UpdatedAt:    now,
		})
	}
	return contacts
}

// SanitizeContactItem trims and normalizes a single contact item so the CSV
// upload and JSON create paths accept and normalize identical values (e.g. a
// phone number without a leading "+" or an email with mixed case/whitespace).
func SanitizeContactItem(item ContactItem) ContactItem {
	item.Name = strings.TrimSpace(item.Name)
	item.Emails = sanitizeUniqueStrings(item.Emails, func(value string) string {
		return strings.ToLower(strings.TrimSpace(value))
	})
	item.PhoneNumbers = sanitizeUniqueStrings(item.PhoneNumbers, func(value string) string {
		var base request
		return base.sanitizeAddress(value)
	})
	if item.Properties == nil {
		item.Properties = map[string]string{}
	}
	return item
}