All files / src/composables useProfileTodos.ts

56.88% Statements 62/109
100% Branches 7/7
33.33% Functions 1/3
56.88% Lines 62/109

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  1x 1x 1x                                                                                                                                                                                                             1x 4x   4x 2x 2x 2x 2x 2x 2x 2x 2x 4x 2x 2x 2x 2x 2x 2x 2x 2x 4x 1x 1x 1x 1x 1x 1x 1x 1x 4x 2x 2x 2x 2x 2x 2x 2x 2x 4x 1x 1x 1x 1x 1x 1x 1x 1x 4x 1x 1x 1x 1x 1x 1x 1x 1x   4x 4x             1x                                                                            
// region Dependency Injection
import {ref} from 'vue'
import {useRouter} from "vue-router";
import api from '@/libs/axios'
// endregion Dependency Injection
 
// region Component Import
// endregion Component Import
 
// region interface
 
export type ProfileStatus = {
  basic_profile: boolean   // ユーザー基本情報テーブルに行があるか
  contact_info: boolean    // 連絡先テーブルに行があるか
  employment_history: boolean
  bank_account: boolean
  emergency_contact: boolean
  user_address: boolean
}
 
export type TodoItem = {
  id: string
  title: string
  subtitle?: string
  icon?: string
  routeName?: string
  onClick?: () => void
}
// endregion interface
 
// region constants
// const api = useApi()
// endregion constants
 
// region props
// endregion props
 
// region variable
// endregion variable
 
// region properties
// endregion properties
 
// region emits
// endregion emits
 
// region validator
// endregion validator
 
// region methods
/**
 * APIからプロフィール状態を取得
 * 将来モック → 実装切替しやすいよう export しておく
 */
export async function fetchStatus(): Promise<ProfileStatus> {
  // ▼ 本実装:APIに差し替え
  // const res = await fetch('/api/profile/status', { credentials: 'include' })
  // if (!res.ok) throw new Error('failed to fetch profile status')
  // return await res.json()
  const addrRes = await api.get('/fl/address/exists')
  const hasAddress = Boolean(addrRes.data?.hasAddress)
 
  // ▼ モック
  await new Promise((r) => setTimeout(r, 400))
  // const [
  //   // basicP,
  //   // contactP,
  //   // employmentP,
  //   // bankP,
  //   // emergencyP,
  //   addressP, // 住所有無
  // ] = await Promise.allSettled([
  //   // api.get('/api/profile/basic/status'),
  //   // api.get('/api/profile/contact/status'),
  //   // api.get('/api/profile/employment/status'),
  //   // api.get('/api/profile/bank/status'),
  //   // api.get('/api/profile/emergency/status'),
  //   api.get('/fl/profile/address/exists'), // ← 追加
  // ])
 
  // const safe = (r: PromiseSettledResult<any>, key: string) =>
  //   r.status === 'fulfilled'
  //     ? Boolean(r.value?.data?.[key] ?? r.value?.data?.ok ?? r.value?.data)
  //     : false
 
  return {
    basic_profile: false,
    // basic_profile:     safe(basicP, 'ok'),
    contact_info: true,
    // contact_info:      safe(contactP, 'ok'),
    employment_history: false,
    // employment_history:safe(employmentP, 'ok'),
    bank_account: false,
    // bank_account:      safe(bankP, 'ok'),
    emergency_contact: true,
    // emergency_contact: safe(emergencyP, 'ok'),
    user_address: hasAddress,
    // user_address:      addressP.status === 'fulfilled'
    //   ? Boolean(addressP.value?.data?.hasAddress)
    //   : false, // 失敗時は未完扱いにフォールバック
  }
}
 
/**
 * 状態からTODO配列を構築(他所でも使い回せるよう外出し)
 */
export function buildTodosFromStatus(s: ProfileStatus): TodoItem[] {
  const list: TodoItem[] = []
 
  if (!s.basic_profile) {
    list.push({
      id: 'basic_profile',
      title: '基本情報を登録',
      subtitle: '氏名・表示名・生年月日など',
      icon: 'mdi-account',
      routeName: 'ProfileBasicEdit',
    })
  }
  if (!s.contact_info) {
    list.push({
      id: 'contact_info',
      title: '連絡先を登録',
      subtitle: 'メール・電話番号・住所',
      icon: 'mdi-phone',
      routeName: 'ProfileContactEdit',
    })
  }
  if (!s.employment_history) {
    list.push({
      id: 'employment_history',
      title: '職歴を登録',
      subtitle: '現在の所属・これまでの経験',
      icon: 'mdi-briefcase',
      routeName: 'ProfileEmploymentEdit',
    })
  }
  if (!s.bank_account) {
    list.push({
      id: 'bank_account',
      title: '振込口座を登録',
      subtitle: '給与振込先の金融機関情報',
      icon: 'mdi-bank',
      routeName: 'ProfileBankEdit',
    })
  }
  if (!s.emergency_contact) {
    list.push({
      id: 'emergency_contact',
      title: '緊急連絡先を登録',
      subtitle: '家族・友人などの連絡先',
      icon: 'mdi-account-alert',
      routeName: 'ProfileEmergencyEdit',
    })
  }
  if (!s.user_address) {
    list.push({
      id: 'user_address',
      title: 'ご住所を登録',
      subtitle: '経歴書・履歴書に記載するご住所',
      icon: 'mdi-account-alert',
      routeName: 'ProfileEmergencyEdit',
    })
  }
 
  return list
}
// endregion methods
 
// region export
/**
 * プロフィールのTODOリストを管理するコンポーザブル
 */
export const useProfileTodos = () => {
  const loading = ref(false)
  const errorMessage = ref<string | null>(null)
  const todos = ref<TodoItem[]>([])
  const router = useRouter()
 
  const refresh = async (): Promise<void> => {
    loading.value = true
    errorMessage.value = null
    try {
      const s = await fetchStatus()
      todos.value = buildTodosFromStatus(s)
    } catch (e: unknown) {
      errorMessage.value = e instanceof Error ? e.message : '読み込みに失敗しました'
      todos.value = []
    } finally {
      loading.value = false
    }
  }
 
  const onAction = (t: TodoItem): void => {
    if (t.onClick) {
      t.onClick()
      return
    }
    if (t.routeName) {
      router.push({ name: t.routeName })
    }
  }
 
  return {
    loading,
    errorMessage,
    todos,
    refresh,
    onAction,
  }
}// endregion export