Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Lead management #44

Merged
merged 5 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion auth-model
Submodule auth-model updated 3 files
+6 −5 common.proto
+138 −0 lead.proto
+1 −1 profile.proto
10 changes: 10 additions & 0 deletions db/auth_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type AuthDbInterface interface {
Profile(tenant string) ProfileRepositoryInterface
Tenant() TenantRepositoryInterface
ProfileMaster(tenant string) ProfileMasterRepositoryInterface
Lead(tenant string) LeadRepositoryInterface
}

type AuthDb struct{}
Expand Down Expand Up @@ -47,3 +48,12 @@ func (a *AuthDb) ProfileMaster(tenant string) ProfileMasterRepositoryInterface {

return &ProfileMasterRepository{baseRepo}
}

func (a *AuthDb) Lead(tenant string) LeadRepositoryInterface {
baseRepo := odm.UnimplementedBootRepository[models.LeadModel]{
Database: tenant + "_auth",
CollectionName: "lead",
}

return &LeadRepository{baseRepo}
}
147 changes: 147 additions & 0 deletions db/lead_repository.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
package db

import (
pb "github.com/Kotlang/authGo/generated"
"github.com/Kotlang/authGo/models"
"github.com/SaiNageswarS/go-api-boot/logger"
"github.com/SaiNageswarS/go-api-boot/odm"
"go.mongodb.org/mongo-driver/bson"
"go.uber.org/zap"
)

type LeadRepositoryInterface interface {
odm.BootRepository[models.LeadModel]
FindByIds(ids []string) (chan []models.LeadModel, chan error)
GetLeads(leadFilters *pb.LeadFilters, PageSize, PageNumber int64) (leads []models.LeadModel, totalCount int)
}

type LeadRepository struct {
odm.UnimplementedBootRepository[models.LeadModel]
}

func (l *LeadRepository) FindByIds(ids []string) (chan []models.LeadModel, chan error) {
filter := bson.M{
"_id": bson.M{
"$in": ids,
},
}
return l.Find(filter, nil, 0, 0)
}

func (l *LeadRepository) GetLeads(leadFilters *pb.LeadFilters, PageSize, PageNumber int64) (leads []models.LeadModel, totalCount int) {

// get the filter
filter := getLeadFilter(leadFilters)

// get the leads and total count
skip := PageNumber * PageSize
resultChan, errChan := l.Find(filter, nil, PageSize, skip)
totalCountResChan, countErrChan := l.CountDocuments(filter)
totalCount = 0

select {
case count := <-totalCountResChan:
totalCount = int(count)
case err := <-countErrChan:
logger.Error("Error fetching lead count", zap.Error(err))
}

select {
case res := <-resultChan:
leads = res
case err := <-errChan:
logger.Error("Error fetching leads", zap.Error(err))
}

return leads, totalCount
}

func getLeadFilter(leadFilters *pb.LeadFilters) bson.M {

if leadFilters == nil {
return bson.M{}
}

filter := bson.M{}

// if operator type is not unspecified then add it to filter
if leadFilters.OperatorType != pb.OperatorType_UNSPECIFIED_OPERATOR {
filter["operatorType"] = leadFilters.OperatorType.String()
}

// if channel is not unspecified then add it to filter
if leadFilters.Channel != pb.LeadChannel_UNSPECIFIED_CHANNEL {
filter["channel"] = leadFilters.Channel.String()
}

if leadFilters.Source != "" {
filter["source"] = leadFilters.Source
}

if leadFilters.LandSizeInAcres != pb.LandSizeInAcres_UnspecifiedLandSize {
filter["landSizeInAcres"] = leadFilters.LandSizeInAcres.String()
}

if leadFilters.FarmingType != pb.FarmingType_UnspecifiedFarming {
filter["farmingType"] = leadFilters.FarmingType.String()
}

// if certification details are not nil then add it to filter if not empty
if leadFilters.CertificationDetails != nil {

filter["certificationDetails.isCertified"] = leadFilters.CertificationDetails.IsCertified

if leadFilters.CertificationDetails.CertificationAgency != "" {
filter["certificationDetails.certificationAgency"] = leadFilters.CertificationDetails.CertificationAgency
}
if leadFilters.CertificationDetails.CertificationName != "" {
filter["certificationDetails.certificationName"] = leadFilters.CertificationDetails.CertificationName
}
}

if leadFilters.MainProfession != "" {
filter["mainProfession"] = leadFilters.MainProfession
}

if leadFilters.OrganizationName != "" {
filter["organizationName"] = leadFilters.OrganizationName
}

if leadFilters.SideProfession != "" {
filter["sideProfession"] = leadFilters.SideProfession
}

if leadFilters.Education != "" {
filter["education"] = leadFilters.Education
}

if leadFilters.Status != pb.Status_UNSPECIFIED_STATUS {
filter["status"] = leadFilters.Status.String()
}

if leadFilters.AddressFilters != nil {
addressFilters := getAddressFilter(leadFilters.AddressFilters)

if len(addressFilters) > 0 {
filter["addresses"] = bson.M{
"$elemMatch": addressFilters,
}
}
}

return filter
}

func getAddressFilter(addressfilter *pb.AddressFilters) bson.M {
filter := bson.M{}
if addressfilter.City != "" {
filter["city"] = addressfilter.City
}
if addressfilter.State != "" {
filter["state"] = addressfilter.State
}
if addressfilter.Country != "" {
filter["country"] = addressfilter.Country
}
return filter
}
2 changes: 2 additions & 0 deletions inject.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type Inject struct {
LoginVerifiedService *service.LoginVerifiedService
ProfileService *service.ProfileService
ProfileMasterService *service.ProfileMasterService
LeadService *service.LeadService
UnaryInterceptors []grpc.UnaryServerInterceptor
StreamInterceptors []grpc.StreamServerInterceptor
}
Expand All @@ -35,6 +36,7 @@ func NewInject() *Inject {
inj.LoginVerifiedService = service.NewLoginVerifiedService(inj.AuthDb)
inj.ProfileService = service.NewProfileService(inj.AuthDb, inj.CloudFns)
inj.ProfileMasterService = service.NewProfileMasterService(inj.AuthDb)
inj.LeadService = service.NewLeadService(inj.AuthDb)

return inj
}
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func main() {
pb.RegisterLoginVerifiedServer(bootServer.GrpcServer, inject.LoginVerifiedService)
pb.RegisterProfileServer(bootServer.GrpcServer, inject.ProfileService)
pb.RegisterProfileMasterServer(bootServer.GrpcServer, inject.ProfileMasterService)
pb.RegisterLeadServiceServer(bootServer.GrpcServer, inject.LeadService)

bootServer.Start(grpcPort, webPort)
}
30 changes: 30 additions & 0 deletions models/lead_model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package models

import "github.com/google/uuid"

type LeadModel struct {
LeadId string `bson:"_id"`
Name string `bson:"name"`
PhoneNumber string `bson:"phoneNumber"`
OperatorType string `bson:"operatorType"`
Channel string `bson:"channel"`
Source string `bson:"source"`
Addresses []Addresses `bson:"addresses"`
LandSizeInAcres string `bson:"landSizeInAcres"`
FarmingType string `bson:"farmingType"`
CertificationDetails CertificateModel `bson:"certificationDetails"`
Crops []string `bson:"crops"`
MainProfession string `bson:"mainProfession"`
OrganizationName string `bson:"organizationName"`
SideProfession string `bson:"sideProfession"`
UserInterviewNotes string `bson:"userInterviewNotes"`
Education string `bson:"education"`
}

func (m *LeadModel) Id() string {
if m.LeadId == "" {
m.LeadId = uuid.New().String()
}

return m.LeadId
}
31 changes: 16 additions & 15 deletions models/profile_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type Location struct {
}

type Addresses struct {
Type string `bson:"type" json:"type"`
Address string `bson:"address" json:"address"`
City string `bson:"city" json:"city"`
State string `bson:"state" json:"state"`
Expand All @@ -25,21 +26,21 @@ type DeletionInfo struct {
}

type ProfileModel struct {
UserId string `bson:"_id" json:"userId"`
Name string `bson:"name,omitempty" json:"name"`
PhotoUrl string `bson:"photoUrl" json:"photoUrl"`
Addresses map[string]Addresses `bson:"addresses" json:"addresses"`
Location Location `bson:"location" json:"location"`
FarmingType string `bson:"farmingType" json:"farmingType"`
Bio string `bson:"bio" json:"bio"`
Crops []string `bson:"crops" json:"crops"`
YearsSinceOrganicFarming int `bson:"yearsSinceOrganicFarming" json:"yearsSinceOrganicFarming"`
Gender string `bson:"gender" json:"gender" copier:"-"`
IsVerified bool `bson:"isVerified" json:"isVerified"`
PreferredLanguage string `bson:"preferredLanguage" json:"preferredLanguage"`
CertificationDetails CertificateModel `bson:"certificationDetails" json:"certificationDetails"`
CreatedOn int64 `bson:"createdOn,omitempty" json:"createdOn"`
LandSizeInAcres string `bson:"landSizeInAcres" json:"landSizeInAcres"`
UserId string `bson:"_id" json:"userId"`
Name string `bson:"name,omitempty" json:"name"`
PhotoUrl string `bson:"photoUrl" json:"photoUrl"`
Addresses []Addresses `bson:"addresses" json:"addresses"`
Location Location `bson:"location" json:"location"`
FarmingType string `bson:"farmingType" json:"farmingType"`
Bio string `bson:"bio" json:"bio"`
Crops []string `bson:"crops" json:"crops"`
YearsSinceOrganicFarming int `bson:"yearsSinceOrganicFarming" json:"yearsSinceOrganicFarming"`
Gender string `bson:"gender" json:"gender" copier:"-"`
IsVerified bool `bson:"isVerified" json:"isVerified"`
PreferredLanguage string `bson:"preferredLanguage" json:"preferredLanguage"`
CertificationDetails CertificateModel `bson:"certificationDetails" json:"certificationDetails"`
CreatedOn int64 `bson:"createdOn,omitempty" json:"createdOn"`
LandSizeInAcres string `bson:"landSizeInAcres" json:"landSizeInAcres"`
}

func (m *ProfileModel) Id() string {
Expand Down
Loading
Loading