public Query(IGoogleDriveService googleDriveService, IDriveImportRepository driveImportRepository) { Name = "Query"; FieldAsync <BooleanGraphType>( "haveToken", resolve: async context => { Token token = await googleDriveService.GetGoogleToken(); return(token != null && !string.IsNullOrEmpty(token.RefreshToken)); } ); /// query Reviews($searchTerm: String, $from: Int, $to: Int, $orderBy: String, $status: Boolean) FieldAsync <StringGraphType>( "getOwnerEmail", arguments: new QueryArguments( new QueryArgument <StringGraphType> { Name = "accountName", Description = "Account Name" } ), resolve: async context => { string email = string.Empty; string accountName = context.GetArgument <string>("accountName"); Token token = await googleDriveService.GetGoogleToken(); if (token != null) { string newFolderId = string.Empty; FolderIds folderIds = await driveImportRepository.LoadFolderIds(accountName); if (folderIds != null) { newFolderId = folderIds.NewFolderId; } else { newFolderId = await googleDriveService.FindNewFolderId(accountName); } ListFilesResponse listFilesResponse = await googleDriveService.ListFiles(); if (listFilesResponse != null) { var owners = listFilesResponse.Files.Where(f => f.Id.Equals(newFolderId)).Select(o => o.Owners.Distinct()).FirstOrDefault(); if (owners != null) { email = owners.Select(o => o.EmailAddress).FirstOrDefault(); } else { newFolderId = await googleDriveService.FindNewFolderId(accountName); owners = listFilesResponse.Files.Where(f => f.Id.Equals(newFolderId)).Select(o => o.Owners.Distinct()).FirstOrDefault(); if (owners != null) { email = owners.Select(o => o.EmailAddress).FirstOrDefault(); } } } } return(email); } ); FieldAsync <StringGraphType>( "sheetLink", resolve: async context => { return(await googleDriveService.GetSheetLink()); } ); }
public async Task <IActionResult> ListImages() { Response.Headers.Add("Cache-Control", "no-cache"); string newFolderId; string doneFolderId; string errorFolderId; string importFolderId; string accountFolderId; string imagesFolderId; string accountName = this._httpContextAccessor.HttpContext.Request.Headers[DriveImportConstants.VTEX_ACCOUNT_HEADER_NAME]; FolderIds folderIds = await _driveImportRepository.LoadFolderIds(accountName); if (folderIds != null) { accountFolderId = folderIds.AccountFolderId; imagesFolderId = folderIds.ImagesFolderId; newFolderId = folderIds.NewFolderId; doneFolderId = folderIds.DoneFolderId; errorFolderId = folderIds.ErrorFolderId; } else { Dictionary <string, string> folders = await _googleDriveService.ListFolders(); // Id, Name if (!folders.ContainsValue(DriveImportConstants.FolderNames.IMPORT)) { importFolderId = await _googleDriveService.CreateFolder(DriveImportConstants.FolderNames.IMPORT); } else { importFolderId = folders.FirstOrDefault(x => x.Value == DriveImportConstants.FolderNames.IMPORT).Key; } if (string.IsNullOrEmpty(importFolderId)) { _context.Vtex.Logger.Info("DriveImport", null, $"Could not find '{DriveImportConstants.FolderNames.IMPORT}' folder"); return(Json($"Could not find {DriveImportConstants.FolderNames.IMPORT} folder")); } folders = await _googleDriveService.ListFolders(importFolderId); if (!folders.ContainsValue(accountName)) { accountFolderId = await _googleDriveService.CreateFolder(accountName, importFolderId); } else { accountFolderId = folders.FirstOrDefault(x => x.Value == accountName).Key; } if (string.IsNullOrEmpty(accountFolderId)) { _context.Vtex.Logger.Info("DriveImport", null, $"Could not find {accountFolderId} folder"); return(Json($"Could not find {accountFolderId} folder")); } folders = await _googleDriveService.ListFolders(accountFolderId); if (!folders.ContainsValue(DriveImportConstants.FolderNames.IMAGES)) { imagesFolderId = await _googleDriveService.CreateFolder(DriveImportConstants.FolderNames.IMAGES, accountFolderId); } else { imagesFolderId = folders.FirstOrDefault(x => x.Value == DriveImportConstants.FolderNames.IMAGES).Key; } if (string.IsNullOrEmpty(imagesFolderId)) { _context.Vtex.Logger.Info("DriveImport", null, $"Could not find {imagesFolderId} folder"); return(Json($"Could not find {imagesFolderId} folder")); } folders = await _googleDriveService.ListFolders(imagesFolderId); if (!folders.ContainsValue(DriveImportConstants.FolderNames.NEW)) { newFolderId = await _googleDriveService.CreateFolder(DriveImportConstants.FolderNames.NEW, imagesFolderId); bool setPermission = await _googleDriveService.SetPermission(newFolderId); if (!setPermission) { _context.Vtex.Logger.Error("DriveImport", "SetPermission", $"Could not set permissions on '{DriveImportConstants.FolderNames.NEW}' folder {newFolderId}"); } } else { newFolderId = folders.FirstOrDefault(x => x.Value == DriveImportConstants.FolderNames.NEW).Key; } if (!folders.ContainsValue(DriveImportConstants.FolderNames.DONE)) { doneFolderId = await _googleDriveService.CreateFolder(DriveImportConstants.FolderNames.DONE, imagesFolderId); } else { doneFolderId = folders.FirstOrDefault(x => x.Value == DriveImportConstants.FolderNames.DONE).Key; } if (!folders.ContainsValue(DriveImportConstants.FolderNames.ERROR)) { errorFolderId = await _googleDriveService.CreateFolder(DriveImportConstants.FolderNames.ERROR, imagesFolderId); } else { errorFolderId = folders.FirstOrDefault(x => x.Value == DriveImportConstants.FolderNames.ERROR).Key; } } if (folderIds == null) { folderIds = new FolderIds { AccountFolderId = accountFolderId, DoneFolderId = doneFolderId, ErrorFolderId = errorFolderId, ImagesFolderId = imagesFolderId, ImportFolderId = imagesFolderId, NewFolderId = newFolderId }; _driveImportRepository.SaveFolderIds(folderIds, accountName); } Dictionary <string, string> images = new Dictionary <string, string>(); ListFilesResponse imageFiles = new ListFilesResponse(); imageFiles.Files = new List <GoogleFile>(); string nextPageToken = string.Empty; do { ListFilesResponse listFilesResponse = await _googleDriveService.ListImagesInFolder(newFolderId, nextPageToken); imageFiles.Files.AddRange(listFilesResponse.Files); nextPageToken = listFilesResponse.NextPageToken; } while (!string.IsNullOrEmpty(nextPageToken)); return(Json(imageFiles)); }