All files / src/components AdminCreditsPanel.vue

98.21% Statements 55/56
94.44% Branches 51/54
100% Functions 13/13
100% Lines 52/52

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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227  37x 2x   2x                                         1x       1x       2x         2x       2x   2x 2x         2x 2x 2x                 2x       6x             2x       3x               2x                                         1x                                 17x 17x 17x   17x 17x 17x           17x   2x 1x   1x               2x       2x 2x 2x     27x 17x   25x               17x   28x       28x 28x       4x 2x       2x 2x 2x   2x 2x 1x 1x 1x   1x 1x                                                                
<template>
  <div class="admin-credits-panel">
    <p class="text-muted">{{ t('admin.creditsDescription') }}</p>
 
    <div class="mb-3">
      <label for="credits-search" class="form-label">{{ t('admin.searchUser') }}</label>
      <input
        id="credits-search"
        :value="searchQuery"
        type="text"
        class="form-control"
        :placeholder="t('admin.searchUserPlaceholder')"
        @input="onSearchInput"
      />
    </div>
 
    <LoadingSpinner v-if="adminStore.lookupLoading" />
 
    <ErrorAlert
      v-else-if="adminStore.lookupError"
      :title="t('common.error')"
      :message="adminStore.lookupError"
    />
 
    <div v-else-if="adminStore.userSearchResults.length > 0" class="search-results mb-3">
      <div
        v-for="user in adminStore.userSearchResults"
        :key="user.id"
        class="search-result-item"
        @click="selectUser(user)"
      >
        <strong>{{ user.name || user.email }}</strong>
        <span v-if="user.nickname" class="text-muted"> @{{ user.nickname }}</span>
        <div class="text-muted small">{{ user.email }}</div>
      </div>
    </div>
 
    <div v-if="adminStore.selectedUser" class="user-card card p-3 mt-3">
      <div class="text-muted small mb-2">
        {{ t('admin.selected') }}
        {{ adminStore.selectedUser.name || adminStore.selectedUser.email }}
      </div>
      <div class="d-flex justify-content-between align-items-start flex-wrap gap-3">
        <div>
          <h5 class="mb-1">{{ adminStore.selectedUser.name || adminStore.selectedUser.email }}</h5>
          <div class="text-muted small">{{ adminStore.selectedUser.email }}</div>
          <div v-if="adminStore.selectedUser.nickname" class="text-muted small">
            @{{ adminStore.selectedUser.nickname }}
          </div>
        </div>
        <div class="text-end">
          <div class="text-muted small">{{ t('credits.balance') }}</div>
          <div class="balance-value">
            {{ adminStore.selectedUser.creditsBalance.toLocaleString() }}
          </div>
        </div>
      </div>
 
      <hr />
 
      <form class="row g-2" @submit.prevent="openConfirm">
        <div class="col-auto">
          <label for="adjust-amount" class="form-label">{{ t('admin.adjustAmount') }}</label>
          <input
            id="adjust-amount"
            v-model.number="amount"
            type="number"
            class="form-control"
            step="1"
            required
          />
        </div>
        <div class="col">
          <label for="adjust-reason" class="form-label">{{ t('admin.adjustReason') }}</label>
          <input
            id="adjust-reason"
            v-model.trim="reason"
            type="text"
            class="form-control"
            maxlength="500"
            required
            :placeholder="t('admin.adjustReasonPlaceholder')"
          />
        </div>
        <div class="col-auto">
          <label class="form-label d-block">&nbsp;</label>
          <button
            type="submit"
            class="btn"
            :class="isGrant ? 'btn-success' : 'btn-danger'"
            :disabled="!canSubmit"
          >
            {{ isGrant ? t('admin.grant') : t('admin.deduct') }}
          </button>
        </div>
      </form>
    </div>
 
    <ConfirmModal
      :is-open="confirmOpen"
      :title="t('admin.confirmAdjustTitle')"
      :message="confirmMessage"
      :confirm-label="isGrant ? t('admin.grant') : t('admin.deduct')"
      variant="primary"
      @confirm="applyAdjustment"
      @cancel="confirmOpen = false"
    />
  </div>
</template>
 
<script setup lang="ts">
// @implements UC-CREDITS-006.1
import { ref, computed } from 'vue'
import { useAdminStore, type AdminUserLookupResult } from '@/stores/admin'
import { useToastStore } from '@/stores/toast'
import { useUILanguage } from '@/composables/useUILanguage'
import { useDebouncedSearch } from '@/composables/useDebouncedSearch'
import { getErrorMessage } from '@/utils/errors'
import LoadingSpinner from '@/components/LoadingSpinner.vue'
import ErrorAlert from '@/components/ErrorAlert.vue'
import ConfirmModal from '@/components/ConfirmModal.vue'
 
const adminStore = useAdminStore()
const toastStore = useToastStore()
const { t } = useUILanguage()
 
const amount = ref<number | null>(null)
const reason = ref('')
const confirmOpen = ref(false)
 
// Same type-and-select interaction as the flow-member invite search
// (InviteMemberFormSidebar/MembersPage) — live results as you type, click
// one to select it. Search text under 2 chars just clears the results
// rather than hitting the API.
const { searchQuery, scheduleSearch } = useDebouncedSearch(
  async (query) => {
    if (query.length >= 2) {
      await adminStore.searchUsersForCredits(query)
    } else {
      adminStore.clearUserSearchResults()
    }
  },
  300,
  { auto: false }
)
 
function onSearchInput(event: Event) {
  scheduleSearch((event.target as HTMLInputElement).value)
}
 
function selectUser(user: AdminUserLookupResult) {
  adminStore.selectUserForCredits(user)
  amount.value = null
  reason.value = ''
}
 
const isGrant = computed(() => (amount.value ?? 0) >= 0)
const canSubmit = computed(
  () =>
    Number.isInteger(amount.value) &&
    amount.value !== 0 &&
    reason.value.length > 0 &&
    !adminStore.adjustLoading
)
 
// The reason isn't repeated here — it's already right there in the form the
// admin just filled in, and showing it twice just made the modal noisy.
const confirmMessage = computed(() => {
  const target =
    adminStore.selectedUser?.name ||
    adminStore.selectedUser?.nickname ||
    adminStore.selectedUser?.email ||
    ''
  const verb = isGrant.value ? t('admin.grant') : t('admin.deduct')
  return `${verb} ${Math.abs(amount.value ?? 0).toLocaleString()} — ${target}`
})
 
function openConfirm() {
  if (!canSubmit.value) return
  confirmOpen.value = true
}
 
async function applyAdjustment() {
  confirmOpen.value = false
  const user = adminStore.selectedUser
  Iif (!user || amount.value === null) return
 
  try {
    await adminStore.adjustUserCredits(user.id, amount.value, reason.value)
    toastStore.success(t('admin.adjustSuccess'))
    amount.value = null
    reason.value = ''
  } catch (error: any) {
    console.error('Failed to adjust credits:', error)
    toastStore.error(getErrorMessage(error, t('admin.adjustError'), t))
  }
}
</script>
 
<style scoped>
.balance-value {
  font-size: 1.5rem;
  font-weight: 600;
}
 
.search-results {
  max-height: 200px;
  overflow-y: auto;
  border: 1px solid var(--bw-border);
  border-radius: 4px;
}
 
.search-result-item {
  padding: 0.75rem;
  cursor: pointer;
  border-bottom: 1px solid var(--bw-border);
}
 
.search-result-item:last-child {
  border-bottom: none;
}
 
.search-result-item:hover {
  background: var(--bw-surface-muted);
}
</style>