Fix/update correspondent to not fail (#202)

* Updated CONTRIBUTING.md as such that it runs the full package and not main.go

* Check if a correspondent is already created

* Added a script to ease running the backend quickly

* Changed run script to comply with suggestions by code rabbit
This commit is contained in:
Simon Pamies 2025-02-08 19:30:28 +01:00 committed by GitHub
parent 4e71f5f841
commit 7c7c593faa
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 37 additions and 10 deletions

View file

@ -91,7 +91,18 @@ We welcome pull requests (PRs). Please follow these guidelines:
4. **Run the backend server**:
```bash
go run main.go
mkdir dist
touch dist/index.html
go build
./paperless-gpt
```
5. **Run the backend server with frontend built in**:
```bash
cd web-app && npm install && npm run build && cp -r dist ..
go build
./paperless-gpt
```
### Frontend Setup

7
build-and-run.sh Executable file
View file

@ -0,0 +1,7 @@
#!/usr/bin/env bash
set -o allexport
source .env
set +o allexport
go build
./paperless-gpt

View file

@ -365,12 +365,12 @@ func (client *PaperlessClient) UpdateDocuments(ctx context.Context, documents []
updatedFields["correspondent"] = correspondentID
} else {
newCorrespondent := instantiateCorrespondent(document.SuggestedCorrespondent)
newCorrespondentID, err := client.CreateCorrespondent(context.Background(), newCorrespondent)
newCorrespondentID, err := client.CreateOrGetCorrespondent(context.Background(), newCorrespondent)
if err != nil {
log.Errorf("Error creating correspondent with name %s: %v\n", document.SuggestedCorrespondent, err)
log.Errorf("Error creating/getting correspondent with name %s: %v\n", document.SuggestedCorrespondent, err)
return err
}
log.Infof("Created correspondent with name %s and ID %d\n", document.SuggestedCorrespondent, newCorrespondentID)
log.Infof("Using correspondent with name %s and ID %d\n", document.SuggestedCorrespondent, newCorrespondentID)
updatedFields["correspondent"] = newCorrespondentID
}
}
@ -612,17 +612,27 @@ func instantiateCorrespondent(name string) Correspondent {
}
}
// CreateCorrespondent creates a new correspondent in Paperless-NGX
func (client *PaperlessClient) CreateCorrespondent(ctx context.Context, correspondent Correspondent) (int, error) {
url := "api/correspondents/"
// CreateOrGetCorrespondent creates a new correspondent or returns existing one if name already exists
func (client *PaperlessClient) CreateOrGetCorrespondent(ctx context.Context, correspondent Correspondent) (int, error) {
// First try to find existing correspondent
correspondents, err := client.GetAllCorrespondents(ctx)
if err != nil {
return 0, fmt.Errorf("error fetching correspondents: %w", err)
}
// Marshal the correspondent data to JSON
// Check if correspondent already exists
if id, exists := correspondents[correspondent.Name]; exists {
log.Infof("Using existing correspondent with name %s and ID %d", correspondent.Name, id)
return id, nil
}
// If not found, create new correspondent
url := "api/correspondents/"
jsonData, err := json.Marshal(correspondent)
if err != nil {
return 0, err
}
// Send the POST request
resp, err := client.Do(ctx, "POST", url, bytes.NewBuffer(jsonData))
if err != nil {
return 0, err
@ -634,7 +644,6 @@ func (client *PaperlessClient) CreateCorrespondent(ctx context.Context, correspo
return 0, fmt.Errorf("error creating correspondent: %d, %s", resp.StatusCode, string(bodyBytes))
}
// Decode the response body to get the ID of the created correspondent
var createdCorrespondent struct {
ID int `json:"id"`
}