hngprojects / hng_boilerplate_nestjs

Description
Apache License 2.0
181 stars 105 forks source link

[FEAT] API Endpoint For Organisation Creation by users - Backend #102

Closed Cyberguru1 closed 1 week ago

Cyberguru1 commented 1 month ago

Description

Develop a backend API for creating organisations, including authentication and authorization checks, input validation, and error handling.

Acceptance Criteria

  1. Authentication Middleware

    • Implement middleware to ensure the user is authenticated using JWT.
    • If authentication fails, return a 401 Unauthorized status code.
    • If authenticated, proceed with the request.
  2. Create Organisation Endpoint

    • Accepts HTTP POST requests at /api/v1/organizations.
    • Creates a new organisation with a 201 Created status code if the user request is valid.
  3. Feild Validation

    • Validate all supplied fields.
    • If validation fails, return a 422 Unprocessable Entity status code with detailed error messages.

Request Body

{
  "name": "String",
  "description": "String",
  "email": "string",
  "industry": "string",
  "type": "string",
  "country": "string",
  "address": "string",
  "state": "string",

}

Successful Response

{
  "status": "success",
  "message": "organisation created successfully",
  "data": {
    "id": "String",
    "name": "String",
    "description": "String",
    "owner_id": "string",
    "slug" : "string",
    "email": "string",
    "industry": "string",
    "type": "string",
    "country": "string",
    "address": "string",
    "state": "string",
    "created_at": "2024-01-01T12:00:00Z",
    "updated_at": "2024-06-01T12:00:00Z"
  },
 "status_code": 201
}

Unsuccessful Response

{
  "status": "Bad Request",
  "message": "Client error",
  "status_code": 400
}

Validation Error Response

{
  "errors": [
    {
      "field": "String",
      "message": "String"
    }
  ]
}

Unauthenticated Error Response

{
  "status": "Unauthorized",
  "message": "User not authenticated",
  "status_code": 401
}

Database Design

Table organisations {
  id uuid [primary key]
  slug varchar(255) [unique]
  owner_id uuid [foreign key to users(id)]
  name varchar(255)
  email varchar(255)
  industry varchar(255)
  type varchar(255)
  country varchar(255)
  address varchar(255)
  state varchar(255)
  description text
  created_at timestamp
  updated_at timestamp
}

Table organisations_user {
  user_id uuid [foreign key to users(id)]
  organisation_id uuid [foreign key to organisations(id)]
  role varchar(255)
  created_at timestamp
  updated_at timestamp
}

Ref: users.id < organisations_user.user_id
Ref: organisations.id < organisations_user.organisation_id

Purpose

Provide a backend service that allows admins to create new organisations, ensuring proper authentication, authorization, and validation.

Requirements

Expected Outcome

The API endpoint allows users to create new organisations with appropriate validation and authentication

Task

Testing

markessien commented 1 month ago

Orgs are not by admin only. You need to link the org owner. Do not use nigeria specific things like LGA. Org names are not unique. You need a unique org slug

markessien commented 1 month ago

IS this not a duplicate?

Cyberguru1 commented 1 month ago

we confirmed before creating, it's not sir

buka4rill commented 1 month ago

Follow this format please https://hng11.slack.com/archives/C07C462K430/p1721213593284649

Cyberguru1 commented 1 month ago

Updated sir @buka4rill

nedssoft commented 1 month ago

@Cyberguru1 You don't need to prefix the fields with org_. The table name is already organisations, hence the org_ prefixes are redundant. It should be something like

Table organisations {
  id uuid [primary key]
  slug varchar(255) [unique]
  owner_id uuid [foreign key to users(userId)]
  name varchar(255) [unique]
  email varchar(255)
  industry varchar(255)
  type varchar(255)
  country varchar(255)
  address varchar(255)
  state varchar(255)
  description text
  created_at timestamp
  updated_at timestamp
}
Cyberguru1 commented 1 month ago

done sir @nedssoft

NdubuisiJr commented 1 month ago

@Cyberguru1 name should not be unique. Organisation names are not unique. Also, organisation should have a many-to-many relationship with user table

Cyberguru1 commented 1 month ago

Update it sir @NdubuisiJr

Cyberguru1 commented 1 month ago

Test case done by QA tester link