-
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.
- Loading branch information
Showing
8 changed files
with
187 additions
and
11 deletions.
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,24 @@ | ||
import { NextFunction, Request, Response } from "express"; | ||
import { OrganisationService } from "../services/createOrg.services"; | ||
|
||
export const createOrganisation = async (req: Request, res: Response, next: NextFunction) => { | ||
try { | ||
const payload = req.body; | ||
const user = req.user; | ||
const userId = user.id; | ||
|
||
const organisationService = new OrganisationService(); | ||
const newOrganisation = await organisationService.createOrganisation(payload, userId); | ||
|
||
const respObj = { | ||
status: "success", | ||
message: "organisation created successfully", | ||
data: newOrganisation, | ||
status_code: 201 | ||
} | ||
|
||
return res.status(201).json(respObj); | ||
} catch (error) { | ||
return next(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
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,28 @@ | ||
import { Entity, ManyToOne, Column, PrimaryColumn, JoinColumn } from 'typeorm'; | ||
import { User } from "./user"; | ||
import { Organization } from "./organization"; | ||
import { UserRole } from '../enums/userRoles'; | ||
import ExtendedBaseEntity from './extended-base-entity'; | ||
|
||
@Entity() | ||
export class UserOrganization extends ExtendedBaseEntity { | ||
@PrimaryColumn() | ||
userId: string; | ||
|
||
@PrimaryColumn() | ||
organizationId: string; | ||
|
||
@ManyToOne(() => User, user => user.userOrganizations) | ||
@JoinColumn({ name: 'userId' }) | ||
user: User; | ||
|
||
@ManyToOne(() => Organization, organization => organization.userOrganizations) | ||
@JoinColumn({ name: 'organizationId' }) | ||
organization: Organization; | ||
|
||
@Column({ | ||
type: "enum", | ||
enum: UserRole, | ||
}) | ||
role: UserRole; | ||
} |
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,9 @@ | ||
import { Router } from "express"; | ||
import { authMiddleware } from "../middleware"; | ||
import { createOrganisation } from "../controllers/createorgController" | ||
|
||
const organisationRoute = Router(); | ||
|
||
organisationRoute.post("/organisations", authMiddleware, createOrganisation); | ||
|
||
export { organisationRoute } |
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,38 @@ | ||
import AppDataSource from "../data-source"; | ||
import { Organization, User } from "../models"; | ||
import { UserRole } from "../enums/userRoles"; | ||
import { ICreateOrganisation, IOrganisationService } from "../types"; | ||
import { HttpError } from "../middleware"; | ||
import { UserOrganization } from "../models/user-organisation"; | ||
|
||
export class OrganisationService implements IOrganisationService { | ||
public async createOrganisation(payload: ICreateOrganisation, userId: string): Promise<{ | ||
newOrganisation: Partial<Organization>; | ||
}> { | ||
try { | ||
const organisation = new Organization(); | ||
organisation.owner_id = userId; | ||
Object.assign(organisation, payload); | ||
|
||
const newOrganisation = await AppDataSource.manager.save(organisation); | ||
|
||
const userOrganization = new UserOrganization(); | ||
userOrganization.userId = userId; | ||
userOrganization.organizationId = newOrganisation.id; | ||
userOrganization.role = UserRole.ADMIN; | ||
|
||
await AppDataSource.manager.save(userOrganization); | ||
|
||
const user = await AppDataSource | ||
.getRepository(User) | ||
.findOne({ where: { id: userId }, relations: ["userOrganizations", "userOrganizations.organization"] }); | ||
|
||
user.userOrganizations.push(userOrganization); | ||
await AppDataSource.getRepository(User).save(user); | ||
|
||
return { newOrganisation }; | ||
} catch (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