Skip to content

Commit

Permalink
Merge pull request #2 from nadeocfg/Search-filters
Browse files Browse the repository at this point in the history
Search filters
  • Loading branch information
nadeocfg authored Nov 5, 2023
2 parents 621ad91 + c983de6 commit 84d8059
Show file tree
Hide file tree
Showing 33 changed files with 797 additions and 351 deletions.
219 changes: 158 additions & 61 deletions backend/controllers/orders.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -755,11 +755,45 @@ const getOrders = async (
next: NextFunction
) => {
try {
const { page, count, searchValue, sort = 'id,desc' } = request.query;
const {
page,
count,
searchValue,
users,
statuses,
fromDate,
toDate,
sort = 'id,desc',
} = request.query;

const sortBy = (<string>sort).split(',')[0];
const order = (<string>sort).split(',')[1];
const roleCode = request.user?.roleCode || '';
const userId = request.user?.id || '';
let formattedFromDate = fromDate;
let formattedToDate = toDate;
const formattedUsers = format(
'%L',
(users as string).split(',').map(Number)
);
const formattedStatuses = format(
'%L',
(statuses as string).split(',').map(Number)
);

if (formattedFromDate) {
const fromArr = ((fromDate ?? '') as string).split('-');
formattedFromDate = `${fromArr[2] ?? '2000'}-${fromArr[1] ?? '01'}-${
fromArr[0] ?? '01'
}`;
}

if (formattedToDate) {
const toArr = ((toDate ?? '') as string).split('-');
formattedToDate = `${toArr[2] ?? '3000'}-${toArr[1] ?? '01'}-${
toArr[0] ?? '01'
}`;
}

let offset = 0;

Expand All @@ -775,61 +809,76 @@ const getOrders = async (
let total = null;

if (roleCode === 'ADMIN' || roleCode === 'SUPER_ADMIN') {
const getAllOrders = await db.query(
`
SELECT
orders.id,
orders.address,
orders."createdDate",
orders."updatedDate",
orders.comment,
orders."customerId",
orders."serviceManId",
orders."createdBy",
orders."phone",
status.name as "statusName",
status.code as "statusCode",
customers."fullName",
customers."boilerSerial",
users."fullName" as "serviceManFullName",
boilers.name as "boilerName"
FROM
"${process.env.DB_NAME}"."orders" as orders
LEFT JOIN
"${process.env.DB_NAME}"."dictOrderStatuses" as status
ON
orders.status = status.id
LEFT JOIN
"${process.env.DB_NAME}"."customers" as customers
ON
orders."customerId" = customers.id
LEFT JOIN
"${process.env.DB_NAME}"."users" as users
ON
orders."serviceManId" = users.id
LEFT JOIN
"${process.env.DB_NAME}"."dictBoilers" as boilers
ON
customers."boilerId" = boilers.id
WHERE
orders."isActive" = true AND
(LOWER(customers."fullName") LIKE $3 OR
orders.id::text LIKE $3 OR
LOWER(orders.address) LIKE $3 OR
LOWER(users."fullName") LIKE $3 OR
LOWER(customers."boilerSerial") LIKE $3 OR
LOWER(boilers.name) LIKE $3 OR
LOWER(status.name) LIKE $3 OR
LOWER(orders.comment) LIKE $3)
ORDER BY
${format('%I', sortBy)} ${format('%s', order)}
LIMIT
$1
OFFSET
$2;
`,
[count, offset, `%${searchValue}%`.toLowerCase()]
);
const query = `
SELECT
orders.id,
orders.address,
orders."createdDate",
orders."updatedDate",
orders.comment,
orders."customerId",
orders."serviceManId",
orders."createdBy",
orders."phone",
status.name as "statusName",
status.code as "statusCode",
customers."fullName",
customers."boilerSerial",
users."fullName" as "serviceManFullName",
boilers.name as "boilerName"
FROM
"${process.env.DB_NAME}"."orders" as orders
LEFT JOIN
"${process.env.DB_NAME}"."dictOrderStatuses" as status
ON
orders.status = status.id
LEFT JOIN
"${process.env.DB_NAME}"."customers" as customers
ON
orders."customerId" = customers.id
LEFT JOIN
"${process.env.DB_NAME}"."users" as users
ON
orders."serviceManId" = users.id
LEFT JOIN
"${process.env.DB_NAME}"."dictBoilers" as boilers
ON
customers."boilerId" = boilers.id
WHERE
orders."isActive" = true AND
(LOWER(customers."fullName") LIKE $3 OR
orders.id::text LIKE $3 OR
LOWER(orders.address) LIKE $3 OR
LOWER(users."fullName") LIKE $3 OR
LOWER(customers."boilerSerial") LIKE $3 OR
LOWER(boilers.name) LIKE $3 OR
LOWER(status.name) LIKE $3 OR
LOWER(orders.comment) LIKE $3)
${users ? `AND orders."serviceManId" IN (${formattedUsers})` : ''}
${statuses ? `AND orders."status" IN (${formattedStatuses})` : ''}
${
fromDate
? `AND orders."createdDate" > ${format('%L', formattedFromDate)}`
: ''
}
${
toDate
? `AND orders."createdDate" < ${format('%L', formattedToDate)}`
: ''
}
ORDER BY
${format('%I', sortBy)} ${format('%s', order)}
LIMIT
$1
OFFSET
$2;
`;

const getAllOrders = await db.query(query, [
count,
offset,
`%${((searchValue as string) ?? '').toLowerCase()}%`,
]);

total = await db.query(
`
Expand Down Expand Up @@ -861,9 +910,24 @@ const getOrders = async (
LOWER(users."fullName") LIKE $1 OR
LOWER(boilers.name) LIKE $1 OR
LOWER(status.name) LIKE $1 OR
LOWER(orders.comment) LIKE $1);
LOWER(orders.comment) LIKE $1)
${users ? `AND orders."serviceManId" IN (${formattedUsers})` : ''}
${statuses ? `AND orders."status" IN (${formattedStatuses})` : ''}
${
fromDate
? `AND orders."createdDate" > ${format(
'%L',
formattedFromDate
)}`
: ''
}
${
toDate
? `AND orders."createdDate" < ${format('%L', formattedToDate)}`
: ''
}
`,
[`%${searchValue}%`.toLowerCase()]
[`%${((searchValue as string) ?? '').toLowerCase()}%`]
);

orders = getAllOrders.rows;
Expand Down Expand Up @@ -915,14 +979,33 @@ const getOrders = async (
LOWER(boilers.name) LIKE $4 OR
LOWER(status.name) LIKE $4 OR
LOWER(orders.comment) LIKE $4)
${statuses ? `AND orders."status" IN (${formattedStatuses})` : ''}
${
fromDate
? `AND orders."createdDate" > ${format(
'%L',
formattedFromDate
)}`
: ''
}
${
toDate
? `AND orders."createdDate" < ${format('%L', formattedToDate)}`
: ''
}
ORDER BY
${format('%I', sortBy)} ${format('%s', order)}
LIMIT
$1
OFFSET
$2;
`,
[count, offset, userId, `%${searchValue}%`.toLowerCase()]
[
count,
offset,
userId,
`%${((searchValue as string) ?? '').toLowerCase()}%`,
]
);

total = await db.query(
Expand Down Expand Up @@ -956,9 +1039,23 @@ const getOrders = async (
LOWER(users."fullName") LIKE $2 OR
LOWER(boilers.name) LIKE $2 OR
LOWER(status.name) LIKE $2 OR
LOWER(orders.comment) LIKE $2);
LOWER(orders.comment) LIKE $2)
${statuses ? `AND orders."status" IN (${formattedStatuses})` : ''}
${
fromDate
? `AND orders."createdDate" > ${format(
'%L',
formattedFromDate
)}`
: ''
}
${
toDate
? `AND orders."createdDate" < ${format('%L', formattedToDate)}`
: ''
}
`,
[userId, `%${searchValue}%`.toLowerCase()]
[userId, `%${((searchValue as string) ?? '').toLowerCase()}%`]
);

orders = getUserOrders.rows;
Expand Down
42 changes: 22 additions & 20 deletions frontend/src/assets/scss/utils/_layouts.scss
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,6 @@
}
}

.search-row {
display: flex;
flex-direction: column;

h1 {
margin: 16px 0;
}
}

.select-modal {
height: 600px;

Expand All @@ -28,23 +19,34 @@
}
}

@media (min-width: 600px) {
.btn-container {
flex-direction: row;
padding: 0 32px 16px 32px;
.search-row {
display: flex;
flex-direction: column;
flex-wrap: wrap;
align-items: center;
width: 100%;
margin-bottom: 20px;

.MuiButtonBase-root.btn {
width: auto;
.MuiInputBase-root {
flex-grow: 1;
width: 100%;
margin: 16px 0;
}

& + .btn {
margin-left: 8px;
}
}
h1 {
margin: 16px 0;
}
}

@media (min-width: 600px) {
.search-row {
display: flex;
flex-direction: row;
flex-wrap: nowrap;
justify-content: space-between;

.MuiInputBase-root {
width: auto;
margin: 0 16px;
}
}
}
24 changes: 24 additions & 0 deletions frontend/src/components/Btn/Btn.scss
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,30 @@
}
}

&_link {
padding: 0;
background: transparent;
color: var(--primary-color);
text-transform: none;
// border-bottom: 1px solid var(--primary-color);
border-radius: 0;

&.active {
.MuiButton-label svg.MuiSvgIcon-root {
transform: rotate(180deg);
}
}

.MuiButton-label svg.MuiSvgIcon-root {
margin-right: 0;
transition: all 300ms ease;
}

&:hover {
background: transparent;
}
}

&_success {
background: var(--success-color);
color: var(--white-color);
Expand Down
Loading

0 comments on commit 84d8059

Please sign in to comment.