diff --git a/docs/swagger/routes/role.js b/docs/swagger/routes/role.js index 5246c720..8b698204 100644 --- a/docs/swagger/routes/role.js +++ b/docs/swagger/routes/role.js @@ -55,6 +55,42 @@ module.exports = { }, }, }, + '/role/import-excel': { + post: { + tags: ['Role'], + summary: 'Import Excel', + security: [ + { + auth_token: [], + }, + ], + requestBody: { + required: true, + content: { + 'multipart/form-data': { + schema: { + type: 'object', + properties: { + fileExcel: { + type: 'file', + items: { + type: 'string', + format: 'binary', + }, + }, + }, + required: ['fileExcel'], + }, + }, + }, + }, + responses: { + 200: { + description: 'Import Excel', + }, + }, + }, + }, '/role/generate-excel': { get: { tags: ['Role'], diff --git a/src/controllers/Role/controller.ts b/src/controllers/Role/controller.ts index aee2dd66..7e2d19c1 100644 --- a/src/controllers/Role/controller.ts +++ b/src/controllers/Role/controller.ts @@ -1,4 +1,4 @@ -import { Request, Response } from 'express' +import { NextFunction, Request, Response } from 'express' import routes from 'routes/public' import asyncHandler from 'helpers/asyncHandler' import Authorization from 'middlewares/Authorization' @@ -6,6 +6,8 @@ import BuildResponse from 'modules/Response/BuildResponse' import RoleService from 'controllers/Role/service' import { arrayFormatter } from 'helpers/Common' import { formatDateGenerateFile } from 'helpers/Date' +import ConfigMulter from 'modules/ConfigMulter' +import { get } from 'lodash' routes.get( '/role', @@ -48,6 +50,38 @@ routes.get( }) ) +const uploadFile = ConfigMulter({ + dest: 'public/uploads/excel', + allowedExt: ['.xlsx', '.xls'], +}).fields([{ name: 'fileExcel', maxCount: 1 }]) + +const setFileToBody = asyncHandler(async function setFileToBody( + req: Request, + res, + next: NextFunction +) { + const fileExcel = req.pickSingleFieldMulter(['fileExcel']) + + req.setBody(fileExcel) + next() +}) + +routes.post( + '/role/import-excel', + Authorization, + uploadFile, + setFileToBody, + asyncHandler(async function importExcel(req: Request, res: Response) { + const formData = req.getBody() + const fieldExcel = get(formData, 'fileExcel', {}) + + const data = await RoleService.importExcel(fieldExcel) + const buildResponse = BuildResponse.created(data) + + return res.status(200).json(buildResponse) + }) +) + routes.post( '/role', Authorization, diff --git a/src/controllers/Role/service.ts b/src/controllers/Role/service.ts index 2b076820..175dcdee 100644 --- a/src/controllers/Role/service.ts +++ b/src/controllers/Role/service.ts @@ -6,9 +6,10 @@ import useValidation from 'helpers/useValidation' import { RoleAttributes } from 'models/role' import PluginSqlizeQuery from 'modules/SqlizeQuery/PluginSqlizeQuery' import schema from 'controllers/Role/schema' -import ExcelHelper from 'helpers/Excel' +import Excel from 'helpers/Excel' import { isEmpty } from 'lodash' import { validateBoolean } from 'helpers/Common' +import { FileAttributes } from 'interfaces/file' const { Sequelize } = db const { Op } = Sequelize @@ -84,6 +85,16 @@ class RoleService { return data } + /** + * + * @param fieldFiles + */ + public static async importExcel(fieldFiles: FileAttributes) { + const excelJson = Excel.convertToJson(fieldFiles.path) + + return excelJson + } + /** * * @param req - Request @@ -105,7 +116,7 @@ class RoleService { }) } - const stream: Buffer = await ExcelHelper.generate(header, newData) + const stream: Buffer = await Excel.generate(header, newData) return stream }