Skip to content

Commit dac6a27

Browse files
committed
feat: add function find by id
1 parent d4dd8b7 commit dac6a27

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

src/controllers/User/service.ts

+24-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { Transaction } from 'sequelize/types'
77
import UserRoleService from 'controllers/UserRole/service'
88
import PluginSqlizeQuery from 'modules/SqlizeQuery/PluginSqlizeQuery'
99
import schema from 'controllers/User/schema'
10+
import { arrayFormatter } from 'helpers/Common'
1011

1112
const { User, Role } = models
1213
const including = [{ model: Role }]
@@ -46,7 +47,26 @@ class UserService {
4647
})
4748

4849
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+
)
5070
}
5171

5272
return data
@@ -66,7 +86,7 @@ class UserService {
6686
})
6787

6888
// Check Roles is Array, format = ['id_1', 'id_2']
69-
const arrayRoles = Array.isArray(Roles) ? Roles : JSON.parse(Roles)
89+
const arrayRoles = arrayFormatter(Roles)
7090

7191
const listUserRole = []
7292
for (let i = 0; i < arrayRoles.length; i += 1) {
@@ -94,11 +114,11 @@ class UserService {
94114
formData: UserAttributes,
95115
txn?: Transaction
96116
) {
97-
const data = await this.getOne(id)
117+
const data = await this.findById(id)
98118
const { Roles }: any = formData
99119

100120
// Check Roles is Array, format = ['id_1', 'id_2']
101-
const arrayRoles = Array.isArray(Roles) ? Roles : JSON.parse(Roles)
121+
const arrayRoles = arrayFormatter(Roles)
102122

103123
// Destroy data not in UserRole
104124
await UserRoleService.deleteNotInRoleId(id, arrayRoles)

0 commit comments

Comments
 (0)