Skip to content

Commit 543f58e

Browse files
committed
perf: improve logic controller user with role
1 parent 0608eec commit 543f58e

File tree

2 files changed

+64
-41
lines changed

2 files changed

+64
-41
lines changed

Diff for: src/controllers/User/controller.ts

+64-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
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 UserService from 'controllers/User/service'
77
import { arrayFormatter } from 'helpers/Common'
8+
import { UserInstance } from 'models/user'
9+
import UserRoleService from 'controllers/UserRole/service'
810

911
routes.get(
1012
'/user',
@@ -33,30 +35,87 @@ routes.get(
3335
routes.post(
3436
'/user',
3537
Authorization,
36-
asyncHandler(async function createData(req: Request, res: Response) {
38+
asyncHandler(async function createUser(
39+
req: Request,
40+
res: Response,
41+
next: NextFunction
42+
) {
3743
const txn = await req.getTransaction()
3844
const formData = req.getBody()
3945

4046
const data = await UserService.create(formData, txn)
41-
const buildResponse = BuildResponse.created({ data })
47+
req.setState({ userData: data, formData })
48+
49+
next()
50+
}),
51+
asyncHandler(async function createUserRole(req: Request, res: Response) {
52+
const txn = await req.getTransaction()
53+
const data = req.getState('userData') as UserInstance
54+
const Roles = req.getState('formData.Roles') as string[]
55+
56+
// Check Roles is Array, format = ['id_1', 'id_2']
57+
const arrayRoles = arrayFormatter(Roles)
58+
59+
const listUserRole = []
60+
for (let i = 0; i < arrayRoles.length; i += 1) {
61+
const RoleId: string = arrayRoles[i]
62+
const formData = {
63+
UserId: data.id,
64+
RoleId,
65+
}
66+
67+
listUserRole.push(formData)
68+
}
69+
await UserRoleService.bulkCreate(listUserRole, txn)
4270

4371
await txn.commit()
72+
const buildResponse = BuildResponse.created({ data })
73+
4474
return res.status(201).json(buildResponse)
4575
})
4676
)
4777

4878
routes.put(
4979
'/user/:id',
5080
Authorization,
51-
asyncHandler(async function updateData(req: Request, res: Response) {
81+
asyncHandler(async function updateUser(
82+
req: Request,
83+
res: Response,
84+
next: NextFunction
85+
) {
5286
const txn = await req.getTransaction()
5387
const formData = req.getBody()
5488
const { id } = req.getParams()
5589

5690
const data = await UserService.update(id, formData, txn)
57-
const buildResponse = BuildResponse.updated({ data })
91+
req.setState({ userData: data, formData })
92+
93+
next()
94+
}),
95+
asyncHandler(async function updateUserRole(req: Request, res: Response) {
96+
const txn = await req.getTransaction()
97+
const data = req.getState('userData') as UserInstance
98+
const Roles = req.getState('formData.Roles') as string[]
99+
100+
// Check Roles is Array, format = ['id_1', 'id_2']
101+
const arrayRoles = arrayFormatter(Roles)
58102

103+
// Destroy data not in UserRole
104+
await UserRoleService.deleteNotInRoleId(data.id, arrayRoles)
105+
106+
for (let i = 0; i < arrayRoles.length; i += 1) {
107+
const RoleId: string = arrayRoles[i]
108+
const formRole = {
109+
UserId: data.id,
110+
RoleId,
111+
}
112+
113+
// eslint-disable-next-line no-await-in-loop
114+
await UserRoleService.findOrCreate(formRole, txn)
115+
}
59116
await txn.commit()
117+
const buildResponse = BuildResponse.updated({ data })
118+
60119
return res.status(200).json(buildResponse)
61120
})
62121
)

Diff for: src/controllers/User/service.ts

-36
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import { Transaction } from 'sequelize/types'
88
import UserRoleService from 'controllers/UserRole/service'
99
import PluginSqlizeQuery from 'modules/SqlizeQuery/PluginSqlizeQuery'
1010
import schema from 'controllers/User/schema'
11-
import { arrayFormatter } from 'helpers/Common'
1211

1312
const { Sequelize } = db
1413
const { Op } = Sequelize
@@ -83,29 +82,12 @@ class UserService {
8382
* @param txn Transaction Sequelize
8483
*/
8584
public static async create(formData: UserAttributes, txn?: Transaction) {
86-
const { Roles }: any = formData
8785
const value = useValidation(schema.create, formData)
8886

8987
const dataUser = await User.create(value, {
9088
transaction: txn,
9189
})
9290

93-
// Check Roles is Array, format = ['id_1', 'id_2']
94-
const arrayRoles = arrayFormatter(Roles)
95-
96-
const listUserRole = []
97-
for (let i = 0; i < arrayRoles.length; i += 1) {
98-
const RoleId: string = arrayRoles[i]
99-
const formData = {
100-
UserId: dataUser.id,
101-
RoleId,
102-
}
103-
104-
listUserRole.push(formData)
105-
}
106-
107-
await UserRoleService.bulkCreate(listUserRole, txn)
108-
10991
return dataUser
11092
}
11193

@@ -121,24 +103,6 @@ class UserService {
121103
txn?: Transaction
122104
) {
123105
const data = await this.findById(id)
124-
const { Roles }: any = formData
125-
126-
// Check Roles is Array, format = ['id_1', 'id_2']
127-
const arrayRoles = arrayFormatter(Roles)
128-
129-
// Destroy data not in UserRole
130-
await UserRoleService.deleteNotInRoleId(id, arrayRoles)
131-
132-
for (let i = 0; i < arrayRoles.length; i += 1) {
133-
const RoleId: string = arrayRoles[i]
134-
const formRole = {
135-
UserId: id,
136-
RoleId,
137-
}
138-
139-
// eslint-disable-next-line no-await-in-loop
140-
await UserRoleService.findOrCreate(formRole, txn)
141-
}
142106

143107
const value = useValidation(schema.update, {
144108
...data.toJSON(),

0 commit comments

Comments
 (0)