Skip to content

Commit

Permalink
feat: topic/k1ch/ introduce GET:/tenants
Browse files Browse the repository at this point in the history
  • Loading branch information
k1ch committed Jan 3, 2025
1 parent 71fb48f commit 359c167
Show file tree
Hide file tree
Showing 3 changed files with 175 additions and 11 deletions.
61 changes: 50 additions & 11 deletions database/layer/admin-tenant.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
const { pgErrorHandler } = require('../utils/pgErrorHandler')
const { usherDb } = require('./knex')
const { PGPool } = require('./pg_pool')
const pool = new PGPool()

module.exports = {
insertTenant,
updateTenantName,
updateTenantIssClaim,
deleteTenant
}

async function insertTenant (tenantName, issClaim, jwksUri) {
const insertTenant = async (tenantName, issClaim, jwksUri) => {
const sql = 'INSERT INTO usher.tenants (name, iss_claim, jwks_uri) VALUES ($1, $2, $3)'
const sqlParams = [tenantName, issClaim, jwksUri]
try {
Expand All @@ -23,7 +18,7 @@ async function insertTenant (tenantName, issClaim, jwksUri) {
}
}

async function updateTenantName (tenantName, issClaim, newTenantName) {
const updateTenantName = async (tenantName, issClaim, newTenantName) => {
const sql = 'UPDATE usher.tenants SET name = $3 WHERE name = $1 AND iss_claim = $2'
const sqlParams = [tenantName, issClaim, newTenantName]
try {
Expand All @@ -46,7 +41,7 @@ async function updateTenantName (tenantName, issClaim, newTenantName) {
}
}

async function updateTenantIssClaim (tenantName, issClaim, newIssClaim, newJwksUri) {
const updateTenantIssClaim = async (tenantName, issClaim, newIssClaim, newJwksUri) => {
const sql = 'UPDATE usher.tenants SET iss_claim = $3, jwks_uri = $4 WHERE name = $1 AND iss_claim = $2'
const sqlParams = [tenantName, issClaim, newIssClaim, newJwksUri]
try {
Expand All @@ -65,7 +60,7 @@ async function updateTenantIssClaim (tenantName, issClaim, newIssClaim, newJwksU
}
}

async function deleteTenant (tenantName, issClaim) {
const deleteTenant = async (tenantName, issClaim) => {
const sql = 'DELETE FROM usher.tenants WHERE name = $1 AND iss_claim = $2'
const sqlParams = [tenantName, issClaim]
try {
Expand All @@ -79,3 +74,47 @@ async function deleteTenant (tenantName, issClaim) {
return `Delete failed: ${error.message}`
}
}

/**
* Get tenants by optional filters
*
* @param {Object} filters - The filters to apply
* @param {number} [filters.key] - The tenant key
* @param {string} [filters.name] - The name of tenant
* @param {string} [filters.iss_claim] - The iss_claim of tenant
* @param {string} [sort='name'] - The column to sort by
* @param {string} [order='desc'] - The order of sorting (asc or desc)
* @returns {Promise<Array<Object>>} - A promise that resolves to an array of tenants
*/
const getTenants = async (filters = {}, sort = 'key', order = 'asc') => {
try {
const query = usherDb('tenants')
.select('*')

const { key, name, iss_claim } = filters
if (key) {
query.where('tenants.key', key)
}
if (iss_claim) {
query.where('tenants.iss_claim', 'ilike', `%${iss_claim}%`)
}
if (name) {
query.where('tenants.name', 'ilike', `%${name}%`)
}

query.orderBy(sort, order)

const tenants = await query
return tenants
} catch (err) {
throw pgErrorHandler(err)
}
}

module.exports = {
insertTenant,
updateTenantName,
updateTenantIssClaim,
deleteTenant,
getTenants,
}
33 changes: 33 additions & 0 deletions server/src/api_endpoints/tenants/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const createError = require('http-errors')
const dbAdminTenant = require('database/layer/admin-tenant')

/**
* HTTP Request handler
* Get tenants with optional filtering, sorting, and ordering
*
* @param {Object} req - The request object
* @param {Object} res - The response object to send 200 statusCode and a list of tenants
* @param {Function} next - The callback function for the next middleware
* @returns {Promise<void>} - A promise that resolves to void when tenants are retrieved
*/
const getTenants = async (req, res, next) => {
try {
const { sort, order } = req.query
const allowedFilterParameters = ['key', 'name', 'iss_claim']
const filters = allowedFilterParameters.reduce((acc, filter) => {
const filterValue = req.query[filter]
if (filterValue) {
acc[filter] = filterValue
}
return acc
}, {})
const tenants = await dbAdminTenant.getTenants(filters, sort, order)
res.status(200).send(tenants);
} catch ({ httpStatusCode = 500, message }) {
return next(createError(httpStatusCode, { message }))
}
}

module.exports = {
getTenants,
}
92 changes: 92 additions & 0 deletions server/the-usher-openapi-spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1221,6 +1221,73 @@ paths:
default:
$ref: '#/components/responses/Default'

/tenants:
get:
'x-swagger-router-controller': 'tenants/index'
operationId: getTenants
summary: Retrieve a list of tenants
parameters:
- name: key
in: query
description: Filter by tenant key
schema:
type: number
example: 10
- name: name
in: query
description: Filter by tenant name (partial match)
schema:
type: string
example: DMGT
- name: iss_claim
in: query
description: Filter by iss_claim (partial match)
schema:
type: string
example: https://dmgt-prod.auth0.com/
- name: sort
in: query
description: Sort the results by a field. Default is tenant key.
schema:
type: string
example: name
enum:
[
key,
name,
iss_claim,
jwks_uri,
created_at,
updated_at,
]
- name: order
in: query
description: Specify the sort order (asc or desc). Default is ascending.
schema:
type: string
enum: [asc, desc]
tags:
- Admin APIs
security:
- bearerAdminAuth: []
responses:
200:
description: Returns a list of tenants
content:
application/json:
schema:
type: array
items:
$ref: "#/components/schemas/Tenant"
400:
$ref: '#/components/responses/BadRequest'
401:
$ref: '#/components/responses/Unauthorized'
500:
$ref: '#/components/responses/InternalError'
503:
$ref: '#/components/responses/ServiceUnavailableError'

components:

parameters:
Expand Down Expand Up @@ -1550,6 +1617,31 @@ components:
- tenantkey
- sub_claim

Tenant:
type: object
properties:
key:
type: integer
minimum: 1
format: int32
name:
type: string
iss_claim:
type: string
jwks_uri:
type: string
created_at:
type: string
format: date-time
updated_at:
type: string
format: date-time
required:
- key
- name
- iss_claim
- jwks_uri

# SCOPE
#---------------------
ArrayOfScope:
Expand Down

0 comments on commit 359c167

Please sign in to comment.