Skip to content

Commit

Permalink
Merge pull request hngprojects#480 from Ibrahim4Grace/create-org
Browse files Browse the repository at this point in the history
feat: create organization
  • Loading branch information
AdeGneus authored Aug 7, 2024
2 parents 394e797 + 22dc0ad commit 19d9c49
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 25 deletions.
21 changes: 13 additions & 8 deletions src/controllers/OrgController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class OrgController {

/**
* @swagger
* /api/v1/organisations:
* /api/v1/organizations:
* post:
* summary: Create a new organisation
* description: This endpoint allows a user to create a new organisation
Expand Down Expand Up @@ -44,6 +44,9 @@ export class OrgController {
* country:
* type: string
* example: Nigeria
* address:
* type: string
* example: 121 ikeja
* state:
* type: string
* example: Oyo
Expand All @@ -54,6 +57,7 @@ export class OrgController {
* - industry
* - type
* - country
* - address
* - state
* responses:
* '201':
Expand Down Expand Up @@ -93,6 +97,9 @@ export class OrgController {
* country:
* type: string
* example: Nigeria
* address:
* type: string
* example: 121 ikeja
* state:
* type: string
* example: Oyo
Expand Down Expand Up @@ -149,8 +156,6 @@ export class OrgController {
* type: http
* scheme: bearer
* bearerFormat: JWT
*
*
*/

async createOrganisation(req: Request, res: Response, next: NextFunction) {
Expand All @@ -160,15 +165,15 @@ export class OrgController {
const userId = user.id;

const organisationService = new OrgService();
const newOrganisation = await organisationService.createOrganisation(
const new_organisation = await organisationService.createOrganisation(
payload,
userId,
);

const respObj = {
status: "success",
message: "organisation created successfully",
data: newOrganisation,
data: new_organisation,
status_code: 201,
};

Expand All @@ -180,7 +185,7 @@ export class OrgController {

/**
* @swagger
* /api/v1/users/{userId}/organisations:
* /api/v1/users/{userId}/organizations:
* get:
* summary: Get user organizations
* description: Retrieve all organizations associated with a specific user
Expand Down Expand Up @@ -295,7 +300,7 @@ export class OrgController {

/**
* @swagger
* /api/v1/organisations/{org_id}:
* /api/v1/organizations/{org_id}:
* get:
* summary: Get a single organization
* description: Retrieve details of a specific organization by its ID
Expand Down Expand Up @@ -401,7 +406,7 @@ export class OrgController {

/**
* @swagger
* /api/v1/organisations/{org_id}/user/{user_id}:
* /api/v1/organizations/{org_id}/user/{user_id}:
* delete:
* summary: Remove a user from an organization
* description: Delete a user from a specific organization by user ID and organization ID
Expand Down
1 change: 1 addition & 0 deletions src/middleware/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./error";
export * from "./auth";
export * from "./checkUserRole";
export * from "./organizationValidation";
File renamed without changes.
17 changes: 10 additions & 7 deletions src/routes/organisation.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
import Router from "express";
import { OrgController } from "../controllers/OrgController";
import { authMiddleware, checkPermissions } from "../middleware";
import { UserRole } from "../enums/userRoles";
import { organizationValidation } from "../middleware/organization.validation";
import { validateOrgId } from "../middleware/organization.validation";
import {
authMiddleware,
checkPermissions,
organizationValidation,
validateOrgId,
} from "../middleware";

const orgRouter = Router();
const orgController = new OrgController();

orgRouter.get(
"/organisations/:org_id",
"/organizations/:org_id",
authMiddleware,
validateOrgId,
orgController.getSingleOrg.bind(orgController),
);
orgRouter.delete(
"/organisations/:org_id/user/:user_id",
"/organizations/:org_id/user/:user_id",
authMiddleware,
validateOrgId,
orgController.removeUser.bind(orgController),
Expand All @@ -28,7 +31,7 @@ orgRouter.get(
orgController.generateInviteLink.bind(orgController),
);
orgRouter.post(
"/organisations",
"/organizations",
authMiddleware,
organizationValidation,
orgController.createOrganisation.bind(orgController),
Expand All @@ -47,7 +50,7 @@ orgRouter.post(
orgController.sendInviteLinks.bind(orgController),
);
orgRouter.get(
"/users/:id/organisations",
"/users/:id/organizations",
authMiddleware,
orgController.getOrganizations.bind(orgController),
);
Expand Down
2 changes: 1 addition & 1 deletion src/routes/product.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Router } from "express";
import { ProductController } from "../controllers/ProductController";
import { authMiddleware } from "../middleware";
import { validateProductDetails } from "../middleware/product";
import { validateUserToOrg } from "../middleware/organization.validation";
import { validateUserToOrg } from "../middleware/organizationValidation";
import { adminOnly } from "../middleware";

const productRouter = Router();
Expand Down
13 changes: 5 additions & 8 deletions src/services/org.services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,29 @@ import renderTemplate from "../views/email/renderTemplate";
import { Conflict, ResourceNotFound } from "../middleware/error";
import config from "../config/index";
const frontendBaseUrl = config.BASE_URL;

export class OrgService implements IOrgService {
public async createOrganisation(
payload: ICreateOrganisation,
userId: string,
): Promise<{
newOrganisation: Partial<Organization>;
new_organisation: Partial<Organization>;
}> {
try {
const organisation = new Organization();
organisation.owner_id = userId;
Object.assign(organisation, payload);

const newOrganisation = await AppDataSource.manager.save(organisation);
const new_organisation = await AppDataSource.manager.save(organisation);

const userOrganization = new UserOrganization();
userOrganization.userId = userId;
userOrganization.organizationId = newOrganisation.id;
userOrganization.organizationId = new_organisation.id;
userOrganization.role = UserRole.ADMIN;

await AppDataSource.manager.save(userOrganization);

return { newOrganisation };
return { new_organisation };
} catch (error) {
throw new BadRequest("Client error");
}
Expand Down Expand Up @@ -282,10 +283,6 @@ export class OrgService implements IOrgService {
userOrganization.role = UserRole.USER;

await userOrganizationRepository.save(userOrganization);

// if (invitation) {
// await invitationRepository.remove(invitation);
// }
}

public async searchOrganizationMembers(criteria: {
Expand Down
2 changes: 1 addition & 1 deletion src/test/organisation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { AuthService } from "../services/index.ts";
import { authMiddleware } from "../middleware/auth.ts";
import { OrgService } from "../services/org.services.ts";
import { OrgController } from "../controllers/OrgController.ts";
import { validateOrgId } from "../middleware/organization.validation.ts";
import { validateOrgId } from "../middleware/organizationValidation.ts";
import { InvalidInput } from "../middleware/error.ts";
import { authMiddleware } from "../middleware";
import { OrgService } from "../services/organisation.service";
Expand Down

0 comments on commit 19d9c49

Please sign in to comment.