feat: user management search

This commit is contained in:
Sergey Yarkov 2021-10-06 22:39:38 +03:00
parent cded256d0f
commit 0eb55c6be4
2 changed files with 11 additions and 10 deletions

View File

@ -6,6 +6,7 @@ enum StatusCodeEnum {
UNAUTHORIZED = 401, UNAUTHORIZED = 401,
FORBIDDEN = 403, FORBIDDEN = 403,
NOT_FOUND = 404, NOT_FOUND = 404,
CONFLICT = 409,
INTERNAL_SERVER = 500, INTERNAL_SERVER = 500,
SERVICE_UNAVAILABLE = 503, SERVICE_UNAVAILABLE = 503,
} }

View File

@ -68,7 +68,7 @@ export default class UserRepository {
const query = this.User.query(); const query = this.User.query();
if (email) { if (email) {
query.preload('contacts').whereHas('contacts', q => q.where('email', 'like', `%${email}%`)); query.where('email', 'like', `%${email}%`);
} }
if (login) { if (login) {
@ -83,17 +83,17 @@ export default class UserRepository {
query.where('last_name', 'like', `%${last_name}%`); query.where('last_name', 'like', `%${last_name}%`);
} }
if (role) { if (search) {
query.whereHas('roles', q => q.where('slug', role)); query
.orWhere('first_name', 'like', `%${search}%`)
.orWhere('last_name', 'like', `%${search}%`)
.orWhereRaw(`CONCAT(first_name, ' ', last_name) = ?`, [search])
.orWhere('email', 'like', `${search}`)
.orWhere('id', 'like', `${search}`);
} }
// TODO fix search when gets params "serach" and "role" if (role) {
if (search && role) { query.whereHas('roles', q => q.where('slug', role));
query
.where('first_name', 'like', `%${search}%`)
.orWhere('last_name', 'like', `%${search}%`)
.orWhere('email', 'like', `%${search}%`)
.andWhereHas('roles', q => q.where('slug', role));
} }
const data = await query.preload('contacts').preload('roles').orderBy('created_at', 'desc').paginate(page, limit); const data = await query.preload('contacts').preload('roles').orderBy('created_at', 'desc').paginate(page, limit);