feat: accreditation crud
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
-- CreateEnum
|
||||
CREATE TYPE "AccreditationType" AS ENUM ('ACCREDITATION', 'CERTIFICATION');
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "Accreditation" (
|
||||
"id" SERIAL NOT NULL,
|
||||
"title" TEXT NOT NULL,
|
||||
"type" "AccreditationType" NOT NULL,
|
||||
"logo" TEXT,
|
||||
"image" TEXT,
|
||||
"description" TEXT,
|
||||
"sortOrder" INTEGER NOT NULL DEFAULT 1000,
|
||||
"isActive" BOOLEAN NOT NULL DEFAULT true,
|
||||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"updatedAt" TIMESTAMP(3) NOT NULL,
|
||||
|
||||
CONSTRAINT "Accreditation_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
@@ -356,4 +356,25 @@ model InsurancePartner {
|
||||
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
}
|
||||
|
||||
model Accreditation {
|
||||
id Int @id @default(autoincrement())
|
||||
|
||||
title String
|
||||
type AccreditationType
|
||||
logo String?
|
||||
image String?
|
||||
description String? @db.Text
|
||||
|
||||
sortOrder Int @default(1000)
|
||||
isActive Boolean @default(true)
|
||||
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
}
|
||||
|
||||
enum AccreditationType {
|
||||
ACCREDITATION
|
||||
CERTIFICATION
|
||||
}
|
||||
@@ -18,6 +18,7 @@ import importRoutes from './routes/importRoutes.js';
|
||||
import healthCheckRoutes from './routes/healthCheck.route.js';
|
||||
import homepageBannerRoutes from './routes/homepageBanner.routes.js';
|
||||
import insurancePartnerRoutes from './routes/insurancePartner.routes.js';
|
||||
import accreditationRoutes from './routes/accreditation.routes.js';
|
||||
|
||||
dotenv.config();
|
||||
|
||||
@@ -61,6 +62,7 @@ app.use('/api/import', importRoutes);
|
||||
app.use('/api/health-check', healthCheckRoutes);
|
||||
app.use('/api/homepage-banners', homepageBannerRoutes);
|
||||
app.use('/api/insurance-partners', insurancePartnerRoutes);
|
||||
app.use('/api/accreditation', accreditationRoutes);
|
||||
|
||||
const PORT = process.env.PORT || 5008;
|
||||
app.listen(PORT, () => {
|
||||
|
||||
@@ -0,0 +1,189 @@
|
||||
import prisma from '../prisma/client.js';
|
||||
|
||||
export const createAccreditation = async (req, res) => {
|
||||
try {
|
||||
const { title, type, logo, image, description, sortOrder, isActive } = req.body;
|
||||
|
||||
if (!title || !type) {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: 'Title and type are required',
|
||||
});
|
||||
}
|
||||
|
||||
const accreditation = await prisma.accreditation.create({
|
||||
data: {
|
||||
title,
|
||||
type,
|
||||
logo,
|
||||
image,
|
||||
description,
|
||||
sortOrder: sortOrder ? Number(sortOrder) : 1000,
|
||||
isActive: isActive ?? true,
|
||||
},
|
||||
});
|
||||
|
||||
res.status(201).json({
|
||||
success: true,
|
||||
data: accreditation,
|
||||
message: 'Accreditation created successfully',
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('PRISMA TRANSACTION ERROR:', error);
|
||||
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: 'Failed to create accreditation',
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
export const getAccreditations = async (req, res) => {
|
||||
try {
|
||||
const { type } = req.query;
|
||||
|
||||
const accreditations = await prisma.accreditation.findMany({
|
||||
where: type
|
||||
? {
|
||||
type,
|
||||
}
|
||||
: undefined,
|
||||
orderBy: {
|
||||
sortOrder: 'asc',
|
||||
},
|
||||
});
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
data: accreditations,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: 'Failed to fetch accreditations',
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
export const getActiveAccreditations = async (req, res) => {
|
||||
try {
|
||||
const { type } = req.query;
|
||||
|
||||
const accreditations = await prisma.accreditation.findMany({
|
||||
where: {
|
||||
isActive: true,
|
||||
...(type && { type }),
|
||||
},
|
||||
orderBy: {
|
||||
sortOrder: 'asc',
|
||||
},
|
||||
});
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
data: accreditations,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: 'Failed to fetch accreditations',
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
export const getAccreditation = async (req, res) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
|
||||
const accreditation = await prisma.accreditation.findUnique({
|
||||
where: {
|
||||
id: Number(id),
|
||||
},
|
||||
});
|
||||
|
||||
if (!accreditation) {
|
||||
return res.status(404).json({
|
||||
success: false,
|
||||
message: 'Accreditation not found',
|
||||
});
|
||||
}
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
data: accreditation,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: 'Failed to fetch accreditation',
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
export const updateAccreditation = async (req, res) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
|
||||
const { title, type, logo, image, description, sortOrder, isActive } = req.body;
|
||||
|
||||
const updateData = {};
|
||||
|
||||
if (title !== undefined) updateData.title = title;
|
||||
if (type !== undefined) updateData.type = type;
|
||||
if (logo !== undefined) updateData.logo = logo;
|
||||
if (image !== undefined) updateData.image = image;
|
||||
if (description !== undefined) updateData.description = description;
|
||||
if (sortOrder !== undefined) updateData.sortOrder = Number(sortOrder);
|
||||
if (isActive !== undefined) updateData.isActive = isActive;
|
||||
|
||||
const accreditation = await prisma.accreditation.update({
|
||||
where: {
|
||||
id: Number(id),
|
||||
},
|
||||
data: updateData,
|
||||
});
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
data: accreditation,
|
||||
message: 'Accreditation updated successfully',
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('PRISMA TRANSACTION ERROR:', error);
|
||||
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: 'Failed to update accreditation',
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
export const deleteAccreditation = async (req, res) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
|
||||
await prisma.accreditation.delete({
|
||||
where: {
|
||||
id: Number(id),
|
||||
},
|
||||
});
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
message: 'Accreditation deleted successfully',
|
||||
});
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: 'Failed to delete accreditation',
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,24 @@
|
||||
import express from 'express';
|
||||
|
||||
import {
|
||||
createAccreditation,
|
||||
getAccreditations,
|
||||
getActiveAccreditations,
|
||||
getAccreditation,
|
||||
updateAccreditation,
|
||||
deleteAccreditation,
|
||||
} from '../controllers/accreditation.controller.js';
|
||||
|
||||
import jwtAuthMiddleware from '../middleware/auth.js';
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
router.get('/active', getActiveAccreditations);
|
||||
|
||||
router.post('/', jwtAuthMiddleware, createAccreditation);
|
||||
router.get('/getAll', jwtAuthMiddleware, getAccreditations);
|
||||
router.get('/:id', jwtAuthMiddleware, getAccreditation);
|
||||
router.put('/:id', jwtAuthMiddleware, updateAccreditation);
|
||||
router.delete('/:id', jwtAuthMiddleware, deleteAccreditation);
|
||||
|
||||
export default router;
|
||||
Reference in New Issue
Block a user