npiProvider - v1
This commit is contained in:
@@ -4,6 +4,7 @@ import appointmentsRoutes from "./appointments";
|
||||
import appointmentProceduresRoutes from "./appointments-procedures";
|
||||
import usersRoutes from "./users";
|
||||
import staffsRoutes from "./staffs";
|
||||
import npiProvidersRoutes from "./npiProviders";
|
||||
import claimsRoutes from "./claims";
|
||||
import patientDataExtractionRoutes from "./patientDataExtraction";
|
||||
import insuranceCredsRoutes from "./insuranceCreds";
|
||||
@@ -25,6 +26,7 @@ router.use("/appointments", appointmentsRoutes);
|
||||
router.use("/appointment-procedures", appointmentProceduresRoutes);
|
||||
router.use("/users", usersRoutes);
|
||||
router.use("/staffs", staffsRoutes);
|
||||
router.use("/npiProviders", npiProvidersRouter);
|
||||
router.use("/patientDataExtraction", patientDataExtractionRoutes);
|
||||
router.use("/claims", claimsRoutes);
|
||||
router.use("/insuranceCreds", insuranceCredsRoutes);
|
||||
|
||||
101
apps/Backend/src/routes/npiProviders.ts
Normal file
101
apps/Backend/src/routes/npiProviders.ts
Normal file
@@ -0,0 +1,101 @@
|
||||
import express, { Request, Response } from "express";
|
||||
import { z } from "zod";
|
||||
import { npiProviderStorage } from "../storage/npiProviders";
|
||||
import { insertNpiProviderSchema } from "@repo/db/types";
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
router.get("/", async (req: Request, res: Response) => {
|
||||
try {
|
||||
if (!req.user?.id) {
|
||||
return res.status(401).json({ message: "Unauthorized" });
|
||||
}
|
||||
|
||||
const providers = await npiProviderStorage.getNpiProvidersByUser(
|
||||
req.user.id,
|
||||
);
|
||||
res.status(200).json(providers);
|
||||
} catch (err) {
|
||||
res.status(500).json({
|
||||
error: "Failed to fetch NPI providers",
|
||||
details: String(err),
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
router.post("/", async (req: Request, res: Response) => {
|
||||
try {
|
||||
if (!req.user?.id) {
|
||||
return res.status(401).json({ message: "Unauthorized" });
|
||||
}
|
||||
|
||||
const parsed = insertNpiProviderSchema.safeParse({
|
||||
...req.body,
|
||||
userId: req.user.id,
|
||||
});
|
||||
|
||||
if (!parsed.success) {
|
||||
const flat = parsed.error.flatten();
|
||||
const firstError =
|
||||
Object.values(flat.fieldErrors)[0]?.[0] || "Invalid input";
|
||||
|
||||
return res.status(400).json({
|
||||
message: firstError,
|
||||
details: flat.fieldErrors,
|
||||
});
|
||||
}
|
||||
|
||||
const provider = await npiProviderStorage.createNpiProvider(parsed.data);
|
||||
res.status(201).json(provider);
|
||||
} catch (err: any) {
|
||||
if (err.code === "P2002") {
|
||||
return res.status(400).json({
|
||||
message: "This NPI already exists for the user",
|
||||
});
|
||||
}
|
||||
res.status(500).json({
|
||||
error: "Failed to create NPI provider",
|
||||
details: String(err),
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
router.put("/:id", async (req: Request, res: Response) => {
|
||||
try {
|
||||
const id = Number(req.params.id);
|
||||
if (isNaN(id)) return res.status(400).send("Invalid ID");
|
||||
|
||||
const provider = await npiProviderStorage.updateNpiProvider(id, req.body);
|
||||
res.status(200).json(provider);
|
||||
} catch (err) {
|
||||
res.status(500).json({
|
||||
error: "Failed to update NPI provider",
|
||||
details: String(err),
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
router.delete("/:id", async (req: Request, res: Response) => {
|
||||
try {
|
||||
if (!req.user?.id) {
|
||||
return res.status(401).json({ message: "Unauthorized" });
|
||||
}
|
||||
|
||||
const id = Number(req.params.id);
|
||||
if (isNaN(id)) return res.status(400).send("Invalid ID");
|
||||
|
||||
const ok = await npiProviderStorage.deleteNpiProvider(req.user.id, id);
|
||||
if (!ok) {
|
||||
return res.status(404).json({ message: "NPI provider not found" });
|
||||
}
|
||||
|
||||
res.status(204).send();
|
||||
} catch (err) {
|
||||
res.status(500).json({
|
||||
error: "Failed to delete NPI provider",
|
||||
details: String(err),
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
export default router;
|
||||
Reference in New Issue
Block a user