-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #170 from Shabzynana/feat/132-update_organisaion_s…
…uperAdmin Feat/132 update organisation super admin
- Loading branch information
Showing
7 changed files
with
103 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters