Skip to content

Commit

Permalink
Merge pull request #124 from drypa/feature/70-tickets-with-electro
Browse files Browse the repository at this point in the history
Feature/70 tickets with electro
  • Loading branch information
drypa authored Feb 9, 2024
2 parents 86f341d + 59b87eb commit 764aba9
Show file tree
Hide file tree
Showing 38 changed files with 1,136 additions and 363 deletions.
2 changes: 1 addition & 1 deletion backend/device/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (c *Controller) AddDeviceHandler(writer http.ResponseWriter, request *http.
SessionId: request.SessionId,
RefreshToken: request.RefreshToken,
}
err = c.service.Add(ctx, d)
err = c.service.Add(ctx, &d)
if err != nil {
onError(writer, err)
return
Expand Down
10 changes: 6 additions & 4 deletions backend/device/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package repository
import (
"context"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"receipt_collector/dispose"
"receipt_collector/nalogru/device"
Expand All @@ -12,18 +13,19 @@ type Repository struct {
m *mongo.Client
}

//NewRepository creates Repository.
// NewRepository creates Repository.
func NewRepository(m *mongo.Client) *Repository {
return &Repository{m: m}
}

func (r *Repository) Add(ctx context.Context, d device.Device) error {
func (r *Repository) Add(ctx context.Context, d *device.Device) error {
collection := r.getCollection()
_, err := collection.InsertOne(ctx, d)
document, err := collection.InsertOne(ctx, d)
d.Id = document.InsertedID.(primitive.ObjectID)
return err
}

//All returns all devices.
// All returns all devices.
func (r *Repository) All(ctx context.Context) ([]device.Device, error) {
collection := r.getCollection()
cursor, err := collection.Find(ctx, bson.D{})
Expand Down
83 changes: 72 additions & 11 deletions backend/device/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import (
"receipt_collector/nalogru/device"
)

//Service to manage devices.
// Service to manage devices.
type Service struct {
r *repository.Repository
devices []ForRent
}

//NewService creates instance of Service.
// NewService creates instance of Service.
func NewService(ctx context.Context, r *repository.Repository) (*Service, error) {
all, err := r.All(ctx)
if err != nil {
Expand All @@ -32,42 +32,74 @@ func NewService(ctx context.Context, r *repository.Repository) (*Service, error)
return s, nil
}

//Add adds new device.
func (s *Service) Add(ctx context.Context, d device.Device) error {
// Add adds new device.
func (s *Service) Add(ctx context.Context, d *device.Device) error {
for _, v := range s.devices {
if v.ClientSecret == d.ClientSecret {
return errors.New("that device already added")
}
}
d.Update = s.updateDeviceFunc(ctx, *d)

forRent := ForRent{
Device: *d,
IsRent: false,
}
s.devices = append(s.devices, forRent)
return s.r.Add(ctx, d)
}

//Count returns devices count.
// Count returns devices count.
func (s *Service) Count(ctx context.Context) (int, error) {
if ctx.Err() != nil {
return -1, ctx.Err()
}
return len(s.devices), nil
}

//Rent device.
// Rent any device.
func (s *Service) Rent(ctx context.Context) (*device.Device, error) {
if ctx.Err() != nil {
return nil, ctx.Err()
}
for _, v := range s.devices {
if v.IsRent == false {
v.IsRent = true
s.rent(&v)
return &v.Device, nil
}
}
return nil, errors.New("no available devices found")
}

func (s *Service) Update(ctx context.Context, device *device.Device) error {
// RentDevice rent concrete device.
func (s *Service) RentDevice(ctx context.Context, d *device.Device) error {
if ctx.Err() != nil {
return ctx.Err()
}
for _, v := range s.devices {
if device.Id == v.Id {
v.Device = *device
if v.Id == d.Id {
if v.IsRent {
return errors.New("device is already used")
} else {
s.rent(&v)
}
}
}
return errors.New("device not found")
}

func (s *Service) rent(v *ForRent) {
v.IsRent = true
}

func (s *Service) Update(ctx context.Context, device *device.Device, sessionId string, refreshToken string) error {
device.SessionId = sessionId
device.RefreshToken = refreshToken
return s.r.Update(ctx, device)
}

func (s *Service) Free(ctx context.Context, device *device.Device) error {
// Free release the rented device
func (s *Service) Free(_ context.Context, device *device.Device) error {
for _, v := range s.devices {
if device.Id == v.Id {
v.IsRent = false
Expand All @@ -76,3 +108,32 @@ func (s *Service) Free(ctx context.Context, device *device.Device) error {
}
return errors.New("device not found")
}

// All return all registered devices
func (s *Service) All(_ context.Context) []*device.Device {
res := make([]*device.Device, len(s.devices))
for i, d := range s.devices {
res[i] = &d.Device
}
return res
}

func (s *Service) GetByUserId(ctx context.Context, userId string) (*device.Device, error) {
devices, err := s.r.All(ctx)
if err != nil {
return nil, err
}
for _, d := range devices {
if d.UserId == userId {
d.Update = s.updateDeviceFunc(ctx, d)
return &d, nil
}
}
return nil, nil
}

func (s *Service) updateDeviceFunc(ctx context.Context, d device.Device) func(sessionId string, refreshToken string) error {
return func(sessionId string, refreshToken string) error {
return s.Update(ctx, &d, sessionId, refreshToken)
}
}
4 changes: 3 additions & 1 deletion backend/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ module receipt_collector
go 1.20

require (
github.com/drypa/ReceiptCollector/api/inside v0.0.0-20230405091352-65e1efb55137
github.com/drypa/ReceiptCollector/api/inside v0.0.0-20230819122429-dfb6fd2c5a01
github.com/go-co-op/gocron v1.28.2
github.com/goji/httpauth v0.0.0-20160601135302-2da839ab0f4d
github.com/google/uuid v1.3.0
github.com/gorilla/mux v1.8.0
Expand All @@ -24,6 +25,7 @@ require (
github.com/xdg-go/scram v1.1.1 // indirect
github.com/xdg-go/stringprep v1.0.3 // indirect
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect
go.uber.org/atomic v1.9.0 // indirect
golang.org/x/net v0.8.0 // indirect
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
golang.org/x/sys v0.6.0 // indirect
Expand Down
30 changes: 25 additions & 5 deletions backend/go.sum
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/drypa/ReceiptCollector/api/inside v0.0.0-20230405091352-65e1efb55137 h1:2gSdtAgn/1SzR3jIxQybz+G8hFlLLPgwBpwe5se5kf4=
github.com/drypa/ReceiptCollector/api/inside v0.0.0-20230405091352-65e1efb55137/go.mod h1:4ULoAoegdVc7I/NgQ6izbBbXhqheKj8JbANBQ3xfVN0=
github.com/drypa/ReceiptCollector/api/inside v0.0.0-20230819122429-dfb6fd2c5a01 h1:HCdDtLl5QNclGP45Q6dt9d5XEfA5OMf+WRdLUOqiJx4=
github.com/drypa/ReceiptCollector/api/inside v0.0.0-20230819122429-dfb6fd2c5a01/go.mod h1:4ULoAoegdVc7I/NgQ6izbBbXhqheKj8JbANBQ3xfVN0=
github.com/go-co-op/gocron v1.28.2 h1:H9oHUGH+9HZ5mAorbnzRjzXLf4poP+ctZdbtaKRYagc=
github.com/go-co-op/gocron v1.28.2/go.mod h1:39f6KNSGVOU1LO/ZOoZfcSxwlsJDQOKSu8erN0SH48Y=
github.com/goji/httpauth v0.0.0-20160601135302-2da839ab0f4d h1:lBXNCxVENCipq4D1Is42JVOP4eQjlB8TQ6H69Yx5J9Q=
github.com/goji/httpauth v0.0.0-20160601135302-2da839ab0f4d/go.mod h1:nnjvkQ9ptGaCkuDUx6wNykzzlUixGxvkme+H/lnzb+A=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
Expand All @@ -20,22 +23,35 @@ github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/klauspost/compress v1.13.6 h1:P76CopJELS0TiO2mebmnzgWaajssP/EszplttgQxcgc=
github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe h1:iruDEfMl2E6fbMZ9s0scYfZQ84/6SPL6zC8ACM2oIL0=
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/rogpeppe/go-internal v1.8.1 h1:geMPLpDpQOgVyCg5z5GoRwLHepNdb71NXb67XFkP+Eg=
github.com/rogpeppe/go-internal v1.8.1/go.mod h1:JeRgkft04UBgHMgCIwADu4Pn6Mtm5d4nPKWu0nJ5d+o=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
github.com/tidwall/pretty v1.0.2 h1:Z7S3cePv9Jwm1KwS0513MRaoUe3S01WPbLNV40pwWZU=
github.com/tidwall/pretty v1.0.2/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
Expand All @@ -49,6 +65,8 @@ github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d h1:splanxYIlg+5LfHAM
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d/go.mod h1:rHwXgn7JulP+udvsHwJoVG1YGAP6VLg4y9I5dyZdqmA=
go.mongodb.org/mongo-driver v1.11.3 h1:Ql6K6qYHEzB6xvu4+AU0BoRoqf9vFPcc4o7MUIdPW8Y=
go.mongodb.org/mongo-driver v1.11.3/go.mod h1:PTSz5yu21bkT/wXpkS7WR5f0ddqw5quethTUn9WM+2g=
go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.7.0 h1:AvwMYaRytfdeVt3u6mLaxYtErKYjxA2OXjJ1HHq6t3A=
golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU=
Expand Down Expand Up @@ -79,6 +97,8 @@ google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
2 changes: 2 additions & 0 deletions backend/internal/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ type AccountProcessor interface {
GetLoginLink(ctx context.Context, in *api.GetLoginLinkRequest) (*api.LoginLinkResponse, error)
GetUsers(ctx context.Context, req *api.NoParams) (*api.GetUsersResponse, error)
GetUser(ctx context.Context, in *api.GetUserRequest, opts ...grpc.CallOption) (*api.GetUserResponse, error)
RegisterUser(ctx context.Context, in *api.UserRegistrationRequest, opts ...grpc.CallOption) (*api.UserRegistrationResponse, error)
VerifyPhone(ctx context.Context, req *api.PhoneVerificationRequest) (*api.ErrorResponse, error)
}

// ReceiptProcessor is an interface for process receipt requests.
Expand Down
12 changes: 12 additions & 0 deletions backend/internal/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,15 @@ func (s *server) GetRawReceipt(ctx context.Context, in *api.GetRawReceiptReportR
processor := *(s.receiptProcessor)
return processor.GetRawReceipt(ctx, in)
}

// RegisterUser add new user and send registration by phone request to nalog api.
func (s *server) RegisterUser(ctx context.Context, req *api.UserRegistrationRequest) (*api.UserRegistrationResponse, error) {
processor := *(s.accountProcessor)
return processor.RegisterUser(ctx, req)
}

// VerifyPhone validate phone number through SMS.
func (s *server) VerifyPhone(ctx context.Context, req *api.PhoneVerificationRequest) (*api.ErrorResponse, error) {
processor := *(s.accountProcessor)
return processor.VerifyPhone(ctx, req)
}
22 changes: 8 additions & 14 deletions backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ var mongoUser = os.Getenv("MONGO_LOGIN")
var mongoSecret = os.Getenv("MONGO_SECRET")
var openUrl = os.Getenv("OPEN_URL")
var templatePath = os.Getenv("TEMPLATES_PATH")
var clientSecret = os.Getenv("CLIENT_SECRET")

func main() {
log.SetOutput(os.Stdout)
Expand All @@ -56,14 +57,7 @@ func main() {
log.Printf("Failed to create device service: %v\n", err)
return
}

d, err := deviceService.Rent(ctx)
if err != nil {
log.Println("Failed to rent device")
//return
}

nalogruClient := nalogru.NewClient(baseAddress, d)
nalogruClient := nalogru.NewClient(baseAddress)
receiptRepository := receipts.NewRepository(client)
userRepository := users.NewRepository(client)
marketRepository := markets.NewRepository(client)
Expand All @@ -80,23 +74,24 @@ func main() {
// }
//}()

go worker.GetReceiptStart(ctx, settings)
//go worker.GetReceiptStart(ctx, settings)
//go worker.UpdateRawReceiptStart(ctx, settings)
worker.GetElectronicReceiptStart(ctx)
generator := login_url.New(openUrl)

creds, err := credentials.NewServerTLSFromFile("/usr/share/receipts/ssl/certs/certificate.crt", "/usr/share/receipts/ssl/certs/private.key")
if err != nil {
log.Fatalf("failed to load TLS keys: %v", err)
}
var accountProcessor internal.AccountProcessor = login_url.NewProcessor(&userRepository, generator)
var accountProcessor internal.AccountProcessor = users.NewProcessor(&userRepository, generator, nalogruClient, deviceService, clientSecret)
r := render.New(templatePath)

var receiptProcessor internal.ReceiptProcessor = receipts.NewProcessor(&receiptRepository, r)

go internal.Serve(":15000", creds, &accountProcessor, &receiptProcessor)
go reports.Serve(":15001", creds, &userRepository, &receiptReportRepository)

server := startServer(nalogruClient, receiptRepository, userRepository, marketRepository, wasteRepository, deviceService)
server := startServer(receiptRepository, userRepository, marketRepository, wasteRepository, deviceService)

sigChan := make(chan os.Signal)
signal.Notify(sigChan, os.Kill)
Expand All @@ -120,16 +115,15 @@ func getMongoClient() (*mongo.Client, error) {
return mongo_client.New(settings)
}

func startServer(nalogruClient *nalogru.Client,
receiptRepository receipts.Repository,
func startServer(receiptRepository receipts.Repository,
userRepository users.Repository,
marketRepository markets.Repository,
wasteRepository waste.Repository,
devices nalogru.Devices) *http.Server {
marketsController := markets.New(marketRepository)
deviceController := controller.NewController(devices)

receiptsController := receipts.New(receiptRepository, nalogruClient)
receiptsController := receipts.New(receiptRepository)
usersController := users.New(userRepository)
wasteController := waste.New(wasteRepository)
basicAuth := auth.New(userRepository)
Expand Down
Loading

0 comments on commit 764aba9

Please sign in to comment.