Skip to content

Commit

Permalink
feat: add endpoint import excel
Browse files Browse the repository at this point in the history
  • Loading branch information
masb0ymas committed Feb 12, 2021
1 parent ee52f22 commit bafe6b3
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 3 deletions.
36 changes: 36 additions & 0 deletions docs/swagger/routes/role.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down
36 changes: 35 additions & 1 deletion src/controllers/Role/controller.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
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'
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',
Expand Down Expand Up @@ -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,
Expand Down
15 changes: 13 additions & 2 deletions src/controllers/Role/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -105,7 +116,7 @@ class RoleService {
})
}

const stream: Buffer = await ExcelHelper.generate(header, newData)
const stream: Buffer = await Excel.generate(header, newData)

return stream
}
Expand Down

0 comments on commit bafe6b3

Please sign in to comment.