forked from hngprojects/hng_boilerplate_expressjs
-
Notifications
You must be signed in to change notification settings - Fork 0
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 hngprojects#80 from PreciousIfeaka/feat/4-create_o…
…rganisation [feat] Create organisation resource
- Loading branch information
Showing
8 changed files
with
165 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,59 @@ | ||
import { Entity, PrimaryGeneratedColumn, Column, ManyToMany } from "typeorm"; | ||
import { User } from "./user"; | ||
import { Entity, PrimaryGeneratedColumn, Column, OneToMany, ManyToMany, BeforeInsert, UpdateDateColumn } from "typeorm"; | ||
import { User } from "."; | ||
import {v4 as uuidv4} from "uuid" | ||
import { UserOrganization } from "./user-organisation"; | ||
import ExtendedBaseEntity from "./extended-base-entity"; | ||
|
||
@Entity() | ||
export class Organization extends ExtendedBaseEntity { | ||
@PrimaryGeneratedColumn("uuid") | ||
id: string; | ||
|
||
@Column({ unique: true }) | ||
slug: string; | ||
|
||
@Column() | ||
name: string; | ||
|
||
@Column() | ||
@Column({ nullable: true }) | ||
email: string; | ||
|
||
@Column({ nullable: true }) | ||
industry: string; | ||
|
||
@Column({ nullable: true }) | ||
type: string; | ||
|
||
@Column({ nullable: true }) | ||
country: string; | ||
|
||
@Column({ nullable: true }) | ||
address: string; | ||
|
||
@Column({ nullable: true }) | ||
state: string; | ||
|
||
@Column('text', { nullable: true }) | ||
description: string; | ||
|
||
@UpdateDateColumn() | ||
created_at: Date; | ||
|
||
@UpdateDateColumn() | ||
updated_at: Date; | ||
|
||
@Column('uuid') | ||
owner_id: string; | ||
|
||
@OneToMany(() => UserOrganization, userOrganization => userOrganization.organization) | ||
userOrganizations: UserOrganization[]; | ||
|
||
@ManyToMany(() => User, (user) => user.organizations) | ||
users: User[]; | ||
|
||
@BeforeInsert() | ||
generateSlug() { | ||
this.slug = uuidv4(); | ||
} | ||
} | ||
|
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