chore: fix linting and type safety across the project

This commit is contained in:
2026-03-26 15:51:45 +08:00
parent ec057ef2e5
commit 0d0dc187a5
46 changed files with 2461 additions and 312 deletions

View File

@@ -1,4 +1,4 @@
import Elysia from "elysia";
import Elysia, { t } from "elysia";
import { prisma } from "../utils/db";
import logger from "../utils/logger";
@@ -23,6 +23,17 @@ export const complaint = new Elysia({
}
},
{
response: {
200: t.Object({
data: t.Object({
total: t.Number(),
baru: t.Number(),
proses: t.Number(),
selesai: t.Number(),
}),
}),
500: t.Object({ error: t.String() }),
},
detail: { summary: "Get complaint statistics" },
},
)
@@ -42,6 +53,12 @@ export const complaint = new Elysia({
}
},
{
response: {
200: t.Object({
data: t.Array(t.Any()),
}),
500: t.Object({ error: t.String() }),
},
detail: { summary: "Get recent complaints" },
},
)
@@ -61,6 +78,12 @@ export const complaint = new Elysia({
}
},
{
response: {
200: t.Object({
data: t.Array(t.Any()),
}),
500: t.Object({ error: t.String() }),
},
detail: { summary: "Get service letter statistics by type" },
},
)
@@ -80,6 +103,12 @@ export const complaint = new Elysia({
}
},
{
response: {
200: t.Object({
data: t.Array(t.Any()),
}),
500: t.Object({ error: t.String() }),
},
detail: { summary: "Get recent innovation ideas" },
},
)
@@ -88,7 +117,9 @@ export const complaint = new Elysia({
async ({ set }) => {
try {
// Get last 6 months trends for service letters
const trends = await prisma.$queryRaw<any[]>`
const trends = await prisma.$queryRaw<
{ month: string; month_num: number; count: number }[]
>`
SELECT
TO_CHAR("createdAt", 'Mon') as month,
EXTRACT(MONTH FROM "createdAt") as month_num,
@@ -106,6 +137,12 @@ export const complaint = new Elysia({
}
},
{
response: {
200: t.Object({
data: t.Array(t.Any()),
}),
500: t.Object({ error: t.String() }),
},
detail: { summary: "Get service letter trends for last 6 months" },
},
)
@@ -132,6 +169,14 @@ export const complaint = new Elysia({
}
},
{
response: {
200: t.Object({
data: t.Object({
count: t.Number(),
}),
}),
500: t.Object({ error: t.String() }),
},
detail: { summary: "Get service letter count for current week" },
},
);

View File

@@ -1,4 +1,4 @@
import Elysia from "elysia";
import Elysia, { t } from "elysia";
import { prisma } from "../utils/db";
import logger from "../utils/logger";
@@ -24,6 +24,12 @@ export const division = new Elysia({
}
},
{
response: {
200: t.Object({
data: t.Array(t.Any()),
}),
500: t.Object({ error: t.String() }),
},
detail: { summary: "Get all divisions" },
},
)
@@ -48,6 +54,12 @@ export const division = new Elysia({
}
},
{
response: {
200: t.Object({
data: t.Array(t.Any()),
}),
500: t.Object({ error: t.String() }),
},
detail: { summary: "Get recent activities" },
},
)
@@ -66,6 +78,12 @@ export const division = new Elysia({
}
},
{
response: {
200: t.Object({
data: t.Array(t.Any()),
}),
500: t.Object({ error: t.String() }),
},
detail: { summary: "Get division performance metrics" },
},
);

View File

@@ -1,4 +1,4 @@
import Elysia from "elysia";
import Elysia, { t } from "elysia";
import { prisma } from "../utils/db";
import logger from "../utils/logger";
@@ -21,6 +21,12 @@ export const event = new Elysia({
}
},
{
response: {
200: t.Object({
data: t.Array(t.Any()),
}),
500: t.Object({ error: t.String() }),
},
detail: { summary: "Get upcoming events" },
},
)
@@ -49,6 +55,12 @@ export const event = new Elysia({
}
},
{
response: {
200: t.Object({
data: t.Array(t.Any()),
}),
500: t.Object({ error: t.String() }),
},
detail: { summary: "Get events for today" },
},
);

View File

@@ -1,6 +1,6 @@
import { cors } from "@elysiajs/cors";
import { swagger } from "@elysiajs/swagger";
import Elysia from "elysia";
import Elysia, { t } from "elysia";
import { apiMiddleware } from "../middleware/apiMiddleware";
import { auth } from "../utils/auth";
import { apikey } from "./apikey";
@@ -16,12 +16,24 @@ const api = new Elysia({
prefix: "/api",
})
.use(cors())
.get("/health", () => ({ ok: true }))
.all("/auth/*", ({ request }) => auth.handler(request))
.get("/session", async ({ request }) => {
const data = await auth.api.getSession({ headers: request.headers });
return { data };
.get("/health", () => ({ ok: true }), {
response: {
200: t.Object({ ok: t.Boolean() }),
},
})
.all("/auth/*", ({ request }) => auth.handler(request))
.get(
"/session",
async ({ request }) => {
const data = await auth.api.getSession({ headers: request.headers });
return { data };
},
{
response: {
200: t.Object({ data: t.Any() }),
},
},
)
.use(apiMiddleware)
.use(apikey)
.use(profile)

View File

@@ -1,80 +1,69 @@
import Elysia, { t } from "elysia";
import { apiMiddleware } from "../middleware/apiMiddleware";
import { prisma } from "../utils/db";
import logger from "../utils/logger";
interface AuthenticatedUser {
id: string;
email: string;
name?: string | null;
}
export const profile = new Elysia({
prefix: "/profile",
}).post(
"/update",
async ({
body,
set,
user,
}: {
body: { name?: string; image?: string };
set: any;
user?: AuthenticatedUser;
}) => {
try {
if (!user) {
set.status = 401;
return { error: "Unauthorized" };
})
.use(apiMiddleware)
.post(
"/update",
async ({ body, set, user }) => {
try {
if (!user) {
set.status = 401;
return { error: "Unauthorized" };
}
const { name, image } = body;
const updatedUser = await prisma.user.update({
where: { id: user.id },
data: {
name: name || undefined,
image: image || undefined,
},
select: {
id: true,
name: true,
email: true,
image: true,
role: true,
},
});
logger.info({ userId: user.id }, "Profile updated successfully");
return { user: updatedUser };
} catch (error) {
logger.error({ error, userId: user?.id }, "Failed to update profile");
set.status = 500;
return { error: "Failed to update profile" };
}
const { name, image } = body;
const updatedUser = await prisma.user.update({
where: { id: user.id },
data: {
name: name || undefined,
image: image || undefined,
},
select: {
id: true,
name: true,
email: true,
image: true,
role: true,
},
});
logger.info({ userId: user.id }, "Profile updated successfully");
return { user: updatedUser };
} catch (error) {
logger.error({ error, userId: user?.id }, "Failed to update profile");
set.status = 500;
return { error: "Failed to update profile" };
}
},
{
body: t.Object({
name: t.Optional(t.String()),
image: t.Optional(t.String()),
}),
response: {
200: t.Object({
user: t.Object({
id: t.String(),
name: t.Any(),
email: t.String(),
image: t.Any(),
role: t.Any(),
}),
},
{
body: t.Object({
name: t.Optional(t.String()),
image: t.Optional(t.String()),
}),
401: t.Object({ error: t.String() }),
500: t.Object({ error: t.String() }),
},
response: {
200: t.Object({
user: t.Object({
id: t.String(),
name: t.Any(),
email: t.String(),
image: t.Any(),
role: t.Any(),
}),
}),
401: t.Object({ error: t.String() }),
500: t.Object({ error: t.String() }),
},
detail: {
summary: "Update user profile",
description: "Update the authenticated user's name or profile image",
detail: {
summary: "Update user profile",
description: "Update the authenticated user's name or profile image",
},
},
},
);
);

View File

@@ -1,4 +1,4 @@
import Elysia from "elysia";
import Elysia, { t } from "elysia";
import { prisma } from "../utils/db";
import logger from "../utils/logger";
@@ -22,6 +22,16 @@ export const resident = new Elysia({
}
},
{
response: {
200: t.Object({
data: t.Object({
total: t.Number(),
heads: t.Number(),
poor: t.Number(),
}),
}),
500: t.Object({ error: t.String() }),
},
detail: { summary: "Get resident statistics" },
},
)
@@ -46,6 +56,12 @@ export const resident = new Elysia({
}
},
{
response: {
200: t.Object({
data: t.Array(t.Any()),
}),
500: t.Object({ error: t.String() }),
},
detail: { summary: "Get population data per banjar" },
},
)
@@ -69,7 +85,7 @@ export const resident = new Elysia({
take: 10,
}),
// Group by age ranges (simplified calculation)
prisma.$queryRaw<any[]>`
prisma.$queryRaw<{ range: string; count: number }[]>`
SELECT
CASE
WHEN date_part('year', age(now(), "birthDate")) BETWEEN 0 AND 16 THEN '0-16'
@@ -94,6 +110,17 @@ export const resident = new Elysia({
}
},
{
response: {
200: t.Object({
data: t.Object({
religion: t.Array(t.Any()),
gender: t.Array(t.Any()),
occupation: t.Array(t.Any()),
ageGroups: t.Array(t.Any()),
}),
}),
500: t.Object({ error: t.String() }),
},
detail: {
summary:
"Get demographics including religion, gender, occupation and age",