3a4a2cc841
refactor(patient-preview): comment out unused occupation field in patient preview fix(handler): return success response from update operation in crud handler fix search patient add handle del search char for ease use
130 lines
4.0 KiB
TypeScript
130 lines
4.0 KiB
TypeScript
import { xfetch } from '~/composables/useXfetch'
|
|
import { uploadCode, type UploadCodeKey } from '~/lib/constants'
|
|
|
|
const mainUrl = '/api/v1/patient'
|
|
|
|
export async function getPatients(params: any = null) {
|
|
try {
|
|
let url = mainUrl
|
|
if (params && typeof params === 'object' && Object.keys(params).length > 0) {
|
|
const searchParams = new URLSearchParams()
|
|
for (const key in params) {
|
|
if (params[key] !== null && params[key] !== undefined && params[key] !== '') {
|
|
searchParams.append(key, params[key])
|
|
}
|
|
}
|
|
if (params.search) {
|
|
url += `/search/${params.search}`
|
|
searchParams.delete('search')
|
|
}
|
|
|
|
const queryString = searchParams.toString()
|
|
if (queryString) url += `?${queryString}`
|
|
}
|
|
const resp = await xfetch(url, 'GET')
|
|
const result: any = {}
|
|
result.success = resp.success
|
|
result.body = (resp.body as Record<string, any>) || {}
|
|
return result
|
|
} catch (error) {
|
|
console.error('Error fetching patients:', error)
|
|
throw new Error('Failed to fetch patients')
|
|
}
|
|
}
|
|
|
|
export async function getPatientDetail(id: number) {
|
|
try {
|
|
const resp = await xfetch(`${mainUrl}/${id}`, 'GET')
|
|
const result: any = {}
|
|
result.success = resp.success
|
|
result.body = (resp.body as Record<string, any>) || {}
|
|
return result
|
|
} catch (error) {
|
|
console.error('Error fetching patient detail:', error)
|
|
throw new Error('Failed to get patient detail')
|
|
}
|
|
}
|
|
|
|
export async function getPatientByIdentifier(search: string) {
|
|
try {
|
|
const urlPath =
|
|
search.length === 16 ? `by-resident-identity/search=${encodeURIComponent(search)}` : `/search/${search}`
|
|
const url = `${mainUrl}/${urlPath}`
|
|
const resp = await xfetch(url, 'GET')
|
|
const result: any = {}
|
|
result.success = resp.success
|
|
result.body = (resp.body as Record<string, any>) || {}
|
|
result.type = urlPath.includes('by-resident-identity') ? 'resident-identity' : 'identity'
|
|
return result
|
|
} catch (error) {
|
|
console.error('Error fetching patient by identifier:', error)
|
|
throw new Error('Failed to get patient by identifier')
|
|
}
|
|
}
|
|
|
|
export async function postPatient(record: any) {
|
|
try {
|
|
const resp = await xfetch(mainUrl, 'POST', record)
|
|
const result: any = {}
|
|
result.success = resp.success
|
|
result.body = (resp.body as Record<string, any>) || {}
|
|
return result
|
|
} catch (error) {
|
|
console.error('Error posting patient:', error)
|
|
throw new Error('Failed to post patient')
|
|
}
|
|
}
|
|
|
|
export async function patchPatient(id: number, record: any) {
|
|
try {
|
|
const resp = await xfetch(`${mainUrl}/${id}`, 'PATCH', record)
|
|
const result: any = {}
|
|
result.success = resp.success
|
|
result.body = (resp.body as Record<string, any>) || {}
|
|
return result
|
|
} catch (error) {
|
|
console.error('Error patching patient:', error)
|
|
throw new Error('Failed to patch patient')
|
|
}
|
|
}
|
|
|
|
export async function removePatient(id: number) {
|
|
try {
|
|
const resp = await xfetch(`${mainUrl}/${id}`, 'DELETE')
|
|
const result: any = {}
|
|
result.success = resp.success
|
|
result.body = (resp.body as Record<string, any>) || {}
|
|
return result
|
|
} catch (error) {
|
|
console.error('Error deleting record:', error)
|
|
throw new Error('Failed to delete patient')
|
|
}
|
|
}
|
|
|
|
export async function uploadAttachment(file: File, userId: number, key: UploadCodeKey) {
|
|
try {
|
|
const resolvedKey = uploadCode[key]
|
|
if (!resolvedKey) {
|
|
throw new Error(`Invalid upload code key: ${key}`)
|
|
}
|
|
|
|
// siapkan form-data body
|
|
const formData = new FormData()
|
|
formData.append('code', resolvedKey)
|
|
formData.append('content', file)
|
|
|
|
// kirim via xfetch
|
|
const resp = await xfetch(`${mainUrl}/${userId}/upload`, 'POST', formData)
|
|
|
|
// struktur hasil sama seperti patchPatient
|
|
const result: any = {}
|
|
result.success = resp.success
|
|
result.body = (resp.body as Record<string, any>) || {}
|
|
|
|
return result
|
|
} catch (error) {
|
|
console.error('Error uploading attachment:', error)
|
|
throw new Error('Failed to upload attachment')
|
|
}
|
|
}
|