init files
This commit is contained in:
144
apps/Backend/src/routes/payments.ts
Normal file
144
apps/Backend/src/routes/payments.ts
Normal file
@@ -0,0 +1,144 @@
|
||||
import { Router } from "express";
|
||||
import { Request, Response } from "express";
|
||||
import { storage } from "../storage";
|
||||
import { z } from "zod";
|
||||
import { ClaimUncheckedCreateInputObjectSchema } from "@repo/db/usedSchemas";
|
||||
|
||||
const router = Router();
|
||||
|
||||
// Define Zod schemas
|
||||
const ClaimSchema = (
|
||||
ClaimUncheckedCreateInputObjectSchema as unknown as z.ZodObject<any>
|
||||
).omit({
|
||||
id: true,
|
||||
createdAt: true,
|
||||
updatedAt: true,
|
||||
});
|
||||
|
||||
type InsertClaim = z.infer<typeof ClaimSchema>;
|
||||
|
||||
const updateClaimSchema = (
|
||||
ClaimUncheckedCreateInputObjectSchema as unknown as z.ZodObject<any>
|
||||
)
|
||||
.omit({
|
||||
id: true,
|
||||
createdAt: true,
|
||||
updatedAt: true,
|
||||
})
|
||||
.partial();
|
||||
|
||||
type UpdateClaim = z.infer<typeof updateClaimSchema>;
|
||||
|
||||
|
||||
// GET /api/payments/recent
|
||||
router.get('/recent', async (req, res) => {
|
||||
try {
|
||||
const userId = req.user.id;
|
||||
const limit = parseInt(req.query.limit as string) || 10;
|
||||
const offset = parseInt(req.query.offset as string) || 0;
|
||||
|
||||
const [payments, totalCount] = await Promise.all([
|
||||
storage.getRecentPaymentsByUser(userId, limit, offset),
|
||||
storage.getTotalPaymentCountByUser(userId),
|
||||
]);
|
||||
|
||||
res.json({ payments, totalCount });
|
||||
} catch (err) {
|
||||
console.error('Failed to fetch payments:', err);
|
||||
res.status(500).json({ message: 'Failed to fetch recent payments' });
|
||||
}
|
||||
});
|
||||
|
||||
// GET /api/payments/claim/:claimId
|
||||
router.get('/claim/:claimId', async (req: Request, res: Response): Promise<any> => {
|
||||
try {
|
||||
const userId = req.user.id;
|
||||
const claimId = parseInt(req.params.claimId);
|
||||
|
||||
const payment = await storage.getPaymentByClaimId(userId, claimId);
|
||||
if (!payment) return res.status(404).json({ message: 'Payment not found' });
|
||||
|
||||
res.json(payment);
|
||||
} catch (err) {
|
||||
console.error('Failed to fetch payment by claim:', err);
|
||||
res.status(500).json({ message: 'Failed to fetch payment' });
|
||||
}
|
||||
});
|
||||
|
||||
// GET /api/payments/patient/:patientId
|
||||
router.get('/patient/:patientId', async (req, res) => {
|
||||
try {
|
||||
const userId = req.user.id;
|
||||
const patientId = parseInt(req.params.patientId);
|
||||
|
||||
const payments = await storage.getPaymentsByPatientId(userId, patientId);
|
||||
res.json(payments);
|
||||
} catch (err) {
|
||||
console.error('Failed to fetch patient payments:', err);
|
||||
res.status(500).json({ message: 'Failed to fetch patient payments' });
|
||||
}
|
||||
});
|
||||
|
||||
// GET /api/payments/filter
|
||||
router.get('/filter', async (req, res) => {
|
||||
try {
|
||||
const userId = req.user.id;
|
||||
const { from, to } = req.query;
|
||||
const fromDate = new Date(from as string);
|
||||
const toDate = new Date(to as string);
|
||||
|
||||
const payments = await storage.getPaymentsByDateRange(userId, fromDate, toDate);
|
||||
res.json(payments);
|
||||
} catch (err) {
|
||||
console.error('Failed to filter payments:', err);
|
||||
res.status(500).json({ message: 'Failed to filter payments' });
|
||||
}
|
||||
});
|
||||
|
||||
// POST /api/payments/:claimId
|
||||
router.post('/:claimId', body('totalBilled').isDecimal(),(req: Request, res: Response): Promise<any> => {
|
||||
const errors = validationResult(req);
|
||||
if (!errors.isEmpty()) return res.status(400).json({ errors: errors.array() });
|
||||
|
||||
try {
|
||||
const userId = req.user.id;
|
||||
const claimId = parseInt(req.params.claimId);
|
||||
const { totalBilled } = req.body;
|
||||
|
||||
const payment = await storage.createPayment({ userId, claimId, totalBilled });
|
||||
res.status(201).json(payment);
|
||||
} catch (err) {
|
||||
console.error('Failed to create payment:', err);
|
||||
res.status(500).json({ message: 'Failed to create payment' });
|
||||
}
|
||||
});
|
||||
|
||||
// PUT /api/payments/:id
|
||||
router.put('/:id', async (req, res) => {
|
||||
try {
|
||||
const userId = req.user.id;
|
||||
const id = parseInt(req.params.id);
|
||||
const updates = req.body;
|
||||
|
||||
const updated = await storage.updatePayment(userId, id, updates);
|
||||
res.json(updated);
|
||||
} catch (err) {
|
||||
console.error('Failed to update payment:', err);
|
||||
res.status(500).json({ message: 'Failed to update payment' });
|
||||
}
|
||||
});
|
||||
|
||||
// DELETE /api/payments/:id
|
||||
router.delete('/:id', async (req, res) => {
|
||||
try {
|
||||
const userId = req.user.id;
|
||||
const id = parseInt(req.params.id);
|
||||
await storage.deletePayment(userId, id);
|
||||
res.json({ message: 'Payment deleted' });
|
||||
} catch (err) {
|
||||
console.error('Failed to delete payment:', err);
|
||||
res.status(500).json({ message: 'Failed to delete payment' });
|
||||
}
|
||||
});
|
||||
|
||||
export default router;
|
||||
@@ -9,8 +9,13 @@ import {
|
||||
PdfFileUncheckedCreateInputObjectSchema,
|
||||
PdfGroupUncheckedCreateInputObjectSchema,
|
||||
PdfCategorySchema,
|
||||
PaymentUncheckedCreateInputObjectSchema,
|
||||
PaymentTransactionCreateInputObjectSchema,
|
||||
ServiceLinePaymentCreateInputObjectSchema,
|
||||
} from "@repo/db/usedSchemas";
|
||||
import { z } from "zod";
|
||||
import { Prisma } from "@repo/db/generated/prisma";
|
||||
|
||||
|
||||
//creating types out of schema auto generated.
|
||||
type Appointment = z.infer<typeof AppointmentUncheckedCreateInputObjectSchema>;
|
||||
@@ -160,6 +165,41 @@ export interface ClaimPdfMetadata {
|
||||
uploadedAt: Date;
|
||||
}
|
||||
|
||||
// Base Payment type
|
||||
type Payment = z.infer<typeof PaymentUncheckedCreateInputObjectSchema>;
|
||||
type PaymentTransaction = z.infer<typeof PaymentTransactionCreateInputObjectSchema>;
|
||||
type ServiceLinePayment = z.infer<typeof ServiceLinePaymentCreateInputObjectSchema>
|
||||
|
||||
|
||||
const insertPaymentSchema = (
|
||||
PaymentUncheckedCreateInputObjectSchema as unknown as z.ZodObject<any>
|
||||
).omit({
|
||||
id: true,
|
||||
createdAt: true,
|
||||
updatedAt: true,
|
||||
});
|
||||
type InsertPayment = z.infer<typeof insertPaymentSchema>;
|
||||
|
||||
|
||||
const updatePaymentSchema = (
|
||||
PaymentUncheckedCreateInputObjectSchema as unknown as z.ZodObject<any>
|
||||
)
|
||||
.omit({
|
||||
id: true,
|
||||
createdAt: true,
|
||||
})
|
||||
.partial();
|
||||
type UpdatePayment = z.infer<typeof updatePaymentSchema>;
|
||||
|
||||
|
||||
type PaymentWithExtras = Prisma.PaymentGetPayload<{
|
||||
include: {
|
||||
transactions: true;
|
||||
servicePayments: true;
|
||||
claim: true;
|
||||
};
|
||||
}>;
|
||||
|
||||
export interface IStorage {
|
||||
// User methods
|
||||
getUser(id: number): Promise<User | undefined>;
|
||||
@@ -290,7 +330,7 @@ export interface IStorage {
|
||||
updates: Partial<Pick<PdfFile, "filename" | "pdfData">>
|
||||
): Promise<PdfFile | undefined>;
|
||||
|
||||
// Group management
|
||||
// PDF Group management
|
||||
createPdfGroup(
|
||||
patientId: number,
|
||||
title: string,
|
||||
@@ -309,6 +349,17 @@ export interface IStorage {
|
||||
updates: Partial<Pick<PdfGroup, "title" | "category">>
|
||||
): Promise<PdfGroup | undefined>;
|
||||
deletePdfGroup(id: number): Promise<boolean>;
|
||||
|
||||
// Payment methods:
|
||||
createPayment(data: InsertPayment): Promise<Payment>;
|
||||
updatePayment(id: number, updates: UpdatePayment): Promise<Payment>;
|
||||
deletePayment(id: number): Promise<void>;
|
||||
getPaymentById(id: number): Promise<PaymentWithExtras | null>;
|
||||
getPaymentByClaimId(claimId: number): Promise<PaymentWithExtras | null>;
|
||||
getPaymentsByPatientId(patientId: number, userId: number): Promise<PaymentWithExtras[]>;
|
||||
getRecentPaymentsByUser(userId: number, limit: number, offset: number): Promise<PaymentWithExtras[]>;
|
||||
getPaymentsByDateRange(userId: number, from: Date, to: Date): Promise<PaymentWithExtras[]>;
|
||||
getTotalPaymentCountByUser(userId: number): Promise<number>;
|
||||
}
|
||||
|
||||
export const storage: IStorage = {
|
||||
@@ -831,4 +882,91 @@ export const storage: IStorage = {
|
||||
return false;
|
||||
}
|
||||
},
|
||||
|
||||
// Payment Methods
|
||||
|
||||
async createPayment(payment: InsertPayment): Promise<Payment> {
|
||||
return db.payment.create({ data: payment as Payment });
|
||||
},
|
||||
|
||||
async updatePayment(id: number, updates: UpdatePayment): Promise<Payment> {
|
||||
return db.payment.update({ where: { id }, data: updates });
|
||||
},
|
||||
|
||||
async deletePayment(id: number): Promise<void> {
|
||||
await db.payment.delete({ where: { id } });
|
||||
},
|
||||
|
||||
async getPaymentById(id: number): Promise<PaymentWithExtras | null> {
|
||||
return db.payment.findUnique({
|
||||
where: { id },
|
||||
include: {
|
||||
claim: true,
|
||||
transactions: true,
|
||||
servicePayments: true,
|
||||
},
|
||||
});
|
||||
},
|
||||
|
||||
async getPaymentByClaimId(claimId: number): Promise<PaymentWithExtras | null> {
|
||||
return db.payment.findFirst({
|
||||
where: { claimId },
|
||||
include: {
|
||||
claim: true,
|
||||
transactions: true,
|
||||
servicePayments: true,
|
||||
},
|
||||
});
|
||||
},
|
||||
|
||||
|
||||
async getPaymentsByPatientId(patientId: number, userId: number): Promise<PaymentWithExtras[]> {
|
||||
return db.payment.findMany({
|
||||
where: {
|
||||
patientId,
|
||||
userId,
|
||||
},
|
||||
include: {
|
||||
claim: true,
|
||||
transactions: true,
|
||||
servicePayments: true,
|
||||
},
|
||||
});
|
||||
},
|
||||
|
||||
async getRecentPaymentsByUser(userId: number, limit: number, offset: number): Promise<PaymentWithExtras[]> {
|
||||
return db.payment.findMany({
|
||||
where: { userId },
|
||||
orderBy: { createdAt: "desc" },
|
||||
skip: offset,
|
||||
take: limit,
|
||||
include: {
|
||||
claim: true,
|
||||
transactions: true,
|
||||
servicePayments: true,
|
||||
},
|
||||
});
|
||||
},
|
||||
|
||||
async getPaymentsByDateRange(userId: number, from: Date, to: Date): Promise<PaymentWithExtras[]> {
|
||||
return db.payment.findMany({
|
||||
where: {
|
||||
userId,
|
||||
createdAt: {
|
||||
gte: from,
|
||||
lte: to,
|
||||
},
|
||||
},
|
||||
orderBy: { createdAt: "desc" },
|
||||
include: {
|
||||
claim: true,
|
||||
transactions: true,
|
||||
servicePayments: true,
|
||||
},
|
||||
});
|
||||
},
|
||||
|
||||
async getTotalPaymentCountByUser(userId: number): Promise<number> {
|
||||
return db.payment.count({ where: { userId } });
|
||||
},
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user