Context module for managing templates and documents via Google Drive.
Google Drive is the single source of truth for file content. This module mirrors file metadata (name, google_doc_id, status, thumbnails, variables) to the local database for fast listing and audit tracking.
API layers
This module provides combined operations (Drive + DB). For direct access:
- Drive-only — Use
PhoenixKitDocumentCreator.GoogleDocsClientfor raw Google Drive/Docs API calls (create files, list folders, export PDF, move files) without touching the local database. - DB-only — Use
list_templates_from_db/0,list_documents_from_db/0,load_cached_thumbnails/1,persist_thumbnail/2for local DB queries. - Combined — Use
create_template/2,create_document/2,sync_from_drive/0,delete_template/2, etc. which coordinate between Drive and DB.
Summary
Functions
Create a blank document in the documents folder. Returns {:ok, %{doc_id, name, url}}.
Create a document from a template by copying and filling variables.
Create a blank template in the templates folder. Returns {:ok, %{doc_id, name, url}}.
Move a document to the deleted/documents folder.
Move a template to the deleted/templates folder.
Detect {{ variables }} in a Google Doc's text content.
Get the Google Drive URL for the documents folder.
Export a Google Doc to PDF. Returns {:ok, pdf_binary}.
Fetch thumbnails for a list of Drive files asynchronously.
Get the folder IDs (auto-discovers if not cached).
List documents from the local DB. Returns maps compatible with the LiveView.
List templates from the local DB. Returns maps compatible with the LiveView.
Load cached thumbnails from DB for a list of google_doc_ids.
Log a manual user action to the activity feed.
Move a file into the managed documents folder and classify it as a document.
Move a file into the managed templates folder and classify it as a template.
Persist a thumbnail data URI to the DB by google_doc_id.
Re-discover folder IDs from Drive.
Persist the file's current parent folder as its accepted location.
Sync local DB with Google Drive.
Get the Google Drive URL for the templates folder.
Upsert a document record from a Google Drive file map.
Upsert a template record from a Google Drive file map.
Functions
Create a blank document in the documents folder. Returns {:ok, %{doc_id, name, url}}.
Options
:actor_uuid— UUID of the user performing the action (for activity logging)
@spec create_document_from_template(String.t(), map(), keyword()) :: {:ok, map()} | {:error, term()}
Create a document from a template by copying and filling variables.
- Copies the template Google Doc to the documents folder
- Replaces all
{{variable}}placeholders with values - Persists the document record with variable_values and template link
- Returns
{:ok, %{doc_id, url}}
Create a blank template in the templates folder. Returns {:ok, %{doc_id, name, url}}.
Options
:actor_uuid— UUID of the user performing the action (for activity logging)
Move a document to the deleted/documents folder.
Options
:actor_uuid— UUID of the user performing the action (for activity logging)
Move a template to the deleted/templates folder.
Options
:actor_uuid— UUID of the user performing the action (for activity logging)
Detect {{ variables }} in a Google Doc's text content.
@spec documents_folder_url() :: String.t() | nil
Get the Google Drive URL for the documents folder.
Export a Google Doc to PDF. Returns {:ok, pdf_binary}.
Options
:actor_uuid— UUID of the user performing the action (for activity logging):name— document name (for activity metadata)
Fetch thumbnails for a list of Drive files asynchronously.
Spawns a task per file that sends {:thumbnail_result, file_id, data_uri}
back to the caller. Also persists thumbnails to the DB.
@spec get_folder_ids() :: map()
Get the folder IDs (auto-discovers if not cached).
@spec list_documents_from_db() :: [map()]
List documents from the local DB. Returns maps compatible with the LiveView.
@spec list_templates_from_db() :: [map()]
List templates from the local DB. Returns maps compatible with the LiveView.
Load cached thumbnails from DB for a list of google_doc_ids.
Log a manual user action to the activity feed.
Move a file into the managed documents folder and classify it as a document.
Options
:actor_uuid— UUID of the user performing the action (for activity logging)
Move a file into the managed templates folder and classify it as a template.
Options
:actor_uuid— UUID of the user performing the action (for activity logging)
Persist a thumbnail data URI to the DB by google_doc_id.
@spec refresh_folders() :: map()
Re-discover folder IDs from Drive.
Persist the file's current parent folder as its accepted location.
Options
:actor_uuid— UUID of the user performing the action (for activity logging)
@spec sync_from_drive() :: :ok | {:error, :sync_failed}
Sync local DB with Google Drive.
Fetches file lists from both Drive folders, upserts all found files, marks DB records as "lost" if their google_doc_id is no longer in Drive, and recovers "lost" records that reappear.
@spec templates_folder_url() :: String.t() | nil
Get the Google Drive URL for the templates folder.
@spec upsert_document_from_drive(map(), map()) :: {:ok, PhoenixKitDocumentCreator.Schemas.Document.t()} | {:error, Ecto.Changeset.t()}
Upsert a document record from a Google Drive file map.
@spec upsert_template_from_drive(map(), map()) :: {:ok, PhoenixKitDocumentCreator.Schemas.Template.t()} | {:error, Ecto.Changeset.t()}
Upsert a template record from a Google Drive file map.