diff --git a/config/default.json b/config/default.json index 2ebe70b..a9941f9 100644 --- a/config/default.json +++ b/config/default.json @@ -24,7 +24,7 @@ "port": "5432", "username": "postgres", "password": "123", - "database": "bri_erp" + "database": "bri-erp" }, "auth": { "refresh_token_lifetime_day": 30, diff --git a/src/controllers/auth.ts b/src/controllers/auth.ts index 6bb96a3..c056347 100644 --- a/src/controllers/auth.ts +++ b/src/controllers/auth.ts @@ -19,15 +19,17 @@ export class AuthController { static async login(req: Request, res: Response, next: NextFunction): Promise { /* #swagger.tags = ['Auth'] - #swagger.parameters['body'] = { - in: 'body', - description: 'Login data', + #swagger.requestBody = { required: true, - schema: { - username: "admin", - password: "123456" + content: { + "application/json": { + schema: { + $ref: "#/components/schemas/login" + } + } } - }*/ + } + */ try { const schema = Joi.object().keys({ @@ -35,9 +37,6 @@ export class AuthController { password: Joi.string().alphanum().min(6).required().label('Password') }); - console.log('req.body', req.body) - console.log('req', req) - const param: User = await schema.validateAsync(req.body); const userRepository = OrmHelper.DB.getRepository(User); diff --git a/src/controllers/user.ts b/src/controllers/user.ts index a39562a..e79e52f 100644 --- a/src/controllers/user.ts +++ b/src/controllers/user.ts @@ -44,7 +44,9 @@ export class UserController { #swagger.parameters['order_direction'] = { in: 'query', required: true, - enum: [ "ASC", "DESC"] + schema: { + '@enum': ['ASC', 'DESC'] + } } */ @@ -103,23 +105,21 @@ export class UserController { static async create(req: Request, res: Response, next: NextFunction): Promise { /* #swagger.tags = ['User'] -#swagger.security = [{ + #swagger.security = [{ "bearerAuth": [] }] - #swagger.parameters['body'] = { - in: 'body', - description: 'User data', + + #swagger.requestBody = { required: true, - schema: { - email: "admin@gmail.com", - username: "admin", - password: "123456", - retype_password: "123456", - name: "Administrator", - id_role: "4a6991b6-78a0-4b3b-bf5c-fa0af5f69be8", - status:"Y" + content: { + "application/json": { + schema: { + $ref: "#/components/schemas/user" + } + } } - }*/ + } + */ try { const schema = Joi.object().keys({ @@ -158,7 +158,7 @@ export class UserController { static async updateProfile(req: Request, res: Response, next: NextFunction): Promise { /* #swagger.tags = ['User'] -#swagger.security = [{ + #swagger.security = [{ "bearerAuth": [] }] #swagger.parameters['id'] = { @@ -168,16 +168,17 @@ export class UserController { type: 'string' } - #swagger.parameters['body'] = { - in: 'body', - description: 'User data', + #swagger.requestBody = { required: true, - schema: { - email: "admin@gmail.com", - username: "admin", - name: "Administrator" + content: { + "application/json": { + schema: { + $ref: "#/components/schemas/user_profile" + } + } } - }*/ + } + */ try { const schema = Joi.object().keys({ @@ -217,7 +218,7 @@ export class UserController { static async updatePassword(req: Request, res: Response, next: NextFunction): Promise { /* #swagger.tags = ['User'] -#swagger.security = [{ + #swagger.security = [{ "bearerAuth": [] }] #swagger.parameters['id'] = { @@ -227,15 +228,17 @@ export class UserController { type: 'string' } - #swagger.parameters['body'] = { - in: 'body', - description: 'User data', + #swagger.requestBody = { required: true, - schema: { - password: "123456", - retype_password: "123456", + content: { + "application/json": { + schema: { + $ref: "#/components/schemas/user_password" + } + } } - }*/ + } + */ try { const schema = Joi.object().keys({ @@ -275,7 +278,7 @@ export class UserController { static async update(req: Request, res: Response, next: NextFunction): Promise { /* #swagger.tags = ['User'] -#swagger.security = [{ + #swagger.security = [{ "bearerAuth": [] }] #swagger.parameters['id'] = { @@ -285,20 +288,17 @@ export class UserController { type: 'string' } - #swagger.parameters['body'] = { - in: 'body', - description: 'User data', + #swagger.requestBody = { required: true, - schema: { - email: "admin@gmail.com", - username: "admin", - password: "123456", - retype_password: "123456", - name: "Administrator", - id_role: "4a6991b6-78a0-4b3b-bf5c-fa0af5f69be8", - status:"Y" + content: { + "application/json": { + schema: { + $ref: "#/components/schemas/user" + } + } } - }*/ + } + */ try { const schema = Joi.object().keys({ @@ -349,7 +349,7 @@ export class UserController { static async delete(req: Request, res: Response, next: NextFunction): Promise { /* #swagger.tags = ['User'] -#swagger.security = [{ + #swagger.security = [{ "bearerAuth": [] }] #swagger.parameters['id'] = { @@ -386,7 +386,7 @@ export class UserController { static async restore(req: Request, res: Response, next: NextFunction): Promise { /* #swagger.tags = ['User'] -#swagger.security = [{ + #swagger.security = [{ "bearerAuth": [] }] #swagger.parameters['id'] = { diff --git a/src/controllers/user_activity.ts b/src/controllers/user_activity.ts index a3c795f..5d3f9df 100644 --- a/src/controllers/user_activity.ts +++ b/src/controllers/user_activity.ts @@ -16,6 +16,9 @@ export class UserActivityController { static async list(req: Request, res: Response, next: NextFunction): Promise { /* #swagger.tags = ['User Activity'] + #swagger.security = [{ + "bearerAuth": [] + }] #swagger.parameters['filter'] = { in: 'query', type: 'string' @@ -102,20 +105,21 @@ export class UserActivityController { static async create(req: Request, res: Response, next: NextFunction): Promise { /* #swagger.tags = ['User Activity'] - #swagger.parameters['body'] = { - in: 'body', - description: 'User Activity data', + #swagger.security = [{ + "bearerAuth": [] + }] + + #swagger.requestBody = { required: true, - schema: { - id_user: "4a6991b6-78a0-4b3b-bf5c-fa0af5f69be8", - refresh_token: "1a1221b6-78a0-4b3b-bf5c-fa0af5f69be8", - module: "dashboard", - description: "Main Dashboard", - action: "V", - url: "https://bri-dev.shiblysolution.id/", - data_body:"{\"from\":\"2024-11-01\",\"to\":\"2024-11-26\"}" + content: { + "application/json": { + schema: { + $ref: "#/components/schemas/user_activity" + } + } } - }*/ + } + */ try { const schema = Joi.object().keys({ diff --git a/src/controllers/user_refresh_token.ts b/src/controllers/user_refresh_token.ts index 01135ca..38679c1 100644 --- a/src/controllers/user_refresh_token.ts +++ b/src/controllers/user_refresh_token.ts @@ -1,4 +1,5 @@ -import { Request, Response, NextFunction, Application } from "express"; +import { Response, NextFunction, Application } from "express"; +import { Request } from "express-jwt"; import { ReturnHelper } from "../helpers/express/return"; import { OrmHelper } from "../helpers/orm"; import Joi from "joi"; @@ -14,6 +15,9 @@ export class UserRefreshTokenController { static async list(req: Request, res: Response, next: NextFunction): Promise { /* #swagger.tags = ['User Refresh Token'] + #swagger.security = [{ + "bearerAuth": [] + }] #swagger.parameters['filter'] = { in: 'query', type: 'string' @@ -59,11 +63,14 @@ export class UserRefreshTokenController { let whereVal: any = {}; if (param.filter) { - whereAttr = 'name like :filter_like or username like :filter_like or email = :filter '; + whereAttr = '(name like :filter_like or username like :filter_like or email = :filter) and '; whereVal['filter'] = param.filter whereVal['filter_like'] = '%' + param.filter + '%' } + whereAttr += 'id_user = :id_user' + whereVal['id_user'] = req.auth.data.id; + const res_count = repo.createQueryBuilder() .where(whereAttr, whereVal); const res_list = repo.createQueryBuilder() diff --git a/src/controllers/user_roles.ts b/src/controllers/user_roles.ts index e46859a..f29374c 100644 --- a/src/controllers/user_roles.ts +++ b/src/controllers/user_roles.ts @@ -8,6 +8,7 @@ import { Paging } from "../types/paging"; import CommonHelper from "../helpers/common"; import { Status } from "../types/status"; import { UserRole } from "../entity/users_roles"; +import { RoleList } from "../types/role"; const log: Logger = new Logger({ name: '[UserRoleController]', type: 'pretty' }); @@ -15,6 +16,9 @@ export class UserRoleController { static async list(req: Request, res: Response, next: NextFunction): Promise { /* #swagger.tags = ['User Roles'] + #swagger.security = [{ + "bearerAuth": [] + }] #swagger.parameters['filter'] = { in: 'query', type: 'string' @@ -98,19 +102,46 @@ export class UserRoleController { } } + static async role(req: Request, res: Response, next: NextFunction): Promise { + /* + #swagger.tags = ['User Roles'] + #swagger.security = [{ + "bearerAuth": [] + }] + */ + + try { + const current_page = 1; + const total_count_data = CommonHelper.countObject(RoleList); + const list_data = CommonHelper.objectFlip(RoleList); + const count_data = CommonHelper.countObject(RoleList); + + return ReturnHelper.successResponselist(res, 200, Language.lang.success_view, count_data, current_page, total_count_data, list_data); + } catch (e: unknown) { + log.error(e); + const err = e as Error; + + return ReturnHelper.errorResponse(res, 400, 401, Language.lang.failed_view, err.message); + } + } + static async create(req: Request, res: Response, next: NextFunction): Promise { /* #swagger.tags = ['User Roles'] - #swagger.parameters['body'] = { - in: 'body', - description: 'User role data', + #swagger.security = [{ + "bearerAuth": [] + }] + #swagger.requestBody = { required: true, - schema: { - name: "Administrator", - roles: ["dashboard"], - status:"Y" + content: { + "application/json": { + schema: { + $ref: "#/components/schemas/user_role" + } + } } - }*/ + } + */ try { const schema = Joi.object().keys({ @@ -140,6 +171,9 @@ export class UserRoleController { static async update(req: Request, res: Response, next: NextFunction): Promise { /* #swagger.tags = ['User Roles'] + #swagger.security = [{ + "bearerAuth": [] + }] #swagger.parameters['id'] = { in: 'path', description: 'User role ID.', @@ -147,16 +181,17 @@ export class UserRoleController { type: 'string' } - #swagger.parameters['body'] = { - in: 'body', - description: 'User role data', + #swagger.requestBody = { required: true, - schema: { - name: "Administrator", - roles: ["dashboard"], - status:"Y" + content: { + "application/json": { + schema: { + $ref: "#/components/schemas/user_role" + } + } } - }*/ + } + */ try { const schema = Joi.object().keys({ @@ -196,6 +231,9 @@ export class UserRoleController { static async delete(req: Request, res: Response, next: NextFunction): Promise { /* #swagger.tags = ['User Roles'] + #swagger.security = [{ + "bearerAuth": [] + }] #swagger.parameters['id'] = { in: 'path', description: 'User role ID.', @@ -230,6 +268,9 @@ export class UserRoleController { static async restore(req: Request, res: Response, next: NextFunction): Promise { /* #swagger.tags = ['User Roles'] + #swagger.security = [{ + "bearerAuth": [] + }] #swagger.parameters['id'] = { in: 'path', description: 'User role ID.', diff --git a/src/helpers/common.ts b/src/helpers/common.ts index 33d1f37..7e1bcc1 100644 --- a/src/helpers/common.ts +++ b/src/helpers/common.ts @@ -46,6 +46,16 @@ export default class CommonHelper { return ret; } + static objectFlip(obj: any): any { + const ret = {}; + + Object.keys(obj).forEach(key => { + ret[obj[key]] = key; + }); + + return ret; + } + static countObject(obj: any): number { let c = 0; diff --git a/src/routes/private.ts b/src/routes/private.ts index 2b6a0e9..5f523c8 100644 --- a/src/routes/private.ts +++ b/src/routes/private.ts @@ -22,6 +22,7 @@ export class RoutePrivate { app.put('/api/user/restore/:id', UserController.restore) app.get('/api/user_role/list', UserRoleController.list) + app.get('/api/user_role/role', UserRoleController.role) app.post('/api/user_role/create', UserRoleController.create) app.put('/api/user_role/update/:id', UserRoleController.update) app.delete('/api/user_role/delete/:id', UserRoleController.delete) diff --git a/src/swagger/builder.js b/src/swagger/builder.js index 878fae1..e7b0e60 100644 --- a/src/swagger/builder.js +++ b/src/swagger/builder.js @@ -9,6 +9,47 @@ const doc = { }, host: config.get('server.host_swagger') + ':' + config.get('server.port'), components: { + schemas: { + login: { + $username: "admin", + $password: "123456" + }, + user: { + $email: "admin@gmail.com", + $username: "admin", + $password: "123456", + $retype_password: "123456", + $name: "Administrator", + $id_role: "4a6991b6-78a0-4b3b-bf5c-fa0af5f69be8", + $status: "Y" + }, + user_profile: { + $email: "admin@gmail.com", + $username: "admin", + $name: "Administrator" + }, + user_password: { + $password: "123456", + $retype_password: "123456" + }, + user_role: { + $name: "Administrator", + $roles: ["dashboard"], + $status: "Y" + }, + user_activity: { + $id_user: "4a6991b6-78a0-4b3b-bf5c-fa0af5f69be8", + $refresh_token: "1a1221b6-78a0-4b3b-bf5c-fa0af5f69be8", + $module: "dashboard", + $description: "Main Dashboard", + $action: "V", + $url: "https://bri-dev.shiblysolution.id/", + $data_body: "{\"from\":\"2024-11-01\",\"to\":\"2024-11-26\"}" + } + }, + parameters: { + + }, securitySchemes: { bearerAuth: { type: 'http', diff --git a/src/swagger/swagger.json b/src/swagger/swagger.json index 445b9c6..5e2694e 100644 --- a/src/swagger/swagger.json +++ b/src/swagger/swagger.json @@ -61,12 +61,12 @@ "name": "order_direction", "in": "query", "required": true, - "enum": [ - "ASC", - "DESC" - ], "schema": { - "type": "string" + "type": "string", + "enum": [ + "ASC", + "DESC" + ] } } ], @@ -88,47 +88,6 @@ "User" ], "description": "", - "parameters": [ - { - "name": "body", - "in": "body", - "description": "User data", - "required": true, - "schema": { - "type": "object", - "properties": { - "email": { - "type": "string", - "example": "admin@gmail.com" - }, - "username": { - "type": "string", - "example": "admin" - }, - "password": { - "type": "string", - "example": "123456" - }, - "retype_password": { - "type": "string", - "example": "123456" - }, - "name": { - "type": "string", - "example": "Administrator" - }, - "id_role": { - "type": "string", - "example": "4a6991b6-78a0-4b3b-bf5c-fa0af5f69be8" - }, - "status": { - "type": "string", - "example": "Y" - } - } - } - } - ], "responses": { "default": { "description": "" @@ -138,7 +97,17 @@ { "bearerAuth": [] } - ] + ], + "requestBody": { + "required": true, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/user" + } + } + } + } } }, "/api/user/update/{id}": { @@ -169,15 +138,11 @@ } ], "requestBody": { + "required": true, "content": { "application/json": { "schema": { - "type": "object", - "properties": { - "id": { - "example": "any" - } - } + "$ref": "#/components/schemas/user" } } } @@ -212,15 +177,11 @@ } ], "requestBody": { + "required": true, "content": { "application/json": { "schema": { - "type": "object", - "properties": { - "id": { - "example": "any" - } - } + "$ref": "#/components/schemas/user_profile" } } } @@ -255,15 +216,11 @@ } ], "requestBody": { + "required": true, "content": { "application/json": { "schema": { - "type": "object", - "properties": { - "id": { - "example": "any" - } - } + "$ref": "#/components/schemas/user_password" } } } @@ -391,7 +348,30 @@ "default": { "description": "" } - } + }, + "security": [ + { + "bearerAuth": [] + } + ] + } + }, + "/api/user_role/role": { + "get": { + "tags": [ + "User Roles" + ], + "description": "", + "responses": { + "default": { + "description": "" + } + }, + "security": [ + { + "bearerAuth": [] + } + ] } }, "/api/user_role/create": { @@ -400,40 +380,25 @@ "User Roles" ], "description": "", - "parameters": [ - { - "name": "body", - "in": "body", - "description": "User role data", - "required": true, - "schema": { - "type": "object", - "properties": { - "name": { - "type": "string", - "example": "Administrator" - }, - "roles": { - "type": "array", - "example": [ - "dashboard" - ], - "items": { - "type": "string" - } - }, - "status": { - "type": "string", - "example": "Y" - } - } - } - } - ], "responses": { "default": { "description": "" } + }, + "security": [ + { + "bearerAuth": [] + } + ], + "requestBody": { + "required": true, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/user_role" + } + } + } } } }, @@ -459,16 +424,17 @@ "description": "" } }, + "security": [ + { + "bearerAuth": [] + } + ], "requestBody": { + "required": true, "content": { "application/json": { "schema": { - "type": "object", - "properties": { - "id": { - "example": "any" - } - } + "$ref": "#/components/schemas/user_role" } } } @@ -496,7 +462,12 @@ "default": { "description": "" } - } + }, + "security": [ + { + "bearerAuth": [] + } + ] } }, "/api/user_role/restore/{id}": { @@ -520,7 +491,12 @@ "default": { "description": "" } - } + }, + "security": [ + { + "bearerAuth": [] + } + ] } }, "/api/user_activity": { @@ -586,7 +562,12 @@ "default": { "description": "" } - } + }, + "security": [ + { + "bearerAuth": [] + } + ] } }, "/api/user_activity/create": { @@ -595,51 +576,25 @@ "User Activity" ], "description": "", - "parameters": [ - { - "name": "body", - "in": "body", - "description": "User Activity data", - "required": true, - "schema": { - "type": "object", - "properties": { - "id_user": { - "type": "string", - "example": "4a6991b6-78a0-4b3b-bf5c-fa0af5f69be8" - }, - "refresh_token": { - "type": "string", - "example": "1a1221b6-78a0-4b3b-bf5c-fa0af5f69be8" - }, - "module": { - "type": "string", - "example": "dashboard" - }, - "description": { - "type": "string", - "example": "Main Dashboard" - }, - "action": { - "type": "string", - "example": "V" - }, - "url": { - "type": "string", - "example": "https://bri-dev.shiblysolution.id/" - }, - "data_body": { - "type": "string", - "example": "{'from':'2024-11-01','to':'2024-11-26'}" - } - } - } - } - ], "responses": { "default": { "description": "" } + }, + "security": [ + { + "bearerAuth": [] + } + ], + "requestBody": { + "required": true, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/user_activity" + } + } + } } } }, @@ -698,7 +653,12 @@ "default": { "description": "" } - } + }, + "security": [ + { + "bearerAuth": [] + } + ] } }, "/api/refresh_token/force_logout/{refresh_token}": { @@ -731,31 +691,20 @@ "Auth" ], "description": "", - "parameters": [ - { - "name": "body", - "in": "body", - "description": "Login data", - "required": true, - "schema": { - "type": "object", - "properties": { - "username": { - "type": "string", - "example": "admin" - }, - "password": { - "type": "string", - "example": "123456" - } - } - } - } - ], "responses": { "default": { "description": "" } + }, + "requestBody": { + "required": true, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/login" + } + } + } } } }, @@ -809,6 +758,176 @@ } }, "components": { + "schemas": { + "login": { + "type": "object", + "properties": { + "username": { + "type": "string", + "example": "admin" + }, + "password": { + "type": "string", + "example": "123456" + } + }, + "required": [ + "username", + "password" + ] + }, + "user": { + "type": "object", + "properties": { + "email": { + "type": "string", + "example": "admin@gmail.com" + }, + "username": { + "type": "string", + "example": "admin" + }, + "password": { + "type": "string", + "example": "123456" + }, + "retype_password": { + "type": "string", + "example": "123456" + }, + "name": { + "type": "string", + "example": "Administrator" + }, + "id_role": { + "type": "string", + "example": "4a6991b6-78a0-4b3b-bf5c-fa0af5f69be8" + }, + "status": { + "type": "string", + "example": "Y" + } + }, + "required": [ + "email", + "username", + "password", + "retype_password", + "name", + "id_role", + "status" + ] + }, + "user_profile": { + "type": "object", + "properties": { + "email": { + "type": "string", + "example": "admin@gmail.com" + }, + "username": { + "type": "string", + "example": "admin" + }, + "name": { + "type": "string", + "example": "Administrator" + } + }, + "required": [ + "email", + "username", + "name" + ] + }, + "user_password": { + "type": "object", + "properties": { + "password": { + "type": "string", + "example": "123456" + }, + "retype_password": { + "type": "string", + "example": "123456" + } + }, + "required": [ + "password", + "retype_password" + ] + }, + "user_role": { + "type": "object", + "properties": { + "name": { + "type": "string", + "example": "Administrator" + }, + "roles": { + "type": "array", + "example": [ + "dashboard" + ], + "items": { + "type": "string" + } + }, + "status": { + "type": "string", + "example": "Y" + } + }, + "required": [ + "name", + "roles", + "status" + ] + }, + "user_activity": { + "type": "object", + "properties": { + "id_user": { + "type": "string", + "example": "4a6991b6-78a0-4b3b-bf5c-fa0af5f69be8" + }, + "refresh_token": { + "type": "string", + "example": "1a1221b6-78a0-4b3b-bf5c-fa0af5f69be8" + }, + "module": { + "type": "string", + "example": "dashboard" + }, + "description": { + "type": "string", + "example": "Main Dashboard" + }, + "action": { + "type": "string", + "example": "V" + }, + "url": { + "type": "string", + "example": "https://bri-dev.shiblysolution.id/" + }, + "data_body": { + "type": "string", + "example": "{\"from\":\"2024-11-01\",\"to\":\"2024-11-26\"}" + } + }, + "required": [ + "id_user", + "refresh_token", + "module", + "description", + "action", + "url", + "data_body" + ] + } + }, + "parameters": {}, "securitySchemes": { "bearerAuth": { "type": "http",