|
1 |
| -/* eslint-disable no-unused-vars */ |
2 |
| -/* eslint-disable no-param-reassign */ |
3 | 1 | import models from 'models'
|
4 |
| -import db from 'models/_instance' |
5 | 2 | import ResponseError from 'modules/Response/ResponseError'
|
6 | 3 | import useValidation from 'helpers/useValidation'
|
7 | 4 | import { UserAttributes } from 'models/user'
|
8 | 5 | import { Transaction } from 'sequelize/types'
|
9 | 6 | import UserRoleService from 'controllers/UserRole/service'
|
10 | 7 | import PluginSqlizeQuery from 'modules/SqlizeQuery/PluginSqlizeQuery'
|
11 |
| -import BuildResponse from 'modules/Response/BuildResponse' |
12 | 8 | import schema from './schema'
|
13 | 9 |
|
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 }] |
19 | 12 |
|
20 | 13 | class UserService {
|
21 | 14 | /**
|
@@ -50,9 +43,7 @@ class UserService {
|
50 | 43 | })
|
51 | 44 |
|
52 | 45 | 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') |
56 | 47 | }
|
57 | 48 |
|
58 | 49 | return data
|
@@ -102,14 +93,7 @@ class UserService {
|
102 | 93 | const arrayRoles = Array.isArray(Roles) ? Roles : JSON.parse(Roles)
|
103 | 94 |
|
104 | 95 | // 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) |
113 | 97 |
|
114 | 98 | const listUserRole = []
|
115 | 99 | for (let i = 0; i < arrayRoles.length; i += 1) {
|
@@ -139,15 +123,7 @@ class UserService {
|
139 | 123 | public static async delete(id: string) {
|
140 | 124 | const data = await this.getOne(id)
|
141 | 125 |
|
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) |
151 | 127 | await data.destroy()
|
152 | 128 | }
|
153 | 129 | }
|
|
0 commit comments