feat: parse project metadata from PF links
This commit is contained in:
@@ -356,6 +356,10 @@ func (a *App) CreateProject(ctx context.Context, ownerID int64, p ProjectPayload
|
||||
}
|
||||
p.Title = title
|
||||
p.DealType = deal
|
||||
p, err = a.enrichProjectPayloadFromURL(ctx, p)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := validateProjectRequired(p); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -395,6 +399,10 @@ func (a *App) UpdateProject(ctx context.Context, ownerID, projectID int64, p Pro
|
||||
p = mergeProjectPayload(current, p)
|
||||
p.Title = title
|
||||
p.DealType = deal
|
||||
p, err = a.enrichProjectPayloadFromURL(ctx, p)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := validateProjectRequired(p); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -437,6 +445,51 @@ func mergeProjectPayload(current *Project, p ProjectPayload) ProjectPayload {
|
||||
return p
|
||||
}
|
||||
|
||||
func (a *App) enrichProjectPayloadFromURL(ctx context.Context, p ProjectPayload) (ProjectPayload, error) {
|
||||
url := cleanPtr(p.OurURL)
|
||||
if url == nil || a.Worker == nil {
|
||||
return p, nil
|
||||
}
|
||||
parsed, err := a.Worker.ParseOwnListing(ctx, *url)
|
||||
if err != nil {
|
||||
if projectMissingParsedFields(p) {
|
||||
return p, fmt.Errorf("parse our_url: %w", err)
|
||||
}
|
||||
return p, nil
|
||||
}
|
||||
return applyParsedOwnListing(p, parsed), nil
|
||||
}
|
||||
|
||||
func projectMissingParsedFields(p ProjectPayload) bool {
|
||||
return p.OurPrice == nil ||
|
||||
cleanPtr(p.DLDPermit) == nil ||
|
||||
cleanPtr(p.Building) == nil ||
|
||||
p.Bedrooms == nil ||
|
||||
p.SizeSqft == nil
|
||||
}
|
||||
|
||||
func applyParsedOwnListing(p ProjectPayload, parsed *ParsedOwnListing) ProjectPayload {
|
||||
if parsed == nil {
|
||||
return p
|
||||
}
|
||||
if parsed.OurPrice != nil && *parsed.OurPrice > 0 {
|
||||
p.OurPrice = parsed.OurPrice
|
||||
}
|
||||
if permit := cleanPtr(parsed.DLDPermit); permit != nil {
|
||||
p.DLDPermit = permit
|
||||
}
|
||||
if building := cleanPtr(parsed.Building); building != nil {
|
||||
p.Building = building
|
||||
}
|
||||
if parsed.Bedrooms != nil {
|
||||
p.Bedrooms = parsed.Bedrooms
|
||||
}
|
||||
if parsed.SizeSqft != nil && *parsed.SizeSqft > 0 {
|
||||
p.SizeSqft = parsed.SizeSqft
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
func validateProjectRequired(p ProjectPayload) error {
|
||||
if cleanString(p.Title) == "" {
|
||||
return fmt.Errorf("title is required")
|
||||
|
||||
Reference in New Issue
Block a user