Skip to main content

User Document Query Documentation

This code provides functions for querying and manipulating user document data in the database. It uses the PocketBase DAO for database interactions and includes functions for inserting, updating, and synchronizing user document records.

Functions

InsertUserDocument

func InsertUserDocument(app core.App, dao *daos.Dao, req *entity.UserDocumentTable) (*models.Record, error)

Inserts a new user document record into the database.

  • app: The PocketBase app instance.
  • dao: The PocketBase DAO.
  • req: A pointer to a UserDocumentTable struct containing the user document data to be inserted.
  • Returns: A pointer to a models.Record containing the inserted record, or an error if the operation fails.

UpdateUserDocument

func UpdateUserDocument(app core.App, dao *daos.Dao, req *entity.UserDocumentTable) (*models.Record, error)

Updates an existing user document record in the database.

  • app: The PocketBase app instance.
  • dao: The PocketBase DAO.
  • req: A pointer to a UserDocumentTable struct containing the updated user document data.
  • Returns: A pointer to a models.Record containing the updated record, or an error if the operation fails.

SyncUserDocuments

func SyncUserDocuments(app core.App, dao *daos.Dao, userProfileID string, req []*entity.UserDocumentTable) error

Synchronizes the user documents for a given user profile by deleting documents that are not in the request and inserting or updating the provided documents.

  • app: The PocketBase app instance.
  • dao: The PocketBase DAO.
  • userProfileID: The ID of the user profile.
  • req: A slice of pointers to UserDocumentTable structs containing the new user document data.
  • Returns: An error if the operation fails.

deleteUserDocumentsNotInRequest

func deleteUserDocumentsNotInRequest(dao *daos.Dao, currentUserDocs []models.Record, req []*entity.UserDocumentTable) error

Deletes user documents that are not in the request.

  • dao: The PocketBase DAO.
  • currentUserDocs: A slice of models.Record containing the current user documents.
  • req: A slice of pointers to UserDocumentTable structs containing the new user document data.
  • Returns: An error if the operation fails.

shouldDeleteDocument

func shouldDeleteDocument(currentDoc models.Record, req []*entity.UserDocumentTable) bool

Checks if the document should be deleted.

  • currentDoc: A models.Record containing the current user document.
  • req: A slice of pointers to UserDocumentTable structs containing the new user document data.
  • Returns: A boolean indicating whether the document should be deleted.

deleteDocument

func deleteDocument(dao *daos.Dao, doc models.Record) error

Deletes the document record and its associated media file if it exists.

  • dao: The PocketBase DAO.
  • doc: A models.Record containing the user document to be deleted.
  • Returns: An error if the operation fails.

insertOrUpdateUserDocument

func insertOrUpdateUserDocument(app core.App, dao *daos.Dao, doc *entity.UserDocumentTable) error

Inserts a new document or updates an existing document.

  • app: The PocketBase app instance.
  • dao: The PocketBase DAO.
  • doc: A pointer to a UserDocumentTable struct containing the user document data.
  • Returns: An error if the operation fails.

Example Usage

Initializing the DAO

Before using the functions, you need to initialize the PocketBase DAO.

package main

import (
"fio-backend/queries"
"github.com/pocketbase/pocketbase"
"github.com/pocketbase/pocketbase/daos"
)

func main() {
// Initialize PocketBase
pb := pocketbase.New()

// Initialize the DAO
dao := daos.New(pb.DB())
}

Inserting a User Document

package main

import (
"fio-backend/entity"
"fio-backend/queries"
"github.com/pocketbase/pocketbase"
"github.com/pocketbase/pocketbase/daos"
"github.com/pocketbase/pocketbase/core"
"fmt"
)

func main() {
// Initialize PocketBase
pb := pocketbase.New()

// Initialize the DAO
dao := daos.New(pb.DB())

// Create a new user document
newUserDocument := &entity.UserDocumentTable{
UserProfileID: "user123",
DocumentType: entity.UDT_GOV_ID,
DocumentNumber: "123456789",
FileBase64: "base64-encoded-file",
}

// Insert the new user document
record, err := queries.InsertUserDocument(pb, dao, newUserDocument)
if err != nil {
fmt.Println("Error:", err)
return
}

fmt.Println("Inserted User Document Record:", record)
}

Updating a User Document

package main

import (
"fio-backend/entity"
"fio-backend/queries"
"github.com/pocketbase/pocketbase"
"github.com/pocketbase/pocketbase/daos"
"github.com/pocketbase/pocketbase/core"
"fmt"
)

func main() {
// Initialize PocketBase
pb := pocketbase.New()

// Initialize the DAO
dao := daos.New(pb.DB())

// Retrieve the user document to update
userDocumentID := "document123"
userDocument, err := queries.GetUserDocumentByID(dao, userDocumentID)
if err != nil {
fmt.Println("Error:", err)
return
}

// Update the user document fields
userDocument.DocumentNumber = "987654321"
userDocument.FileBase64 = "new-base64-encoded-file"

// Update the user document in the database
updatedRecord, err := queries.UpdateUserDocument(pb, dao, userDocument)
if err != nil {
fmt.Println("Error:", err)
return
}

fmt.Println("Updated User Document Record:", updatedRecord)
}

Synchronizing User Documents

package main

import (
"fio-backend/entity"
"fio-backend/queries"
"github.com/pocketbase/pocketbase"
"github.com/pocketbase/pocketbase/daos"
"github.com/pocketbase/pocketbase/core"
"fmt"
)

func main() {
// Initialize PocketBase
pb := pocketbase.New()

// Initialize the DAO
dao := daos.New(pb.DB())

// Create new user document data
newUserDocuments := []*entity.UserDocumentTable{
{
UserProfileID: "user123",
DocumentType: entity.UDT_GOV_ID,
DocumentNumber: "123456789",
FileBase64: "base64-encoded-file",
},
{
UserProfileID: "user123",
DocumentType: entity.UDT_DRIVING_LICENSE,
DocumentNumber: "DL123456",
FileBase64: "base64-encoded-file",
},
}

// Synchronize user documents for a user profile
userProfileID := "user123"
err := queries.SyncUserDocuments(pb, dao, userProfileID, newUserDocuments)
if err != nil {
fmt.Println("Error:", err)
return
}

fmt.Println("User documents synchronized successfully")
}

This documentation provides an overview of the user document query functions, their parameters, and example usage. By following the examples, you can insert, update, and synchronize user document records in the database.