Commit 684ba1cd authored by wahwahid's avatar wahwahid
Browse files

Group Reformat + Get List

parent 5083d195
......@@ -6,6 +6,12 @@ import { Group } from '../group'
export class GroupQueryDto extends PaginationDto {
@ApiProperty({ required: false })
search?: string = ''
@ApiProperty({ required: false, enum: ['user', 'admin'] })
entity?: string
@ApiProperty({ required: false })
org?: number
}
export class GroupResponseDto extends JsonResponse {
......
......@@ -13,19 +13,6 @@ import { AuthEntityDto } from '@module/auth/dto/authlogin.dto'
export class GroupController {
constructor(protected readonly groupService: GroupService) {}
@Get('/list/:entity')
@UseGuards(AuthGuard('jwt'))
@ApiBearerAuth()
@ApiUnauthorizedResponse({
description: 'Not logged in.',
type: JsonErrorResponse,
})
@ApiOkResponse({ description: 'Group found.', type: GroupResponseDto })
async getGroupEntity(@Request() req, @Query() query: GroupQueryDto, @Param() param: AuthEntityDto) {
const { search, page, view } = query
return this.groupService.findByEntity(search, { page, view }, param.entity.toLowerCase())
}
@Get('/list')
@UseGuards(AuthGuard('jwt'))
@ApiBearerAuth()
......@@ -35,8 +22,8 @@ export class GroupController {
})
@ApiOkResponse({ description: 'Group found.', type: GroupResponseDto })
async getGroup(@Request() req, @Query() query: GroupQueryDto) {
const { search, page, view } = query
return this.groupService.find(search, { page, view })
const { search, entity, org, page, view } = query
return this.groupService.find(search, { page, view }, { entity, org })
}
@Get('/:id')
......@@ -51,18 +38,6 @@ export class GroupController {
return this.groupService.findById(id)
}
@Get('/organization/:id')
@UseGuards(AuthGuard('jwt'))
@ApiBearerAuth()
@ApiUnauthorizedResponse({
description: 'Not logged in.',
type: JsonErrorResponse,
})
@ApiOkResponse({ description: 'Group found.', type: Group })
async getGroupByOrganization(@Request() req, @Param('id') id: number) {
return this.groupService.findByOrganization(id)
}
@Post('/add/:entity')
@UseGuards(AuthGuard('jwt'))
@ApiBearerAuth()
......@@ -72,7 +47,7 @@ export class GroupController {
})
@ApiCreatedResponse({ description: 'Group added.', type: GroupProccessResponseDto })
async addGroup(@Request() req, @Body() body: Group, @Param() param: AuthEntityDto) {
return this.groupService.add(body, req.user, param.entity.toLowerCase())
return this.groupService.add(body, req.user.user_id, param.entity.toLowerCase())
}
@Post('/update/:entity')
......@@ -84,7 +59,7 @@ export class GroupController {
})
@ApiCreatedResponse({ description: 'Group updated.', type: GroupProccessResponseDto })
async updateGroup(@Request() req, @Body() body: Group, @Param() param: AuthEntityDto) {
return this.groupService.update(body, req.user, param.entity.toLowerCase())
return this.groupService.update(body, req.user.user_id, param.entity.toLowerCase())
}
@Post('/delete/:entity')
......@@ -96,6 +71,6 @@ export class GroupController {
})
@ApiCreatedResponse({ description: 'Group deleted.', type: GroupProccessResponseDto })
async deleteGroup(@Request() req, @Body() body: Group, @Param() param: AuthEntityDto) {
return this.groupService.delete(body.group_id, req.user, param.entity.toLowerCase())
return this.groupService.delete(body.group_id, req.user.user_id, param.entity.toLowerCase())
}
}
import { Injectable, Inject } from '@nestjs/common'
import { Group } from './group'
import { KNEX_MYSQL } from '@common/constant'
import { Admin } from '@module/admin/admin'
import moment = require('moment')
@Injectable()
export class GroupService {
......@@ -11,10 +9,21 @@ export class GroupService {
async find(
search: string,
{ page, view },
{ entity, org },
knex: any = this.knex
): Promise<Group | undefined> {
return knex('groups')
let query = knex('groups')
.where('group_name', 'like', `%${search}%`)
if (entity) {
query.andWhere('group_entity', entity)
}
if (org) {
query.andWhere('group_org', org)
}
return query
.limit(view)
.offset((page - 1) * view)
}
......@@ -75,13 +84,14 @@ export class GroupService {
async add(
item: Group,
user: Admin,
user_id: number,
entity: string,
knex: any = this.knex
) {
item.group_created_at = moment().format('YYYY-MM-DD HH:mm:ss')
item.group_created_by = user.adm_id
item.group_created_at = knex.fn.now()
item.group_created_by = user_id
item.group_entity = entity
const result = await knex('groups').insert(item)
if (result.length > 0) {
return true
......@@ -92,15 +102,21 @@ export class GroupService {
async update(
item: Group,
user: Admin,
user_id: number,
entity: string,
knex: any = this.knex
) {
const group_id = item.group_id
delete item.group_id
item.group_updated_at = moment().format('YYYY-MM-DD HH:mm:ss')
item.group_updated_by = user.adm_id
const result = await knex('groups').where({group_id, group_entity: entity}).update(item)
item.group_updated_at = knex.fn.now()
item.group_updated_by = user_id
const result = await knex('groups')
.where({
group_id,
group_entity: entity
})
.update(item)
if (result.length > 0) {
return true
} else {
......@@ -110,11 +126,20 @@ export class GroupService {
async delete(
group_id: number,
user: Admin,
user_id: number,
entity: string,
knex: any = this.knex
) {
const result = await knex('groups').where({group_id, group_entity: entity}).update({group_delete: 1, group_updated_at: moment().format('YYYY-MMMM-DD HH:mm:ss'), group_updated_by: user.adm_id})
const result = await knex('groups')
.where({
group_id,
group_entity: entity
})
.update({
group_delete: 1,
group_updated_at: knex.fn.now(),
group_updated_by: user_id
})
if (result.length > 0) {
return true
} else {
......
......@@ -4,6 +4,7 @@ export class Group {
@ApiProperty()
group_id: number
@ApiProperty()
group_entity: string
@ApiProperty()
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment