Files
vitify-nuxt/server/api/practitioner/index.get.ts
2025-04-22 10:56:56 +07:00

66 lines
1.5 KiB
TypeScript

import { useToString } from '@vueuse/core'
import { take } from 'echarts/types/src/component/helper/interactionMutex.js'
export default defineEventHandler(async (event) => {
const query = getQuery(event)
const name = useToString(query.name).value.split(' ')
function pipelineFilter(keywords) {
var result = [{}]
keywords.forEach((keyword, index) => {
result[index] = {
$match: {
$or: [
{
'name.family': {
$regex: (index === 0 ? '^' : '') + keyword,
$options: 'i',
},
},
{
'name.given': {
$regex: (index === 0 ? '^' : '') + keyword,
$options: 'i',
},
},
],
},
}
})
return result
}
try {
if (Object.keys(query).length === 0) {
return await prisma.practitioner.aggregateRaw({
pipeline: [{ $limit: 100 }],
})
}
const practitioners = await prisma.practitioner.aggregateRaw({
pipeline: [
// { $unwind: { path: '$name.family' } },
...pipelineFilter(name),
{
$limit: 10,
},
// {
// $project: {
// id: 1,
// name: 1,
// },
// },
],
})
return practitioners
} catch (error) {
return {
status: 500,
body: {
messages: 'failed to fetch practitioners',
code: error,
},
}
}
})