mirror of
https://github.com/icereed/paperless-gpt.git
synced 2025-03-12 12:58:02 -05:00
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:
parent
4e71f5f841
commit
7c7c593faa
3 changed files with 37 additions and 10 deletions
|
@ -91,7 +91,18 @@ We welcome pull requests (PRs). Please follow these guidelines:
|
||||||
4. **Run the backend server**:
|
4. **Run the backend server**:
|
||||||
|
|
||||||
```bash
|
```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
|
### Frontend Setup
|
||||||
|
|
7
build-and-run.sh
Executable file
7
build-and-run.sh
Executable file
|
@ -0,0 +1,7 @@
|
||||||
|
#!/usr/bin/env bash
|
||||||
|
set -o allexport
|
||||||
|
source .env
|
||||||
|
set +o allexport
|
||||||
|
|
||||||
|
go build
|
||||||
|
./paperless-gpt
|
27
paperless.go
27
paperless.go
|
@ -365,12 +365,12 @@ func (client *PaperlessClient) UpdateDocuments(ctx context.Context, documents []
|
||||||
updatedFields["correspondent"] = correspondentID
|
updatedFields["correspondent"] = correspondentID
|
||||||
} else {
|
} else {
|
||||||
newCorrespondent := instantiateCorrespondent(document.SuggestedCorrespondent)
|
newCorrespondent := instantiateCorrespondent(document.SuggestedCorrespondent)
|
||||||
newCorrespondentID, err := client.CreateCorrespondent(context.Background(), newCorrespondent)
|
newCorrespondentID, err := client.CreateOrGetCorrespondent(context.Background(), newCorrespondent)
|
||||||
if err != nil {
|
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
|
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
|
updatedFields["correspondent"] = newCorrespondentID
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -612,17 +612,27 @@ func instantiateCorrespondent(name string) Correspondent {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateCorrespondent creates a new correspondent in Paperless-NGX
|
// CreateOrGetCorrespondent creates a new correspondent or returns existing one if name already exists
|
||||||
func (client *PaperlessClient) CreateCorrespondent(ctx context.Context, correspondent Correspondent) (int, error) {
|
func (client *PaperlessClient) CreateOrGetCorrespondent(ctx context.Context, correspondent Correspondent) (int, error) {
|
||||||
url := "api/correspondents/"
|
// 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)
|
jsonData, err := json.Marshal(correspondent)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send the POST request
|
|
||||||
resp, err := client.Do(ctx, "POST", url, bytes.NewBuffer(jsonData))
|
resp, err := client.Do(ctx, "POST", url, bytes.NewBuffer(jsonData))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
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))
|
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 {
|
var createdCorrespondent struct {
|
||||||
ID int `json:"id"`
|
ID int `json:"id"`
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue