Exemplo n.º 1
0
        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());
            }
                );
        }
Exemplo n.º 2
0
        public async Task <IActionResult> GetOwnerEmail()
        {
            string email       = null;
            string accountName = this._httpContextAccessor.HttpContext.Request.Headers[DriveImportConstants.VTEX_ACCOUNT_HEADER_NAME];

            try
            {
                Token token = await _googleDriveService.GetGoogleToken();

                if (token != null)
                {
                    string    newFolderId    = string.Empty;
                    string    imagesFolderId = string.Empty;
                    FolderIds folderIds      = await _driveImportRepository.LoadFolderIds(accountName);

                    if (folderIds != null)
                    {
                        newFolderId    = folderIds.NewFolderId;
                        imagesFolderId = folderIds.ImagesFolderId;
                        _context.Vtex.Logger.Info("GetOwnerEmail", null, $"New Folder Id: {newFolderId}");
                    }
                    else
                    {
                        _context.Vtex.Logger.Info("GetOwnerEmail", null, "Could not load folder structure from storage.");
                        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
                        {
                            _context.Vtex.Logger.Info("GetOwnerEmail", null, "Could not find owners. (1)");
                            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();
                            }
                            else
                            {
                                _context.Vtex.Logger.Info("GetOwnerEmail", null, "Could not find owners. (2)");
                                owners = listFilesResponse.Files.Where(f => f.Id.Equals(imagesFolderId)).Select(o => o.Owners.Distinct()).FirstOrDefault();
                            }
                            if (owners != null)
                            {
                                email = owners.Select(o => o.EmailAddress).FirstOrDefault();
                            }
                            else
                            {
                                _context.Vtex.Logger.Info("GetOwnerEmail", null, "Could not find owners. (3)");
                            }
                        }
                    }
                }
                else
                {
                    _context.Vtex.Logger.Info("GetOwnerEmail", null, "Could not load Token.");
                }
            }
            catch (Exception ex)
            {
                _context.Vtex.Logger.Error("GetOwnerEmail", null, $"Error getting Drive owner", ex);
            }

            _context.Vtex.Logger.Info("GetOwnerEmail", null, $"Email = {email}");

            Response.Headers.Add("Cache-Control", "no-cache");
            return(Json(email));
        }