From b8d664b4d42a7df3f78f7b3b4f95f0c965728851 Mon Sep 17 00:00:00 2001 From: MAC Date: Tue, 23 Jul 2024 14:32:54 +0100 Subject: [PATCH] delete organisation: superadmin --- src/controllers/AdminController.ts | 48 ++++++++++++++++++++++++++++-- src/controllers/index.ts | 2 ++ src/index.ts | 5 +++- src/routes/admin.ts | 15 ++++++++-- src/routes/index.ts | 1 + src/services/admin.services.ts | 38 +++++++++++++++++++++-- src/services/index.ts | 1 + 7 files changed, 103 insertions(+), 7 deletions(-) diff --git a/src/controllers/AdminController.ts b/src/controllers/AdminController.ts index d0d378d1..25611a03 100644 --- a/src/controllers/AdminController.ts +++ b/src/controllers/AdminController.ts @@ -1,2 +1,46 @@ -// src -console.log("hello") \ No newline at end of file +// src/controllers/UserController.ts +import { Request, Response } from "express"; +import { AdminOrganisationService } from "../services"; +import { HttpError } from "../middleware"; + +class AdminOrganisationController { + private adminService: AdminOrganisationService; + + constructor() { + this.adminService = new AdminOrganisationService(); + } + + async updateOrg(req: Request, res: Response): Promise { + try { + const org = await this.adminService.update(req); + res.status(200).json({ + success: true, + message: "Organisation Updated Successfully", + data: { + id: org.id, + name: org.name, + email: org.email, + slug: org.slug, + type: org.type, + industry: org.industry, + state: org.state, + country: org.country, + address: org.address, + created_at: org.created_at, + updated_at: org.updated_at, + }, + status_code: 200, + }); + } catch (error) { + if (error instanceof HttpError) { + res.status(error.status_code).json({ message: error.message }); + } else { + res + .status(500) + .json({ message: error.message || "Internal Server Error" }); + } + } + } +} + +export default AdminOrganisationController; diff --git a/src/controllers/index.ts b/src/controllers/index.ts index 0b8a2507..62b29fb8 100644 --- a/src/controllers/index.ts +++ b/src/controllers/index.ts @@ -6,3 +6,5 @@ export * from "./NotificationController" export * from "./HelpController"; export * from "./NotificationController"; export * from "./roleController" +export * from "./AdminController" + diff --git a/src/index.ts b/src/index.ts index cd153fbb..1098f56c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -14,7 +14,8 @@ import { notificationRouter, smsRouter, productRouter, - jobRouter + jobRouter, + adminRouter } from "./routes"; import { routeNotFound, errorHandler } from "./middleware"; import { orgRouter } from "./routes/organisation"; @@ -45,6 +46,7 @@ server.use(express.json()); server.get("/", (req: Request, res: Response) => { res.send("Hello world"); }); +server.use("/api/v1/admin", adminRouter); server.use("/api/v1", userRouter, orgRouter, organisationRoute); server.use("/api/v1/auth", authRoute); server.use("/api/v1/help-center", helpRouter); @@ -57,6 +59,7 @@ server.use(errorHandler); server.use("/api/v1/settings", notificationRouter); server.use("/api/v1/jobs", jobRouter); + AppDataSource.initialize() .then(async () => { // seed().catch(log.error); diff --git a/src/routes/admin.ts b/src/routes/admin.ts index d0d378d1..549730e1 100644 --- a/src/routes/admin.ts +++ b/src/routes/admin.ts @@ -1,2 +1,13 @@ -// src -console.log("hello") \ No newline at end of file +import { Router } from "express"; +import AdminOrganisationController from "../controllers/AdminController"; +import { authMiddleware, checkPermissions, } from "../middleware"; +import { UserRole } from "../enums/userRoles"; + +const adminRouter = Router(); +const adminOrganisationController = new AdminOrganisationController(); + +adminRouter.patch("/organisation/:id", authMiddleware, +checkPermissions([UserRole.SUPER_ADMIN]), +adminOrganisationController.updateOrg.bind(adminOrganisationController)); + +export { adminRouter }; \ No newline at end of file diff --git a/src/routes/index.ts b/src/routes/index.ts index 9370b286..f323ded3 100644 --- a/src/routes/index.ts +++ b/src/routes/index.ts @@ -9,3 +9,4 @@ export * from "./sms"; export * from "./sms"; export * from "./notificationsettings"; export * from "./job"; +export * from "./admin"; diff --git a/src/services/admin.services.ts b/src/services/admin.services.ts index 2be77d0c..6d5f1857 100644 --- a/src/services/admin.services.ts +++ b/src/services/admin.services.ts @@ -1,2 +1,36 @@ -// aa -console.log("hello") \ No newline at end of file +// / src/services/AdminOrganisationService.ts +import { NextFunction, Request, Response } from "express"; +import { User, Organization } from "../models"; +import AppDataSource from "../data-source"; +import { HttpError } from "../middleware"; + +export class AdminOrganisationService { + + public async update(req: Request): Promise { + try { + const { name, email, industry, type, country, address, state } = req.body; + const org_id = req.params.id; + + const orgRepository = AppDataSource.getRepository(Organization); + // Check if organisation exists + const oldOrg = await orgRepository.findOne({ + where: { id: org_id }, + }); + if (!oldOrg) { + throw new HttpError(404, "Not Found"); + } + + //Update Organisation on DB + await orgRepository.update(org_id, { name, email, industry, type, country, address, state }); + //Fetch Updated organisation + const newOrg = await orgRepository.findOne({ + where: { id: org_id }, + }); + return newOrg; + } catch (error) { + console.error(error); + throw new HttpError(error.status || 500, error.message || error); + } + } +} + diff --git a/src/services/index.ts b/src/services/index.ts index 970c5d39..b061a135 100644 --- a/src/services/index.ts +++ b/src/services/index.ts @@ -2,3 +2,4 @@ export * from './auth.services'; export * from './user.services'; export * from './help.services'; export * from './product.services'; +export * from './admin.services';