Skip to content

Commit

Permalink
feat: LC-65 hide archived categories for not admin user
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaszbilka committed Aug 16, 2024
1 parent 3e7509f commit 2993a90
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions apps/api/src/categories/categories.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { count, eq, like, sql } from "drizzle-orm";
import { and, count, eq, isNull, like, sql } from "drizzle-orm";
import { HttpException, HttpStatus, Inject, Injectable } from "@nestjs/common";

import {
Expand All @@ -16,8 +16,11 @@ import { UserRole, UserRoles } from "src/users/schemas/user-roles";
export class CategoriesService {
constructor(@Inject("DB") private readonly db: DatabasePg) {}

private createLikeFilter(filter: string) {
return like(categories.title, `%${filter.toLowerCase()}%`);
private createCategoriesFilters(filter: string, isAdmin: boolean) {
return and(
like(categories.title, `%${filter.toLowerCase()}%`),
isAdmin ? undefined : isNull(categories.archivedAt),
);
}

private getColumnToSortBy(sort: string, isAdmin: boolean) {
Expand All @@ -42,9 +45,8 @@ export class CategoriesService {
} = query;

const { sortOrder, sortedField } = getSortOptions(sort);
const filterCondition = this.createLikeFilter(filter);

const isAdmin = userRole === UserRoles.admin;
const filterCondition = this.createCategoriesFilters(filter, isAdmin);

const selectedColumns = {
id: categories.id,
Expand Down

0 comments on commit 2993a90

Please sign in to comment.