Skip to content

Commit 3154c6b

Browse files
committed
refactor: user service
1 parent ae718d2 commit 3154c6b

File tree

1 file changed

+5
-29
lines changed

1 file changed

+5
-29
lines changed

src/controllers/User/service.ts

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,14 @@
1-
/* eslint-disable no-unused-vars */
2-
/* eslint-disable no-param-reassign */
31
import models from 'models'
4-
import db from 'models/_instance'
52
import ResponseError from 'modules/Response/ResponseError'
63
import useValidation from 'helpers/useValidation'
74
import { UserAttributes } from 'models/user'
85
import { Transaction } from 'sequelize/types'
96
import UserRoleService from 'controllers/UserRole/service'
107
import PluginSqlizeQuery from 'modules/SqlizeQuery/PluginSqlizeQuery'
11-
import BuildResponse from 'modules/Response/BuildResponse'
128
import schema from './schema'
139

14-
const { Sequelize } = db
15-
const { Op } = Sequelize
16-
17-
const { User, Role, UserRole } = models
18-
const including = [{ model: Role }]
10+
const { User, UserRole } = models
11+
const including = [{ model: UserRole }]
1912

2013
class UserService {
2114
/**
@@ -50,9 +43,7 @@ class UserService {
5043
})
5144

5245
if (!data) {
53-
throw new ResponseError.NotFound(
54-
'Data tidak ditemukan atau sudah terhapus!'
55-
)
46+
throw new ResponseError.NotFound('data not found or has been deleted')
5647
}
5748

5849
return data
@@ -102,14 +93,7 @@ class UserService {
10293
const arrayRoles = Array.isArray(Roles) ? Roles : JSON.parse(Roles)
10394

10495
// Destroy data not in UserRole
105-
await UserRole.destroy({
106-
where: {
107-
UserId: id,
108-
RoleId: {
109-
[Op.notIn]: arrayRoles,
110-
},
111-
},
112-
})
96+
await UserRoleService.deleteNotInRoleId(id, arrayRoles)
11397

11498
const listUserRole = []
11599
for (let i = 0; i < arrayRoles.length; i += 1) {
@@ -139,15 +123,7 @@ class UserService {
139123
public static async delete(id: string) {
140124
const data = await this.getOne(id)
141125

142-
// Destroy user data in UserRole
143-
await UserRole.destroy({
144-
where: {
145-
UserId: {
146-
[Op.in]: id,
147-
},
148-
},
149-
})
150-
126+
await UserRoleService.deleteByUserId(id)
151127
await data.destroy()
152128
}
153129
}

0 commit comments

Comments
 (0)