Auto-sync permit competitors in monitoring PF
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 35s

This commit is contained in:
Grendgi
2026-06-05 12:09:51 +03:00
parent 76975c3c6c
commit 6966e6810c
7 changed files with 333 additions and 44 deletions

View File

@@ -52,20 +52,22 @@ type Project struct {
}
type Listing struct {
ID int64 `json:"id"`
ProjectID int64 `json:"project_id"`
Source string `json:"source"`
ExternalID string `json:"external_id"`
URL string `json:"url"`
Title *string `json:"title"`
AgentName *string `json:"agent_name"`
AgencyName *string `json:"agency_name"`
CurrentPrice *float64 `json:"current_price"`
Currency *string `json:"currency"`
Status string `json:"status"`
FirstSeenAt *string `json:"first_seen_at"`
LastSeenAt *string `json:"last_seen_at"`
PriceHistory []PricePoint `json:"price_history,omitempty"`
ID int64 `json:"id"`
ProjectID int64 `json:"project_id"`
Source string `json:"source"`
ExternalID string `json:"external_id"`
URL string `json:"url"`
Title *string `json:"title"`
AgentName *string `json:"agent_name"`
AgencyName *string `json:"agency_name"`
PermitNumber *string `json:"permit_number"`
AutoDiscovered bool `json:"auto_discovered"`
CurrentPrice *float64 `json:"current_price"`
Currency *string `json:"currency"`
Status string `json:"status"`
FirstSeenAt *string `json:"first_seen_at"`
LastSeenAt *string `json:"last_seen_at"`
PriceHistory []PricePoint `json:"price_history,omitempty"`
}
type PricePoint struct {
@@ -157,6 +159,8 @@ func (a *App) InitDB(ctx context.Context) error {
title VARCHAR(500),
agent_name VARCHAR(300),
agency_name VARCHAR(300),
permit_number VARCHAR(100),
auto_discovered BOOLEAN NOT NULL DEFAULT 0,
current_price FLOAT,
currency VARCHAR(10),
status VARCHAR(7) NOT NULL,
@@ -178,7 +182,10 @@ func (a *App) InitDB(ctx context.Context) error {
return err
}
}
return a.migrateEmployees(ctx)
if err := a.migrateEmployees(ctx); err != nil {
return err
}
return a.migrateCompetitorListings(ctx)
}
func (a *App) migrateEmployees(ctx context.Context) error {
@@ -208,6 +215,37 @@ func (a *App) migrateEmployees(ctx context.Context) error {
return err
}
func (a *App) migrateCompetitorListings(ctx context.Context) error {
rows, err := a.DB.QueryContext(ctx, `PRAGMA table_info(competitor_listings)`)
if err != nil {
return err
}
defer rows.Close()
columns := map[string]bool{}
for rows.Next() {
var cid int
var name, typ string
var notNull int
var defaultValue any
var pk int
if err := rows.Scan(&cid, &name, &typ, &notNull, &defaultValue, &pk); err != nil {
return err
}
columns[name] = true
}
if !columns["permit_number"] {
if _, err := a.DB.ExecContext(ctx, `ALTER TABLE competitor_listings ADD COLUMN permit_number VARCHAR(100)`); err != nil {
return err
}
}
if !columns["auto_discovered"] {
if _, err := a.DB.ExecContext(ctx, `ALTER TABLE competitor_listings ADD COLUMN auto_discovered BOOLEAN NOT NULL DEFAULT 0`); err != nil {
return err
}
}
return nil
}
func cleanPtr(value *string) *string {
if value == nil {
return nil