All files / msw / handlers bot-accounts.ts

34% Statements 17/50
18.18% Branches 6/33
55.55% Functions 5/9
36.58% Lines 15/41

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    68x 68x   48x 48x 48x 48x 48x 48x                   7x 7x 7x                                                                                   1x 4x 1x 1x                                    
import { http, HttpResponse } from 'msw'
import { mockBotAccounts, mockRepositories } from '@/msw/mock-data'
 
let nextId = 100
 
export const botAccountHandlers = [
  http.get('/api/v1/bot-accounts', ({ request }) => {
    const url = new URL(request.url)
    const limit = parseInt(url.searchParams.get('limit') ?? '50')
    const offset = parseInt(url.searchParams.get('offset') ?? '0')
    const paginated = mockBotAccounts.slice(offset, offset + limit)
    const hasMore = offset + limit < mockBotAccounts.length
    return HttpResponse.json({
      data: paginated,
      meta: { limit, returned: paginated.length, has_more: hasMore },
    })
  }),
 
  http.get('/api/v1/bot-accounts/:id', ({ params }) => {
    const account = mockBotAccounts.find((a) => a.id === params.id)
    if (!account) return new HttpResponse(null, { status: 404 })
  I  return HttpResponse.json(account)
  }),
 
  http.post('/api/v1/bot-accounts', async ({ request }) => {
    const body = await request.json() as Record<string, unknown>
    if (!body.name || String(body.name).length < 3 || String(body.name).length > 100) {
      return HttpResponse.json(
        { error: 'Name must be 3-100 characters' },
        { status: 400 },
      )
    }
    const now = new Date().toISOString()
    const account = {
      id: `botacct-new-${nextId++}`,
      name: body.name as string,
      description: (body.description as string) ?? null,
      created_at: now,
      created_by: 'system',
      updated_at: now,
      updated_by: 'system',
    }
    mockBotAccounts.push(account)
    return HttpResponse.json(account, { status: 201 })
  }),

  http.put('/api/v1/bot-accounts/:id', async ({ params, request }) => {
    const body = await request.json() as Record<string, unknown>
    const account = mockBotAccounts.find((a) => a.id === params.id)
    if (!account) return new HttpResponse(null, { status: 404 })
    if (body.name !== undefined) {
      const name = String(body.name)
      if (name.length < 3 || name.length > 100) {
        return HttpResponse.json(
          { error: 'Name must be 3-100 characters' },
          { status: 400 },
        )
      }
    }
    const now = new Date().toISOString()
    if (body.name !== undefined) account.name = body.name as string
    if (body.description !== undefined) account.description = (body.description as string) ?? null
    if (body.token_encrypted !== undefined) {
      ; (account as Record<string, unknown>).token_encrypted = body.token_encrypted as string
    }
    account.updated_at = now
  E  account.updated_by = 'system'
    return HttpResponse.json(account)
  }),

  http.delete('/api/v1/bot-accounts/:id', ({ params }) => {
    const id = params.id as string
    const usedBy = mockRepositories.filter((r) => (r as Record<string, unknown>).bot_account_id === id)
    if (usedBy.length > 0) {
      return HttpResponse.json(
        { error: 'Cannot delete: bot account is in use by repositories' },
        { status: 409 },
      )
    }
    const idx = mockBotAccounts.findIndex((a) => a.id === id)
    if (idx === -1) return new HttpResponse(null, { status: 404 })
    mockBotAccounts.splice(idx, 1)
    return new HttpResponse(null, { status: 204 })
  }),
]