All files / src/api/drive/folder delete.ts

100% Statements 22/22
95.45% Branches 21/22
100% Functions 1/1
100% Lines 22/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113          53x                                                                                                 53x 111x 111x   111x 31x     80x   80x                   40x 16x 8x   8x     24x           40x   40x 32x 8x         24x 8x   16x 8x             16x          
import { createRoute, RouteHandler } from '@hono/zod-openapi'
import { ErrorSchema, FolderDeleteRequestSchema, FolderDeleteResponseSchema } from '../../../schema/drive'
import { deleteFolderContents } from '../../../utils/googleDrive'
 
import type { EnvHono } from '../../..'
export const deleteFolderContentsRoute = createRoute({
  method: 'delete',
  path: '/delete-folder-contents',
  request: {
    body: {
      content: {
        'application/json': {
          schema: FolderDeleteRequestSchema
        }
      }
    }
  },
  responses: {
    200: {
      content: {
        'application/json': {
          schema: FolderDeleteResponseSchema
        }
      },
      description: 'Folder contents deleted successfully'
    },
    400: {
      content: {
        'application/json': {
          schema: ErrorSchema
        }
      },
      description: 'Bad request'
    },
    403: {
      content: {
        'application/json': {
          schema: ErrorSchema
        }
      },
      description: 'Forbidden - folder access denied'
    },
    500: {
      content: {
        'application/json': {
          schema: ErrorSchema
        }
      },
      description: 'Internal server error'
    }
  },
  tags: ['Folder API']
})
 
export const deleteFolderContentsHandler: RouteHandler<typeof deleteFolderContentsRoute, EnvHono> = async (c) => {
  try {
    const { GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET, GOOGLE_REFRESH_TOKEN, GOOGLE_DRIVE_DEFAULT_FOLDER_ID } = c.env
 
    if (!GOOGLE_CLIENT_ID || !GOOGLE_CLIENT_SECRET || !GOOGLE_REFRESH_TOKEN || !GOOGLE_DRIVE_DEFAULT_FOLDER_ID) {
      return c.json({ error: 'Missing required environment variables' }, 400)
    }
 
    const { folderId } = await c.req.json()
 
    const result = await deleteFolderContents(
      folderId,
      {
        clientId: GOOGLE_CLIENT_ID,
        clientSecret: GOOGLE_CLIENT_SECRET,
        refreshToken: GOOGLE_REFRESH_TOKEN
      },
      GOOGLE_DRIVE_DEFAULT_FOLDER_ID
    )
 
    if (!result.success) {
      if (result.error === 'Unauthorized folder access') {
        return c.json({ error: result.error, details: result.details }, 403)
      }
      return c.json({ error: result.error || 'Delete folder contents failed' }, 400)
    }
 
    return c.json({
      success: true,
      message: result.message
    }, 200)
 
  } catch (error) {
    console.error('Delete folder contents error:', error)
 
    if (error instanceof Error) {
      if (error.message.includes('Failed to refresh access token')) {
        return c.json({
          error: 'Failed to refresh access token',
          details: error.message.replace('Failed to refresh access token: ', '')
        }, 500)
      }
      if (error.message.includes('No access token')) {
        return c.json({ error: 'Failed to get access token' }, 500)
      }
      if (error.message.includes('Failed to list folder contents')) {
        return c.json({
          error: 'Failed to list folder contents',
          details: error.message.replace('Failed to list folder contents: ', '')
        }, 500)
      }
    }
 
    return c.json({
      error: 'Failed to delete folder contents',
      details: error instanceof Error ? error.message : 'Unknown error'
    }, 500)
  }
}