initial
This commit is contained in:
@ -18,6 +18,7 @@
|
|||||||
"compression": "^1.7.5",
|
"compression": "^1.7.5",
|
||||||
"config": "^3.3.12",
|
"config": "^3.3.12",
|
||||||
"cors": "^2.8.5",
|
"cors": "^2.8.5",
|
||||||
|
"Entity": "file:../entity",
|
||||||
"express": "^4.21.1",
|
"express": "^4.21.1",
|
||||||
"express-jwt": "^8.4.1",
|
"express-jwt": "^8.4.1",
|
||||||
"fast-csv": "^5.0.2",
|
"fast-csv": "^5.0.2",
|
||||||
|
|||||||
@ -2,7 +2,6 @@ import { Response, NextFunction, Application } from "express";
|
|||||||
import { Request } from "express-jwt";
|
import { Request } from "express-jwt";
|
||||||
import { ReturnHelper } from "../helpers/express/return";
|
import { ReturnHelper } from "../helpers/express/return";
|
||||||
import { OrmHelper } from "../helpers/orm";
|
import { OrmHelper } from "../helpers/orm";
|
||||||
import { User } from "../entity/users";
|
|
||||||
import Joi from "joi";
|
import Joi from "joi";
|
||||||
import { ILogObj, Logger } from "tslog";
|
import { ILogObj, Logger } from "tslog";
|
||||||
import { Language } from "../langs/lang";
|
import { Language } from "../langs/lang";
|
||||||
@ -10,13 +9,14 @@ import { Paging } from "../types/paging";
|
|||||||
import CommonHelper from "../helpers/common";
|
import CommonHelper from "../helpers/common";
|
||||||
import * as fastcsv from 'fast-csv';
|
import * as fastcsv from 'fast-csv';
|
||||||
import dayjs from "dayjs";
|
import dayjs from "dayjs";
|
||||||
|
import { Branch } from "Entity";
|
||||||
|
|
||||||
const log: Logger<ILogObj> = new Logger({ name: '[UserController]', type: 'pretty' });
|
const log: Logger<ILogObj> = new Logger({ name: '[BranchController]', type: 'pretty' });
|
||||||
|
|
||||||
export class UserController {
|
export class BranchController {
|
||||||
static async list(req: Request, res: Response, next: NextFunction): Promise<Response> {
|
static async list(req: Request, res: Response, next: NextFunction): Promise<Response> {
|
||||||
/*
|
/*
|
||||||
#swagger.tags = ['User']
|
#swagger.tags = ['Branch']
|
||||||
#swagger.parameters['filter'] = {
|
#swagger.parameters['filter'] = {
|
||||||
in: 'query',
|
in: 'query',
|
||||||
type: 'string'
|
type: 'string'
|
||||||
@ -68,7 +68,7 @@ export class UserController {
|
|||||||
|
|
||||||
const param: Paging = await schema.validateAsync(req.query);
|
const param: Paging = await schema.validateAsync(req.query);
|
||||||
|
|
||||||
const userRepository = OrmHelper.DB.getRepository(User);
|
const repo = OrmHelper.DB.getRepository(Branch);
|
||||||
|
|
||||||
const offset = (param.page - 1) * param.limit
|
const offset = (param.page - 1) * param.limit
|
||||||
|
|
||||||
@ -76,14 +76,14 @@ export class UserController {
|
|||||||
let whereVal: any = {};
|
let whereVal: any = {};
|
||||||
|
|
||||||
if (param.filter) {
|
if (param.filter) {
|
||||||
whereAttr = 'name like :filter_like or username like :filter_like or email = :filter ';
|
whereAttr = 'name like :filter_like ';
|
||||||
whereVal['filter'] = param.filter
|
// whereVal['filter'] = param.filter
|
||||||
whereVal['filter_like'] = '%' + param.filter + '%'
|
whereVal['filter_like'] = '%' + param.filter + '%'
|
||||||
}
|
}
|
||||||
|
|
||||||
const res_count = userRepository.createQueryBuilder()
|
const res_count = repo.createQueryBuilder()
|
||||||
.where(whereAttr, whereVal);
|
.where(whereAttr, whereVal);
|
||||||
const res_list = userRepository.createQueryBuilder()
|
const res_list = repo.createQueryBuilder()
|
||||||
.where(whereAttr, whereVal)
|
.where(whereAttr, whereVal)
|
||||||
.orderBy(param.order_field, param.order_direction)
|
.orderBy(param.order_field, param.order_direction)
|
||||||
.offset(offset)
|
.offset(offset)
|
||||||
@ -110,7 +110,7 @@ export class UserController {
|
|||||||
|
|
||||||
static async export(req: Request, res: Response, next: NextFunction): Promise<void | Response> {
|
static async export(req: Request, res: Response, next: NextFunction): Promise<void | Response> {
|
||||||
/*
|
/*
|
||||||
#swagger.tags = ['User']
|
#swagger.tags = ['Branch']
|
||||||
#swagger.parameters['filter'] = {
|
#swagger.parameters['filter'] = {
|
||||||
in: 'query',
|
in: 'query',
|
||||||
type: 'string'
|
type: 'string'
|
||||||
@ -148,18 +148,18 @@ export class UserController {
|
|||||||
|
|
||||||
const param: Paging = await schema.validateAsync(req.query);
|
const param: Paging = await schema.validateAsync(req.query);
|
||||||
|
|
||||||
const userRepository = OrmHelper.DB.getRepository(User);
|
const repo = OrmHelper.DB.getRepository(Branch);
|
||||||
|
|
||||||
let whereAttr = '';
|
let whereAttr = '';
|
||||||
let whereVal: any = {};
|
let whereVal: any = {};
|
||||||
|
|
||||||
if (param.filter) {
|
if (param.filter) {
|
||||||
whereAttr = 'name like :filter_like or username like :filter_like or email = :filter ';
|
whereAttr = 'name like :filter_like ';
|
||||||
whereVal['filter'] = param.filter
|
// whereVal['filter'] = param.filter
|
||||||
whereVal['filter_like'] = '%' + param.filter + '%'
|
whereVal['filter_like'] = '%' + param.filter + '%'
|
||||||
}
|
}
|
||||||
|
|
||||||
const filename = "user.csv";
|
const filename = "branch.csv";
|
||||||
|
|
||||||
res.setHeader('Content-Type', 'text/csv');
|
res.setHeader('Content-Type', 'text/csv');
|
||||||
res.setHeader('Content-Disposition', 'attachment; filename=' + filename);
|
res.setHeader('Content-Disposition', 'attachment; filename=' + filename);
|
||||||
@ -167,7 +167,7 @@ export class UserController {
|
|||||||
const csvStream = fastcsv.format({
|
const csvStream = fastcsv.format({
|
||||||
headers: true,
|
headers: true,
|
||||||
writeHeaders: true,
|
writeHeaders: true,
|
||||||
transform: (row: User): any => ({
|
transform: (row: Branch): any => ({
|
||||||
...row,
|
...row,
|
||||||
created_at: dayjs(row.created_at).format('DD-MM-YYYY HH:MM:ss'),
|
created_at: dayjs(row.created_at).format('DD-MM-YYYY HH:MM:ss'),
|
||||||
})
|
})
|
||||||
@ -179,16 +179,14 @@ export class UserController {
|
|||||||
const fetchAndWrite = async (page: any) => {
|
const fetchAndWrite = async (page: any) => {
|
||||||
const offset = (page - 1) * limit
|
const offset = (page - 1) * limit
|
||||||
|
|
||||||
const data = await userRepository.createQueryBuilder()
|
const data = await repo.createQueryBuilder()
|
||||||
.where(whereAttr, whereVal)
|
.where(whereAttr, whereVal)
|
||||||
.select([
|
.select([
|
||||||
'id',
|
'id',
|
||||||
|
'code',
|
||||||
'name',
|
'name',
|
||||||
'email',
|
|
||||||
'username',
|
|
||||||
'status',
|
'status',
|
||||||
'created_at'])
|
'created_at'])
|
||||||
.addSelect('(select role.name from user_roles role where role.id=User.id_role )', 'role_name')
|
|
||||||
.orderBy(param.order_field, param.order_direction)
|
.orderBy(param.order_field, param.order_direction)
|
||||||
.offset(offset)
|
.offset(offset)
|
||||||
.limit(limit).getRawMany();
|
.limit(limit).getRawMany();
|
||||||
@ -217,7 +215,7 @@ export class UserController {
|
|||||||
|
|
||||||
static async create(req: Request, res: Response, next: NextFunction): Promise<Response> {
|
static async create(req: Request, res: Response, next: NextFunction): Promise<Response> {
|
||||||
/*
|
/*
|
||||||
#swagger.tags = ['User']
|
#swagger.tags = ['Branch']
|
||||||
#swagger.security = [{
|
#swagger.security = [{
|
||||||
"bearerAuth": []
|
"bearerAuth": []
|
||||||
}]
|
}]
|
||||||
@ -227,7 +225,7 @@ export class UserController {
|
|||||||
content: {
|
content: {
|
||||||
"application/json": {
|
"application/json": {
|
||||||
schema: {
|
schema: {
|
||||||
$ref: "#/components/schemas/user"
|
$ref: "#/components/schemas/branch"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -236,27 +234,17 @@ export class UserController {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const schema = Joi.object().keys({
|
const schema = Joi.object().keys({
|
||||||
email: Joi.string().email().max(64).required().label('Email'),
|
code: Joi.string().max(6).required().label('Code'),
|
||||||
username: Joi.string().max(64).required().label('Username'),
|
name: Joi.string().max(256).required().label('Name'),
|
||||||
password: Joi.string().alphanum().min(6).required().label('Password'),
|
|
||||||
retype_password: Joi.ref('password'),
|
|
||||||
name: Joi.string().max(64).required().label('Name'),
|
|
||||||
id_role: Joi.string().uuid().required().label('Role'),
|
|
||||||
status: Joi.string().required().label('Status'),
|
status: Joi.string().required().label('Status'),
|
||||||
});
|
});
|
||||||
|
|
||||||
const param: User = await schema.validateAsync(req.body);
|
const param: Branch = await schema.validateAsync(req.body);
|
||||||
|
|
||||||
const data = new User()
|
const data = new Branch()
|
||||||
data.name = param.name
|
data.code = param.code
|
||||||
data.username = param.username
|
|
||||||
data.password = param.password
|
|
||||||
data.email = param.email
|
|
||||||
data.id_role = param.id_role
|
|
||||||
data.status = param.status
|
data.status = param.status
|
||||||
|
|
||||||
data.hashPassword();
|
|
||||||
|
|
||||||
await OrmHelper.DB.manager.save(data);
|
await OrmHelper.DB.manager.save(data);
|
||||||
|
|
||||||
return ReturnHelper.successResponseAny(res, 200, Language.lang.success_insert, data);
|
return ReturnHelper.successResponseAny(res, 200, Language.lang.success_insert, data);
|
||||||
@ -268,125 +256,15 @@ export class UserController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static async updateProfile(req: Request, res: Response, next: NextFunction): Promise<Response> {
|
|
||||||
/*
|
|
||||||
#swagger.tags = ['User']
|
|
||||||
#swagger.security = [{
|
|
||||||
"bearerAuth": []
|
|
||||||
}]
|
|
||||||
|
|
||||||
#swagger.requestBody = {
|
|
||||||
required: true,
|
|
||||||
description: "This action will effect to user related token JWT logged",
|
|
||||||
content: {
|
|
||||||
"application/json": {
|
|
||||||
schema: {
|
|
||||||
$ref: "#/components/schemas/user_profile"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
try {
|
|
||||||
const schema = Joi.object().keys({
|
|
||||||
id: Joi.string().uuid().required().label('ID'),
|
|
||||||
email: Joi.string().email().max(64).required().label('Email'),
|
|
||||||
username: Joi.string().max(64).required().label('Username'),
|
|
||||||
name: Joi.string().max(64).required().label('Name'),
|
|
||||||
});
|
|
||||||
|
|
||||||
req.body.id = req.auth.data.id;
|
|
||||||
|
|
||||||
const param: User = await schema.validateAsync(req.body);
|
|
||||||
|
|
||||||
const userRepository = OrmHelper.DB.getRepository(User);
|
|
||||||
|
|
||||||
const data = await userRepository.findOneBy({ id: param.id });
|
|
||||||
|
|
||||||
if (data != null) {
|
|
||||||
data.name = param.name
|
|
||||||
data.username = param.username
|
|
||||||
data.email = param.email
|
|
||||||
|
|
||||||
await userRepository.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 updatePassword(req: Request, res: Response, next: NextFunction): Promise<Response> {
|
|
||||||
/*
|
|
||||||
#swagger.tags = ['User']
|
|
||||||
#swagger.security = [{
|
|
||||||
"bearerAuth": []
|
|
||||||
}]
|
|
||||||
|
|
||||||
#swagger.requestBody = {
|
|
||||||
required: true,
|
|
||||||
description: "This action will effect to user related token JWT logged",
|
|
||||||
content: {
|
|
||||||
"application/json": {
|
|
||||||
schema: {
|
|
||||||
$ref: "#/components/schemas/user_password"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
try {
|
|
||||||
const schema = Joi.object().keys({
|
|
||||||
id: Joi.string().uuid().required().label('ID'),
|
|
||||||
password: Joi.string().required().label('Password'),
|
|
||||||
retype_password: Joi.ref('password'),
|
|
||||||
});
|
|
||||||
|
|
||||||
req.body.id = req.auth.data.id;
|
|
||||||
|
|
||||||
const param: User = await schema.validateAsync(req.body);
|
|
||||||
|
|
||||||
const userRepository = OrmHelper.DB.getRepository(User);
|
|
||||||
|
|
||||||
const data = await userRepository.findOneBy({ id: param.id });
|
|
||||||
|
|
||||||
if (data != null) {
|
|
||||||
if (param.password) {
|
|
||||||
data.password = param.password
|
|
||||||
data.hashPassword();
|
|
||||||
}
|
|
||||||
|
|
||||||
await userRepository.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 update(req: Request, res: Response, next: NextFunction): Promise<Response> {
|
static async update(req: Request, res: Response, next: NextFunction): Promise<Response> {
|
||||||
/*
|
/*
|
||||||
#swagger.tags = ['User']
|
#swagger.tags = ['Branch']
|
||||||
#swagger.security = [{
|
#swagger.security = [{
|
||||||
"bearerAuth": []
|
"bearerAuth": []
|
||||||
}]
|
}]
|
||||||
#swagger.parameters['id'] = {
|
#swagger.parameters['id'] = {
|
||||||
in: 'path',
|
in: 'path',
|
||||||
description: 'User ID.',
|
description: 'Branch ID.',
|
||||||
required: true,
|
required: true,
|
||||||
type: 'string'
|
type: 'string'
|
||||||
}
|
}
|
||||||
@ -396,7 +274,7 @@ export class UserController {
|
|||||||
content: {
|
content: {
|
||||||
"application/json": {
|
"application/json": {
|
||||||
schema: {
|
schema: {
|
||||||
$ref: "#/components/schemas/user"
|
$ref: "#/components/schemas/branch"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -406,36 +284,25 @@ export class UserController {
|
|||||||
try {
|
try {
|
||||||
const schema = Joi.object().keys({
|
const schema = Joi.object().keys({
|
||||||
id: Joi.string().uuid().required().label('ID'),
|
id: Joi.string().uuid().required().label('ID'),
|
||||||
email: Joi.string().email().max(64).required().label('Email'),
|
code: Joi.string().max(6).required().label('Code'),
|
||||||
username: Joi.string().max(64).required().label('Username'),
|
name: Joi.string().max(256).required().label('Name'),
|
||||||
password: Joi.string().allow('').optional().label('Password'),
|
|
||||||
retype_password: Joi.ref('password'),
|
|
||||||
name: Joi.string().max(64).required().label('Name'),
|
|
||||||
id_role: Joi.string().uuid().required().label('Role'),
|
|
||||||
status: Joi.string().required().label('Status'),
|
status: Joi.string().required().label('Status'),
|
||||||
});
|
});
|
||||||
|
|
||||||
req.body.id = req.params['id'];
|
req.body.id = req.params['id'];
|
||||||
|
|
||||||
const param: User = await schema.validateAsync(req.body);
|
const param: Branch = await schema.validateAsync(req.body);
|
||||||
|
|
||||||
const userRepository = OrmHelper.DB.getRepository(User);
|
const repo = OrmHelper.DB.getRepository(Branch);
|
||||||
|
|
||||||
const data = await userRepository.findOneBy({ id: param.id });
|
const data = await repo.findOneBy({ id: param.id });
|
||||||
|
|
||||||
if (data != null) {
|
if (data != null) {
|
||||||
|
data.code = param.code
|
||||||
data.name = param.name
|
data.name = param.name
|
||||||
data.username = param.username
|
|
||||||
data.email = param.email
|
|
||||||
data.id_role = param.id_role
|
|
||||||
data.status = param.status
|
data.status = param.status
|
||||||
|
|
||||||
if (param.password) {
|
await repo.save(data);
|
||||||
data.password = param.password
|
|
||||||
data.hashPassword();
|
|
||||||
}
|
|
||||||
|
|
||||||
await userRepository.save(data);
|
|
||||||
|
|
||||||
return ReturnHelper.successResponseAny(res, 200, Language.lang.success_update, data);
|
return ReturnHelper.successResponseAny(res, 200, Language.lang.success_update, data);
|
||||||
} else {
|
} else {
|
||||||
@ -451,13 +318,13 @@ export class UserController {
|
|||||||
|
|
||||||
static async delete(req: Request, res: Response, next: NextFunction): Promise<Response> {
|
static async delete(req: Request, res: Response, next: NextFunction): Promise<Response> {
|
||||||
/*
|
/*
|
||||||
#swagger.tags = ['User']
|
#swagger.tags = ['Branch']
|
||||||
#swagger.security = [{
|
#swagger.security = [{
|
||||||
"bearerAuth": []
|
"bearerAuth": []
|
||||||
}]
|
}]
|
||||||
#swagger.parameters['id'] = {
|
#swagger.parameters['id'] = {
|
||||||
in: 'path',
|
in: 'path',
|
||||||
description: 'User ID.',
|
description: 'Branch ID.',
|
||||||
required: true,
|
required: true,
|
||||||
type: 'string'
|
type: 'string'
|
||||||
}
|
}
|
||||||
@ -467,11 +334,11 @@ export class UserController {
|
|||||||
id: Joi.string().uuid().required().label('ID')
|
id: Joi.string().uuid().required().label('ID')
|
||||||
});
|
});
|
||||||
|
|
||||||
const param: User = await schema.validateAsync(req.params);
|
const param: Branch = await schema.validateAsync(req.params);
|
||||||
|
|
||||||
const userRepository = OrmHelper.DB.getRepository(User);
|
const repo = OrmHelper.DB.getRepository(Branch);
|
||||||
|
|
||||||
const affected = (await userRepository.softDelete({ id: param.id })).affected;
|
const affected = (await repo.softDelete({ id: param.id })).affected;
|
||||||
|
|
||||||
if (affected > 0) {
|
if (affected > 0) {
|
||||||
return ReturnHelper.successResponseAny(res, 200, Language.lang.success_delete, {});
|
return ReturnHelper.successResponseAny(res, 200, Language.lang.success_delete, {});
|
||||||
@ -488,14 +355,14 @@ export class UserController {
|
|||||||
|
|
||||||
static async restore(req: Request, res: Response, next: NextFunction): Promise<Response> {
|
static async restore(req: Request, res: Response, next: NextFunction): Promise<Response> {
|
||||||
/*
|
/*
|
||||||
#swagger.tags = ['User']
|
#swagger.tags = ['Branch']
|
||||||
#swagger.security = [{
|
#swagger.security = [{
|
||||||
"bearerAuth": []
|
"bearerAuth": []
|
||||||
}]
|
}]
|
||||||
|
|
||||||
#swagger.parameters['id'] = {
|
#swagger.parameters['id'] = {
|
||||||
in: 'path',
|
in: 'path',
|
||||||
description: 'User ID.',
|
description: 'Branch ID.',
|
||||||
required: true,
|
required: true,
|
||||||
type: 'string'
|
type: 'string'
|
||||||
}
|
}
|
||||||
@ -505,11 +372,11 @@ export class UserController {
|
|||||||
id: Joi.string().uuid().required().label('ID')
|
id: Joi.string().uuid().required().label('ID')
|
||||||
});
|
});
|
||||||
|
|
||||||
const param: User = await schema.validateAsync(req.params);
|
const param: Branch = await schema.validateAsync(req.params);
|
||||||
|
|
||||||
const userRepository = OrmHelper.DB.getRepository(User);
|
const repo = OrmHelper.DB.getRepository(Branch);
|
||||||
|
|
||||||
const affected = (await userRepository.restore({ id: param.id })).affected;
|
const affected = (await repo.restore({ id: param.id })).affected;
|
||||||
|
|
||||||
if (affected > 0) {
|
if (affected > 0) {
|
||||||
return ReturnHelper.successResponseAny(res, 200, Language.lang.success_restore, {});
|
return ReturnHelper.successResponseAny(res, 200, Language.lang.success_restore, {});
|
||||||
393
src/controllers/product.ts
Normal file
393
src/controllers/product.ts
Normal file
@ -0,0 +1,393 @@
|
|||||||
|
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 "../types/paging";
|
||||||
|
import CommonHelper from "../helpers/common";
|
||||||
|
import * as fastcsv from 'fast-csv';
|
||||||
|
import dayjs from "dayjs";
|
||||||
|
import { Product } from "Entity";
|
||||||
|
|
||||||
|
const log: Logger<ILogObj> = new Logger({ name: '[ProductController]', type: 'pretty' });
|
||||||
|
|
||||||
|
export class ProductController {
|
||||||
|
static async list(req: Request, res: Response, next: NextFunction): Promise<Response> {
|
||||||
|
/*
|
||||||
|
#swagger.tags = ['Product']
|
||||||
|
#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(Product);
|
||||||
|
|
||||||
|
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 = ['Product']
|
||||||
|
#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(Product);
|
||||||
|
|
||||||
|
let whereAttr = '';
|
||||||
|
let whereVal: any = {};
|
||||||
|
|
||||||
|
if (param.filter) {
|
||||||
|
whereAttr = 'name like :filter_like ';
|
||||||
|
// whereVal['filter'] = param.filter
|
||||||
|
whereVal['filter_like'] = '%' + param.filter + '%'
|
||||||
|
}
|
||||||
|
|
||||||
|
const filename = "product.csv";
|
||||||
|
|
||||||
|
res.setHeader('Content-Type', 'text/csv');
|
||||||
|
res.setHeader('Content-Disposition', 'attachment; filename=' + filename);
|
||||||
|
|
||||||
|
const csvStream = fastcsv.format({
|
||||||
|
headers: true,
|
||||||
|
writeHeaders: true,
|
||||||
|
transform: (row: Product): 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 = ['Product']
|
||||||
|
#swagger.security = [{
|
||||||
|
"bearerAuth": []
|
||||||
|
}]
|
||||||
|
|
||||||
|
#swagger.requestBody = {
|
||||||
|
required: true,
|
||||||
|
content: {
|
||||||
|
"application/json": {
|
||||||
|
schema: {
|
||||||
|
$ref: "#/components/schemas/product"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
try {
|
||||||
|
const schema = Joi.object().keys({
|
||||||
|
code: Joi.string().max(6).required().label('Code'),
|
||||||
|
name: Joi.string().max(256).required().label('Name'),
|
||||||
|
status: Joi.string().required().label('Status'),
|
||||||
|
});
|
||||||
|
|
||||||
|
const param: Product = await schema.validateAsync(req.body);
|
||||||
|
|
||||||
|
const data = new Product()
|
||||||
|
data.code = param.code
|
||||||
|
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 = ['Product']
|
||||||
|
#swagger.security = [{
|
||||||
|
"bearerAuth": []
|
||||||
|
}]
|
||||||
|
#swagger.parameters['id'] = {
|
||||||
|
in: 'path',
|
||||||
|
description: 'Product ID.',
|
||||||
|
required: true,
|
||||||
|
type: 'string'
|
||||||
|
}
|
||||||
|
|
||||||
|
#swagger.requestBody = {
|
||||||
|
required: true,
|
||||||
|
content: {
|
||||||
|
"application/json": {
|
||||||
|
schema: {
|
||||||
|
$ref: "#/components/schemas/product"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
try {
|
||||||
|
const schema = Joi.object().keys({
|
||||||
|
id: Joi.string().uuid().required().label('ID'),
|
||||||
|
code: Joi.string().max(6).required().label('Code'),
|
||||||
|
name: Joi.string().max(256).required().label('Name'),
|
||||||
|
status: Joi.string().required().label('Status'),
|
||||||
|
});
|
||||||
|
|
||||||
|
req.body.id = req.params['id'];
|
||||||
|
|
||||||
|
const param: Product = await schema.validateAsync(req.body);
|
||||||
|
|
||||||
|
const repo = OrmHelper.DB.getRepository(Product);
|
||||||
|
|
||||||
|
const data = await repo.findOneBy({ id: param.id });
|
||||||
|
|
||||||
|
if (data != null) {
|
||||||
|
data.code = param.code
|
||||||
|
data.name = param.name
|
||||||
|
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 = ['Product']
|
||||||
|
#swagger.security = [{
|
||||||
|
"bearerAuth": []
|
||||||
|
}]
|
||||||
|
#swagger.parameters['id'] = {
|
||||||
|
in: 'path',
|
||||||
|
description: 'Product ID.',
|
||||||
|
required: true,
|
||||||
|
type: 'string'
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
try {
|
||||||
|
const schema = Joi.object().keys({
|
||||||
|
id: Joi.string().uuid().required().label('ID')
|
||||||
|
});
|
||||||
|
|
||||||
|
const param: Product = await schema.validateAsync(req.params);
|
||||||
|
|
||||||
|
const repo = OrmHelper.DB.getRepository(Product);
|
||||||
|
|
||||||
|
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 = ['Product']
|
||||||
|
#swagger.security = [{
|
||||||
|
"bearerAuth": []
|
||||||
|
}]
|
||||||
|
|
||||||
|
#swagger.parameters['id'] = {
|
||||||
|
in: 'path',
|
||||||
|
description: 'Product ID.',
|
||||||
|
required: true,
|
||||||
|
type: 'string'
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
try {
|
||||||
|
const schema = Joi.object().keys({
|
||||||
|
id: Joi.string().uuid().required().label('ID')
|
||||||
|
});
|
||||||
|
|
||||||
|
const param: Product = await schema.validateAsync(req.params);
|
||||||
|
|
||||||
|
const repo = OrmHelper.DB.getRepository(Product);
|
||||||
|
|
||||||
|
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,10 +1,7 @@
|
|||||||
import config from 'config';
|
import config from 'config';
|
||||||
import { DataSource } from "typeorm";
|
import { DataSource } from "typeorm";
|
||||||
import { User } from "../entity/users";
|
|
||||||
import { TrialBalance } from '../entity/trial_balance';
|
|
||||||
import { ILogObj, Logger } from 'tslog';
|
import { ILogObj, Logger } from 'tslog';
|
||||||
import { LNKOLEK } from '../entity/lnkolek';
|
import { Branch, Product } from 'Entity'
|
||||||
import { CifAccount } from '../entity/cif_account';
|
|
||||||
|
|
||||||
export class OrmHelper {
|
export class OrmHelper {
|
||||||
static DB: DataSource = null
|
static DB: DataSource = null
|
||||||
@ -23,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: [User, TrialBalance, LNKOLEK, CifAccount],
|
entities: [Product, Branch],
|
||||||
subscribers: [],
|
subscribers: [],
|
||||||
migrations: [],
|
migrations: [],
|
||||||
})
|
})
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
import express from 'express';
|
import express from 'express';
|
||||||
import { UserController } from '../controllers/user';
|
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 {
|
||||||
@ -7,11 +8,18 @@ export class RoutePublic {
|
|||||||
|
|
||||||
app.use(Language.apply)
|
app.use(Language.apply)
|
||||||
|
|
||||||
app.get('/api/user/list', UserController.list)
|
app.get('/api/branch/list', BranchController.list)
|
||||||
app.get('/api/user/export', UserController.export)
|
app.get('/api/branch/export', BranchController.export)
|
||||||
app.post('/api/user/create', UserController.create)
|
app.post('/api/branch/create', BranchController.create)
|
||||||
app.put('/api/user/update/:id', UserController.update)
|
app.put('/api/branch/update/:id', BranchController.update)
|
||||||
app.delete('/api/user/delete/:id', UserController.delete)
|
app.delete('/api/branch/delete/:id', BranchController.delete)
|
||||||
app.put('/api/user/restore/:id', UserController.restore)
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1,4 +1,4 @@
|
|||||||
const swaggerAutogen = require('swagger-autogen')();
|
const swaggerAutogen = require('swagger-autogen')({ openapi: '3.0.0' });
|
||||||
const config = require('config');
|
const config = require('config');
|
||||||
|
|
||||||
const doc = {
|
const doc = {
|
||||||
@ -7,7 +7,27 @@ const doc = {
|
|||||||
description: config.get('app.description'),
|
description: config.get('app.description'),
|
||||||
version: config.get('app.version')
|
version: config.get('app.version')
|
||||||
},
|
},
|
||||||
host: config.get('server.host_swagger') + ':' + config.get('server.port'),
|
servers: [
|
||||||
|
{
|
||||||
|
url: config.get('server.host_swagger'),
|
||||||
|
description: 'Environtment ' + config.get('app.env')
|
||||||
|
},
|
||||||
|
],
|
||||||
|
components: {
|
||||||
|
schemas: {
|
||||||
|
product: {
|
||||||
|
$code: "123456",
|
||||||
|
$name: "Product name",
|
||||||
|
$description: "Product description",
|
||||||
|
$status: "Y",
|
||||||
|
},
|
||||||
|
branch: {
|
||||||
|
$code: "123456",
|
||||||
|
$name: "Branch name",
|
||||||
|
$status: "Y",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const outputFile = './swagger.json';
|
const outputFile = './swagger.json';
|
||||||
|
|||||||
Reference in New Issue
Block a user