Skip to content

Commit

Permalink
feat: add function find by id
Browse files Browse the repository at this point in the history
  • Loading branch information
masb0ymas committed Jan 8, 2021
1 parent d4dd8b7 commit dac6a27
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions src/controllers/User/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Transaction } from 'sequelize/types'
import UserRoleService from 'controllers/UserRole/service'
import PluginSqlizeQuery from 'modules/SqlizeQuery/PluginSqlizeQuery'
import schema from 'controllers/User/schema'
import { arrayFormatter } from 'helpers/Common'

const { User, Role } = models
const including = [{ model: Role }]
Expand Down Expand Up @@ -46,7 +47,26 @@ class UserService {
})

if (!data) {
throw new ResponseError.NotFound('data not found or has been deleted')
throw new ResponseError.NotFound(
'user data not found or has been deleted'
)
}

return data
}

/**
*
* @param id
* note: find by id only find data not include relation
*/
public static async findById(id: string) {
const data = await User.findByPk(id)

if (!data) {
throw new ResponseError.NotFound(
'user data not found or has been deleted'
)
}

return data
Expand All @@ -66,7 +86,7 @@ class UserService {
})

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

const listUserRole = []
for (let i = 0; i < arrayRoles.length; i += 1) {
Expand Down Expand Up @@ -94,11 +114,11 @@ class UserService {
formData: UserAttributes,
txn?: Transaction
) {
const data = await this.getOne(id)
const data = await this.findById(id)
const { Roles }: any = formData

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

// Destroy data not in UserRole
await UserRoleService.deleteNotInRoleId(id, arrayRoles)
Expand Down

0 comments on commit dac6a27

Please sign in to comment.