@@ -7,6 +7,7 @@ import { Transaction } from 'sequelize/types'
7
7
import UserRoleService from 'controllers/UserRole/service'
8
8
import PluginSqlizeQuery from 'modules/SqlizeQuery/PluginSqlizeQuery'
9
9
import schema from 'controllers/User/schema'
10
+ import { arrayFormatter } from 'helpers/Common'
10
11
11
12
const { User, Role } = models
12
13
const including = [ { model : Role } ]
@@ -46,7 +47,26 @@ class UserService {
46
47
} )
47
48
48
49
if ( ! data ) {
49
- throw new ResponseError . NotFound ( 'data not found or has been deleted' )
50
+ throw new ResponseError . NotFound (
51
+ 'user data not found or has been deleted'
52
+ )
53
+ }
54
+
55
+ return data
56
+ }
57
+
58
+ /**
59
+ *
60
+ * @param id
61
+ * note: find by id only find data not include relation
62
+ */
63
+ public static async findById ( id : string ) {
64
+ const data = await User . findByPk ( id )
65
+
66
+ if ( ! data ) {
67
+ throw new ResponseError . NotFound (
68
+ 'user data not found or has been deleted'
69
+ )
50
70
}
51
71
52
72
return data
@@ -66,7 +86,7 @@ class UserService {
66
86
} )
67
87
68
88
// Check Roles is Array, format = ['id_1', 'id_2']
69
- const arrayRoles = Array . isArray ( Roles ) ? Roles : JSON . parse ( Roles )
89
+ const arrayRoles = arrayFormatter ( Roles )
70
90
71
91
const listUserRole = [ ]
72
92
for ( let i = 0 ; i < arrayRoles . length ; i += 1 ) {
@@ -94,11 +114,11 @@ class UserService {
94
114
formData : UserAttributes ,
95
115
txn ?: Transaction
96
116
) {
97
- const data = await this . getOne ( id )
117
+ const data = await this . findById ( id )
98
118
const { Roles } : any = formData
99
119
100
120
// Check Roles is Array, format = ['id_1', 'id_2']
101
- const arrayRoles = Array . isArray ( Roles ) ? Roles : JSON . parse ( Roles )
121
+ const arrayRoles = arrayFormatter ( Roles )
102
122
103
123
// Destroy data not in UserRole
104
124
await UserRoleService . deleteNotInRoleId ( id , arrayRoles )
0 commit comments