Skip to content

Commit bafe6b3

Browse files
committed
feat: add endpoint import excel
1 parent ee52f22 commit bafe6b3

File tree

3 files changed

+84
-3
lines changed

3 files changed

+84
-3
lines changed

docs/swagger/routes/role.js

+36
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,42 @@ module.exports = {
5555
},
5656
},
5757
},
58+
'/role/import-excel': {
59+
post: {
60+
tags: ['Role'],
61+
summary: 'Import Excel',
62+
security: [
63+
{
64+
auth_token: [],
65+
},
66+
],
67+
requestBody: {
68+
required: true,
69+
content: {
70+
'multipart/form-data': {
71+
schema: {
72+
type: 'object',
73+
properties: {
74+
fileExcel: {
75+
type: 'file',
76+
items: {
77+
type: 'string',
78+
format: 'binary',
79+
},
80+
},
81+
},
82+
required: ['fileExcel'],
83+
},
84+
},
85+
},
86+
},
87+
responses: {
88+
200: {
89+
description: 'Import Excel',
90+
},
91+
},
92+
},
93+
},
5894
'/role/generate-excel': {
5995
get: {
6096
tags: ['Role'],

src/controllers/Role/controller.ts

+35-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
import { Request, Response } from 'express'
1+
import { NextFunction, Request, Response } from 'express'
22
import routes from 'routes/public'
33
import asyncHandler from 'helpers/asyncHandler'
44
import Authorization from 'middlewares/Authorization'
55
import BuildResponse from 'modules/Response/BuildResponse'
66
import RoleService from 'controllers/Role/service'
77
import { arrayFormatter } from 'helpers/Common'
88
import { formatDateGenerateFile } from 'helpers/Date'
9+
import ConfigMulter from 'modules/ConfigMulter'
10+
import { get } from 'lodash'
911

1012
routes.get(
1113
'/role',
@@ -48,6 +50,38 @@ routes.get(
4850
})
4951
)
5052

53+
const uploadFile = ConfigMulter({
54+
dest: 'public/uploads/excel',
55+
allowedExt: ['.xlsx', '.xls'],
56+
}).fields([{ name: 'fileExcel', maxCount: 1 }])
57+
58+
const setFileToBody = asyncHandler(async function setFileToBody(
59+
req: Request,
60+
res,
61+
next: NextFunction
62+
) {
63+
const fileExcel = req.pickSingleFieldMulter(['fileExcel'])
64+
65+
req.setBody(fileExcel)
66+
next()
67+
})
68+
69+
routes.post(
70+
'/role/import-excel',
71+
Authorization,
72+
uploadFile,
73+
setFileToBody,
74+
asyncHandler(async function importExcel(req: Request, res: Response) {
75+
const formData = req.getBody()
76+
const fieldExcel = get(formData, 'fileExcel', {})
77+
78+
const data = await RoleService.importExcel(fieldExcel)
79+
const buildResponse = BuildResponse.created(data)
80+
81+
return res.status(200).json(buildResponse)
82+
})
83+
)
84+
5185
routes.post(
5286
'/role',
5387
Authorization,

src/controllers/Role/service.ts

+13-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ import useValidation from 'helpers/useValidation'
66
import { RoleAttributes } from 'models/role'
77
import PluginSqlizeQuery from 'modules/SqlizeQuery/PluginSqlizeQuery'
88
import schema from 'controllers/Role/schema'
9-
import ExcelHelper from 'helpers/Excel'
9+
import Excel from 'helpers/Excel'
1010
import { isEmpty } from 'lodash'
1111
import { validateBoolean } from 'helpers/Common'
12+
import { FileAttributes } from 'interfaces/file'
1213

1314
const { Sequelize } = db
1415
const { Op } = Sequelize
@@ -84,6 +85,16 @@ class RoleService {
8485
return data
8586
}
8687

88+
/**
89+
*
90+
* @param fieldFiles
91+
*/
92+
public static async importExcel(fieldFiles: FileAttributes) {
93+
const excelJson = Excel.convertToJson(fieldFiles.path)
94+
95+
return excelJson
96+
}
97+
8798
/**
8899
*
89100
* @param req - Request
@@ -105,7 +116,7 @@ class RoleService {
105116
})
106117
}
107118

108-
const stream: Buffer = await ExcelHelper.generate(header, newData)
119+
const stream: Buffer = await Excel.generate(header, newData)
109120

110121
return stream
111122
}

0 commit comments

Comments
 (0)