update master data
This commit is contained in:
410
src/controllers/menu.ts
Normal file
410
src/controllers/menu.ts
Normal file
@ -0,0 +1,410 @@
|
|||||||
|
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";
|
||||||
|
import { ILogObj, Logger } from "tslog";
|
||||||
|
import { Language } from "../langs/lang";
|
||||||
|
import { Paging } from "entity";
|
||||||
|
import CommonHelper from "../helpers/common";
|
||||||
|
import * as fastcsv from 'fast-csv';
|
||||||
|
import dayjs from "dayjs";
|
||||||
|
import { Menu } from "entity";
|
||||||
|
|
||||||
|
const log: Logger<ILogObj> = new Logger({ name: '[MenuController]', type: 'pretty' });
|
||||||
|
|
||||||
|
export class MenuController {
|
||||||
|
static async list(req: Request, res: Response, next: NextFunction): Promise<Response> {
|
||||||
|
/*
|
||||||
|
#swagger.tags = ['Menu']
|
||||||
|
#swagger.parameters['filter'] = {
|
||||||
|
in: 'query',
|
||||||
|
type: 'string'
|
||||||
|
}
|
||||||
|
#swagger.parameters['limit'] = {
|
||||||
|
in: 'query',
|
||||||
|
required: true,
|
||||||
|
type: 'number'
|
||||||
|
}
|
||||||
|
#swagger.parameters['page'] = {
|
||||||
|
in: 'query',
|
||||||
|
required: true,
|
||||||
|
type: 'number'
|
||||||
|
}
|
||||||
|
#swagger.parameters['with_deleted'] = {
|
||||||
|
in: 'query',
|
||||||
|
required: true,
|
||||||
|
type: 'boolean'
|
||||||
|
}
|
||||||
|
#swagger.parameters['order_field'] = {
|
||||||
|
in: 'query',
|
||||||
|
required: true,
|
||||||
|
type: 'string'
|
||||||
|
}
|
||||||
|
#swagger.parameters['order_direction'] = {
|
||||||
|
in: 'query',
|
||||||
|
required: true,
|
||||||
|
schema: {
|
||||||
|
'@enum': ['ASC', 'DESC']
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#swagger.parameters['token'] = {
|
||||||
|
in: 'query',
|
||||||
|
required: true,
|
||||||
|
type: 'string'
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
try {
|
||||||
|
const schema = Joi.object().keys({
|
||||||
|
filter: Joi.string().allow('').optional().label('Filter'),
|
||||||
|
page: Joi.number().required().min(1).label('Page'),
|
||||||
|
limit: Joi.number().required().min(1).label('Limit'),
|
||||||
|
with_deleted: Joi.bool().required().label('With Deleted'),
|
||||||
|
order_field: Joi.string().required().label('Order Field'),
|
||||||
|
order_direction: Joi.string().allow('asc', 'desc').required().label('Order Direction'),
|
||||||
|
token: Joi.string().required().label('Token'),
|
||||||
|
});
|
||||||
|
|
||||||
|
const param: Paging = await schema.validateAsync(req.query);
|
||||||
|
|
||||||
|
const repo = OrmHelper.DB.getRepository(Menu);
|
||||||
|
|
||||||
|
const offset = (param.page - 1) * param.limit
|
||||||
|
|
||||||
|
let whereAttr = '';
|
||||||
|
let whereVal: any = {};
|
||||||
|
|
||||||
|
if (param.filter) {
|
||||||
|
whereAttr = 'name like :filter_like ';
|
||||||
|
// whereVal['filter'] = param.filter
|
||||||
|
whereVal['filter_like'] = '%' + param.filter + '%'
|
||||||
|
}
|
||||||
|
|
||||||
|
const res_count = repo.createQueryBuilder()
|
||||||
|
.where(whereAttr, whereVal);
|
||||||
|
const res_list = repo.createQueryBuilder()
|
||||||
|
.where(whereAttr, whereVal)
|
||||||
|
.orderBy(param.order_field, param.order_direction)
|
||||||
|
.offset(offset)
|
||||||
|
.limit(param.limit);
|
||||||
|
|
||||||
|
if (param.with_deleted) {
|
||||||
|
res_count.withDeleted();
|
||||||
|
res_list.withDeleted();
|
||||||
|
}
|
||||||
|
|
||||||
|
const current_page = param.page;
|
||||||
|
const total_count_data = await res_count.getCount();
|
||||||
|
const list_data = await res_list.getMany();
|
||||||
|
const count_data = CommonHelper.countObject(list_data);
|
||||||
|
|
||||||
|
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 export(req: Request, res: Response, next: NextFunction): Promise<void | Response> {
|
||||||
|
/*
|
||||||
|
#swagger.tags = ['Menu']
|
||||||
|
#swagger.parameters['filter'] = {
|
||||||
|
in: 'query',
|
||||||
|
type: 'string'
|
||||||
|
}
|
||||||
|
#swagger.parameters['filter'] = {
|
||||||
|
in: 'query',
|
||||||
|
type: 'string'
|
||||||
|
}
|
||||||
|
#swagger.parameters['order_field'] = {
|
||||||
|
in: 'query',
|
||||||
|
required: true,
|
||||||
|
type: 'string'
|
||||||
|
}
|
||||||
|
#swagger.parameters['order_direction'] = {
|
||||||
|
in: 'query',
|
||||||
|
required: true,
|
||||||
|
schema: {
|
||||||
|
'@enum': ['ASC', 'DESC']
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#swagger.parameters['token'] = {
|
||||||
|
in: 'query',
|
||||||
|
required: true,
|
||||||
|
type: 'string'
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
try {
|
||||||
|
const schema = Joi.object().keys({
|
||||||
|
filter: Joi.string().allow('').optional().label('Filter'),
|
||||||
|
order_field: Joi.string().required().label('Order Field'),
|
||||||
|
order_direction: Joi.string().allow('asc', 'desc').required().label('Order Direction'),
|
||||||
|
token: Joi.string().required().label('Token'),
|
||||||
|
});
|
||||||
|
|
||||||
|
const param: Paging = await schema.validateAsync(req.query);
|
||||||
|
|
||||||
|
const repo = OrmHelper.DB.getRepository(Menu);
|
||||||
|
|
||||||
|
let whereAttr = '';
|
||||||
|
let whereVal: any = {};
|
||||||
|
|
||||||
|
if (param.filter) {
|
||||||
|
whereAttr = 'module like :filter_like or name like :filter_like ';
|
||||||
|
// whereVal['filter'] = param.filter
|
||||||
|
whereVal['filter_like'] = '%' + param.filter + '%'
|
||||||
|
}
|
||||||
|
|
||||||
|
const filename = "menu.csv";
|
||||||
|
|
||||||
|
res.setHeader('Content-Type', 'text/csv');
|
||||||
|
res.setHeader('Content-Disposition', 'attachment; filename=' + filename);
|
||||||
|
|
||||||
|
const csvStream = fastcsv.format({
|
||||||
|
headers: true,
|
||||||
|
writeHeaders: true,
|
||||||
|
transform: (row: Menu): any => ({
|
||||||
|
...row,
|
||||||
|
created_at: dayjs(row.created_at).format('DD-MM-YYYY HH:MM:ss'),
|
||||||
|
})
|
||||||
|
});
|
||||||
|
csvStream.pipe(res);
|
||||||
|
|
||||||
|
const limit = 50;
|
||||||
|
|
||||||
|
const fetchAndWrite = async (page: any) => {
|
||||||
|
const offset = (page - 1) * limit
|
||||||
|
|
||||||
|
const data = await repo.createQueryBuilder()
|
||||||
|
.where(whereAttr, whereVal)
|
||||||
|
.select([
|
||||||
|
'id',
|
||||||
|
'code',
|
||||||
|
'name',
|
||||||
|
'status',
|
||||||
|
'created_at'])
|
||||||
|
.orderBy(param.order_field, param.order_direction)
|
||||||
|
.offset(offset)
|
||||||
|
.limit(limit).getRawMany();
|
||||||
|
|
||||||
|
if (CommonHelper.countObject(data) === 0) {
|
||||||
|
csvStream.end();
|
||||||
|
} else {
|
||||||
|
data.forEach((item: any) => csvStream.write(item));
|
||||||
|
|
||||||
|
if (CommonHelper.countObject(data) == limit) {
|
||||||
|
fetchAndWrite(page + 1);
|
||||||
|
} else {
|
||||||
|
csvStream.end();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
fetchAndWrite(1);
|
||||||
|
} 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<Response> {
|
||||||
|
/*
|
||||||
|
#swagger.tags = ['Menu']
|
||||||
|
#swagger.security = [{
|
||||||
|
"bearerAuth": []
|
||||||
|
}]
|
||||||
|
|
||||||
|
#swagger.requestBody = {
|
||||||
|
required: true,
|
||||||
|
content: {
|
||||||
|
"application/json": {
|
||||||
|
schema: {
|
||||||
|
$ref: "#/components/schemas/menu"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
try {
|
||||||
|
const schema = Joi.object().keys({
|
||||||
|
module: Joi.string().max(128).required().label('Module'),
|
||||||
|
name: Joi.string().max(128).required().label('Name'),
|
||||||
|
link: Joi.string().max(256).required().label('Link'),
|
||||||
|
id_parent: Joi.string().uuid().optional().allow('').label('ID Parent'),
|
||||||
|
order: Joi.number().min(1).required().label('Order'),
|
||||||
|
icon: Joi.string().max(128).required().label('Icon'),
|
||||||
|
status: Joi.string().required().label('Status'),
|
||||||
|
});
|
||||||
|
|
||||||
|
const param: Menu = await schema.validateAsync(req.body);
|
||||||
|
|
||||||
|
const data = new Menu()
|
||||||
|
data.module = param.module
|
||||||
|
data.name = param.name
|
||||||
|
data.link = param.link
|
||||||
|
data.id_parent = param.id_parent
|
||||||
|
data.order = param.order
|
||||||
|
data.icon = param.icon
|
||||||
|
data.status = param.status
|
||||||
|
|
||||||
|
await OrmHelper.DB.manager.save(data);
|
||||||
|
|
||||||
|
return ReturnHelper.successResponseAny(res, 200, Language.lang.success_insert, data);
|
||||||
|
} catch (e: unknown) {
|
||||||
|
log.error(e);
|
||||||
|
const err = e as Error;
|
||||||
|
|
||||||
|
return ReturnHelper.errorResponse(res, 500, 401, Language.lang.failed_insert, err.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static async update(req: Request, res: Response, next: NextFunction): Promise<Response> {
|
||||||
|
/*
|
||||||
|
#swagger.tags = ['Menu']
|
||||||
|
#swagger.security = [{
|
||||||
|
"bearerAuth": []
|
||||||
|
}]
|
||||||
|
#swagger.parameters['id'] = {
|
||||||
|
in: 'path',
|
||||||
|
description: 'Menu ID.',
|
||||||
|
required: true,
|
||||||
|
type: 'string'
|
||||||
|
}
|
||||||
|
|
||||||
|
#swagger.requestBody = {
|
||||||
|
required: true,
|
||||||
|
content: {
|
||||||
|
"application/json": {
|
||||||
|
schema: {
|
||||||
|
$ref: "#/components/schemas/menu"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
try {
|
||||||
|
const schema = Joi.object().keys({
|
||||||
|
id: Joi.string().uuid().required().label('ID'),
|
||||||
|
module: Joi.string().max(128).required().label('Module'),
|
||||||
|
name: Joi.string().max(128).required().label('Name'),
|
||||||
|
link: Joi.string().max(256).required().label('Link'),
|
||||||
|
id_parent: Joi.string().uuid().optional().allow('').label('ID Parent'),
|
||||||
|
order: Joi.number().min(1).required().label('Order'),
|
||||||
|
icon: Joi.string().max(128).required().label('Icon'),
|
||||||
|
status: Joi.string().required().label('Status'),
|
||||||
|
});
|
||||||
|
|
||||||
|
req.body.id = req.params['id'];
|
||||||
|
|
||||||
|
const param: Menu = await schema.validateAsync(req.body);
|
||||||
|
|
||||||
|
const repo = OrmHelper.DB.getRepository(Menu);
|
||||||
|
|
||||||
|
const data = await repo.findOneBy({ id: param.id });
|
||||||
|
|
||||||
|
if (data != null) {
|
||||||
|
data.module = param.module
|
||||||
|
data.name = param.name
|
||||||
|
data.link = param.link
|
||||||
|
data.id_parent = param.id_parent
|
||||||
|
data.order = param.order
|
||||||
|
data.icon = param.icon
|
||||||
|
data.status = param.status
|
||||||
|
|
||||||
|
await repo.save(data);
|
||||||
|
|
||||||
|
return ReturnHelper.successResponseAny(res, 200, Language.lang.success_update, data);
|
||||||
|
} else {
|
||||||
|
return ReturnHelper.errorResponse(res, 404, 401, Language.lang.failed_not_found, "");
|
||||||
|
}
|
||||||
|
} catch (e: unknown) {
|
||||||
|
log.error(e);
|
||||||
|
const err = e as Error;
|
||||||
|
|
||||||
|
return ReturnHelper.errorResponse(res, 500, 402, Language.lang.failed_update, err.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static async delete(req: Request, res: Response, next: NextFunction): Promise<Response> {
|
||||||
|
/*
|
||||||
|
#swagger.tags = ['Menu']
|
||||||
|
#swagger.security = [{
|
||||||
|
"bearerAuth": []
|
||||||
|
}]
|
||||||
|
#swagger.parameters['id'] = {
|
||||||
|
in: 'path',
|
||||||
|
description: 'Menu ID.',
|
||||||
|
required: true,
|
||||||
|
type: 'string'
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
try {
|
||||||
|
const schema = Joi.object().keys({
|
||||||
|
id: Joi.string().uuid().required().label('ID')
|
||||||
|
});
|
||||||
|
|
||||||
|
const param: Menu = await schema.validateAsync(req.params);
|
||||||
|
|
||||||
|
const repo = OrmHelper.DB.getRepository(Menu);
|
||||||
|
|
||||||
|
const affected = (await repo.softDelete({ id: param.id })).affected;
|
||||||
|
|
||||||
|
if (affected > 0) {
|
||||||
|
return ReturnHelper.successResponseAny(res, 200, Language.lang.success_delete, {});
|
||||||
|
} else {
|
||||||
|
return ReturnHelper.errorResponse(res, 404, 401, Language.lang.failed_not_found, "");
|
||||||
|
}
|
||||||
|
} catch (e: unknown) {
|
||||||
|
log.error(e);
|
||||||
|
const err = e as Error;
|
||||||
|
|
||||||
|
return ReturnHelper.errorResponse(res, 500, 402, Language.lang.failed_delete, err.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static async restore(req: Request, res: Response, next: NextFunction): Promise<Response> {
|
||||||
|
/*
|
||||||
|
#swagger.tags = ['Menu']
|
||||||
|
#swagger.security = [{
|
||||||
|
"bearerAuth": []
|
||||||
|
}]
|
||||||
|
|
||||||
|
#swagger.parameters['id'] = {
|
||||||
|
in: 'path',
|
||||||
|
description: 'Menu ID.',
|
||||||
|
required: true,
|
||||||
|
type: 'string'
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
try {
|
||||||
|
const schema = Joi.object().keys({
|
||||||
|
id: Joi.string().uuid().required().label('ID')
|
||||||
|
});
|
||||||
|
|
||||||
|
const param: Menu = await schema.validateAsync(req.params);
|
||||||
|
|
||||||
|
const repo = OrmHelper.DB.getRepository(Menu);
|
||||||
|
|
||||||
|
const affected = (await repo.restore({ id: param.id })).affected;
|
||||||
|
|
||||||
|
if (affected > 0) {
|
||||||
|
return ReturnHelper.successResponseAny(res, 200, Language.lang.success_restore, {});
|
||||||
|
} else {
|
||||||
|
return ReturnHelper.errorResponse(res, 404, 401, Language.lang.failed_not_found, "");
|
||||||
|
}
|
||||||
|
} catch (e: unknown) {
|
||||||
|
log.error(e);
|
||||||
|
const err = e as Error;
|
||||||
|
|
||||||
|
return ReturnHelper.errorResponse(res, 500, 402, Language.lang.failed_restore, err.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,7 +1,7 @@
|
|||||||
import config from 'config';
|
import config from 'config';
|
||||||
import { DataSource } from "typeorm";
|
import { DataSource } from "typeorm";
|
||||||
import { ILogObj, Logger } from 'tslog';
|
import { ILogObj, Logger } from 'tslog';
|
||||||
import { Branch, Product } from 'entity'
|
import { Branch, Menu, Product } from 'entity'
|
||||||
|
|
||||||
export class OrmHelper {
|
export class OrmHelper {
|
||||||
static DB: DataSource = null
|
static DB: DataSource = null
|
||||||
@ -20,7 +20,7 @@ export class OrmHelper {
|
|||||||
database: String(config.get("database.database")),
|
database: String(config.get("database.database")),
|
||||||
synchronize: true,
|
synchronize: true,
|
||||||
logging: true,
|
logging: true,
|
||||||
entities: [Product, Branch],
|
entities: [Product, Branch, Menu],
|
||||||
subscribers: [],
|
subscribers: [],
|
||||||
migrations: [],
|
migrations: [],
|
||||||
})
|
})
|
||||||
|
|||||||
@ -1,12 +1,36 @@
|
|||||||
import express from 'express';
|
import express from 'express';
|
||||||
import { Language } from '../langs/lang';
|
import { Language } from '../langs/lang';
|
||||||
import JwtHelper from '../helpers/jwt';
|
import JwtHelper from '../helpers/jwt';
|
||||||
|
import { BranchController } from '../controllers/branch';
|
||||||
|
import { ProductController } from '../controllers/product';
|
||||||
|
import { MenuController } from '../controllers/menu';
|
||||||
|
|
||||||
export class RoutePrivate {
|
export class RoutePrivate {
|
||||||
static setup(app: express.Application) {
|
static setup(app: express.Application) {
|
||||||
|
|
||||||
// app.use(Language.apply)
|
app.use(Language.apply)
|
||||||
|
|
||||||
JwtHelper.secure(app);
|
JwtHelper.secure(app);
|
||||||
|
|
||||||
|
app.get('/api/branch/list', BranchController.list)
|
||||||
|
app.get('/api/branch/export', BranchController.export)
|
||||||
|
app.post('/api/branch/create', BranchController.create)
|
||||||
|
app.put('/api/branch/update/:id', BranchController.update)
|
||||||
|
app.delete('/api/branch/delete/:id', BranchController.delete)
|
||||||
|
app.put('/api/branch/restore/:id', BranchController.restore)
|
||||||
|
|
||||||
|
app.get('/api/product/list', ProductController.list)
|
||||||
|
app.get('/api/product/export', ProductController.export)
|
||||||
|
app.post('/api/product/create', ProductController.create)
|
||||||
|
app.put('/api/product/update/:id', ProductController.update)
|
||||||
|
app.delete('/api/product/delete/:id', ProductController.delete)
|
||||||
|
app.put('/api/product/restore/:id', ProductController.restore)
|
||||||
|
|
||||||
|
app.get('/api/menu/list', MenuController.list)
|
||||||
|
app.get('/api/menu/export', MenuController.export)
|
||||||
|
app.post('/api/menu/create', MenuController.create)
|
||||||
|
app.put('/api/menu/update/:id', MenuController.update)
|
||||||
|
app.delete('/api/menu/delete/:id', MenuController.delete)
|
||||||
|
app.put('/api/menu/restore/:id', MenuController.restore)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1,25 +1,9 @@
|
|||||||
import express from 'express';
|
import express from 'express';
|
||||||
import { BranchController } from '../controllers/branch';
|
|
||||||
import { ProductController } from '../controllers/product';
|
|
||||||
import { Language } from '../langs/lang';
|
import { Language } from '../langs/lang';
|
||||||
|
|
||||||
export class RoutePublic {
|
export class RoutePublic {
|
||||||
static setup(app: express.Application) {
|
static setup(app: express.Application) {
|
||||||
|
|
||||||
app.use(Language.apply)
|
app.use(Language.apply)
|
||||||
|
|
||||||
app.get('/api/branch/list', BranchController.list)
|
|
||||||
app.get('/api/branch/export', BranchController.export)
|
|
||||||
app.post('/api/branch/create', BranchController.create)
|
|
||||||
app.put('/api/branch/update/:id', BranchController.update)
|
|
||||||
app.delete('/api/branch/delete/:id', BranchController.delete)
|
|
||||||
app.put('/api/branch/restore/:id', BranchController.restore)
|
|
||||||
|
|
||||||
app.get('/api/product/list', ProductController.list)
|
|
||||||
app.get('/api/product/export', ProductController.export)
|
|
||||||
app.post('/api/product/create', ProductController.create)
|
|
||||||
app.put('/api/product/update/:id', ProductController.update)
|
|
||||||
app.delete('/api/product/delete/:id', ProductController.delete)
|
|
||||||
app.put('/api/product/restore/:id', ProductController.restore)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -26,6 +26,14 @@ const doc = {
|
|||||||
$name: "Branch name",
|
$name: "Branch name",
|
||||||
$status: "Y",
|
$status: "Y",
|
||||||
},
|
},
|
||||||
|
menu: {
|
||||||
|
$module: "Dashboard",
|
||||||
|
$name: "Dashboard",
|
||||||
|
$link: "/",
|
||||||
|
$id_parent: "",
|
||||||
|
$order: 1,
|
||||||
|
$icon: ""
|
||||||
|
},
|
||||||
},
|
},
|
||||||
parameters: {
|
parameters: {
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user