first commit

This commit is contained in:
2025-04-22 10:56:56 +07:00
commit af123c091b
147 changed files with 778063 additions and 0 deletions
+32
View File
@@ -0,0 +1,32 @@
import { useToString } from '@vueuse/core'
export default defineEventHandler(async (event) => {
const query = getQuery(event)
const parent = JSON.parse(query.parent)
try {
const results = await prisma.address.aggregateRaw({
pipeline: [
{ $unwind: { path: '$regencies' } },
{
$match: {
_id: parent._id,
},
},
{
$project: {
_id: '$regencies._id',
name: '$regencies.name',
},
},
],
})
return results
} catch (error) {
// Return error if fetching users fails
return {
status: 500,
body: { message: 'Failed to fetch regencies' },
}
}
})
+22
View File
@@ -0,0 +1,22 @@
import { useToString } from '@vueuse/core'
export default defineEventHandler(async (event) => {
const query = getQuery(event)
const search = useToString(query.search)
try {
const results = await prisma.country.findMany()
return results.map((r) => {
return {
_id: r.id,
name: r.name,
}
})
} catch (error) {
// Return error if fetching users fails
return {
status: 500,
body: { message: 'Failed to fetch countries' },
}
}
})
+33
View File
@@ -0,0 +1,33 @@
import { useToString } from '@vueuse/core'
export default defineEventHandler(async (event) => {
const query = getQuery(event)
const parent = JSON.parse(query.parent)
try {
const results = await prisma.address.aggregateRaw({
pipeline: [
{ $unwind: { path: '$regencies' } },
{ $unwind: { path: '$regencies.districts' } },
{
$match: {
'regencies._id': parent._id,
},
},
{
$project: {
_id: '$regencies.districts._id',
name: '$regencies.districts.name',
},
},
],
})
return results
} catch (error) {
// Return error if fetching users fails
return {
status: 500,
body: { message: 'Failed to fetch districts' },
}
}
})
+93
View File
@@ -0,0 +1,93 @@
import { useToString } from '@vueuse/core'
export default defineEventHandler(async (event) => {
const query = getQuery(event)
const keywords = useToString(query.search).value.split(' ')
function pipelineFilter(keywords) {
var result = [{}]
keywords.forEach((keyword, index) => {
result[index] = {
$match: {
$or: [
{
'regencies.districts.villages.name': {
$regex: (index === 0 ? '^' : '') + keyword,
$options: 'i',
},
},
{
'regencies.districts.name': {
$regex: (index === 0 ? '^' : '') + keyword,
$options: 'i',
},
},
{
'regencies.name': {
$regex: (index === 0 ? '^(KOTA|KABUPATEN) ' : '') + keyword,
$options: 'i',
},
},
{
name: {
$regex: (index === 0 ? '^' : '') + keyword,
$options: 'i',
},
},
],
},
}
})
return result
}
try {
const results = await prisma.address.aggregateRaw({
pipeline: [
{ $unwind: { path: '$regencies' } },
{ $unwind: { path: '$regencies.districts' } },
{
$unwind: {
path: '$regencies.districts.villages',
},
},
...pipelineFilter(keywords),
{
$limit: 10,
},
{
$project: {
_id: 1,
name: 1,
regencies: {
_id: 1,
name: 1,
districts: {
_id: 1,
name: 1,
villages: {
_id: 1,
name: 1,
},
},
},
},
},
],
})
return results.map((result) => {
return {
_id: 'ID',
name: 'INDONESIA',
states: result,
display: `DESA/KEL ${result.regencies.districts.villages.name} KEC. ${result.regencies.districts.name} ${result.regencies.name} PROVINSI ${result.name}`,
}
})
} catch (error) {
// Return error if fetching users fails
return {
status: 500,
body: { message: 'Failed to fetch address' },
}
}
})
+19
View File
@@ -0,0 +1,19 @@
export default defineEventHandler(async (event) => {
const query = getQuery(event)
const parent = JSON.parse(query.parent)
try {
const results = await prisma.postal.findFirst({
where: {
id: parseInt(parent._id),
},
})
return results.postal
} catch (error) {
// Return error if fetching users fails
return {
status: 500,
body: { message: 'Failed to fetch postal' },
}
}
})
+29
View File
@@ -0,0 +1,29 @@
export default defineEventHandler(async (event) => {
const query = getQuery(event)
const parent = JSON.parse(query.parent)
if (parent._id !== 'ID') {
return {}
}
try {
const results = await prisma.address.findMany({
select: {
id: true,
name: true,
},
})
return results.map((r) => {
return {
_id: r.id,
name: r.name,
}
})
} catch (error) {
// Return error if fetching users fails
return {
status: 500,
body: { message: 'Failed to fetch states' },
}
}
})
+34
View File
@@ -0,0 +1,34 @@
import { useToString } from '@vueuse/core'
export default defineEventHandler(async (event) => {
const query = getQuery(event)
const parent = JSON.parse(query.parent)
try {
const results = await prisma.address.aggregateRaw({
pipeline: [
{ $unwind: { path: '$regencies' } },
{ $unwind: { path: '$regencies.districts' } },
{ $unwind: { path: '$regencies.districts.villages' } },
{
$match: {
'regencies.districts._id': parent._id,
},
},
{
$project: {
_id: '$regencies.districts.villages._id',
name: '$regencies.districts.villages.name',
},
},
],
})
return results
} catch (error) {
// Return error if fetching users fails
return {
status: 500,
body: { message: 'Failed to fetch villages' },
}
}
})