2026-03-14 01:14:30 +00:00
|
|
|
package platform
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
|
|
|
|
"time"
|
|
|
|
|
|
2026-04-16 10:15:28 +00:00
|
|
|
"github.com/Gleipnir-Technology/bob"
|
2026-04-17 02:59:01 +00:00
|
|
|
"github.com/Gleipnir-Technology/bob/dialect/psql"
|
|
|
|
|
"github.com/Gleipnir-Technology/bob/dialect/psql/sm"
|
2026-03-14 01:14:30 +00:00
|
|
|
"github.com/Gleipnir-Technology/nidus-sync/db"
|
|
|
|
|
"github.com/Gleipnir-Technology/nidus-sync/db/enums"
|
|
|
|
|
"github.com/Gleipnir-Technology/nidus-sync/db/models"
|
2026-04-14 19:59:32 +00:00
|
|
|
"github.com/Gleipnir-Technology/nidus-sync/platform/types"
|
2026-03-14 01:14:30 +00:00
|
|
|
"github.com/aarondl/opt/omit"
|
|
|
|
|
"github.com/aarondl/opt/omitnull"
|
2026-04-16 10:47:13 +00:00
|
|
|
//"github.com/rs/zerolog/log"
|
2026-04-17 02:59:01 +00:00
|
|
|
"github.com/stephenafamo/scan"
|
2026-03-14 01:14:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Create a lead from the given signal and site
|
2026-04-14 19:59:32 +00:00
|
|
|
func LeadCreate(ctx context.Context, user User, signal_id int32, site_id int32, pool_location *types.Location) (*int32, error) {
|
2026-03-14 01:14:30 +00:00
|
|
|
txn, err := db.PGInstance.BobDB.BeginTx(ctx, nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("start transaction: %w", err)
|
|
|
|
|
}
|
2026-04-16 10:15:28 +00:00
|
|
|
defer txn.Rollback(ctx)
|
|
|
|
|
|
|
|
|
|
lead_id, err := leadCreate(ctx, txn, user, signal_id, site_id, pool_location)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("inner leadcreate: %w", err)
|
|
|
|
|
}
|
|
|
|
|
txn.Commit(ctx)
|
|
|
|
|
return lead_id, nil
|
|
|
|
|
}
|
2026-03-14 01:14:30 +00:00
|
|
|
|
2026-04-16 10:15:28 +00:00
|
|
|
func leadCreate(ctx context.Context, txn bob.Executor, user User, signal_id int32, site_id int32, pool_location *types.Location) (*int32, error) {
|
2026-03-14 01:14:30 +00:00
|
|
|
lead, err := models.Leads.Insert(&models.LeadSetter{
|
|
|
|
|
Created: omit.From(time.Now()),
|
|
|
|
|
Creator: omit.From(int32(user.ID)),
|
|
|
|
|
// ID
|
2026-03-22 01:22:44 +00:00
|
|
|
OrganizationID: omit.From(int32(user.Organization.ID)),
|
2026-03-14 01:14:30 +00:00
|
|
|
SiteID: omitnull.From(site_id),
|
|
|
|
|
Type: omit.From(enums.LeadtypeGreenPool),
|
|
|
|
|
}).One(ctx, txn)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("failed to create lead: %w", err)
|
|
|
|
|
}
|
|
|
|
|
return &lead.ID, nil
|
|
|
|
|
}
|
2026-04-17 21:40:04 +00:00
|
|
|
func leadsBySiteID(ctx context.Context, site_ids []int32) (map[int32][]*types.Lead, error) {
|
2026-04-17 02:59:01 +00:00
|
|
|
rows, err := bob.All(ctx, db.PGInstance.BobDB, psql.Select(
|
|
|
|
|
sm.Columns(
|
2026-04-17 19:43:40 +00:00
|
|
|
models.Leads.Columns.ID.As("id"),
|
|
|
|
|
models.Leads.Columns.SiteID.As("site_id"),
|
|
|
|
|
models.Leads.Columns.Type.As("type"),
|
|
|
|
|
),
|
|
|
|
|
sm.From(models.Leads.Name()),
|
2026-04-17 02:59:01 +00:00
|
|
|
sm.Where(
|
|
|
|
|
models.Leads.Columns.SiteID.EQ(psql.Any(site_ids)),
|
|
|
|
|
),
|
2026-04-17 21:40:04 +00:00
|
|
|
), scan.StructMapper[*types.Lead]())
|
2026-04-17 02:59:01 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("query leads: %w", err)
|
|
|
|
|
}
|
2026-04-17 20:51:07 +00:00
|
|
|
lead_ids := make([]int32, len(rows))
|
|
|
|
|
for i, row := range rows {
|
|
|
|
|
lead_ids[i] = row.ID
|
|
|
|
|
}
|
|
|
|
|
compliance_report_requests, err := ComplianceReportRequestByLeadID(ctx, lead_ids)
|
|
|
|
|
for _, row := range rows {
|
|
|
|
|
crrs, ok := compliance_report_requests[row.ID]
|
|
|
|
|
if !ok {
|
|
|
|
|
return nil, fmt.Errorf("impossible")
|
|
|
|
|
}
|
|
|
|
|
row.ComplianceReportRequests = crrs
|
|
|
|
|
}
|
2026-04-17 21:40:04 +00:00
|
|
|
results := make(map[int32][]*types.Lead, len(site_ids))
|
2026-04-17 02:59:01 +00:00
|
|
|
for _, site_id := range site_ids {
|
2026-04-17 21:40:04 +00:00
|
|
|
results[site_id] = make([]*types.Lead, 0)
|
2026-04-17 02:59:01 +00:00
|
|
|
}
|
|
|
|
|
for _, row := range rows {
|
|
|
|
|
leads, ok := results[row.SiteID]
|
|
|
|
|
if !ok {
|
|
|
|
|
return nil, fmt.Errorf("impossible")
|
|
|
|
|
}
|
|
|
|
|
leads = append(leads, row)
|
|
|
|
|
results[row.SiteID] = leads
|
|
|
|
|
}
|
|
|
|
|
return results, nil
|
|
|
|
|
}
|