-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the phone field into the contact
- Loading branch information
Showing
22 changed files
with
2,370 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
|
||
# MDBoostrap upgrade protocol | ||
|
||
Before uploading a new version of MDboostrap some manual changes needs to be done. | ||
|
||
### Change the flags source file | ||
|
||
You need to run the following command: | ||
|
||
```bash | ||
sed -i 's#https://mdbootstrap.com/img/svg/flags.png#/assets/images/svg/flags.png#g' ./assets/public/css/mdb.min.css | ||
``` | ||
|
||
The command above will force the framework to fetch the flags.png assets from our server instead of the mdboostrap.com server. |
Large diffs are not rendered by default.
Oops, something went wrong.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
DROP TABLE IF EXISTS phones; | ||
|
||
DROP INDEX IF EXISTS idx_phones_id; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
CREATE TABLE IF NOT EXISTS phones ( | ||
"id" TEXT NOT NULL, | ||
"type" TEXT NOT NULL, | ||
"iso2_region_code" TEXT NOT NULL, | ||
"international_formatted" TEXT NOT NULL, | ||
"national_formatted" TEXT NOT NULL, | ||
"normalized" TEXT NOT NULL, | ||
"contact_id" TEXT NOT NULL, | ||
"created_at" TEXT NOT NULL, | ||
FOREIGN KEY(contact_id) REFERENCES contacts(id) ON UPDATE RESTRICT ON DELETE CASCADE | ||
) STRICT; | ||
|
||
CREATE UNIQUE INDEX IF NOT EXISTS idx_phones_id ON phones(id); | ||
CREATE INDEX IF NOT EXISTS idx_phones_international_id ON phones(international_formatted); | ||
CREATE INDEX IF NOT EXISTS idx_phones_national_id ON phones(national_formatted); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package phonenumbers | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/Peltoche/gnocchi/internal/service/contacts" | ||
"github.com/Peltoche/gnocchi/internal/tools" | ||
"github.com/Peltoche/gnocchi/internal/tools/sqlstorage" | ||
) | ||
|
||
//go:generate mockery --name Service | ||
type Service interface { | ||
Create(ctx context.Context, cmd *CreateCmd) (*Phone, error) | ||
GetAllForContact(ctx context.Context, contact *contacts.Contact, cmd *sqlstorage.PaginateCmd) ([]Phone, error) | ||
} | ||
|
||
func Init( | ||
tools tools.Tools, | ||
db sqlstorage.Querier, | ||
) Service { | ||
store := newSqlStorage(db) | ||
|
||
return newService(tools, store) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package phonenumbers | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/Peltoche/gnocchi/internal/service/contacts" | ||
"github.com/Peltoche/gnocchi/internal/tools/uuid" | ||
) | ||
|
||
type Phone struct { | ||
createdAt time.Time | ||
id uuid.UUID | ||
phoneType string | ||
internationalFormatted string | ||
nationalFormatted string | ||
normalized string | ||
contactID uuid.UUID | ||
iso2RegionCode string | ||
} | ||
|
||
func (p Phone) ID() uuid.UUID { return p.id } | ||
func (p Phone) Type() string { return p.phoneType } | ||
func (p Phone) InternationalFormatted() string { return p.internationalFormatted } | ||
func (p Phone) NationalFormatted() string { return p.nationalFormatted } | ||
func (p Phone) ISO2RegionCode() string { return p.iso2RegionCode } | ||
func (p Phone) ContactID() uuid.UUID { return p.contactID } | ||
func (p Phone) CreatedAt() time.Time { return p.createdAt } | ||
|
||
type CreateCmd struct { | ||
Contact *contacts.Contact | ||
Type string | ||
Region string | ||
Input string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package phonenumbers | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/Peltoche/gnocchi/internal/service/contacts" | ||
"github.com/Peltoche/gnocchi/internal/tools/uuid" | ||
"github.com/brianvoe/gofakeit/v7" | ||
"github.com/nyaruka/phonenumbers" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
type FakePhoneBuilder struct { | ||
t testing.TB | ||
phone *Phone | ||
} | ||
|
||
func NewFakePhone(t testing.TB) *FakePhoneBuilder { | ||
t.Helper() | ||
|
||
uuidProvider := uuid.NewProvider() | ||
createdAt := gofakeit.DateRange(time.Now().Add(-time.Hour*1000), time.Now()) | ||
|
||
iso2RegionCode := "US" | ||
|
||
num, err := phonenumbers.Parse(gofakeit.Phone(), iso2RegionCode) | ||
require.NoError(t, err) | ||
|
||
return &FakePhoneBuilder{ | ||
t: t, | ||
phone: &Phone{ | ||
id: uuidProvider.New(), | ||
phoneType: "Home", | ||
iso2RegionCode: iso2RegionCode, | ||
internationalFormatted: phonenumbers.Format(num, phonenumbers.INTERNATIONAL), | ||
nationalFormatted: phonenumbers.Format(num, phonenumbers.NATIONAL), | ||
contactID: uuidProvider.New(), | ||
createdAt: createdAt, | ||
}, | ||
} | ||
} | ||
|
||
func (f *FakePhoneBuilder) WithContact(contact *contacts.Contact) *FakePhoneBuilder { | ||
f.phone.contactID = contact.ID() | ||
|
||
return f | ||
} | ||
|
||
func (f *FakePhoneBuilder) Build() *Phone { | ||
return f.phone | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package phonenumbers | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_Phone_model_getters(t *testing.T) { | ||
phone := NewFakePhone(t).Build() | ||
|
||
assert.Equal(t, phone.id, phone.ID()) | ||
assert.Equal(t, phone.phoneType, phone.Type()) | ||
assert.Equal(t, phone.internationalFormatted, phone.InternationalFormatted()) | ||
assert.Equal(t, phone.nationalFormatted, phone.NationalFormatted()) | ||
assert.Equal(t, phone.iso2RegionCode, phone.ISO2RegionCode()) | ||
assert.Equal(t, phone.contactID, phone.ContactID()) | ||
assert.Equal(t, phone.createdAt, phone.CreatedAt()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package phonenumbers | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/Peltoche/gnocchi/internal/service/contacts" | ||
"github.com/Peltoche/gnocchi/internal/tools" | ||
"github.com/Peltoche/gnocchi/internal/tools/clock" | ||
"github.com/Peltoche/gnocchi/internal/tools/errs" | ||
"github.com/Peltoche/gnocchi/internal/tools/sqlstorage" | ||
"github.com/Peltoche/gnocchi/internal/tools/uuid" | ||
"github.com/nyaruka/phonenumbers" | ||
) | ||
|
||
//go:generate mockery --name storage | ||
type storage interface { | ||
Save(ctx context.Context, p *Phone) error | ||
GetAllForContact(ctx context.Context, contact *contacts.Contact, cmd *sqlstorage.PaginateCmd) ([]Phone, error) | ||
} | ||
|
||
type service struct { | ||
storage storage | ||
uuid uuid.Service | ||
clock clock.Clock | ||
} | ||
|
||
// newService create a new user service. | ||
func newService(tools tools.Tools, storage storage) *service { | ||
return &service{ | ||
storage: storage, | ||
uuid: tools.UUID(), | ||
clock: tools.Clock(), | ||
} | ||
} | ||
|
||
func (s *service) Create(ctx context.Context, cmd *CreateCmd) (*Phone, error) { | ||
number, err := phonenumbers.Parse(cmd.Input, strings.ToUpper(cmd.Region)) | ||
if err != nil { | ||
return nil, errs.Validation(fmt.Errorf("invalid phone number %q: %w", cmd.Input, err)) | ||
} | ||
|
||
internationalFormated := phonenumbers.Format(number, phonenumbers.INTERNATIONAL) | ||
|
||
phoneType := strings.TrimSpace(cmd.Type) | ||
if len(phoneType) == 0 { | ||
return nil, errs.Validation(errors.New("invalid/missing phone type")) | ||
} | ||
|
||
phone := Phone{ | ||
createdAt: s.clock.Now(), | ||
id: s.uuid.New(), | ||
phoneType: phoneType, | ||
internationalFormatted: internationalFormated, | ||
nationalFormatted: phonenumbers.Format(number, phonenumbers.NATIONAL), | ||
normalized: strings.NewReplacer(" ", "", "+", "").Replace(internationalFormated), | ||
contactID: cmd.Contact.ID(), | ||
iso2RegionCode: strings.ToUpper(cmd.Region), | ||
} | ||
|
||
err = s.storage.Save(ctx, &phone) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to save into storage: %w", err) | ||
} | ||
|
||
return &phone, nil | ||
} | ||
|
||
func (s *service) GetAllForContact(ctx context.Context, contact *contacts.Contact, cmd *sqlstorage.PaginateCmd) ([]Phone, error) { | ||
res, err := s.storage.GetAllForContact(ctx, contact, cmd) | ||
if errors.Is(err, errNotFound) { | ||
return nil, errs.NotFound(err) | ||
} | ||
|
||
if err != nil { | ||
return nil, errs.Internal(err) | ||
} | ||
|
||
return res, nil | ||
} |
Oops, something went wrong.