Overview
DriveManager handles the automated creation and management of the Drive folder hierarchy for SGD-MCS entities. It creates organized folder structures based on entity type, year/cohort, and entity identifiers. Source:~/workspace/source/Backend/services/DriveManager.js
This service handles both folder management and file operations for the Drive integration.
Folder Structure
The system creates this hierarchy:getSystemRootFolder
Obtains the root folder for the entire system.Google Drive Folder object (not serializable)
Behavior
- Tries to get folder by
ROOT_FOLDER_IDfrom Config.js - Falls back to creating “SGD_DATABASE_ROOT” in user’s Drive root
- Uses
getOrCreateFolder()to ensure folder exists
This function is used internally. To get folder information from the frontend, use
getSystemRootFolderId().getSystemRootFolderId
Gets the root folder ID and metadata (frontend-compatible).Whether the operation succeeded
Folder ID in Google Drive
Folder name
Direct URL to folder in Drive
Example
createEntityFolder
Creates the folder structure for a new entity.Entity type:
estudiante, docente, externo, evento, or tesisEntity data object containing ID and naming fields
Required Data Fields by Type
Required Data Fields by Type
Estudiante:
ID_Estudiante: Student IDNombre1,Apellido1: Name for folderCohorte_Ingreso: Cohort (e.g., “2024-1”) for organization
ID_Docente: Teacher IDNombre1,Apellido1: Name
ID_Tesis: Thesis IDTitulo_Investigacion: TitleAño: Year for organization
ID_Evento: Event IDNombre_Evento: Event name
ID_Externo: External IDNombre1,Apellido1: Name
Created folder’s Drive ID
Direct URL to the folder
Example
Cohort Handling (Students):
- Accepts ISO date strings: “2023-01-01T05:00:00.000Z” → extracted to “2023-1”
- Accepts direct format: “2024-1”
- Month 1-6 = semester 1, Month 7-12 = semester 2
syncEntityFolder
Creates or verifies the Drive folder for an existing entity.Entity type:
estudiante, docente, externo, evento, or tesisEntity ID (e.g., “EST0001”)
Whether the operation succeeded
Folder ID in Drive
Direct URL to folder
Description of what happened
Example
Behavior
- Searches for entity by ID in the corresponding Sheet
- Checks if
ID_Carpeta_Drivecolumn has a value - If folder exists and not trashed: returns existing folder info
- If folder missing or trashed: creates new folder and updates Sheet
- Logs audit trail for folder creation
This function is automatically called by
updateItem() if an entity has no folder.getEntityFiles
Retrieves all files within an entity’s Drive folder.Drive folder ID
Array of file objects (empty array if error)
File Object Structure
File ID in Drive
File name
MIME type (e.g., “application/pdf”, “image/jpeg”)
Direct URL to file
File size in bytes
ISO timestamp of last modification
Example
This function only returns files, not subfolders. Use
getFolderStructure() from DriveFileManager for folder hierarchies.deleteEntityFolder
Moves an entity’s folder to Drive trash.Drive folder ID to delete
Whether the operation succeeded
Result message
Example
getOrCreateFolder
Utility function that gets or creates a folder by name.Parent Google Drive Folder object
Name of the folder to get or create
Google Drive Folder object
Behavior
- Searches for existing folder with exact name match
- Returns existing folder if found
- Creates new folder if not found
- Idempotent - safe to call multiple times
This is a helper function used internally by other DriveManager functions. It’s not typically called from the frontend.
getSubfolderNameByType
Maps entity type to its container folder name.Entity type
Folder name for that entity type
Mapping Table
| Type | Folder Name |
|---|---|
estudiante | Estudiantes |
docente | Docentes |
tesis | Tesis |
evento | Eventos |
externo | Externos |
| (other) | Otros |
Implementation Notes
Folder Organization
FromDriveManager.js:73-110:
- Students: Organized by cohort (“2024-1”, “2024-2”)
- Thesis: Organized by year
- Other entities: Organized by current year
Error Handling
All functions handle Drive API errors gracefully:- Invalid folder IDs return empty/error responses
- Trashed folders are detected and recreated
- Missing permissions log errors but don’t crash
Audit Integration
FromDriveManager.js:188-197:
All folder operations are logged via logDocumentAction() with:
- Action type (SYNC_FOLDER, DELETE_ENTITY_FOLDER)
- Folder ID and name
- Associated entity information
- Contextual details