mirror of
https://github.com/sasjs/server.git
synced 2025-12-10 19:34:34 +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 {
|
||||
Security,
|
||||
Route,
|
||||
Tags,
|
||||
Path,
|
||||
Query,
|
||||
Example,
|
||||
@@ -8,8 +10,7 @@ import {
|
||||
Patch,
|
||||
Delete,
|
||||
Body,
|
||||
Hidden,
|
||||
Security
|
||||
Hidden
|
||||
} from 'tsoa'
|
||||
import bcrypt from 'bcryptjs'
|
||||
|
||||
@@ -31,6 +32,7 @@ interface userDetailsResponse {
|
||||
|
||||
@Security('bearerAuth')
|
||||
@Route('SASjsApi/user')
|
||||
@Tags('User')
|
||||
export default class UserController {
|
||||
/**
|
||||
* 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: {
|
||||
type: String,
|
||||
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'
|
||||
const AutoIncrement = require('mongoose-sequence')(mongoose)
|
||||
|
||||
|
||||
@@ -1,19 +1,17 @@
|
||||
import express from 'express'
|
||||
import { createClient } from '../../controllers/createClient'
|
||||
import ClientController from '../../controllers/client'
|
||||
import { registerClientValidation } from '../../utils'
|
||||
|
||||
const clientRouter = express.Router()
|
||||
|
||||
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)
|
||||
|
||||
const controller = new ClientController()
|
||||
try {
|
||||
const savedClient = await createClient(data)
|
||||
res.send({
|
||||
clientId: savedClient.clientId,
|
||||
clientSecret: savedClient.clientSecret
|
||||
})
|
||||
const response = await controller.createClient(body)
|
||||
res.send(response)
|
||||
} catch (err: any) {
|
||||
res.status(403).send(err.toString())
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ import { MongoMemoryServer } from 'mongodb-memory-server'
|
||||
import request from 'supertest'
|
||||
import app from '../../../app'
|
||||
import UserController from '../../../controllers/user'
|
||||
import { createClient } from '../../../controllers/createClient'
|
||||
import ClientController from '../../../controllers/client'
|
||||
import {
|
||||
generateAccessToken,
|
||||
generateAuthCode,
|
||||
@@ -29,11 +29,12 @@ describe('auth', () => {
|
||||
let con: Mongoose
|
||||
let mongoServer: MongoMemoryServer
|
||||
const userController = new UserController()
|
||||
const clientController = new ClientController()
|
||||
|
||||
beforeAll(async () => {
|
||||
mongoServer = await MongoMemoryServer.create()
|
||||
con = await mongoose.connect(mongoServer.getUri())
|
||||
await createClient({ clientId, clientSecret })
|
||||
await clientController.createClient({ clientId, clientSecret })
|
||||
await populateClients()
|
||||
})
|
||||
|
||||
|
||||
@@ -2,8 +2,8 @@ import mongoose, { Mongoose } from 'mongoose'
|
||||
import { MongoMemoryServer } from 'mongodb-memory-server'
|
||||
import request from 'supertest'
|
||||
import app from '../../../app'
|
||||
import { createClient } from '../../../controllers/createClient'
|
||||
import UserController from '../../../controllers/user'
|
||||
import ClientController from '../../../controllers/client'
|
||||
import { generateAccessToken } from '../auth'
|
||||
import { saveTokensInDB } from '../../../utils'
|
||||
|
||||
@@ -27,6 +27,7 @@ describe('client', () => {
|
||||
let con: Mongoose
|
||||
let mongoServer: MongoMemoryServer
|
||||
const userController = new UserController()
|
||||
const clientController = new ClientController()
|
||||
|
||||
beforeAll(async () => {
|
||||
mongoServer = await MongoMemoryServer.create()
|
||||
@@ -115,7 +116,7 @@ describe('client', () => {
|
||||
})
|
||||
|
||||
it('should respond with Forbidden if clientId is already present', async () => {
|
||||
await createClient(newClient)
|
||||
await clientController.createClient(newClient)
|
||||
|
||||
const res = await request(app)
|
||||
.post('/SASjsApi/client')
|
||||
|
||||
@@ -13,7 +13,6 @@ import {
|
||||
|
||||
const userRouter = express.Router()
|
||||
|
||||
// create user
|
||||
userRouter.post('/', authenticateAccessToken, verifyAdmin, async (req, res) => {
|
||||
const { error, value: body } = registerUserValidation(req.body)
|
||||
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) => {
|
||||
const { userId } = req.params
|
||||
|
||||
@@ -50,7 +48,6 @@ userRouter.get('/:userId', authenticateAccessToken, async (req: any, res) => {
|
||||
}
|
||||
})
|
||||
|
||||
// update user
|
||||
userRouter.patch(
|
||||
'/:userId',
|
||||
authenticateAccessToken,
|
||||
@@ -73,7 +70,6 @@ userRouter.patch(
|
||||
}
|
||||
)
|
||||
|
||||
// delete user
|
||||
userRouter.delete(
|
||||
'/:userId',
|
||||
authenticateAccessToken,
|
||||
|
||||
Reference in New Issue
Block a user