feature/#22-API-Tokens #49

Merged
jkeffects merged 8 commits from feature/#22-API-Tokens into develop 2025-01-22 10:59:46 +00:00
5 changed files with 187 additions and 2 deletions
Showing only changes of commit 36ecccd0dc - Show all commits

View file

@ -0,0 +1,141 @@
import { Request, Response } from "express";
import ApiService from "../../../service/user/apiService";
import ApiFactory from "../../../factory/admin/user/api";
import ApiPermissionService from "../../../service/user/apiPermissionService";
import PermissionHelper from "../../../helpers/permissionHelper";
import { CreateApiCommand, DeleteApiCommand, UpdateApiCommand } from "../../../command/user/api/apiCommand";
import ApiCommandHandler from "../../../command/user/api/apiCommandHandler";
import { UpdateApiPermissionsCommand } from "../../../command/user/api/apiPermissionCommand";
import ApiPermissionCommandHandler from "../../../command/user/api/apiPermissionCommandHandler";
/**
* @description get All apis
* @param req {Request} Express req object
* @param res {Response} Express res object
* @returns {Promise<*>}
*/
export async function getAllApis(req: Request, res: Response): Promise<any> {
let apis = await ApiService.getAll();
res.json(ApiFactory.mapToBase(apis));
}
/**
* @description get api by id
* @param req {Request} Express req object
* @param res {Response} Express res object
* @returns {Promise<*>}
*/
export async function getApiById(req: Request, res: Response): Promise<any> {
const id = parseInt(req.params.id);
let api = await ApiService.getById(id);
res.json(ApiFactory.mapToSingle(api));
}
/**
* @description get api token by id
* @param req {Request} Express req object
* @param res {Response} Express res object
* @returns {Promise<*>}
*/
export async function getApiTokenById(req: Request, res: Response): Promise<any> {
const id = parseInt(req.params.id);
let { token } = await ApiService.getTokenById(id);
res.send(token);
}
/**
* @description get permissions by api
* @param req {Request} Express req object
* @param res {Response} Express res object
* @returns {Promise<*>}
*/
export async function getApiPermissions(req: Request, res: Response): Promise<any> {
const id = parseInt(req.params.id);
let permissions = await ApiPermissionService.getByApi(id);
res.json(PermissionHelper.convertToObject(permissions.map((p) => p.permission)));
}
/**
* @description create new api
* @param req {Request} Express req object
* @param res {Response} Express res object
* @returns {Promise<*>}
*/
export async function createApi(req: Request, res: Response): Promise<any> {
let title = req.body.title;
let expiry = req.body.expiry;
// TODO: create jwt as token to prevent random string tests
let createApi: CreateApiCommand = {
token: "",
title: title,
expiry: expiry,
};
await ApiCommandHandler.create(createApi);
res.sendStatus(204);
}
/**
* @description update api data
* @param req {Request} Express req object
* @param res {Response} Express res object
* @returns {Promise<*>}
*/
export async function updateApi(req: Request, res: Response): Promise<any> {
const id = parseInt(req.params.id);
let title = req.body.title;
let expiry = req.body.expiry;
let updateApi: UpdateApiCommand = {
id: id,
title: title,
expiry: expiry,
};
await ApiCommandHandler.update(updateApi);
res.sendStatus(204);
}
/**
* @description update api assigned permission strings
* @param req {Request} Express req object
* @param res {Response} Express res object
* @returns {Promise<*>}
*/
export async function updateApiPermissions(req: Request, res: Response): Promise<any> {
const id = parseInt(req.params.id);
let permissions = req.body.permissions;
let permissionStrings = PermissionHelper.convertToStringArray(permissions);
let updateApiPermissions: UpdateApiPermissionsCommand = {
apiId: id,
permissions: permissionStrings,
};
await ApiPermissionCommandHandler.updatePermissions(updateApiPermissions);
res.sendStatus(204);
}
/**
* @description delete api by id
* @param req {Request} Express req object
* @param res {Response} Express res object
* @returns {Promise<*>}
*/
export async function deleteApi(req: Request, res: Response): Promise<any> {
const id = parseInt(req.params.id);
let deleteApi: DeleteApiCommand = {
id: id,
};
await ApiCommandHandler.delete(deleteApi);
res.sendStatus(204);
}

View file

@ -1,4 +1,5 @@
import { Column, CreateDateColumn, Entity, PrimaryColumn } from "typeorm";
import { Column, CreateDateColumn, Entity, OneToMany, PrimaryColumn } from "typeorm";
import { apiPermission } from "./api_permission";
@Entity()
export class api {
@ -19,4 +20,7 @@ export class api {
@Column({ type: "datetime", nullable: true })
expiry?: Date;
@OneToMany(() => apiPermission, (apiPermission) => apiPermission.api)
permissions: apiPermission[];
}

View file

@ -1,4 +1,4 @@
import { Column, Entity, ManyToOne, PrimaryColumn } from "typeorm";
import { Column, Entity, ManyToOne, OneToMany, PrimaryColumn } from "typeorm";
import { PermissionObject, PermissionString } from "../../type/permissionTypes";
import { api } from "./api";

View file

@ -0,0 +1,30 @@
import { api } from "../../../entity/user/api";
import PermissionHelper from "../../../helpers/permissionHelper";
import { ApiViewModel } from "../../../viewmodel/admin/user/api.models";
export default abstract class ApiFactory {
/**
* @description map record to api
* @param {api} record
* @returns {apiViewModel}
*/
public static mapToSingle(record: api): ApiViewModel {
return {
id: record.id,
permissions: PermissionHelper.convertToObject(record.permissions.map((e) => e.permission)),
title: record.title,
expiry: record.expiry,
lastUsage: record.lastUsage,
createdAt: record.createdAt,
};
}
/**
* @description map records to api
* @param {Array<api>} records
* @returns {Array<apiViewModel>}
*/
public static mapToBase(records: Array<api>): Array<ApiViewModel> {
return records.map((r) => this.mapToSingle(r));
}
}

View file

@ -0,0 +1,10 @@
import { PermissionObject } from "../../../type/permissionTypes";
export interface ApiViewModel {
id: number;
permissions: PermissionObject;
title: string;
createdAt: Date;
lastUsage?: Date;
expiry?: Date;
}