Skip to content

Commit

Permalink
Merge pull request #170 from Shabzynana/feat/132-update_organisaion_s…
Browse files Browse the repository at this point in the history
…uperAdmin

Feat/132 update organisation super admin
  • Loading branch information
Idimmusix authored Jul 23, 2024
2 parents 9c21012 + b8d664b commit bd6f9a0
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 1 deletion.
46 changes: 46 additions & 0 deletions src/controllers/AdminController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// 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<void> {
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;
2 changes: 2 additions & 0 deletions src/controllers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ export * from "./NotificationController"
export * from "./HelpController";
export * from "./NotificationController";
export * from "./roleController"
export * from "./AdminController"

5 changes: 4 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import {
notificationRouter,
smsRouter,
productRouter,
jobRouter
jobRouter,
adminRouter
} from "./routes";
import { routeNotFound, errorHandler } from "./middleware";
import { orgRouter } from "./routes/organisation";
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
13 changes: 13 additions & 0 deletions src/routes/admin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
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 };
1 change: 1 addition & 0 deletions src/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ export * from "./sms";
export * from "./sms";
export * from "./notificationsettings";
export * from "./job";
export * from "./admin";
36 changes: 36 additions & 0 deletions src/services/admin.services.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// / 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<Organization> {
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);
}
}
}

1 change: 1 addition & 0 deletions src/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from './auth.services';
export * from './user.services';
export * from './help.services';
export * from './product.services';
export * from './admin.services';

0 comments on commit bd6f9a0

Please sign in to comment.