Skip to content

Paattis/ideabarrel-backend

Repository files navigation

IdeaBarrel

Backend for the App for the course TX00CK67-3010

Bootstrapped based on these instructions.

API documentation available at /docs endpoint

Development

To run the backend in development mode

  • install the necessary packages
$ npm install
  • create a local MySQL or MariaDB development database
  • populate the .env file
#App variables
APP_ENV=DEVELOPEMENT
# APP_ENV=PRODUCTION
# APP_ENV=CI

# only needed when running production
SERVER_IP= 

DATABASE_URL=(your database url)
PORT=(port)
ACCESS_TOKEN_SECRET=(your JWT secret)

# Admin user created with seeding script (/src/db/seed.ts)
ADMIN_EMAIL=(your admin email)
ADMIN_PW=(your admin password)
  • Run the Prisma migrations to add the tables to your development database
$ npx prisma db migrate dev
  • Create typescript types for Prisma autogen models (if changes to schema)
$ npx prisma generate
  • Run seed script (if not ran automatically when you migrated)
$ npx prisma db seed
  • build/test and then run the backend in development mode
$ npm run build
$ npm run dev

Updating in production

$ make update_prod

API Endpoints

Users

GET - /users

Summary

Get all of the user profiles.

Required Privileges

  • Authenticated users
  • Admin

Response application/json

[
  {
    "name": "Victor Mike",
    "profile_img": "",
    "email": "victor.mike@app.com",
    "id": 10,
    "created_at": "2022-11-23T17:23:24.903Z",
    "role": {
      "name": "Junior DevOps Engineer",
      "id": 1
    },
    "comments": [
      {
        "content": "Nice idea",
        "id": 2,
        "updated_at": "2022-11-24T10:36:10.190Z",
        "idea": {
          "id": 1
        }
      },
    ],
    "ideas": [
      {
        "id": 5,
        "created_at": "2022-11-29T14:19:22.952Z",
        "title": "New idea",
        "content": "Some cool idea, must be implemented."
      }
    ],
    "likes": [
      {
        "idea_id": 1
      }
    ]
  },
]

GET - /users/:id

Summary

Get user profile with specified id.

Required Privileges

  • Authenticated users
  • Admin

Response application/json

{
  "name": "Victor Mike",
  "profile_img": "",
  "email": "victor.mike@app.com",
  "id": 10,
  "created_at": "2022-11-23T17:23:24.903Z",
  "role": {
    "name": "Junior DevOps Engineer",
    "id": 1
  },
  "comments": [
    {
      "content": "Nice idea",
      "id": 2,
      "updated_at": "2022-11-24T10:36:10.190Z",
      "idea": {
        "id": 1
      }
    },
  ],
  "ideas": [
    {
      "id": 5,
      "created_at": "2022-11-29T14:19:22.952Z",
      "title": "New idea",
      "content": "Some cool idea, must be implemented."
    }
  ],
  "likes": [
    {
      "idea_id": 1
    }
  ]
},

POST - /users

Summary

Create new user profile.

Required Privileges

  • all

Request multipart/form-data

name:     Victor Mike,
role_id:  2,
password: PassWord123,
email:    victor.mike@app.com,
avatar:   image file

Response application/json

{
  "name": "Victor Mike",
  "profile_img": "1669052777822-668015599.jpg",
  "email": "victor.mike@app.com",
  "id": 10,
  "created_at": "2022-11-21T17:46:18.001Z",
  "role": {
    "name": "Junior DevOps Engineer",
    "id": 1
  },
  "comments": [],
  "ideas": [],
  "likes": []
}

PUT - /users/:id

Summary

Update user profile

Required Privileges

  • Authenticated users, who are also the target of the update
  • Admins

Request application/json

  {
    "name": "Micktor Vike",
    "email": "micktor.vike@app.com",
    "role_id": 8,
    "password": "NewPassword123"
  }

Response application/json

{
  "name": "Micktor Vike",
  "profile_img": "1669050855379-231410051.jpg",
  "email": "micktor.Vike@app.com",
  "id": 2,
  "created_at": "2022-11-21T15:02:10.929Z",
  "role": {
    "name": "Senior DevOps Engineer",
    "id":8
  },
  "ideas": [
    {
      "id": 5,
      "created_at": "2022-11-29T14:19:22.952Z",
      "title": "New idea",
      "content": "Some cool idea, must be implemented."
    }
  ],
  "comments": [
    {
      "content": "Nice idea",
      "id": 2,
      "updated_at": "2022-11-24T10:36:10.190Z",
      "idea": {
        "id": 1
      }
    },
  ],
  "likes": [
    {
      "idea_id": 1
    }
  ]
}

PUT - /users/:id/img

Summary

Adds image file as specified users avatar.

Required Privileges

  • same user as action target
  • admin

Request multipart/form-data

avatar: image file

Response application/json

{
  "name": "Micktor Vike",
  "profile_img": "NEW-IMG.jpg",
  "email": "micktor.Vike@app.com",
  "id": 2,
  "created_at": "2022-11-21T15:02:10.929Z",
  "role": {
    "name": "Senior DevOps Engineer",
    "id":8
  },
  "ideas": [
    {
      "id": 5,
      "created_at": "2022-11-29T14:19:22.952Z",
      "title": "New idea",
      "content": "Some cool idea, must be implemented."
    }
  ],
  "comments": [
    {
      "content": "Nice idea",
      "id": 2,
      "updated_at": "2022-11-24T10:36:10.190Z",
      "idea": {
        "id": 1
      }
    },
  ],
  "likes": [
    {
      "idea_id": 1
    }
  ]
}

DELETE - /users/:id/img

Summary

Deletes profile avatar from specified user, and returns that user.

Required Privileges

  • same user as action target
  • admin

Response application/json

{
  "name": "Micktor Vike",
  "profile_img": "",
  "email": "micktor.Vike@app.com",
  "id": 2,
  "created_at": "2022-11-21T15:02:10.929Z",
  "role": {
    "name": "Senior DevOps Engineer",
    "id":8
  },
  "ideas": [
    {
      "id": 5,
      "created_at": "2022-11-29T14:19:22.952Z",
      "title": "New idea",
      "content": "Some cool idea, must be implemented."
    }
  ],
  "comments": [
    {
      "content": "Nice idea",
      "id": 2,
      "updated_at": "2022-11-24T10:36:10.190Z",
      "idea": {
        "id": 1
      }
    },
  ],
  "likes": [
    {
      "idea_id": 1
    }
  ]
}

Authentication

POST - /auth/login

Summary

Authenticate user with email and password.

Required Privileges

  • none

Request application/json

{
  "email": "victor.mike@app.com",
  "password": "Password123"
}

Response application/json

{
  "name": "Victor Mike",
  "profile_img": "",
  "email": "victor.mike@app.com",
  "id": 10,
  "created_at": "2022-11-23T17:23:24.903Z",
  "role": {
    "name": "Junior DevOps Engineer",
    "id": 1
  },
  "comments": [
    {
      "content": "Nice idea",
      "id": 2,
      "updated_at": "2022-11-24T10:36:10.190Z",
      "idea": {
        "id": 1
      }
    },
  ],
  "ideas": [
    {
      "id": 5,
      "created_at": "2022-11-29T14:19:22.952Z",
      "title": "New idea",
      "content": "Some cool idea, must be implemented."
    }
  ],
  "likes": [
    {
      "idea_id": 1
    }
  ],
  "token": "JWT_TOKEN_WITH_ID_AND_ROLE_ID"
}

POST - /auth/login/token

Summary

Authenticate user with JWT (Bearer)

Required Privileges

  • Authenticated users
  • Admin

Response application/json

{
  "name": "Victor Mike",
  "profile_img": "",
  "email": "victor.mike@app.com",
  "id": 10,
  "created_at": "2022-11-23T17:23:24.903Z",
  "role": {
    "name": "Junior DevOps Engineer",
    "id": 1
  },
  "comments": [
    {
      "content": "Nice idea",
      "id": 2,
      "updated_at": "2022-11-24T10:36:10.190Z",
      "idea": {
        "id": 1
      }
    },
  ],
  "ideas": [
    {
      "id": 5,
      "created_at": "2022-11-29T14:19:22.952Z",
      "title": "New idea",
      "content": "Some cool idea, must be implemented."
    }
  ],
  "likes": [
    {
      "idea_id": 1
    }
  ],
  "token": "JWT_TOKEN_WITH_ID_AND_ROLE_ID"
}

Roles

POST - /roles

Summary

Create new role.

Required privileges

  • admin

Request application/json

{
  "name": "Senior Engineer"
}

Response application/json

{
  "id": 1,
  "name": "Senior Engineer",
  "users": []
}

PUT - /roles/:id

Summary

Update role with specifed id.

Required privileges

  • admin

Request application/json

{
  "name": "New name for role"
}

Response application/json

{
  "id": 1,
  "name": "New name for role",
  "users": [
    {
      "name": "Victor Mike",
      "id": 10
    },
  ]
}

GET - /roles

Summary

Get all of the roles.

Required privileges

  • authenticated user
  • admin

Response application/json

[
  {
    "id": 1,
    "name": "Senior Developer",
  },
  {
    "id": 2,
    "name": "Senior Engineer",
  },
]

GET - /roles?usr=1

Summary

Get all of the roles, with subscribed users attached to them.

Required privileges

  • authenticated user
  • admin

Response application/json

[
  {
    "id": 1,
    "name": "Senior Developer",
    "users": [
      {
        "name": "User 1",
        "id": 1
      }
    ]
  },
  {
    "id": 2,
    "name": "Senior Engineer",
    "users": [
      {
        "name": "User 2",
        "id": 3
      }
    ]
  },
]

GET - /roles/:id

Required privileges

  • authenticated user
  • admin Response application/json
{
  "id": 1,
  "name": "Senior Developer",
}

GET - /roles/:id?usr=1

Summary

Get role with specified id, with all users subscribed to it.

Required privileges

  • authenticated user
  • admin

Response application/json

{
  "id": 1,
  "name": "Senior Developer",
  "users": [
    {
      "name": "User 1",
      "id": 1
    }
  ]
}

DELETE - /roles/:id

Required privileges

  • admin

Response application/json

{
  "id": 1,
  "name": "Senior Developer",
},

Ideas

GET - /ideas

Summary

Get all of the existing ideas.

Querystring

?desc=likes               # or comments, or date
?asc=likes                # or comments, or date
?page_num=1
?tags=1,2,3

Response application/json

[
  {
    "id": 1,
    "title": "Add coffee machine",
    "content": "We really should have access to free coffee.",
    "created_at": "2022-11-23T17:52:40.243Z",
    "user": {
      "id": 1,
      "name": "John Doe"
    },
    "comments": [
      {
        "content": "Nice idea",
        "user": {
          "id": 10,
          "name": "Victor Mike"
        },
        "id": 2,
        "created_at": "2022-11-24T10:36:10.190Z"
      },
    ],
    "likes": [
      {
        "user_id": 1
      }
    ],
    "tags": [
      {
        "tag": {
          "name": "Cafeteria",
          "id": 15
        }
      },
    ]
  },
]

GET - /ideas/:id

Summary

Get idea with specified id.

Required privileges

  • authenticated user

Response application/json

{
    "id": 1,
    "title": "Add coffee machine",
    "content": "We really should have access to free coffee.",
    "created_at": "2022-11-23T17:52:40.243Z",
    "user": {
      "id": 1,
      "name": "John Doe"
    },
    "comments": [
      {
        "content": "Nice idea",
        "user": {
          "id": 10,
          "name": "Victor Mike"
        },
        "id": 2,
        "created_at": "2022-11-24T10:36:10.190Z"
      },
    ],
    "likes": [
      {
        "user_id": 1
      }
    ],
    "tags": [
      {
        "tag": {
          "name": "Cafeteria",
          "id": 15
        }
      },
    ]
  }

POST - /ideas

Summary

Create new idea. Required Privileges

  • authenticated user
  • admin

Request application/json

{
  "title": "New idea",
  "content": "Some cool idea, must be implemented.",
  "tags": [ 1, 17 ]
}

Response application/json

{
  "id": 5,
  "created_at": "2022-11-29T14:19:22.952Z",
  "comments": [],
  "user": {
    "id": 10,
    "name": "Victor Mike"
  },
  "content": "Some cool idea, must be implemented.",
  "likes": [
    {
      "idea_id": 1
    }
  ],
  "title": "New idea",
  "tags": [
    {
      "tag": {
        "name": "Management",
        "id": 1
      }
    },
    {
      "tag": {
        "name": "RnD",
        "id": 17
      }
    }
  ]
}

PUT - /ideas/:id

Summary

Update idea with specified id.

Required Privileges

  • authenticated owner
  • admin

Request application/json

{
  "title": "New idea (Updated)",
  "content": "Some cool idea, must be implemented. (Or not)",
  "tags": [ 1 ]
}

Response application/json

{
  "id": 5,
  "user": {
    "id": 10,
    "name": "Victor Mike"
  },
  "title": "New idea (Updated)",
  "content": "Some cool idea, must be implemented. (Or not)",
  "created_at": "2022-11-29T14:19:22.952Z",
  "comments": [
    {
      "content": "Not gonna happen",
      "user": {
        "id": 1,
        "name": "John Doe"
      },
      "id": 2,
      "created_at": "2022-11-24T10:36:10.190Z"
    },
  ],
  "likes": [
    {
      "user_id": 1
    }
  ],
  "tags": [
    {
      "tag": {
        "name": "Cafeteria",
        "id": 1
      }
    },
  ]
}

DELETE - /ideas/:id

Summary

Remove idea with specified id.

Required Privileges

  • authenticated owner
  • admin

Response application/json

{
  "id": 5,
  "user": {
    "id": 10,
    "name": "Victor Mike"
  },
  "title": "New idea (Updated)",
  "content": "Some cool idea, must be implemented. (Or not)",
  "created_at": "2022-11-29T14:19:22.952Z",
  "comments": [
    {
      "content": "Not gonna happen",
      "user": {
        "id": 1,
        "name": "John Doe"
      },
      "id": 2,
      "created_at": "2022-11-24T10:36:10.190Z"
    },
  ],
  "likes": [
    {
      "user_id": 1
    }
  ],
  "tags": [
    {
      "tag": {
        "name": "Cafeteria",
        "id": 1
      }
    },
  ]
}

Tags

GET - /tags

Summary

Get all of the existing tags.

Required Privileges

  • authenticated user

Response application/json

[
  {
    "id": 1,
    "name": "Food",
    "description": "Ideas related to food.",
  },
  {
    "id": 2,
    "name": "Management",
    "description": "Ideas related to management.",
  },
]

GET - /tags?usr=1

Summary

Get all of the existing tags, and include users who have subscribed to them.

Required Privileges

  • authenticated user

Response application/json

[
  {
    "id": 1,
    "name": "Food",
    "description": "Ideas related to food.",
    "users": [
      {
        "name": "Victor Mike",
        "id": 10
      }
    ]
  },
  {
    "id": 2,
    "name": "Management",
    "description": "Ideas related to management.",
    "users": [
      {
        "name": "John Doe",
        "id": 2
      },
      {
        "name": "Victor Mike",
        "id": 10
      }
    ]
  },
]

GET - /tags/:id

Summary

Get tag with specified id.

Required Privileges

  • authenticated user

Response application/json

{
  "id": 1,
  "name": "Food",
  "description": "Ideas related to food.",
},

GET - /tags/:id?usr=1

Summary

Get tag with specified id. Include users that have subscribed to it. Required Privileges

  • authenticated user

Response application/json

{
  "id": 1,
  "name": "Food",
  "description": "Ideas related to food.",
  "users": [
    {
      "name": "Victor Mike",
      "id": 10
    }
  ]
},

POST - /tags

Summary

Create new tag. Description field is optional.

Required Privileges

  • admin

Request application/json

{
  "name": "Snacks",
  "description": "Ideas related to snacks served in office"
}
{
  "name": "Snacks",
}

Response application/json

{
  "id": 1,
  "name": "Snacks",
  "description": "Ideas related to snacks served in office"
}

POST - /tags/:tagId/user/:userId

Summary User subscribes to specified tag.

Required Privileges

  • authenticated user (same as target)
  • admin

Response application/json

{
  "id": 1,
  "name": "Snacks",
  "description": "Ideas related to snacks served in office",
  "users": [
    {
      "user": {
        "name": "Victor Mike",
        "id": 10
      }
    }
  ]
}

PUT - /tags/:tagId

Summary

Update tag with specified id.

Required Privileges

  • admin

Request application/json

{
  "name": "Snacks V2",
  "description": "Ideas related to snacks served in office",
}

Response application/json

{
  "id": 2,
  "name": "Snacks V2",
  "description": "Ideas related to snacks served in office"
}

DELETE - /tags/:tagId/user/:userId

Summary

User unsubscribes from specified tag.

Required Privileges

  • authenticated user (same as target)
  • admin

Response application/json

{
  "id": 1,
  "name": "Snacks",
  "description": "Ideas related to snacks served in office",
  "users": []
}

DELETE - /tags/:id

Summary

Delete specified tag.

Required Privileges

  • admin

Response application/json

{
  "id": 1,
  "name": "Snacks",
  "description": "Ideas related to snacks served in office"
}

Comments

POST - /comments

Summary

Create new comment on idea.

Required Privileges

  • Authenticated user.
  • admin

Request application/json

{
  "content": "Cool idea :)",
  "idea_id": 1
}

Response application/json

  {
  "content": "Cool idea :)",
  "user": {
    "id": 10,
    "name": "Victor Mike"
  },
  "id": 2,
  "idea": {
    "id": 1,
    "user_id": 1
  },
  "created_at": "2022-11-24T10:36:10.190Z"
}

DELETE - /comments/:id

Summary

Delete specified Comment.

Required Privileges

  • Authenticated user who owns the comment
  • admin

Response application/json

  {
  "content": "Comment on some idea",
  "user": {
    "id": 10,
    "name": "Victor Mike"
  },
  "id": 2,
  "idea": {
    "id": 1,
    "user_id": 1
  },
  "created_at": "2022-11-24T10:36:10.190Z"
}

PUT - /comments/:id

Summary

Update specified Comment.

Required Privileges

  • Authenticated user who owns the comment
  • admin

Request application/json

{
  "content": "Updated comment content",
}

Response application/json

  {
  "content": "Updated comment content",
  "user": {
    "id": 10,
    "name": "Victor Mike"
  },
  "id": 2,
  "idea": {
    "id": 1,
    "user_id": 1
  },
  "created_at": "2022-11-24T10:36:10.190Z"
}

GET - /comments

Get all of the comments.

Required Privileges

  • Authenticated user
  • admin

Response application/json

[
  {
    "content": "Nice idea",
    "user": {
      "id": 10,
      "name": "Victor Mike"
    },
    "id": 2,
    "idea": {
      "id": 1,
      "user_id": 1
    },
    "created_at": "2022-11-24T10:36:10.190Z"
  },
]

GET - /comments/:id

Summary

Get comment with specified id.

Required Privileges

  • Authenticated user
  • admin

Response application/json

{
  "content": "Nice idea",
  "user": {
    "id": 10,
    "name": "Victor Mike"
  },
  "id": 2,
  "idea": {
    "id": 1,
    "user_id": 1
  },
  "created_at": "2022-11-24T10:36:10.190Z"
},

Likes

POST - /likes/idea/:ideaId

Summary

User likes specified idea.

Required

  • Authenticated user
  • admin

Response application/json

{
  "user": {
    "id": 1,
    "name": "admin"
  },
  "id": 30,
  "idea": {
    "id": 1,
    "user_id": 1
  },
  "created_at": "2022-11-30T19:01:28.126Z"
}

DELETE - /likes/idea/:ideaId

Summary

User removes his/hers like on specified idea.

Required Privileges

  • Authenticated user
  • admin

Response application/json

{
  "user": {
    "id": 10,
    "name": "Victor Mike"
  },
  "id": 30,
  "idea": {
    "id": 1,
    "user_id": 1
  },
  "created_at": "2022-11-30T19:01:28.126Z"
}

GET - /likes

Summary

Get all of the likes.

Required Privileges

  • admin

Response application/json

[
  {
    "user": {
      "id": 10,
      "name": "Victor Mike"
    },
    "id": 30,
    "idea": {
      "id": 1,
      "user_id": 1
    },
    "created_at": "2022-11-30T19:01:28.126Z"
  }
]

GET - /likes/:likeId

Summary

Get like with specified id

Required Privileges

  • admin

Response application/json

{
  "user": {
    "id": 10,
    "name": "Victor Mike"
  },
  "id": 30,
  "idea": {
    "id": 1,
    "user_id": 1
  },
  "created_at": "2022-11-30T19:01:28.126Z"
}

GET - /likes/idea/{ideaId}

Summary

Get all of the likes associated with specified idea. Also includes count of the ideas.

Required Privileges

  • Authenticated users
  • Admin

Response application/json

{
  "count": 2
  "likes": [
    {
      "id": 1,
      "user": {
        "id": 10,
        "name": "Victor Mike"
      }
    },
    {
      "id": 2,
      "user": {
        "id": 20,
        "name": "Bob Mike"
      }
    }
  ],
}

DELETE - /likes/:likeId

Summary

Admin can remove any like.

Required Privileges

  • admin

Response application/json

{
  "user": {
    "id": 10,
    "name": "Victor Mike"
  },
  "id": 30,
  "idea": {
    "id": 1,
    "user_id": 1
  },
  "created_at": "2022-11-30T19:01:28.126Z"
}

License

MIT License

About

No description, website, or topics provided.

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages