2021-07-06 12:23:35 -07:00
|
|
|
/*
|
|
|
|
|
REST API library for /user route.
|
|
|
|
|
*/
|
|
|
|
|
|
2021-07-12 17:37:49 -07:00
|
|
|
// Public npm libraries.
|
2022-09-08 08:58:39 -07:00
|
|
|
import Router from 'koa-router'
|
2021-07-06 12:23:35 -07:00
|
|
|
|
2021-07-12 17:37:49 -07:00
|
|
|
// Local libraries.
|
2022-09-08 08:58:39 -07:00
|
|
|
import UserRESTControllerLib from './controller.js'
|
|
|
|
|
|
|
|
|
|
import Validators from '../middleware/validators.js'
|
2021-07-12 17:37:49 -07:00
|
|
|
|
2025-05-19 12:17:09 -04:00
|
|
|
import config from '../../../../config/index.js'
|
|
|
|
|
|
2021-07-12 17:37:49 -07:00
|
|
|
let _this
|
|
|
|
|
|
|
|
|
|
class UserRouter {
|
2021-07-06 12:23:35 -07:00
|
|
|
constructor (localConfig = {}) {
|
2021-07-10 09:04:03 -07:00
|
|
|
// Dependency Injection.
|
|
|
|
|
this.adapters = localConfig.adapters
|
|
|
|
|
if (!this.adapters) {
|
|
|
|
|
throw new Error(
|
|
|
|
|
'Instance of Adapters library required when instantiating PostEntry REST Controller.'
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
this.useCases = localConfig.useCases
|
|
|
|
|
if (!this.useCases) {
|
|
|
|
|
throw new Error(
|
|
|
|
|
'Instance of Use Cases library required when instantiating PostEntry REST Controller.'
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-12 17:37:49 -07:00
|
|
|
const dependencies = {
|
|
|
|
|
adapters: this.adapters,
|
|
|
|
|
useCases: this.useCases
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Encapsulate dependencies.
|
2025-05-19 12:17:09 -04:00
|
|
|
this.config = config
|
2021-07-12 17:37:49 -07:00
|
|
|
this.userRESTController = new UserRESTControllerLib(dependencies)
|
|
|
|
|
this.validators = new Validators()
|
|
|
|
|
|
|
|
|
|
// Instantiate the router and set the base route.
|
|
|
|
|
const baseUrl = '/users'
|
|
|
|
|
this.router = new Router({ prefix: baseUrl })
|
|
|
|
|
|
|
|
|
|
_this = this
|
2021-07-06 12:23:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
attach (app) {
|
2021-07-12 17:37:49 -07:00
|
|
|
if (!app) {
|
|
|
|
|
throw new Error(
|
|
|
|
|
'Must pass app object when attaching REST API controllers.'
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Define the routes and attach the controller.
|
2025-05-19 12:17:09 -04:00
|
|
|
this.router.post('/', this.createUser)
|
2021-07-12 17:37:49 -07:00
|
|
|
this.router.get('/', this.getAll)
|
|
|
|
|
this.router.get('/:id', this.getById)
|
|
|
|
|
this.router.put('/:id', this.updateUser)
|
|
|
|
|
this.router.delete('/:id', this.deleteUser)
|
|
|
|
|
|
|
|
|
|
// Attach the Controller routes to the Koa app.
|
|
|
|
|
app.use(this.router.routes())
|
|
|
|
|
app.use(this.router.allowedMethods())
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-19 12:17:09 -04:00
|
|
|
async createUser (ctx, next) {
|
|
|
|
|
if (process.env.DISABLE_NEW_ACCOUNTS) {
|
|
|
|
|
await _this.validators.ensureAdmin(ctx, next)
|
|
|
|
|
}
|
|
|
|
|
await _this.userRESTController.createUser(ctx, next)
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-12 17:37:49 -07:00
|
|
|
async getAll (ctx, next) {
|
|
|
|
|
await _this.validators.ensureUser(ctx, next)
|
|
|
|
|
await _this.userRESTController.getUsers(ctx, next)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async getById (ctx, next) {
|
|
|
|
|
await _this.validators.ensureUser(ctx, next)
|
|
|
|
|
await _this.userRESTController.getUser(ctx, next)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async updateUser (ctx, next) {
|
|
|
|
|
await _this.validators.ensureTargetUserOrAdmin(ctx, next)
|
|
|
|
|
await _this.userRESTController.getUser(ctx, next)
|
|
|
|
|
await _this.userRESTController.updateUser(ctx, next)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async deleteUser (ctx, next) {
|
|
|
|
|
await _this.validators.ensureTargetUserOrAdmin(ctx, next)
|
|
|
|
|
await _this.userRESTController.getUser(ctx, next)
|
|
|
|
|
await _this.userRESTController.deleteUser(ctx, next)
|
2021-07-06 12:23:35 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-08 08:58:39 -07:00
|
|
|
export default UserRouter
|