mirror of
https://github.com/sasjs/server.git
synced 2026-01-18 03:10:06 +00:00
chore: added client docs + tags
This commit is contained in:
44
src/controllers/client.ts
Normal file
44
src/controllers/client.ts
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
import { Security, Route, Tags, Example, Post, Body } from 'tsoa'
|
||||||
|
|
||||||
|
import Client, { ClientPayload } from '../model/Client'
|
||||||
|
|
||||||
|
@Security('bearerAuth')
|
||||||
|
@Route('SASjsApi/client')
|
||||||
|
@Tags('Client')
|
||||||
|
export default class ClientController {
|
||||||
|
/**
|
||||||
|
* Create client with the following attributes: ClientId, ClientSecret. Admin only task.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
@Example<ClientPayload>({
|
||||||
|
clientId: 'someFormattedClientID1234',
|
||||||
|
clientSecret: 'someRandomCryptoString'
|
||||||
|
})
|
||||||
|
@Post('/')
|
||||||
|
public async createClient(
|
||||||
|
@Body() body: ClientPayload
|
||||||
|
): Promise<ClientPayload> {
|
||||||
|
return createClient(body)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const createClient = async (data: any): Promise<ClientPayload> => {
|
||||||
|
const { clientId, clientSecret } = data
|
||||||
|
|
||||||
|
// Checking if client is already in the database
|
||||||
|
const clientExist = await Client.findOne({ clientId })
|
||||||
|
if (clientExist) throw new Error('Client ID already exists.')
|
||||||
|
|
||||||
|
// Create a new client
|
||||||
|
const client = new Client({
|
||||||
|
clientId,
|
||||||
|
clientSecret
|
||||||
|
})
|
||||||
|
|
||||||
|
const savedClient = await client.save()
|
||||||
|
|
||||||
|
return {
|
||||||
|
clientId: savedClient.clientId,
|
||||||
|
clientSecret: savedClient.clientSecret
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,22 +0,0 @@
|
|||||||
import Client from '../model/Client'
|
|
||||||
|
|
||||||
export const createClient = async (data: any) => {
|
|
||||||
const { clientId, clientSecret } = data
|
|
||||||
|
|
||||||
// Checking if client is already in the database
|
|
||||||
const clientExist = await Client.findOne({ clientId })
|
|
||||||
if (clientExist) throw new Error('Client ID already exists.')
|
|
||||||
|
|
||||||
// Create a new client
|
|
||||||
const client = new Client({
|
|
||||||
clientId,
|
|
||||||
clientSecret
|
|
||||||
})
|
|
||||||
|
|
||||||
const savedClient = await client.save()
|
|
||||||
|
|
||||||
return {
|
|
||||||
clientId: savedClient.clientId,
|
|
||||||
clientSecret: savedClient.clientSecret
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,5 +1,7 @@
|
|||||||
import {
|
import {
|
||||||
|
Security,
|
||||||
Route,
|
Route,
|
||||||
|
Tags,
|
||||||
Path,
|
Path,
|
||||||
Query,
|
Query,
|
||||||
Example,
|
Example,
|
||||||
@@ -8,8 +10,7 @@ import {
|
|||||||
Patch,
|
Patch,
|
||||||
Delete,
|
Delete,
|
||||||
Body,
|
Body,
|
||||||
Hidden,
|
Hidden
|
||||||
Security
|
|
||||||
} from 'tsoa'
|
} from 'tsoa'
|
||||||
import bcrypt from 'bcryptjs'
|
import bcrypt from 'bcryptjs'
|
||||||
|
|
||||||
@@ -31,6 +32,7 @@ interface userDetailsResponse {
|
|||||||
|
|
||||||
@Security('bearerAuth')
|
@Security('bearerAuth')
|
||||||
@Route('SASjsApi/user')
|
@Route('SASjsApi/user')
|
||||||
|
@Tags('User')
|
||||||
export default class UserController {
|
export default class UserController {
|
||||||
/**
|
/**
|
||||||
* Get list of all users (username, displayname). All users can request this.
|
* Get list of all users (username, displayname). All users can request this.
|
||||||
|
|||||||
@@ -1,6 +1,19 @@
|
|||||||
import mongoose from 'mongoose'
|
import mongoose, { Schema } from 'mongoose'
|
||||||
|
|
||||||
const clientSchema = new mongoose.Schema({
|
export interface ClientPayload {
|
||||||
|
/**
|
||||||
|
* Client ID
|
||||||
|
* @example "someFormattedClientID1234"
|
||||||
|
*/
|
||||||
|
clientId: string
|
||||||
|
/**
|
||||||
|
* Client Secret
|
||||||
|
* @example "someRandomCryptoString"
|
||||||
|
*/
|
||||||
|
clientSecret: string
|
||||||
|
}
|
||||||
|
|
||||||
|
const ClientSchema = new Schema<ClientPayload>({
|
||||||
clientId: {
|
clientId: {
|
||||||
type: String,
|
type: String,
|
||||||
required: true
|
required: true
|
||||||
@@ -11,4 +24,4 @@ const clientSchema = new mongoose.Schema({
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
export default mongoose.model('Client', clientSchema)
|
export default mongoose.model('Client', ClientSchema)
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
import { number } from 'joi'
|
|
||||||
import mongoose, { Schema, model } from 'mongoose'
|
import mongoose, { Schema, model } from 'mongoose'
|
||||||
const AutoIncrement = require('mongoose-sequence')(mongoose)
|
const AutoIncrement = require('mongoose-sequence')(mongoose)
|
||||||
|
|
||||||
|
|||||||
@@ -1,19 +1,17 @@
|
|||||||
import express from 'express'
|
import express from 'express'
|
||||||
import { createClient } from '../../controllers/createClient'
|
import ClientController from '../../controllers/client'
|
||||||
import { registerClientValidation } from '../../utils'
|
import { registerClientValidation } from '../../utils'
|
||||||
|
|
||||||
const clientRouter = express.Router()
|
const clientRouter = express.Router()
|
||||||
|
|
||||||
clientRouter.post('/', async (req, res) => {
|
clientRouter.post('/', async (req, res) => {
|
||||||
const { error, value: data } = registerClientValidation(req.body)
|
const { error, value: body } = registerClientValidation(req.body)
|
||||||
if (error) return res.status(400).send(error.details[0].message)
|
if (error) return res.status(400).send(error.details[0].message)
|
||||||
|
|
||||||
|
const controller = new ClientController()
|
||||||
try {
|
try {
|
||||||
const savedClient = await createClient(data)
|
const response = await controller.createClient(body)
|
||||||
res.send({
|
res.send(response)
|
||||||
clientId: savedClient.clientId,
|
|
||||||
clientSecret: savedClient.clientSecret
|
|
||||||
})
|
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
res.status(403).send(err.toString())
|
res.status(403).send(err.toString())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import { MongoMemoryServer } from 'mongodb-memory-server'
|
|||||||
import request from 'supertest'
|
import request from 'supertest'
|
||||||
import app from '../../../app'
|
import app from '../../../app'
|
||||||
import UserController from '../../../controllers/user'
|
import UserController from '../../../controllers/user'
|
||||||
import { createClient } from '../../../controllers/createClient'
|
import ClientController from '../../../controllers/client'
|
||||||
import {
|
import {
|
||||||
generateAccessToken,
|
generateAccessToken,
|
||||||
generateAuthCode,
|
generateAuthCode,
|
||||||
@@ -29,11 +29,12 @@ describe('auth', () => {
|
|||||||
let con: Mongoose
|
let con: Mongoose
|
||||||
let mongoServer: MongoMemoryServer
|
let mongoServer: MongoMemoryServer
|
||||||
const userController = new UserController()
|
const userController = new UserController()
|
||||||
|
const clientController = new ClientController()
|
||||||
|
|
||||||
beforeAll(async () => {
|
beforeAll(async () => {
|
||||||
mongoServer = await MongoMemoryServer.create()
|
mongoServer = await MongoMemoryServer.create()
|
||||||
con = await mongoose.connect(mongoServer.getUri())
|
con = await mongoose.connect(mongoServer.getUri())
|
||||||
await createClient({ clientId, clientSecret })
|
await clientController.createClient({ clientId, clientSecret })
|
||||||
await populateClients()
|
await populateClients()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -2,8 +2,8 @@ import mongoose, { Mongoose } from 'mongoose'
|
|||||||
import { MongoMemoryServer } from 'mongodb-memory-server'
|
import { MongoMemoryServer } from 'mongodb-memory-server'
|
||||||
import request from 'supertest'
|
import request from 'supertest'
|
||||||
import app from '../../../app'
|
import app from '../../../app'
|
||||||
import { createClient } from '../../../controllers/createClient'
|
|
||||||
import UserController from '../../../controllers/user'
|
import UserController from '../../../controllers/user'
|
||||||
|
import ClientController from '../../../controllers/client'
|
||||||
import { generateAccessToken } from '../auth'
|
import { generateAccessToken } from '../auth'
|
||||||
import { saveTokensInDB } from '../../../utils'
|
import { saveTokensInDB } from '../../../utils'
|
||||||
|
|
||||||
@@ -27,6 +27,7 @@ describe('client', () => {
|
|||||||
let con: Mongoose
|
let con: Mongoose
|
||||||
let mongoServer: MongoMemoryServer
|
let mongoServer: MongoMemoryServer
|
||||||
const userController = new UserController()
|
const userController = new UserController()
|
||||||
|
const clientController = new ClientController()
|
||||||
|
|
||||||
beforeAll(async () => {
|
beforeAll(async () => {
|
||||||
mongoServer = await MongoMemoryServer.create()
|
mongoServer = await MongoMemoryServer.create()
|
||||||
@@ -115,7 +116,7 @@ describe('client', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('should respond with Forbidden if clientId is already present', async () => {
|
it('should respond with Forbidden if clientId is already present', async () => {
|
||||||
await createClient(newClient)
|
await clientController.createClient(newClient)
|
||||||
|
|
||||||
const res = await request(app)
|
const res = await request(app)
|
||||||
.post('/SASjsApi/client')
|
.post('/SASjsApi/client')
|
||||||
|
|||||||
@@ -13,7 +13,6 @@ import {
|
|||||||
|
|
||||||
const userRouter = express.Router()
|
const userRouter = express.Router()
|
||||||
|
|
||||||
// create user
|
|
||||||
userRouter.post('/', authenticateAccessToken, verifyAdmin, async (req, res) => {
|
userRouter.post('/', authenticateAccessToken, verifyAdmin, async (req, res) => {
|
||||||
const { error, value: body } = registerUserValidation(req.body)
|
const { error, value: body } = registerUserValidation(req.body)
|
||||||
if (error) return res.status(400).send(error.details[0].message)
|
if (error) return res.status(400).send(error.details[0].message)
|
||||||
@@ -37,7 +36,6 @@ userRouter.get('/', authenticateAccessToken, async (req, res) => {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
// get one user
|
|
||||||
userRouter.get('/:userId', authenticateAccessToken, async (req: any, res) => {
|
userRouter.get('/:userId', authenticateAccessToken, async (req: any, res) => {
|
||||||
const { userId } = req.params
|
const { userId } = req.params
|
||||||
|
|
||||||
@@ -50,7 +48,6 @@ userRouter.get('/:userId', authenticateAccessToken, async (req: any, res) => {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
// update user
|
|
||||||
userRouter.patch(
|
userRouter.patch(
|
||||||
'/:userId',
|
'/:userId',
|
||||||
authenticateAccessToken,
|
authenticateAccessToken,
|
||||||
@@ -73,7 +70,6 @@ userRouter.patch(
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
// delete user
|
|
||||||
userRouter.delete(
|
userRouter.delete(
|
||||||
'/:userId',
|
'/:userId',
|
||||||
authenticateAccessToken,
|
authenticateAccessToken,
|
||||||
|
|||||||
Reference in New Issue
Block a user